dm-rails 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -7,9 +7,9 @@ group :runtime do
7
7
 
8
8
  git 'git://github.com/rails/rails.git' do
9
9
 
10
- gem 'activesupport', '~> 3.0.0.beta3', :require => 'active_support'
11
- gem 'actionpack', '~> 3.0.0.beta3', :require => 'action_pack'
12
- gem 'railties', '~> 3.0.0.beta3', :require => 'rails'
10
+ gem 'activesupport', :require => 'active_support'
11
+ gem 'actionpack', :require => 'action_pack'
12
+ gem 'railties', :require => 'rails'
13
13
 
14
14
  end
15
15
 
data/README.rdoc CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
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 activercord 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
4
 
5
- Creating new datamapper apps on rails3 from scratch is actually really easy. Like, *really* easy. The following will guide you through the process.
5
+ Creating new datamapper apps on rails3 from scratch is actually really easy. The following will guide you through the process.
6
6
 
7
7
  == Generating a new application from scratch
8
8
 
@@ -13,9 +13,9 @@ It's really easy to go from zero gems to a working rails3 app using datamapper.
13
13
 
14
14
  Once you have {rails}[http://github.com/rails/rails] and thus {bundler}[http://github.com/carlhuda/bundler] installed, you can bootstrap a rails master branch application with a single command. Yes! A single command. Cool.
15
15
 
16
- rails dm_rails3_app -m http://datamapper.org/templates/rails.rb
16
+ rails new project_name -m http://datamapper.org/templates/rails.rb
17
17
 
18
- Now that was easy! When run, the command will print some options on how to proceed with your newly generated application, to the screen.
18
+ When run, the command will print out some options on how to proceed with your newly generated application.
19
19
 
20
20
  == rspec support
21
21
 
@@ -51,16 +51,14 @@ Unfortunately, I haven't yet been able to get rspec to work with the default sql
51
51
 
52
52
  and you should be good to and use rspec.
53
53
 
54
-
55
54
  == Developing dm-rails
56
55
 
57
56
  Issue the following commands to get a fully functional development environment including datamapper and rails up and running within a minute.
58
57
 
59
58
  gem install bundler # if this is new for you
60
59
  bundle install
61
- rake spec
62
60
 
63
- Now that was easy, wasn't it? Whenever you want to update your dependencies because you want to make sure you develop against master branches, just issue
61
+ Whenever you want to update your dependencies because you want to make sure you develop against master branches, just issue
64
62
 
65
63
  bundle install
66
64
 
@@ -73,21 +71,19 @@ Using {bundler}[http://github.com/carlhuda/bundler] it's really easy to get an a
73
71
 
74
72
  source 'http://rubygems.org'
75
73
 
76
- git 'git://github.com/carlhuda/bundler.git'
77
-
78
- gem 'bundler', '~> 0.9.3'
74
+ RAILS_VERSION = '~> 3.0.0.beta4'
79
75
 
80
- git 'git://github.com/rails/rails.git'
76
+ DM_VERSION = '~> 1.0.0'
81
77
 
82
- gem 'activesupport', '~> 3.0.0.beta1', :require => 'active_support'
83
- gem 'actionpack', '~> 3.0.0.beta1', :require => 'action_pack'
84
- gem 'railties', '~> 3.0.0.beta1', :require => 'rails'
78
+ RSPEC_VERSION = '~> 2.0.0.beta.11'
85
79
 
86
- # Uncomment this if you need actionmailer
87
- # gem 'actionmailer', '~> 3.0.0.beta1', :require => 'action_mailer'
80
+ gem 'activesupport', RAILS_VERSION, :require => 'active_support'
81
+ gem 'actionpack', RAILS_VERSION, :require => 'action_pack'
82
+ gem 'actionmailer', RAILS_VERSION, :require => 'action_mailer'
83
+ gem 'railties', RAILS_VERSION, :require => 'rails'
88
84
 
89
- gem 'data_objects', '~> 0.10.1'
90
- gem 'do_sqlite3', '~> 0.10.1'
85
+ gem 'dm-rails', DM_VERSION
86
+ gem 'dm-#{database}-adapter', DM_VERSION
91
87
 
92
88
  # You can use any of the other available database adapters.
93
89
  # This is only a small excerpt of the list of all available adapters
@@ -98,42 +94,49 @@ Using {bundler}[http://github.com/carlhuda/bundler] it's really easy to get an a
98
94
  #
99
95
  # for a rather complete list of available datamapper adapters and plugins
100
96
 
101
- # gem 'do_mysql', '~> 0.10.1'
102
- # gem 'do_postgres', '~> 0.10.1'
103
- # gem 'do_oracle', '~> 0.10.1'
97
+ # gem 'dm-sqlite-adapter', DM_VERSION
98
+ # gem 'dm-mysql-adapter', DM_VERSION
99
+ # gem 'dm-postgres-adapter', DM_VERSION
100
+ # gem 'dm-oracle-adapter', DM_VERSION
101
+ # gem 'dm-sqlserver-adapter', DM_VERSION
102
+
103
+ gem 'dm-migrations', DM_VERSION
104
+ gem 'dm-types', DM_VERSION
105
+ gem 'dm-validations', DM_VERSION
106
+ gem 'dm-constraints', DM_VERSION
107
+ gem 'dm-transactions', DM_VERSION
108
+ gem 'dm-aggregates', DM_VERSION
109
+ gem 'dm-timestamps', DM_VERSION
110
+ gem 'dm-observer', DM_VERSION
104
111
 
105
- git 'git://github.com/snusnu/dm-core.git', 'branch' => 'active_support'
106
- git "git://github.com/snusnu/dm-more.git", 'branch' => 'active_support'
112
+ group(:test) do
107
113
 
108
- gem 'dm-core', '~> 1.0.0'
109
- gem 'dm-types', '~> 1.0.0'
110
- gem 'dm-validations', '~> 1.0.0'
111
- gem 'dm-constraints', '~> 1.0.0'
112
- gem 'dm-aggregates', '~> 1.0.0'
113
- gem 'dm-timestamps', '~> 1.0.0'
114
- gem 'dm-migrations', '~> 1.0.0'
115
- gem 'dm-observer', '~> 1.0.0'
114
+ gem 'rspec', RSPEC_VERSION
115
+ gem 'rspec-core', RSPEC_VERSION, :require => 'rspec/core'
116
+ gem 'rspec-expectations', RSPEC_VERSION, :require => 'rspec/expectations'
117
+ gem 'rspec-mocks', RSPEC_VERSION, :require => 'rspec/mocks'
118
+ gem 'rspec-rails', RSPEC_VERSION
116
119
 
117
- # Use this instead of git if you're working on dm-rails
118
- # path '/full/path/to/your/local/clone/of/dm-rails'
120
+ end
119
121
 
120
- git 'git://github.com/datamapper/dm-rails.git'
122
+ # ------------------------------------------------------------------------------
121
123
 
122
- gem 'dm-rails', '~> 1.0.0'
124
+ # These gems are only listed here in the Gemfile because we want to pin them
125
+ # to the github repositories for as long as no stable version has been released.
126
+ # The dm-core gem is a hard dependency for dm-rails so it would get pulled in by
127
+ # simply adding dm-rails. The dm-do-adapter gem is a hard dependency for any of
128
+ # the available dm-xxx-adapters. Once we have stable gems available, pinning these
129
+ # gems to github will be optional.
123
130
 
124
- git 'git://github.com/rspec/rspec.git'
125
- git 'git://github.com/rspec/rspec-core.git'
126
- git 'git://github.com/rspec/rspec-expectations.git'
127
- git 'git://github.com/rspec/rspec-mocks.git'
128
- git 'git://github.com/rspec/rspec-rails.git'
131
+ gem 'dm-core', DM_VERSION
132
+ gem 'dm-do-adapter', DM_VERSION
133
+ gem 'dm-active_model', DM_VERSION
134
+
135
+
136
+ # Uncomment this line if you want to see application specific metric data
137
+
138
+ # gem 'rails_metrics', '~> 0.1', :git => 'git://github.com/engineyard/rails_metrics'
129
139
 
130
- group(:test) do
131
- gem 'rspec', '2.0.0.a8'
132
- gem 'rspec-core', '2.0.0.a8', :require => 'rspec/core'
133
- gem 'rspec-expectations', '2.0.0.a8', :require => 'rspec/expectations'
134
- gem 'rspec-mocks', '2.0.0.a8', :require => 'rspec/mocks'
135
- gem 'rspec-rails', '2.0.0.a9'
136
- end
137
140
 
138
141
 
139
142
  == Sample database.yml files
@@ -217,52 +220,54 @@ Once you have defined your database.yml file, dm-rails's rake tasks will be able
217
220
 
218
221
  == Available generators
219
222
 
220
- Due to the awesomeness of rails3 generators we get some cool generators almost for free. 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 would with active_record.
223
+ 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.
221
224
 
222
225
  The following generators are available to help you get started with the typical components of any rails application.
223
226
 
224
- vendor/bin/rails generate controller
225
- vendor/bin/rails generate generator
226
- vendor/bin/rails generate helper
227
- vendor/bin/rails generate integration_test
228
- vendor/bin/rails generate mailer
229
- vendor/bin/rails generate metal
230
- vendor/bin/rails generate migration
231
- vendor/bin/rails generate model
232
- vendor/bin/rails generate model_subclass
233
- vendor/bin/rails generate observer
234
- vendor/bin/rails generate performance_test
235
- vendor/bin/rails generate plugin
236
- vendor/bin/rails generate resource
237
- vendor/bin/rails generate scaffold
238
- vendor/bin/rails generate scaffold_controller
239
- vendor/bin/rails generate session_migration
240
- vendor/bin/rails generate stylesheets
227
+ ...
228
+ rails generate controller
229
+ rails generate generator
230
+ rails generate helper
231
+ rails generate integration_test
232
+ rails generate migration
233
+ rails generate model
234
+ rails generate observer
235
+ rails generate performance_test
236
+ rails generate plugin
237
+ rails generate resource
238
+ rails generate scaffold
239
+ rails generate scaffold_controller
240
+ rails generate session_migration
241
+ rails generate stylesheets
242
+ ...
243
+
244
+ For a complete list run the following in your project's directory
245
+
246
+ rails generate
241
247
 
242
248
 
243
249
  == Available datamapper specific rake tasks
244
250
 
245
251
  To get a list of all available rake tasks in your rails3 app, issue the usual
246
252
 
247
- vendor/bin/rake -T
253
+ rake -T
248
254
 
249
255
  Once you do that, you will see the following rake tasks among others. These are the ones that dm-rails added for us.
250
256
 
251
257
  ...
252
- vendor/bin/rake db:automigrate # Perform destructive automigration
253
- vendor/bin/rake db:autoupgrade # Perform non destructive automigration
254
- vendor/bin/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?
255
- vendor/bin/rake db:create:all # Create all the local databases defined in config/database.yml
256
- vendor/bin/rake db:drop # Drops the database(s) for the current Rails.env - also drops the test database(s) if Rails.env.development?
257
- vendor/bin/rake db:drop:all # Drop all the local databases defined in config/database.yml
258
- vendor/bin/rake db:migrate # Migrate the database to the latest version
259
- vendor/bin/rake db:migrate:down[version] # Migrate down using migrations
260
- vendor/bin/rake db:migrate:up[version] # Migrate up using migrations
261
- vendor/bin/rake db:seed # Load the seed data from db/seeds.rb
262
- vendor/bin/rake db:sessions:clear # Clear the sessions table for DataMapperStore
263
- vendor/bin/rake db:sessions:create # Creates the sessions table for DataMapperStore
264
- vendor/bin/rake db:setup # Create the database, load the schema, and initialize with the seed data
265
-
258
+ rake db:automigrate # Perform destructive automigration of all repositories in the current Rails.env
259
+ rake db:autoupgrade # Perform non destructive automigration of all repositories in the current Rails.env
260
+ 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?
261
+ rake db:create:all # Create all the local databases defined in config/database.yml
262
+ rake db:drop # Drops the database(s) for the current Rails.env - also drops the test database(s) if Rails.env.development?
263
+ rake db:drop:all # Drop all the local databases defined in config/database.yml
264
+ rake db:migrate # Migrate the database to the latest version
265
+ rake db:migrate:down[version] # Migrate down using migrations
266
+ rake db:migrate:up[version] # Migrate up using migrations
267
+ rake db:seed # Load the seed data from db/seeds.rb
268
+ rake db:sessions:clear # Clear the sessions table for DataMapperStore
269
+ rake db:sessions:create # Creates the sessions table for DataMapperStore
270
+ rake db:setup # Create the database, load the schema, and initialize with the seed data
266
271
  ...
267
272
 
268
273
 
@@ -277,34 +282,27 @@ Rails3 makes it easy to expose framework component specific configuration to app
277
282
  Rails::DataMapper::Configuration#raw
278
283
  Rails::DataMapper::Configuration#environments
279
284
  Rails::DataMapper::Configuration#repositories
280
- Rails::DataMapper::Configuration#identity_map=(value)
281
- Rails::DataMapper::Configuration#identity_map
282
- Rails::DataMapper::Configuration#adapter_cascade
283
-
284
285
 
285
286
 
286
287
  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.
287
288
 
288
- ree-1.8.7-2010.01 mungo:datamapper_on_rails3 snusnu$ vendor/bin/rails console
289
- Loading development environment (Rails 3.0.0.beta)
289
+ ree-1.8.7-2010.01 mungo:alfred snusnu$ rails console
290
+ Loading development environment (Rails 3.0.0.beta4)
290
291
  ruby-1.8.7-p248 > require 'pp'
291
292
  => ["PP"]
292
293
  ruby-1.8.7-p248 > pp Rails::DataMapper.configuration
293
- #<Rails::DataMapper::Configuration:0x1025bbc58
294
- @adapter_cascade=Rails::DataMapper::Adapters::Cascade,
295
- @identity_map=true,
296
- @plugins=["dm-validations", "dm-timestamps", "dm-observer", "dm-migrations"],
294
+ #<Rails::DataMapper::Configuration:0x103b46460
297
295
  @raw=
298
296
  {"production"=>
299
297
  {"adapter"=>"mysql",
300
298
  "username"=>"root",
301
- "database"=>"rails3_app_production",
299
+ "database"=>"alfred_production",
302
300
  "host"=>"localhost",
303
301
  "password"=>nil},
304
302
  "development"=>
305
303
  {"adapter"=>"mysql",
306
304
  "username"=>"root",
307
- "database"=>"rails3_app_development",
305
+ "database"=>"alfred_development",
308
306
  "host"=>"localhost",
309
307
  "password"=>nil},
310
308
  "defaults"=>
@@ -315,7 +313,7 @@ As promised, you can easily inspect the configuration from your running rails ap
315
313
  "test"=>
316
314
  {"adapter"=>"mysql",
317
315
  "username"=>"root",
318
- "database"=>"rails3_app_test",
316
+ "database"=>"alfred_test",
319
317
  "host"=>"localhost",
320
318
  "password"=>nil}},
321
319
  @repositories=
@@ -323,28 +321,30 @@ As promised, you can easily inspect the configuration from your running rails ap
323
321
  {"default"=>
324
322
  {"adapter"=>"mysql",
325
323
  "username"=>"root",
326
- "database"=>"rails3_app_production",
324
+ "database"=>"alfred_production",
327
325
  "host"=>"localhost",
328
326
  "password"=>nil}},
329
327
  "development"=>
330
328
  {"default"=>
331
329
  {"adapter"=>"mysql",
332
330
  "username"=>"root",
333
- "database"=>"rails3_app_development",
331
+ "database"=>"alfred_development",
334
332
  "host"=>"localhost",
335
333
  "password"=>nil}},
336
334
  "test"=>
337
335
  {"default"=>
338
336
  {"adapter"=>"mysql",
339
337
  "username"=>"root",
340
- "database"=>"rails3_app_test",
338
+ "database"=>"alfred_test",
341
339
  "host"=>"localhost",
342
- "password"=>nil}}}>
340
+ "password"=>nil}}},
341
+ @root=#<Pathname:/Users/snusnu/projects/github/mine/alfred>>
342
+
343
343
  => nil
344
344
 
345
345
  Additionally, you can reach the configuration object via the standard way that rails provides to expose configuration for framework components and plugins.
346
346
 
347
- Rails.application.config.data_mapper.configuration
347
+ Rails.application.config.data_mapper
348
348
 
349
349
  This will give you the exact same object we inspected in the previous pretty print output from rails console.
350
350
 
@@ -355,15 +355,9 @@ It's easy to extend or adapt dm-rails to meet your specific needs. Thanks to the
355
355
 
356
356
  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.
357
357
 
358
- Rails::DataMapper::Railtie.setup_data_mapper(app)
359
- Rails::DataMapper::Railtie.preload_lib(app)
360
- Rails::DataMapper::Railtie.preload_models(app)
361
-
362
- Rails::DataMapper::Railtie#configure_data_mapper(app)
363
- Rails::DataMapper::Railtie#setup_adapter_cascade(app)
364
- Rails::DataMapper::Railtie#setup_routing_support(app)
358
+ Rails::DataMapper::Railtie.configure_data_mapper(app)
359
+ Rails::DataMapper::Railtie.setup_i18n_support(app)
365
360
  Rails::DataMapper::Railtie#setup_controller_runtime(app)
366
- Rails::DataMapper::Railtie#setup_identity_map(app)
367
361
  Rails::DataMapper::Railtie#setup_logger(app)
368
362
 
369
363
 
@@ -377,12 +371,8 @@ To complete the picture of dm-rails's initialization process, here's an overview
377
371
  setup_logger(app, Rails.logger)
378
372
  end
379
373
 
380
- initializer 'data_mapper.adapter_cascade' do |app|
381
- setup_adapter_cascade(app)
382
- end
383
-
384
- initializer 'data_mapper.routing_support' do |app|
385
- setup_routing_support(app)
374
+ initializer 'data_mapper.i18n_support' do |app|
375
+ setup_i18n_support(app)
386
376
  end
387
377
 
388
378
  # Expose database runtime to controller for logging.
@@ -390,17 +380,20 @@ To complete the picture of dm-rails's initialization process, here's an overview
390
380
  setup_controller_runtime(app)
391
381
  end
392
382
 
393
- initializer 'data_mapper.setup_identity_map' do |app|
394
- setup_identity_map(app)
383
+ # Preload all models once in production mode,
384
+ # and before every request in development mode
385
+ initializer "datamapper.add_to_prepare" do |app|
386
+ config.to_prepare { Rails::DataMapper.preload_models(app) }
395
387
  end
396
388
 
397
- # Run setup code after_initialize to make sure all config/initializers
398
- # are in effect once we setup the connection. This is especially necessary
399
- # for the cascaded adapter wrappers that need to be declared before setup.
400
- # Note that setup_datamapper (and needed methods are defined in class scope)
401
-
389
+ # Run setup code once in after_initialize to make sure all initializers
390
+ # are in effect once we setup the connection. Also, this will make sure
391
+ # that the connection gets set up after all models have been loaded,
392
+ # because #after_initialize is guaranteed to run after #to_prepare.
393
+ # Both production and development environment will execute the setup
394
+ # code only once.
402
395
  config.after_initialize do |app|
403
- setup_data_mapper(app)
396
+ Rails::DataMapper.setup(Rails.env)
404
397
  end
405
398
 
406
399
  If you want to add additional rake tasks in your extension, you can do so by adding the following to your railtie.
@@ -412,16 +405,74 @@ If you want to add additional rake tasks in your extension, you can do so by add
412
405
 
413
406
  == Identity Map support
414
407
 
415
- By default, dm-rails enables the use of the {identity map}[http://datamapper.org/why.html]. If for any reason you want to turn that off, just add the following line to your config/application.rb or your config/environments/*.rb file.
416
-
417
- config.data_mapper.identity_map = false
418
-
419
- Activating the identity map is achieved by installing a middleware that wraps the whole request inside
408
+ Activating the identity map is achieved by installing a middleware that wraps the whole request inside a block
420
409
 
421
410
  DataMapper.repository { ... }
422
411
 
423
412
  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.
424
413
 
414
+ 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.
415
+
416
+ require 'dm-rails/middleware/identity_map'
417
+ class ApplicationController < ActionController::Base
418
+ use Rails::DataMapper::Middleware::IdentityMap
419
+ protect_from_forgery
420
+ end
421
+
422
+ 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 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.
423
+
424
+ # app/controllers/identity_map_controller.rb
425
+ require 'dm-rails/middleware/identity_map'
426
+ class IdentityMapController < ApplicationController
427
+ use Rails::DataMapper::Middleware::IdentityMap
428
+ end
429
+
430
+ # app/controllers/people_controller.rb
431
+ class PeopleController < IdentityMapController
432
+ # ...
433
+ end
434
+
435
+ == Cucumber testing framework support
436
+
437
+ 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:
438
+
439
+ Before do
440
+ repository(:default) do |repository|
441
+ transaction = DataMapper::Transaction.new(repository)
442
+ transaction.begin
443
+ repository.adapter.push_transaction(transaction)
444
+ end
445
+ end
446
+
447
+ After do
448
+ repository(:default).adapter.pop_transaction.rollback
449
+ end
450
+
451
+ This simply wraps each scenario in a transaction which is rolled back.
452
+
453
+ 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):
454
+
455
+ require 'database_cleaner'
456
+ require 'database_cleaner/cucumber'
457
+ DatabaseCleaner.strategy = :truncation
458
+
459
+ == Using adapter specific Resource naming conventions
460
+
461
+ 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
462
+
463
+ # in some config/initializers/file.rb
464
+ convention = DataMapper::NamingConventions::Resource::UnderscoredAndPluralizedWithoutModule
465
+ Rails::DataMapper.configuration.resource_naming_convention[:default] = convention
466
+
467
+ or that you want to use your own naming convention, that is implemented in e.g. a lambda
468
+
469
+ # in some config/initializers/file.rb
470
+ Rails::DataMapper.configuration.resource_naming_convention[:default] = lambda do |value|
471
+ 'tbl' + value.camelize(true)
472
+ end
473
+
474
+ 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.
475
+
425
476
 
426
477
  == Using additional datamapper plugins
427
478
 
@@ -440,8 +491,6 @@ Currently dm-rails publishes the same benchmarking information like active_recor
440
491
 
441
492
  Completed in 9ms (Views: 7.6ms | Models: 0.6ms) with 200
442
493
 
443
- While the SQL issued by DO adapters is already being logged properly, it is not yet published to possible rails subscribers. This is basically the only thing we're still missing in terms of datamapper rails3 notifications integration, compared with active_record. Of course we're not tied to only publishing these messages. If anyone can think of other useful information to publish, it's easy to hook into rails' notification system.
444
-
445
494
 
446
495
  == Current Issues
447
496
 
@@ -455,11 +504,6 @@ While the SQL issued by DO adapters is already being logged properly, it is not
455
504
  * Think about a release strategy supporting both beta releases and master branch
456
505
  * Further README updates
457
506
  * More work on migrations
458
- * Check if the {session store}[http://github.com/datamapper/dm-rails/blob/master/lib/dm-rails/session_store.rb] support works
459
- * Provide a rails template for generating tailored apps for datamapper
460
- * Publish SQL issued by DO (and eventually every adapter) to rails subscribers
461
- * Think about integrating with {dana}[http://github.com/snusnu/dm-accepts_nested_attributes]
462
- * Think about integrating {dm-serializer}[http://github.com/datamapper/dm-more/tree/master/dm-serializer/]
463
507
 
464
508
  == Credits
465
509
 
@@ -481,14 +525,16 @@ Big thanks to everyone working on {datamapper}[http://github.com/datamapper/dm-c
481
525
 
482
526
  Thx to all contributors, every patch, big or small is very much appreciated!
483
527
 
484
- * Martin Gamsjaeger (snusnu)
485
- * Dan Kubb (dkubb)
486
- * Alex Coles (myabc)
487
- * Alex Mankuta
488
- * Foy Savas
528
+ * Martin Gamsjaeger ({snusnu}[http://github.com/snusnu])
529
+ * Dan Kubb ({dkubb}[http://github.com/dkubb])
530
+ * Alex Coles ({myabc}[http://github.com/myabc])
531
+ * Alex Mankuta ({cheba}[http://github.com/cheba])
532
+ * Foy Savas ({foysavas}[http://github.com/foysavas])
489
533
  * Randall Brewer
490
- * Josh Huckabee
491
- * Patrik Sundberg
534
+ * Josh Huckabee {jhuckabee}[http://github.com/jhuckabee]
535
+ * Patrik Sundberg ({sundbp}[http://github.com/sundbp])
536
+ * Corin Langosch ({gucki}[http://github.com/gucki])
537
+ * Jared Morgan ({jm81}[http://github.com/jm81])
492
538
 
493
539
  == Copyright
494
540
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.0.1
data/dm-rails.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{dm-rails}
8
- s.version = "1.0.0"
8
+ s.version = "1.0.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Martin Gamsjaeger (snusnu)", "Dan Kubb"]
12
- s.date = %q{2010-06-08}
12
+ s.date = %q{2010-08-11}
13
13
  s.description = %q{Integrate DataMapper with Rails 3}
14
14
  s.email = %q{gamsnjaga@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -29,7 +29,6 @@ Gem::Specification.new do |s|
29
29
  "lib/dm-rails/configuration.rb",
30
30
  "lib/dm-rails/middleware/identity_map.rb",
31
31
  "lib/dm-rails/railtie.rb",
32
- "lib/dm-rails/railties/benchmarking_mixin.rb",
33
32
  "lib/dm-rails/railties/controller_runtime.rb",
34
33
  "lib/dm-rails/railties/database.rake",
35
34
  "lib/dm-rails/railties/i18n_support.rb",
@@ -15,7 +15,7 @@ module Rails
15
15
  attr_reader :root, :raw
16
16
 
17
17
  def environments
18
- config.keys
18
+ raw.keys
19
19
  end
20
20
 
21
21
  def repositories
@@ -30,16 +30,10 @@ module Rails
30
30
  end
31
31
  end
32
32
 
33
-
34
- def identity_map=(value)
35
- @identity_map = value
36
- end
37
-
38
- def identity_map
39
- @identity_map ||= true
33
+ def resource_naming_convention
34
+ @resource_naming_convention ||= {}
40
35
  end
41
36
 
42
-
43
37
  private
44
38
 
45
39
  def initialize(root, database_yml_hash)
@@ -22,8 +22,6 @@ module Rails
22
22
 
23
23
  class Railtie < Rails::Railtie
24
24
 
25
- log_subscriber :data_mapper, ::DataMapper::Railties::LogSubscriber.new
26
-
27
25
  config.generators.orm :data_mapper, :migration => true
28
26
 
29
27
 
@@ -41,13 +39,8 @@ module Rails
41
39
 
42
40
  def setup_controller_runtime(app)
43
41
  require "dm-rails/railties/controller_runtime"
44
- ActionController::Base.send :include, Rails::DataMapper::Railties::ControllerRuntime
45
- end
46
-
47
- def setup_identity_map(app)
48
- if app.config.data_mapper.identity_map
49
- require 'dm-rails/middleware/identity_map'
50
- app.config.middleware.use Rails::DataMapper::Middleware::IdentityMap
42
+ ActiveSupport.on_load(:action_controller) do
43
+ include Rails::DataMapper::Railties::ControllerRuntime
51
44
  end
52
45
  end
53
46
 
@@ -73,10 +66,6 @@ module Rails
73
66
  setup_controller_runtime(app)
74
67
  end
75
68
 
76
- initializer 'data_mapper.setup_identity_map' do |app|
77
- setup_identity_map(app)
78
- end
79
-
80
69
  # Preload all models once in production mode,
81
70
  # and before every request in development mode
82
71
  initializer "datamapper.add_to_prepare" do |app|
@@ -15,9 +15,9 @@ module Rails
15
15
  def cleanup_view_runtime
16
16
  # TODO add checks if DataMapper is connected to a repository.
17
17
  # If it is, do this, if it isn't, just delegate to super
18
- db_rt_before_render = ::DataMapper.repository.adapter.reset_runtime
18
+ db_rt_before_render = ::DataMapper::Railties::LogSubscriber.reset_runtime
19
19
  runtime = super
20
- db_rt_after_render = ::DataMapper.repository.adapter.reset_runtime
20
+ db_rt_after_render = ::DataMapper::Railties::LogSubscriber.reset_runtime
21
21
  self.db_runtime = db_rt_before_render + db_rt_after_render
22
22
  runtime - db_rt_after_render
23
23
  end
@@ -32,7 +32,7 @@ module Rails
32
32
 
33
33
  def log_process_action(payload)
34
34
  messages, db_runtime = super, payload[:db_runtime]
35
- messages << ("Models: %.1fms" % db_runtime.to_f) if db_runtime
35
+ messages << ("Models: %.3fms" % db_runtime.to_f) if db_runtime
36
36
  messages
37
37
  end
38
38
 
@@ -43,3 +43,4 @@ module Rails
43
43
  end
44
44
  end
45
45
  end
46
+
@@ -35,9 +35,6 @@ namespace :db do
35
35
  desc "Drops the database(s) for the current Rails.env - also drops the test database(s) if Rails.env.development?"
36
36
  task :drop => :environment do
37
37
  Rails::DataMapper.storage.drop_environment(Rails::DataMapper.configuration.repositories[Rails.env])
38
- if Rails.env.development? && Rails::DataMapper.configuration.repositories['test']
39
- Rails::DataMapper.storage.drop_environment(Rails::DataMapper.configuration.repositories['test'])
40
- end
41
38
  end
42
39
 
43
40
 
@@ -48,13 +45,6 @@ namespace :db do
48
45
  ::DataMapper.auto_migrate!(repository.to_sym)
49
46
  ::DataMapper.logger.info "[datamapper] Finished auto_migrate! for :#{repository} repository '#{config['database']}'"
50
47
  end
51
- if Rails.env.development? && Rails::DataMapper.configuration.repositories['test']
52
- Rails::DataMapper.setup('test')
53
- Rails::DataMapper.configuration.repositories['test'].each do |repository, config|
54
- ::DataMapper.auto_migrate!(repository.to_sym)
55
- ::DataMapper.logger.info "[datamapper] Finished auto_migrate! for :#{repository} repository '#{config['database']}'"
56
- end
57
- end
58
48
  end
59
49
 
60
50
  desc 'Perform non destructive automigration of all repositories in the current Rails.env'
@@ -64,13 +54,6 @@ namespace :db do
64
54
  ::DataMapper.auto_upgrade!(repository.to_sym)
65
55
  ::DataMapper.logger.info "[datamapper] Finished auto_upgrade! for :#{repository} repository '#{config['database']}'"
66
56
  end
67
- if Rails.env.development? && Rails::DataMapper.configuration.repositories['test']
68
- Rails::DataMapper.setup('test')
69
- Rails::DataMapper.configuration.repositories['test'].each do |repository, config|
70
- ::DataMapper.auto_upgrade!(repository.to_sym)
71
- ::DataMapper.logger.info "[datamapper] Finished auto_upgrade! for :#{repository} repository '#{config['database']}'"
72
- end
73
- end
74
57
  end
75
58
 
76
59
  desc 'Load the seed data from db/seeds.rb'
@@ -17,7 +17,7 @@ module ActiveSupport
17
17
  module Notifications
18
18
  class Event
19
19
  def duration
20
- @duration ||= payload[:duration] ? (payload[:duration] / 1000.0) : 1000.0 * (@end - @time)
20
+ payload[:duration] ? (payload[:duration] / 1000.0) : 1000.0 * (self.end - self.time)
21
21
  end
22
22
  end
23
23
  end
@@ -1,7 +1,20 @@
1
1
  module DataMapper
2
2
  module Railties
3
3
 
4
- class LogSubscriber < Rails::LogSubscriber
4
+ class LogSubscriber < ActiveSupport::LogSubscriber
5
+
6
+ def self.runtime=(value)
7
+ Thread.current["data_mapper_sql_runtime"] = value
8
+ end
9
+
10
+ def self.runtime
11
+ Thread.current["data_mapper_sql_runtime"] ||= 0
12
+ end
13
+
14
+ def self.reset_runtime
15
+ rt, self.runtime = runtime, 0
16
+ rt
17
+ end
5
18
 
6
19
  def initialize
7
20
  super
@@ -9,14 +22,17 @@ module DataMapper
9
22
  end
10
23
 
11
24
  def sql(event)
12
- name = '%s (%.1fms)' % [event.payload[:name], event.duration]
25
+ self.class.runtime += event.duration
26
+ return unless logger.debug?
27
+
28
+ name = '%s (%.3fms)' % [event.payload[:name], event.duration]
13
29
  sql = event.payload[:sql].squeeze(' ')
14
30
 
15
31
  if odd?
16
- name = color(name, :cyan, true)
32
+ name = color(name, CYAN, true)
17
33
  sql = color(sql, nil, true)
18
34
  else
19
- name = color(name, :magenta, true)
35
+ name = color(name, MAGENTA, true)
20
36
  end
21
37
 
22
38
  debug " #{name} #{sql}"
@@ -34,3 +50,6 @@ module DataMapper
34
50
 
35
51
  end
36
52
  end
53
+
54
+ DataMapper::Railties::LogSubscriber.attach_to :data_mapper
55
+
@@ -2,7 +2,6 @@ require 'active_support/core_ext/hash/except'
2
2
 
3
3
  require 'dm-rails/configuration'
4
4
  require 'dm-rails/railties/log_listener'
5
- require 'dm-rails/railties/benchmarking_mixin'
6
5
 
7
6
  module Rails
8
7
  module DataMapper
@@ -18,7 +17,9 @@ module Rails
18
17
  def self.setup_with_instrumentation(name, options)
19
18
  ::DataMapper.logger.info "[datamapper] Setting up #{name.inspect} repository: '#{options['database']}' on #{options['adapter']}"
20
19
  adapter = ::DataMapper.setup(name, options)
21
- adapter.extend ::DataMapper::Adapters::Benchmarking
20
+ if convention = configuration.resource_naming_convention[name]
21
+ adapter.resource_naming_convention = convention
22
+ end
22
23
  setup_log_listener(options['adapter'])
23
24
  end
24
25
 
@@ -47,7 +47,7 @@ module Rails
47
47
  end
48
48
 
49
49
  def lookup_class(adapter)
50
- klass_name = adapter.camelize.to_sym
50
+ klass_name = normalized_adapter_name(adapter).camelize.to_sym
51
51
 
52
52
  unless Storage.const_defined?(klass_name)
53
53
  raise "Adapter #{adapter} not supported (#{klass_name.inspect})"
@@ -56,6 +56,10 @@ module Rails
56
56
  const_get(klass_name)
57
57
  end
58
58
 
59
+ def normalized_adapter_name(adapter_name)
60
+ adapter_name.to_s == 'sqlite3' ? 'sqlite' : adapter_name
61
+ end
62
+
59
63
  end
60
64
 
61
65
  def initialize(name, config)
@@ -88,7 +92,7 @@ module Rails
88
92
  @charset ||= config['charset'] || ENV['CHARSET'] || 'utf8'
89
93
  end
90
94
 
91
- class Sqlite3 < Storage
95
+ class Sqlite < Storage
92
96
  def _create
93
97
  return if in_memory?
94
98
  ::DataMapper.setup(name, config.merge('database' => path))
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dm-rails
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 0
10
- version: 1.0.0
9
+ - 1
10
+ version: 1.0.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Martin Gamsjaeger (snusnu)
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-06-08 00:00:00 -07:00
19
+ date: 2010-08-11 00:00:00 +02:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -59,7 +59,7 @@ dependencies:
59
59
  requirements:
60
60
  - - ~>
61
61
  - !ruby/object:Gem::Version
62
- hash: 299253627
62
+ hash: -1848230021
63
63
  segments:
64
64
  - 3
65
65
  - 0
@@ -76,7 +76,7 @@ dependencies:
76
76
  requirements:
77
77
  - - ~>
78
78
  - !ruby/object:Gem::Version
79
- hash: 299253627
79
+ hash: -1848230021
80
80
  segments:
81
81
  - 3
82
82
  - 0
@@ -93,7 +93,7 @@ dependencies:
93
93
  requirements:
94
94
  - - ~>
95
95
  - !ruby/object:Gem::Version
96
- hash: 299253627
96
+ hash: -1848230021
97
97
  segments:
98
98
  - 3
99
99
  - 0
@@ -124,7 +124,6 @@ files:
124
124
  - lib/dm-rails/configuration.rb
125
125
  - lib/dm-rails/middleware/identity_map.rb
126
126
  - lib/dm-rails/railtie.rb
127
- - lib/dm-rails/railties/benchmarking_mixin.rb
128
127
  - lib/dm-rails/railties/controller_runtime.rb
129
128
  - lib/dm-rails/railties/database.rake
130
129
  - lib/dm-rails/railties/i18n_support.rb
@@ -1,23 +0,0 @@
1
- module DataMapper
2
- module Adapters
3
- module Benchmarking
4
-
5
- %w[ create read update delete ].each do |method|
6
- class_eval <<-RUBY, __FILE__, __LINE__
7
- def #{method}(*args, &block)
8
- result = nil
9
- @runtime ||= 0
10
- @runtime += Benchmark.ms { result = super(*args, &block) }
11
- result
12
- end
13
- RUBY
14
- end
15
-
16
- def reset_runtime
17
- rt, @runtime = @runtime, 0
18
- rt.to_f
19
- end
20
-
21
- end
22
- end
23
- end