tpitale-dm-rails 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/.document +5 -0
  2. data/Gemfile +63 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +595 -0
  5. data/Rakefile +27 -0
  6. data/VERSION +1 -0
  7. data/dm-rails.gemspec +94 -0
  8. data/lib/dm-rails/configuration.rb +76 -0
  9. data/lib/dm-rails/mass_assignment_security.rb +89 -0
  10. data/lib/dm-rails/middleware/identity_map.rb +20 -0
  11. data/lib/dm-rails/multiparameter_attributes.rb +167 -0
  12. data/lib/dm-rails/railtie.rb +100 -0
  13. data/lib/dm-rails/railties/controller_runtime.rb +45 -0
  14. data/lib/dm-rails/railties/database.rake +106 -0
  15. data/lib/dm-rails/railties/i18n_support.rb +12 -0
  16. data/lib/dm-rails/railties/log_listener.rb +39 -0
  17. data/lib/dm-rails/railties/log_subscriber.rb +54 -0
  18. data/lib/dm-rails/session_store.rb +77 -0
  19. data/lib/dm-rails/setup.rb +84 -0
  20. data/lib/dm-rails/storage.rb +209 -0
  21. data/lib/dm-rails.rb +1 -0
  22. data/lib/generators/data_mapper/migration/migration_generator.rb +30 -0
  23. data/lib/generators/data_mapper/migration/templates/migration.rb +23 -0
  24. data/lib/generators/data_mapper/model/model_generator.rb +23 -0
  25. data/lib/generators/data_mapper/model/templates/model.rb +11 -0
  26. data/lib/generators/data_mapper/observer/observer_generator.rb +19 -0
  27. data/lib/generators/data_mapper/observer/templates/observer.rb +7 -0
  28. data/lib/generators/data_mapper.rb +82 -0
  29. data/spec/models/fake.rb +31 -0
  30. data/spec/models/topic.rb +11 -0
  31. data/spec/spec.opts +3 -0
  32. data/spec/spec_helper.rb +19 -0
  33. data/spec/unit/mass_assignment_security_spec.rb +43 -0
  34. data/spec/unit/multiparameter_attributes_spec.rb +168 -0
  35. data/tasks/clean.rake +6 -0
  36. data/tasks/yard.rake +9 -0
  37. data/tasks/yardstick.rake +20 -0
  38. metadata +161 -0
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/Gemfile ADDED
@@ -0,0 +1,63 @@
1
+ require 'pathname'
2
+
3
+ source 'http://rubygems.org'
4
+
5
+ SOURCE = ENV.fetch('SOURCE', :git).to_sym
6
+ REPO_POSTFIX = SOURCE == :path ? '' : '.git'
7
+ DATAMAPPER = SOURCE == :path ? Pathname(__FILE__).dirname.parent : 'http://github.com/datamapper'
8
+ DM_VERSION = '~> 1.2.0'
9
+ DO_VERSION = '~> 0.10.6'
10
+ RAILS_VERSION = '~> 3.1.0'
11
+ DM_DO_ADAPTERS = %w[ sqlite postgres mysql oracle sqlserver ]
12
+ CURRENT_BRANCH = ENV.fetch('GIT_BRANCH', 'master')
13
+
14
+ # DataMapper dependencies
15
+ gem 'dm-core', DM_VERSION, SOURCE => "#{DATAMAPPER}/dm-core#{REPO_POSTFIX}", :branch => CURRENT_BRANCH
16
+ gem 'dm-active_model', DM_VERSION, SOURCE => "#{DATAMAPPER}/dm-active_model#{REPO_POSTFIX}", :branch => CURRENT_BRANCH
17
+
18
+ # Rails dependencies
19
+ gem 'actionpack', RAILS_VERSION, :require => 'action_pack'
20
+ gem 'railties', RAILS_VERSION, :require => 'rails'
21
+
22
+ group :development do
23
+
24
+ gem 'jeweler', '~> 1.6.4'
25
+ gem 'rake', '~> 0.9.2'
26
+ gem 'rspec', '~> 1.3.2'
27
+
28
+ end
29
+
30
+ platforms :mri_18 do
31
+ group :quality do
32
+
33
+ gem 'rcov', '~> 0.9.10'
34
+ gem 'yard', '~> 0.7.2'
35
+ gem 'yardstick', '~> 0.4'
36
+
37
+ end
38
+ end
39
+
40
+ group :datamapper do
41
+ adapters = ENV['ADAPTER'] || ENV['ADAPTERS']
42
+ adapters = adapters.to_s.tr(',', ' ').split.uniq - %w[ in_memory ]
43
+
44
+ if (do_adapters = DM_DO_ADAPTERS & adapters).any?
45
+ do_options = {}
46
+ do_options[:git] = "#{DATAMAPPER}/do#{REPO_POSTFIX}" if ENV['DO_GIT'] == 'true'
47
+
48
+ gem 'data_objects', DO_VERSION, do_options.dup
49
+
50
+ do_adapters.each do |adapter|
51
+ adapter = 'sqlite3' if adapter == 'sqlite'
52
+ gem "do_#{adapter}", DO_VERSION, do_options.dup
53
+ end
54
+
55
+ gem 'dm-do-adapter', DM_VERSION, SOURCE => "#{DATAMAPPER}/dm-do-adapter#{REPO_POSTFIX}", :branch => CURRENT_BRANCH
56
+ end
57
+
58
+ gem 'dm-migrations', DM_VERSION, SOURCE => "#{DATAMAPPER}/dm-migrations#{REPO_POSTFIX}", :branch => CURRENT_BRANCH
59
+
60
+ adapters.each do |adapter|
61
+ gem "dm-#{adapter}-adapter", DM_VERSION, SOURCE => "#{DATAMAPPER}/dm-#{adapter}-adapter#{REPO_POSTFIX}", :branch => CURRENT_BRANCH
62
+ end
63
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011-2010 The dm-rails team
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,595 @@
1
+ = dm-rails
2
+
3
+ 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.
4
+
5
+ Creating new datamapper apps on rails3 from scratch is actually really easy. The following will guide you through the process.
6
+
7
+ == Generating a new application from scratch
8
+
9
+ 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:
10
+
11
+ gem install rails
12
+
13
+ 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.
14
+
15
+ rails new project_name -m http://datamapper.org/templates/rails.rb
16
+
17
+ When run, the command will print out some options on how to proceed with your newly generated application.
18
+
19
+ 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.
20
+
21
+ == rspec support
22
+
23
+ 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
24
+
25
+ rails generate rspec:install
26
+
27
+ 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.
28
+
29
+ # This file is copied to ~/spec when you run 'ruby script/generate rspec'
30
+ # from the project root directory.
31
+ ENV["RAILS_ENV"] ||= 'test'
32
+ require File.expand_path("../../config/environment", __FILE__)
33
+ require 'rspec/rails'
34
+
35
+ # Requires supporting files with custom matchers and macros, etc,
36
+ # in ./support/ and its subdirectories.
37
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
38
+
39
+ RSpec.configure do |config|
40
+
41
+ # == Mock Framework
42
+ #
43
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
44
+ #
45
+ # config.mock_with :mocha
46
+ # config.mock_with :flexmock
47
+ # config.mock_with :rr
48
+ config.mock_with :rspec
49
+
50
+ config.before(:suite) { DataMapper.auto_migrate! }
51
+
52
+ end
53
+
54
+ With this in place, running
55
+
56
+ ./bin/rake spec
57
+ ./bin/rake spec:models
58
+ ...
59
+
60
+ should work fine. In order to have ./bin/rake available, the bundle needs to be installed with
61
+
62
+ bundle install --binstubs
63
+
64
+ If you haven't done so for whatever reason, use
65
+
66
+ bundle exec rake spec
67
+ bundle exec rake spec:models
68
+
69
+ instead.
70
+
71
+ 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`.
72
+
73
+ == Developing dm-rails
74
+
75
+ Issue the following commands to get a fully functional development environment including datamapper and rails up and running within a minute.
76
+
77
+ gem install bundler # if this is new for you
78
+ bundle install
79
+
80
+ Whenever you want to update your dependencies because you want to make sure you develop against master branches, just issue
81
+
82
+ bundle install
83
+
84
+ again and {bundler}[http://github.com/carlhuda/bundler] will go ahead and fetch the latest commits from the gems you depend on.
85
+
86
+
87
+ == Sample Gemfile
88
+
89
+ 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.
90
+
91
+ source 'http://rubygems.org'
92
+
93
+ RAILS_VERSION = '~> 3.0.4'
94
+ DM_VERSION = '~> 1.1.0'
95
+ RSPEC_VERSION = '~> 2.5.0'
96
+
97
+ gem 'activesupport', RAILS_VERSION, :require => 'active_support'
98
+ gem 'actionpack', RAILS_VERSION, :require => 'action_pack'
99
+ gem 'actionmailer', RAILS_VERSION, :require => 'action_mailer'
100
+ gem 'railties', RAILS_VERSION, :require => 'rails'
101
+
102
+ gem 'dm-rails', '~> 1.1.0'
103
+ gem 'dm-sqlite-adapter', DM_VERSION
104
+
105
+ # You can use any of the other available database adapters.
106
+ # This is only a small excerpt of the list of all available adapters
107
+ # Have a look at
108
+ #
109
+ # http://wiki.github.com/datamapper/dm-core/adapters
110
+ # http://wiki.github.com/datamapper/dm-core/community-plugins
111
+ #
112
+ # for a rather complete list of available datamapper adapters and plugins
113
+
114
+ # gem 'dm-sqlite-adapter', DM_VERSION
115
+ # gem 'dm-mysql-adapter', DM_VERSION
116
+ # gem 'dm-postgres-adapter', DM_VERSION
117
+ # gem 'dm-oracle-adapter', DM_VERSION
118
+ # gem 'dm-sqlserver-adapter', DM_VERSION
119
+
120
+ gem 'dm-migrations', DM_VERSION
121
+ gem 'dm-types', DM_VERSION
122
+ gem 'dm-validations', DM_VERSION
123
+ gem 'dm-constraints', DM_VERSION
124
+ gem 'dm-transactions', DM_VERSION
125
+ gem 'dm-aggregates', DM_VERSION
126
+ gem 'dm-timestamps', DM_VERSION
127
+ gem 'dm-observer', DM_VERSION
128
+
129
+ group(:test) do
130
+
131
+ gem 'rspec-rails', RSPEC_VERSION
132
+
133
+ # To get a detailed overview about what queries get issued and how long they take
134
+ # have a look at rails_metrics. Once you bundled it, you can run
135
+ #
136
+ # rails g rails_metrics Metric
137
+ # rake db:automigrate
138
+ #
139
+ # to generate a model that stores the metrics. You can access them by visiting
140
+ #
141
+ # /rails_metrics
142
+ #
143
+ # in your rails application.
144
+
145
+ # gem 'rails_metrics', '~> 0.1', :git => 'git://github.com/engineyard/rails_metrics'
146
+
147
+ end
148
+
149
+
150
+ == Sample database.yml files
151
+
152
+ 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.
153
+
154
+ An example for setting up a single repository for every environment.
155
+
156
+ defaults: &defaults
157
+ adapter: mysql
158
+ username: root
159
+ password:
160
+ host: localhost
161
+
162
+ development:
163
+ database: rails3_app_development
164
+ <<: *defaults
165
+ test:
166
+ database: rails3_app_test
167
+ <<: *defaults
168
+ production:
169
+ database: rails3_app_production
170
+ <<: *defaults
171
+
172
+ An example of setting up a single repository with a URI, which overrides all other configuration for that environment:
173
+
174
+ development:
175
+ uri: mysql://localhost/rails3_app_development?user=mysql
176
+
177
+ An example for setting up multiple repositories for every environment.
178
+
179
+ mysql_defaults: &mysql_defaults
180
+ adapter: mysql
181
+ username: mysql_user
182
+ password: mysql_sekret
183
+ host: localhost
184
+
185
+ postgres_defaults: &postgres_defaults
186
+ adapter: postgres
187
+ username: postgres_user
188
+ password: postgres_sekret
189
+ host: postgres_host
190
+
191
+ oracle_defaults: &oracle_defaults
192
+ adapter: oracle
193
+ username: oracle_user
194
+ password: oracle_sekret
195
+ host: oracle_host
196
+
197
+
198
+ development:
199
+ database: rails3_mysql_development
200
+ <<: *mysql_defaults
201
+ repositories:
202
+ foo:
203
+ database: rails3_postgres_development
204
+ <<: *postgres_defaults
205
+ bar:
206
+ database: rails3_oracle_development
207
+ <<: *oracle_defaults
208
+
209
+ test:
210
+ database: rails3_mysql_test
211
+ <<: *mysql_defaults
212
+ repositories:
213
+ foo:
214
+ database: rails3_postgres_test
215
+ <<: *postgres_defaults
216
+ bar:
217
+ database: rails3_oracle_test
218
+ <<: *oracle_defaults
219
+
220
+ production:
221
+ database: rails3_mysql_production
222
+ <<: *mysql_defaults
223
+ repositories:
224
+ foo:
225
+ database: rails3_postgres_production
226
+ <<: *postgres_defaults
227
+ bar:
228
+ database: rails3_oracle_production
229
+ <<: *oracle_defaults
230
+
231
+ Once you have defined your database.yml file, dm-rails's rake tasks will be able to create, drop, auto_migrate! and auto_upgrade! all your defined repositories.
232
+
233
+ == Available generators
234
+
235
+ Since the new generators provide hooks that allow the dm-rails gem to provide generators that hook into parts like model, scaffolds or test generation, everything just works like it does with active_record.
236
+
237
+ The following generators are available to help you get started with the typical components of any rails application.
238
+
239
+ ...
240
+ rails generate controller
241
+ rails generate generator
242
+ rails generate helper
243
+ rails generate integration_test
244
+ rails generate migration
245
+ rails generate model
246
+ rails generate observer
247
+ rails generate performance_test
248
+ rails generate plugin
249
+ rails generate resource
250
+ rails generate scaffold
251
+ rails generate scaffold_controller
252
+ rails generate session_migration
253
+ rails generate stylesheets
254
+ ...
255
+
256
+ For a complete list run the following in your project's directory
257
+
258
+ rails generate
259
+
260
+
261
+ == Available datamapper specific rake tasks
262
+
263
+ To get a list of all available rake tasks in your rails3 app, issue the usual
264
+
265
+ rake -T
266
+
267
+ Once you do that, you will see the following rake tasks among others. These are the ones that dm-rails added for us.
268
+
269
+ ...
270
+ rake db:automigrate # Perform destructive automigration of all repositories in the current Rails.env
271
+ rake db:autoupgrade # Perform non destructive automigration of all repositories in the current Rails.env
272
+ rake db:create # Create the database(s) defined in config/database.yml for the current Rails.env - also creates the test database(s) if Rails.env.development?
273
+ rake db:create:all # Create all the local databases defined in config/database.yml
274
+ rake db:drop # Drops the database(s) for the current Rails.env - also drops the test database(s) if Rails.env.development?
275
+ rake db:drop:all # Drop all the local databases defined in config/database.yml
276
+ rake db:migrate # Migrate the database to the latest version
277
+ rake db:migrate:down[version] # Migrate down using migrations
278
+ rake db:migrate:up[version] # Migrate up using migrations
279
+ rake db:seed # Load the seed data from db/seeds.rb
280
+ rake db:sessions:clear # Clear the sessions table for DataMapperStore
281
+ rake db:sessions:create # Creates the sessions table for DataMapperStore
282
+ rake db:setup # Create the database, load the schema, and initialize with the seed data
283
+ ...
284
+
285
+
286
+ == Configuring and introspecting dm-rails
287
+
288
+ Rails3 makes it easy to expose framework component specific configuration to application developers in a uniform and easy to use way. To build on this philosophy, dm-rails exposes its configuration via a single object that is used throughout dm-rails's code to store configuration relevant to datamapper and to rails. You can access it from within your application and of course alter it's settings in your config/application.rb or config/environments files. Here's a quick overview of the API the configuration object exposes. Expect this to grow as we come up with additional useful stuff to configure.
289
+
290
+
291
+ Rails::DataMapper.configuration
292
+ Rails::DataMapper::Configuration.for(database_yml_hash)
293
+
294
+ Rails::DataMapper::Configuration#raw
295
+ Rails::DataMapper::Configuration#environments
296
+ Rails::DataMapper::Configuration#repositories
297
+
298
+
299
+ As promised, you can easily inspect the configuration from your running rails application. Let's start a rails console and have a look what's configured for our app.
300
+
301
+ ree-1.8.7-2010.01 mungo:alfred snusnu$ rails console
302
+ Loading development environment (Rails 3.0.0.beta4)
303
+ ruby-1.8.7-p248 > require 'pp'
304
+ => ["PP"]
305
+ ruby-1.8.7-p248 > pp Rails::DataMapper.configuration
306
+ #<Rails::DataMapper::Configuration:0x103b46460
307
+ @raw=
308
+ {"production"=>
309
+ {"adapter"=>"mysql",
310
+ "username"=>"root",
311
+ "database"=>"alfred_production",
312
+ "host"=>"localhost",
313
+ "password"=>nil},
314
+ "development"=>
315
+ {"adapter"=>"mysql",
316
+ "username"=>"root",
317
+ "database"=>"alfred_development",
318
+ "host"=>"localhost",
319
+ "password"=>nil},
320
+ "defaults"=>
321
+ {"username"=>"root",
322
+ "adapter"=>"mysql",
323
+ "host"=>"localhost",
324
+ "password"=>nil},
325
+ "test"=>
326
+ {"adapter"=>"mysql",
327
+ "username"=>"root",
328
+ "database"=>"alfred_test",
329
+ "host"=>"localhost",
330
+ "password"=>nil}},
331
+ @repositories=
332
+ {"production"=>
333
+ {"default"=>
334
+ {"adapter"=>"mysql",
335
+ "username"=>"root",
336
+ "database"=>"alfred_production",
337
+ "host"=>"localhost",
338
+ "password"=>nil}},
339
+ "development"=>
340
+ {"default"=>
341
+ {"adapter"=>"mysql",
342
+ "username"=>"root",
343
+ "database"=>"alfred_development",
344
+ "host"=>"localhost",
345
+ "password"=>nil}},
346
+ "test"=>
347
+ {"default"=>
348
+ {"adapter"=>"mysql",
349
+ "username"=>"root",
350
+ "database"=>"alfred_test",
351
+ "host"=>"localhost",
352
+ "password"=>nil}}},
353
+ @root=#<Pathname:/Users/snusnu/projects/github/mine/alfred>>
354
+
355
+ => nil
356
+
357
+ Additionally, you can reach the configuration object via the standard way that rails provides to expose configuration for framework components and plugins.
358
+
359
+ Rails.application.config.data_mapper
360
+
361
+ This will give you the exact same object we inspected in the previous pretty print output from rails console.
362
+
363
+
364
+ == Extending dm-rails
365
+
366
+ 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.
367
+
368
+ 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.
369
+
370
+ Rails::DataMapper::Railtie.configure_data_mapper(app)
371
+ Rails::DataMapper::Railtie.setup_i18n_support(app)
372
+ Rails::DataMapper::Railtie#setup_controller_runtime(app)
373
+ Rails::DataMapper::Railtie#setup_logger(app)
374
+
375
+
376
+ 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.
377
+
378
+ initializer 'data_mapper.configuration' do |app|
379
+ configure_data_mapper(app)
380
+ end
381
+
382
+ initializer 'data_mapper.logger' do |app|
383
+ setup_logger(app, Rails.logger)
384
+ end
385
+
386
+ initializer 'data_mapper.i18n_support' do |app|
387
+ setup_i18n_support(app)
388
+ end
389
+
390
+ # Expose database runtime to controller for logging.
391
+ initializer "data_mapper.log_runtime" do |app|
392
+ setup_controller_runtime(app)
393
+ end
394
+
395
+ # Preload all models once in production mode,
396
+ # and before every request in development mode
397
+ initializer "datamapper.add_to_prepare" do |app|
398
+ config.to_prepare { Rails::DataMapper.preload_models(app) }
399
+ end
400
+
401
+ # Run setup code once in after_initialize to make sure all initializers
402
+ # are in effect once we setup the connection. Also, this will make sure
403
+ # that the connection gets set up after all models have been loaded,
404
+ # because #after_initialize is guaranteed to run after #to_prepare.
405
+ # Both production and development environment will execute the setup
406
+ # code only once.
407
+ config.after_initialize do |app|
408
+ Rails::DataMapper.setup(Rails.env)
409
+ end
410
+
411
+ If you want to add additional rake tasks in your extension, you can do so by adding the following to your railtie.
412
+
413
+ rake_tasks do
414
+ load 'path/to/your/tasks.rake'
415
+ end
416
+
417
+
418
+ == Identity Map support
419
+
420
+ Activating the identity map is achieved by installing a middleware that wraps the whole request inside a block
421
+
422
+ DataMapper.repository { ... }
423
+
424
+ Note that this scopes every call to datamapper to the :default repository specified in your database.yml file. If you need to access a different repository from within your actions, just wrap the calls in another DataMapper.repository block. DataMapper stacks the repositories it uses and the innermost will always win. see also next paragraph.
425
+
426
+ In order to activate the Identity Map in your application, you need to explicitly use the provided middleware (or in fact any other middleware that does the job to your liking) in any of your controllers. For example, if you want to enable the Identity Map for all controllers, you would declare to use the middleware in your ApplicationController.
427
+
428
+ require 'dm-rails/middleware/identity_map'
429
+ class ApplicationController < ActionController::Base
430
+ use Rails::DataMapper::Middleware::IdentityMap
431
+ protect_from_forgery
432
+ end
433
+
434
+ For a different scope then :default of other repositories replace/add the line with:
435
+
436
+ use Rails::DataMapper::Middleware::IdentityMap, :myscope
437
+
438
+ If you've created your application using the official templates at http://datamapper.org/templates/rails.rb this has already been added for you. If for some reason you don't want to enable the (default) Identity Map globally for all controllers, you can either just use the middleware in a few selected controllers (in case you don't have too many controllers needing it), or you can create a controller class that uses the middleware, and inherit from that controller in cases where you need Identity Map support.
439
+
440
+ # app/controllers/identity_map_controller.rb
441
+ require 'dm-rails/middleware/identity_map'
442
+ class IdentityMapController < ApplicationController
443
+ use Rails::DataMapper::Middleware::IdentityMap
444
+ end
445
+
446
+ # app/controllers/people_controller.rb
447
+ class PeopleController < IdentityMapController
448
+ # ...
449
+ end
450
+
451
+ == Cucumber testing framework support
452
+
453
+ dm-rails ist working fine with the cucumber testing framework out of the box. In order to have transaction support in cucumber, so that the data generated by the different scenarios doesn't cause problems, create a file "RAILS_APP/features/support/datamapper.rb" with the following contents:
454
+
455
+ Before do
456
+ repository(:default) do |repository|
457
+ transaction = DataMapper::Transaction.new(repository)
458
+ transaction.begin
459
+ repository.adapter.push_transaction(transaction)
460
+ end
461
+ end
462
+
463
+ After do
464
+ repository(:default).adapter.pop_transaction.rollback
465
+ end
466
+
467
+ This simply wraps each scenario in a transaction which is rolled back.
468
+
469
+ If your underlying database doesn't support transactions, you can instead use this code to clean/ truncate the whole database after each scenario (not dm-rails specific):
470
+
471
+ require 'database_cleaner'
472
+ require 'database_cleaner/cucumber'
473
+ DatabaseCleaner.strategy = :truncation
474
+
475
+ == Using adapter specific Resource naming conventions
476
+
477
+ Say you want to namespace your models and don't want your storage names to reflect your module nesting. DataMapper provides easy ways to use just the naming conventions you like. Basically, all you need to do is tell dm-rails and thus DataMapper that you either want to use a predefined naming convention
478
+
479
+ # in some config/initializers/file.rb
480
+ convention = DataMapper::NamingConventions::Resource::UnderscoredAndPluralizedWithoutModule
481
+ Rails::DataMapper.configuration.resource_naming_convention[:default] = convention
482
+
483
+ or that you want to use your own naming convention, that is implemented in e.g. a lambda
484
+
485
+ # in some config/initializers/file.rb
486
+ Rails::DataMapper.configuration.resource_naming_convention[:default] = lambda do |value|
487
+ 'tbl' + value.camelize(true)
488
+ end
489
+
490
+ Field names can be customized in the same manner using `Rails::DataMapper.configuration.resource_naming_convention`.
491
+
492
+ For more detailed documentation about DataMapper naming conventions and the ones that are available by default, have a look at http://rdoc.info/projects/datamapper/dm-core and search for _NamingConventions_ in the Class List.
493
+
494
+ == Mass assignment protection
495
+
496
+ By default, `dm-rails` doesn't activate any support for mass assignment protection.
497
+ You can however activate it for your application by including the relevant module
498
+ either globally into all known models, or on a per model basis (the latter being
499
+ advised, for reasons explained below).
500
+
501
+ # Global installation (config/application.rb is a good place for adding this)
502
+ #
503
+ # Make .attr_protected and .attr_accessible available to all models
504
+ # NOTE: This won't work if you have code that includes DataMapper::Resource
505
+ # into any other module. This is done by dm-is-remixable for example, so to
506
+ # be safe, you should only do that if you really know what you're doing.
507
+ # Quite some plugins make use of dm-is-remixable, so be sure to check that out
508
+ # before you go ahead and do the following in your config/application.rb
509
+ DataMapper::Model.append_inclusions(Rails::DataMapper::MassAssignmentSecurity)
510
+
511
+ # Local installation (recommended)
512
+ #
513
+ # Include the mass assignment protection only into models that actually need it
514
+ # This is the preferred way of doing things, at least for now. You will only
515
+ # have the functionality available where you actually need it, and you don't run
516
+ # into problems with third party code including DataMapper::Resource into other
517
+ # modules.
518
+
519
+ class Person
520
+
521
+ include DataMapper::Resource
522
+ include DataMapper::MassAssignmentSecurity
523
+
524
+ property :id, Serial
525
+ property :login, String
526
+
527
+ attr_protected :login
528
+
529
+ end
530
+
531
+ == Using additional datamapper plugins
532
+
533
+ In order to use additional plugins add them to the Gemfile and require them from inside a file in config/initializers. Once you've done that, update your bundle and you should be ready to use the plugin(s)
534
+
535
+ cd /path/to/your/app
536
+ # edit Gemfile
537
+ bundle install
538
+
539
+ Have a look at this application's {Gemfile}[http://github.com/snusnu/datamapper_on_rails3/blob/master/Gemfile] for an idea of how to use gems from git repositories.
540
+
541
+
542
+ == Rails notification system
543
+
544
+ Currently dm-rails publishes the same benchmarking information like active_record does. This means that you will get output like this in your log files.
545
+
546
+ Completed in 9ms (Views: 7.6ms | Models: 0.6ms) with 200
547
+
548
+ == TODO (not necessarily in that order)
549
+
550
+ * SPECS !!!
551
+ * Think about a release strategy supporting both beta releases and master branch
552
+ * Further README updates
553
+ * More work on migrations
554
+
555
+ == Credits
556
+
557
+ Big thanks to everyone working on {datamapper}[http://github.com/datamapper/dm-core], {rails}[http://github.com/rails/rails], {bundler}[http://github.com/carlhuda/bundler] and open source in general. This will be (and actually already is) an awesome platform for developing web applications.
558
+
559
+
560
+ == Note on Patches/Pull Requests
561
+
562
+ * Fork the project.
563
+ * Make your feature addition or bug fix.
564
+ * Add tests for it. This is important so I don't break it in a
565
+ future version unintentionally.
566
+ * Commit, do not mess with rakefile, version, or history.
567
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
568
+ * Send me a pull request. Bonus points for topic branches.
569
+
570
+
571
+ == The dm-rails team
572
+
573
+ Thx to all contributors, every patch, big or small is very much appreciated!
574
+
575
+ * Martin Gamsjaeger ({snusnu}[http://github.com/snusnu])
576
+ * Dan Kubb ({dkubb}[http://github.com/dkubb])
577
+ * Alex Coles ({myabc}[http://github.com/myabc])
578
+ * Alex Mankuta ({cheba}[http://github.com/cheba])
579
+ * Foy Savas ({foysavas}[http://github.com/foysavas])
580
+ * Randall Brewer
581
+ * Josh Huckabee {jhuckabee}[http://github.com/jhuckabee]
582
+ * Patrik Sundberg ({sundbp}[http://github.com/sundbp])
583
+ * Corin Langosch ({gucki}[http://github.com/gucki])
584
+ * Jared Morgan ({jm81}[http://github.com/jm81])
585
+ * Blake Gentry ({bgentry}[http://github.com/bgentry])
586
+ * Kabari Hendrick ({RipTheJacker}[http://github.com/RipTheJacker])
587
+ * Nate Mueller ({natemueller}[http://github.com/natemueller])
588
+ * Xavier Shay ({xaviershay}[http://github.com/xaviershay])
589
+ * Nico Rieck ({gix}[http://github.com/gix])
590
+ * Piotr Solnica ({solnic}[http://github.com/solnic])
591
+ * Corin Lawson ({maxsum-corin}[http://github.com/maxsum-corin])
592
+
593
+ == Copyright
594
+
595
+ Copyright (c) 2011-2010 The dm-rails team. See {LICENSE}[http://github.com/datamapper/dm-rails/blob/master/LICENSE] for details.