Sinatra, Datamapper, Sass and Heroku

Here are a few notes on a simple prototype I just built with Sinatra, deploying on Heroku. It’s easy, but like everything (it seems) there are always a few tricks you need to remember.

To start, here’s an edited listing of my pthingy project directory (pthingy stands for phonethingy). The main ruby file is pthingy.rb. It essentially contains everything except the templates which are in the views directory.

doug@zeus ~/code/spikes/pthingy (master)
$ la
.gems
Rakefile
config.ru
pthingy.db
pthingy.rb
public/
seeds.rb
views/
doug@zeus ~/code/spikes/pthingy (master)

Setup

Note you have to require libraries explicitly such as haml and sass. For datamapper, you can require just the specific libraries you need or the big kahuna: datamapper.

require 'rubygems'
require 'sinatra'
require 'dm-core'
require 'dm-timestamps'
require 'haml'
require 'sass'

DataMapper

The keys here include requiring the correct libraries you need as well as performing a setup. Note the class definitions include the association assignments.

## Models / DB
#
# If you want the logs displayed you have to do this before the call to setup
DataMapper::Logger.new($stdout, :debug)

DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/pthingy.db")
configure :development do
  enable :logging, :dump_errors, :raise_errors
end

# log = File.new("sinatra.log", "a")
# STDOUT.reopen(log)
# STDERR.reopen(log)

class Event
  include DataMapper::Resource

  property :id,         Serial   # An auto-increment integer key
  property :event_tag,  String   # An event key string.
  property :created_at, DateTime # A DateTime, for any date you might like.

  has n, :posts

end

class Post
  include DataMapper::Resource

  property :id,         Serial   # An auto-increment integer key
  property :url,        Text     # URL of image.
  property :created_at, DateTime # A DateTime, for any date you might like.

  belongs_to :event
  has n, :comments

end

class Comment
  include DataMapper::Resource

  property :id,         Serial
  property :posted_by,  String   # IP address of submitter
  property :body,       Text
  property :created_at, DateTime # A DateTime, for any date you might like.

  belongs_to :post

end

DataMapper.finalize
require  'dm-migrations'
DataMapper.auto_migrate!
# Create or upgrade all tables at once, like magic
# DataMapper.auto_upgrade!

Need to go back and see about auto-upgrade!

HAML

You can set the default format with the set command. You can set utf-8 for content in a before filter. You can use the haml command to render haml views.

set :haml, {:format => :html5 } # default Haml format is :xhtml

# filter
# set utf-8 for outgoing
before do
 headers "Content-Type" => "text/html; charset=utf-8"
end

## Handlers
#
#
get '/' do
 haml :root
end

Helpers

Helpers for views can be added and used in a similar way as Rails.

helpers do
  def post_image(post)
    url = post.url
    image_str = "<img src=" + url + '>'
    "<a href='/comments/" + post.id.to_s + "'>" + image_str + "</a>"
  end

  def how_long_ago(comment)
    diff = DateTime.now - comment.created_at
    hours, mins, secs, ignore_fractions = Date::day_fraction_to_time(diff)
    "%02d:%02d" % [ mins, secs ] + " minutes ago:"
  end
end

SASS

I stumbled a bit on setting this up. There are some incorrect posts out there. This worked for me. NOTE: the styles.sass file goes in the ./views directory.

get '/style.css' do
  content_type 'text/css', :charset => 'utf-8'
  sass :style
end

Heroku

.gem file

Ok, now to publish on heroku. The first thing to do is create a .gems file in the project directory. While most of these things are very straight forward, you have to add a database adapter. In the case of Heroku that means postgres.Note that in development I was using sqlite3.

Caveat: I don’t think you need to specify both the datamapper library and it’s individual libraries.

sinatra -v 1.0
dm-core --version 1.0.0
dm-timestamps --version 1.0.0
dm-migrations --version 1.0.0
haml --version 3.0.9
datamapper
dm-postgres-adapter
ffaker

config.ru

Nothing complicated required here.

require 'pthingy'
run Sinatra::Application

And that’s it! It should work….

As long as you didn’t do something wrong, of course.

 



This entry was posted on Wednesday, September 1st, 2010 at 11:06 am and is filed under DataMapper, HAML, Heroku, SASS, Sinatra. You can follow any responses to this entry through the RSS 2.0 feed. Responses are currently closed, but you can trackback from your own site.

Related Posts

  • No related posts found.

18 Responses to ' Sinatra, Datamapper, Sass and Heroku '

Subscribe to comments with RSS or TrackBack to ' Sinatra, Datamapper, Sass and Heroku '.

  1. 1

    on September 2nd, 2010 at 1:38 pm

    Nice post.

  2. 2
    dgoldie said,

    on September 6th, 2010 at 5:57 pm

    Hey thanks,

    especially for reminding me about the Firefox beta. I had forgotten about it. Unfortunately I can’t reproduce your issues; for me it gives me the same experience — though I am on a Mac.
    In any case, I guess it doesn’t matter, because I really can’t change much. I didn’t build this blog software — it’s actually on WordPress for the time being.

    but thanks,
    -doug.

  3. 3

    on September 13th, 2010 at 2:52 am

    I found this information interesting.

  4. 4
    James Lagant said,

    on September 22nd, 2010 at 9:28 am

    I trust you’d write more write ups. It’s definitely worth reading.

  5. 5
    fsbo said,

    on September 25th, 2010 at 4:41 am

    Hi. I read a few of your other posts and i wanted to say thank you for the informative posts.

  6. 6
    mls said,

    on September 25th, 2010 at 4:41 am

    I’m not such an expert when it comes to this. Useful read, appreciate your posting this.

  7. 7
    annuaire said,

    on September 25th, 2010 at 5:33 am

    Nice . . keep it up

  8. 8
    ntp for 2003 said,

    on September 28th, 2010 at 1:22 am

    This is a interesting post.

  9. 9

    on September 28th, 2010 at 12:06 pm

    Pretty perspective post. Never thought that it was this straightforward after all. I have spent a a lot of my time looking for someone to clarify this subject clearly and you’re the only person that ever did that. Kudos to you! Excellent job, I can’t wait to read more from you!

  10. 10

    on October 11th, 2010 at 2:32 am

    Informative post, this is. It is always nice to come across a post that is useful.

  11. 11

    on October 11th, 2010 at 5:00 am

    Wonderful learn, I simply passed this onto a colleague who was in search of this info. I’ve you bookmarked on Digg.

  12. 12

    on October 14th, 2010 at 12:50 am

    We just couldnt leave your website before saying that I really enjoyed the quality information you offer to your visitors? Will be back soon to check up on new posts

  13. 13

    on October 14th, 2010 at 2:45 am

    Good share, great article, very usefull for us…thanks.

  14. 14

    on October 18th, 2010 at 4:03 am

    Good share, great article, very usefull for us…thanks.

  15. 15

    on October 24th, 2010 at 3:52 pm

    After reading this I thought it was very informative. I appreciate you taking the time to put this blog piece together. I once again find myself spending way to much time both reading and commenting. What ever, it was still worth it

  16. 16

    on October 25th, 2010 at 3:07 am

    It’s really great post, nice blog..I am really looking forward to your next post. very good post informative content, Thanks for sharing!

  17. 17

    on November 30th, 2010 at 7:07 pm

    Congratulations for posting such a useful weblog. Your blog is not only informative but also extremely artistic too. There usually are very couple of people who can write not so simple articles that creatively. Keep up the great writing !!

  18. 18

    on December 1st, 2010 at 2:05 am

    Tremendous! This particular is all I can think pertaining to a blog post like this excellent. This kind of is literally a notably explanatory article post on the blog. You just need to know a lot about this amazing

Sorry, the comment form is closed at this time.