When I (re)started this project, I chose Sinatra to serve my content. I needed a database engine too, and for that I chose DataMapper.
Turns out it was harder than I thought.
Before we start, you will need to install these gems:
$ (sudo) gem install dm-core dm-migrations
bq. Alright! Done. Let’s do this, John. What should I do next?
Fire up your text editor and skip Sinatra’s one file approach and set up your database.rb like this:
# @database.rb
require "dm-core"
require "dm-migrations"
# Configure DataMapper
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://database.db")
# model Article
class Article
include DataMapper::Resource
property :id, Serial
property :title, Text
property :text, Text
property :author, String
property :permalink, Text
property :created_at, DateTime
end
# Update database
DataMapper.auto_upgrade!
class String
# Fix DataMapper's weird space bug
def rm_ln
self.gsub(/\n\n|\n/, "\r")
end
def rm_ln!
self.gsub(/\n\n|\n/, "")
end
end
First, we require the DataMapper gems we installed before. Then, we configure DataMapper to use our database. If ENV['DATABASE_URL'] is set, it uses that instead of sqlite3://database.db. This is necessary for Heroku.
After that, we create the Article model with a bunch of properties. Below, we update the database to use our new Article model.
What we do next, is adding our rm_ln methods to any String object (like mystring = "i'm a string!"). This fixes a strange bug in DataMapper, which adds 12 lines of space in front of some lines in a Text property when you save it. Extremely annoying if you use <pre> elements in your articles, and when you edit them.
In other words, just append .rm_ln to a string and the bug will be solved.
bq. But John, what’s the rm_ln! method for?
That I will tell you, but it’s complicated. If you use my rm_ln method in a textarea (when you edit an article, for example), the lines will mess up. You must use rm_ln! instead.
But when you want to output a string as HTML, as in article templates, you must use rm_ln, and not rm_ln!.
bq. Okay. This means I have to use it everywhere?
Yes. This isn’t the best solution, since you have to append .rm_ln everywhere you want a Text property.
You can add a method to our database.rb to solve this in a better way. Something like this:
# @database.rb
def text(text, htmloutput = true)
if htmloutput
return text.rm_ln
else
return text.rm_ln!
end
end
…and use text @the_text instead. It’s the same thing, I know, but more obvious for others reading your code.
I’ll give you an example to use in your app.rb, or whatever:
# @app.rb
get '/archive' do
@articles = Article.all :limit => 10, :order => 'created_at'
erb :articles # or haml, whatever
end
get '/archive/:permalink' do |permalink|
@article = Article.first :permalink => permalink
erb :article
end
h3(#bonus-tip-pagination). Bonus tip: Pagination
Use my ruby_pagination_logic gem to add pagination to your site. Super-recommended, to improve your site load times.
Be sure to check out the documentation and Github repo.
h3(#questions-or-comments). Questions or comments?
Reply! I will, sooner or later, add a Disqus section.