Starting a Merb + DataMapper project
7 easy steps to get started with a Merb and DataMapper project.
Here’s a quick transcript of what I did to create a brand new Merb + DataMapper project. I called the project ‘tasks’ and I’ll use DataMapper as my ORM and RSpec as the testing framework.
A couple of things before we begin. First, I wrote this as of Merb 0.9.2 and DataMapper 0.3.0, so keep that in mind when you refer back to this later on. Secondly, I feature a step in here where we freeze merb and a couple other gems into the project. This can be skipped if you’re not into that sorta thing, but I do highly suggest it.
Step Zero
Getting things setup is always a good place to start:
1 cd ~/Projects 2 mkdir tasks
This is a good place to initialize any SCM you use. I’ll leave that up to you.
Step One
1 merb-gen app tasks 2 3 cd tasks/ 4 5 gem install merb -i gems --no-rdoc --no-ri 6 gem install merb_datamapper -i gems --no-rdoc --no-ri
merb-gen app tasks generates a “full-stack” style merb application, with the folder organization akin to rails. Then we cd into our brand-spankin-new app and freeze some of the major gems into the project. I highly suggest you don’t skip this step, especially if you plan on deploying this application or working on more than one computer.
The --no-rdoc and --no-ri flags tell gem not to generate the rdoc and ri documentation for each gem. This’ll save you a bunch of headache later on when you try to “Go To File..” in your IDE. That, and you really don’t need them since they’re already chilling on your box in your default gem repo.
Next, to make sure that Merb uses the merb you just froze into gems/
1 merb-gen freezer frozen-merb
From here on out, use script/frozen-merb to start up your app.
Step Two
1 mkdir -p public/javascripts 2 mkdir -p logs
Most web applications are going to have javascript somewhere in them, and public/javascripts wasn’t created for us, so we go ahead and do that. Along the way, we create the logs/ directory as well. Mostly a convenience thing.
Step Three
So now we have a completely generated, yet completely blank web application. Let’s start some configurations:
1 open config/init.rb
This is the file that Merb uses to configure itself when we launch it. We’ve got a couple of things to do here:
1 use_orm :datamapper # uncomment this line 2 use_test :rspec # same here 3 4 c[:session_store] = 'datamapper' # replace 'cookie' with 'datamapper'
The use_orm directive registers DataMapper with Merb. The use_test directive tells Merb that Rspec is involved; technically this isn’t needed, but it never hurts to be clear about what exactly is going on. Finally, we tweak the default setting for where to store sessions, in this case we’re dumping them in our data store.
Step Four
Boot the thing
1 ./script/frozen-merb
...and watch it bomb.
It should tell us that database.yml wasn't found and then generate a database.yml.sample. Now we copy that bad-boy into config/database.yml and drop in our database configuration settings.
I would suggests that you tell your SCM to ignore database.yml but not database.yml.sample, that way you can still pass around common configurations, but still avoid sharing your personal database’s authentication info.
Moving on…
Step Five
Alright, time to actually start coding our tasks project. First off, we need a Task resource.
1 merb-gen resource task
This’ll create all the models and controllers and helpers involved in representing our Resource. It doesn’t, however, set up routes or migrate your database, so lets do that now
1 open config/router.rb
1 r.resources :tasks # insert near commented out "r.resources :posts" 2 3 r.match('/').to(:controller => 'tasks', :action =>'index') # insert near "# r.match('/').to(:controller ...."
The r.resources :tasks registers our RESTful resource with Merb’s router, which then gives us access to all the restful URIs we could possibly want.
After that, we told Merb to route any requests for ”/” to the index page of our tasks controller. Pretty simple stuff.
1 rake dm:db:automigrate
This auto-migrates our database, creating the Sessions table and the Tasks table. We can actually start coding the application now.
Step Six
Beer