Integrate DataMapper with Rails 3
= dm-rails
This gem provides the railtie that allows {datamapper}[http://github.com/datamapper/dm-core] to hook into {rails3}[http://github.com/rails/rails] and thus behave like a rails framework component. Just like activerecord does in rails, {dm-rails}[http://github.com/datamapper/dm-rails] uses the railtie API to hook into rails. The two are actually hooked into rails almost identically.
Creating new datamapper apps on rails3 from scratch is actually really easy. The following will guide you through the process.
== Generating a new application from scratch
To go from zero gems to a working rails3 app using datamapper, all you need are the latest {rubygems}[http://docs.rubygems.org/read/chapter/3] release, {bundler}[http://gembundler.com] and {rails}[http://github.com/rails/rails]. After possibly updating your rubygems, issue the following:
gem install rails
Once you have {rails}[http://github.com/rails/rails] and thus {bundler}[http://github.com/carlhuda/bundler] installed, you can bootstrap a rails application with a single command.
rails new project_name -m http://datamapper.org/templates/rails.rb
When run, the command will print out some options on how to proceed with your newly generated application.
Be aware that this command executes code loaded from the internet! It is currently the simplest way to bootstrap a new application, and a commonly used way to reuse rails application templates. Have a look at {rails templates}[http://github.com/datamapper/datamapper.github.com/tree/master/templates/] to know exactly what we'll do to your system, but be aware that a man-in-the-middle attack could alter that.
== rspec support
I haven't yet tested rspec support extensively, but the basics are working after a few modifications to the spec/spec_helper.rb file that the necessary
rails generate rspec:install
gives us. First you need to uncomment/remove a seemingly invalid rspec configuration option that gets set by default, and you also should turn off transactional_fixtures at least for now. It's pretty common to run DataMapper.auto_migrate! before all specs too. My spec_helper.rb file currently looks like the following, and model specs run fine.
ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", FILE) require 'rspec/rails'
Dir["#{File.dirname(FILE)}/support/**/*.rb"].each {|f| require f}
RSpec.configure do |config|
# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
config.mock_with :rspec
config.before(:suite) { DataMapper.auto_migrate! }end
With this in place, running
./bin/rake spec ./bin/rake spec:models ...
should work fine. In order to have ./bin/rake available, the bundle needs to be installed with
bundle install --binstubs
If you haven't done so for whatever reason, use
bundle exec rake spec bundle exec rake spec:models
instead.
You may have noticed that rspec is included into both the :development and :test groups in the Gemfile the application template generated. I couldn't find another way to make it so that the above mentioned way of running the specs work. If rspec is not included into the :development group, rails must be informed explicitly about the environment it should run in, by prefixing the above rake commands with RAILS_ENV=test.
== Developing dm-rails
Issue the following commands to get a fully functional development environment including datamapper and rails up and running within a minute.
gem install bundler # if this is new for you bundle install
Whenever you want to update your dependencies because you want to make sure you develop against master branches, just issue
bundle install
again and {bundler}[http://github.com/carlhuda/bundler] will go ahead and fetch the latest commits from the gems you depend on.
== Sample Gemfile
Using {bundler}[http://github.com/carlhuda/bundler] it's really easy to get an app going with datamapper and rails3. Just use a Gemfile like this, and bundler will pull in everything needed to run your app. Note that you also must add any additional datamapper plugin or any other gem that you'd like to use to the Gemfile. This makes sure that bundler is able to provide a complete environment containing all required dependencies for your app.
source 'http://rubygems.org'
RAILS_VERSION = '> 3.0.4'
DM_VERSION = '> 1.1.0'
RSPEC_VERSION = '~> 2.5.0'
gem 'activesupport', RAILS_VERSION, :require => 'active_support' gem 'actionpack', RAILS_VERSION, :require => 'action_pack' gem 'actionmailer', RAILS_VERSION, :require => 'action_mailer' gem 'railties', RAILS_VERSION, :require => 'rails'
gem 'dm-rails', '~> 1.1.0' gem 'dm-sqlite-adapter', DM_VERSION
gem 'dm-migrations', DM_VERSION gem 'dm-types', DM_VERSION gem 'dm-validations', DM_VERSION gem 'dm-constraints', DM_VERSION gem 'dm-transactions', DM_VERSION gem 'dm-aggregates', DM_VERSION gem 'dm-timestamps', DM_VERSION gem 'dm-observer', DM_VERSION
group(:test) do
gem 'rspec-rails', RSPEC_VERSION
# To get a detailed overview about what queries get issued and how long they take
# have a look at rails_metrics. Once you bundled it, you can run
#
# rails g rails_metrics Metric
# rake db:automigrate
#
# to generate a model that stores the metrics. You can access them by visiting
#
# /rails_metrics
#
# in your rails application.
# gem 'rails_metrics', '~> 0.1', :git => 'git://github.com/engineyard/rails_metrics'end
== Sample database.yml files
DataMapper supports connecting to and working with multiple repositories easily. In order to be able to take full advantage of that feature in rails, you can configure as many repositories for your different environments as you wish. All you need to do is follow some simple naming conventions and you're good to go. Have a look at the #{adapter}_defaults declarations in the sample files below. That's the only convention you need to follow. Your default declarations should always end with "defaults". This is necessary for dm-rails to not confuse these with any of your environment declarations. In fact, dm-rails looks at the content of your database.yml and rejects every key that matches /defaults/. The remaining entries represent the repository configurations for the available environments.
An example for setting up a single repository for every environment. This will setup an implicit 'default' repository.
defaults: &defaults adapter: mysql username: root password: host: localhost
development: database: rails3_app_development require 'pp' => ["PP"] ruby-1.8.7-p248 > pp Rails::DataMapper.configuration
{"adapter"=>"mysql",
"username"=>"root",
"database"=>"alfred_production",
"host"=>"localhost",
"password"=>nil},
"development"=>
{"adapter"=>"mysql",
"username"=>"root",
"database"=>"alfred_development",
"host"=>"localhost",
"password"=>nil},
"defaults"=>
{"username"=>"root",
"adapter"=>"mysql",
"host"=>"localhost",
"password"=>nil},
"test"=>
{"adapter"=>"mysql",
"username"=>"root",
"database"=>"alfred_test",
"host"=>"localhost",
"password"=>nil}},
@repositories= {"production"=> {"default"=> {"adapter"=>"mysql", "username"=>"root", "database"=>"alfred_production", "host"=>"localhost", "password"=>nil}}, "development"=> {"default"=> {"adapter"=>"mysql", "username"=>"root", "database"=>"alfred_development", "host"=>"localhost", "password"=>nil}}, "test"=> {"default"=> {"adapter"=>"mysql", "username"=>"root", "database"=>"alfred_test", "host"=>"localhost", "password"=>nil}}}, @root=#>
=> nil
Additionally, you can reach the configuration object via the standard way that rails provides to expose configuration for framework components and plugins.
Rails.application.config.data_mapper
This will give you the exact same object we inspected in the previous pretty print output from rails console.
== Extending dm-rails
It's easy to extend or adapt dm-rails to meet your specific needs. Thanks to the railties API it's possible to hook into any part of the initialization process. In order to customize dm-rails, all you need to do is define your own Rails::Railtie class that inherits from Rails::DataMapper::Railtie and require that instead of the standard dm-rails/railtie. During initialization of any rails plugin, the initializers defined by that plugin are run in the order specified. Since dm-rails defines every action that gets called by its initializers as a method on either the Railtie instance or the class, you can just go ahead and overwrite these in your subclass. All those methods get called with the running Rails::Application (always reachable via Rails.application) as single parameter, so you can customize depending on the app's state as much as you wish. Additionally, the initializers are all named, and you can hook your own additional initializers before or after any of the named rails (or dm-rails) initializers.
To give you an idea of what you get when inheriting from Rails::DataMapper::Railtie have a look at the list of methods provided by that object.
Rails::DataMapper::Railtie.configure_data_mapper(app) Rails::DataMapper::Railtie.setup_i18n_support(app) Rails::DataMapper::Railtie#setup_controller_runtime(app) Rails::DataMapper::Railtie#setup_logger(app)
To complete the picture of dm-rails's initialization process, here's an overview of the defined initializers in the order they are called by rails during bootup. Note that every one of these initializers does one single thing; calling one of the methods listed above. This makes it easy to customize each of these steps by overwriting the respective method.
initializer 'data_mapper.configuration' do |app| configure_data_mapper(app) end
initializer 'data_mapper.logger' do |app| setup_logger(app, Rails.logger) end
initializer 'data_mapper.i18n_support' do |app| setup_i18n_support(app) end
initializer "data_mapper.log_runtime" do |app| setup_controller_runtime(app) end
initializer "datamapper.add_to_prepare" do |app| config.to_prepare { Rails::DataMapper.preload_models(app) } end
No open issues yet, or sync has not completed.