dm-rails 1.0.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+
6
+ require 'jeweler'
7
+
8
+ Jeweler::Tasks.new do |gem|
9
+
10
+ gem.name = 'dm-rails'
11
+ gem.summary = 'Use DataMapper with Rails 3'
12
+ gem.description = 'Integrate DataMapper with Rails 3'
13
+ gem.email = 'gamsnjaga@gmail.com'
14
+ gem.homepage = 'http://github.com/datamapper/dm-rails'
15
+ gem.authors = [ 'Martin Gamsjaeger (snusnu)', 'Dan Kubb' ]
16
+
17
+ gem.add_dependency 'dm-core', '~> 1.0.0.rc1'
18
+ gem.add_dependency 'dm-active_model', '~> 0.4'
19
+
20
+ gem.add_dependency 'activesupport', '~> 3.0.0.beta3'
21
+ gem.add_dependency 'actionpack', '~> 3.0.0.beta3'
22
+ gem.add_dependency 'railties', '~> 3.0.0.beta3'
23
+ end
24
+
25
+ Jeweler::GemcutterTasks.new
26
+
27
+ FileList['tasks/**/*.rake'].each { |task| import task }
28
+
29
+ rescue LoadError
30
+ puts 'Jeweler (or a dependency) not available. Install it with: gem install jeweler'
31
+ end
32
+
33
+ task(:spec) {} # stub out the spec task for as long as we don't have any specs
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0.rc1
data/dm-rails.gemspec ADDED
@@ -0,0 +1,85 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{dm-rails}
8
+ s.version = "1.0.0.rc1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Martin Gamsjaeger (snusnu)", "Dan Kubb"]
12
+ s.date = %q{2010-05-19}
13
+ s.description = %q{Integrate DataMapper with Rails 3}
14
+ s.email = %q{gamsnjaga@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "Gemfile",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "dm-rails.gemspec",
28
+ "lib/dm-rails.rb",
29
+ "lib/dm-rails/configuration.rb",
30
+ "lib/dm-rails/middleware/identity_map.rb",
31
+ "lib/dm-rails/railtie.rb",
32
+ "lib/dm-rails/railties/benchmarking_mixin.rb",
33
+ "lib/dm-rails/railties/controller_runtime.rb",
34
+ "lib/dm-rails/railties/database.rake",
35
+ "lib/dm-rails/railties/i18n_support.rb",
36
+ "lib/dm-rails/railties/log_subscriber.rb",
37
+ "lib/dm-rails/session_store.rb",
38
+ "lib/dm-rails/setup.rb",
39
+ "lib/dm-rails/storage.rb",
40
+ "lib/generators/data_mapper.rb",
41
+ "lib/generators/data_mapper/migration/migration_generator.rb",
42
+ "lib/generators/data_mapper/migration/templates/migration.rb",
43
+ "lib/generators/data_mapper/model/model_generator.rb",
44
+ "lib/generators/data_mapper/model/templates/model.rb",
45
+ "lib/generators/data_mapper/observer/observer_generator.rb",
46
+ "lib/generators/data_mapper/observer/templates/observer.rb",
47
+ "tasks/ci.rake",
48
+ "tasks/clean.rake",
49
+ "tasks/local_gemfile.rake",
50
+ "tasks/metrics.rake",
51
+ "tasks/yard.rake",
52
+ "tasks/yardstick.rake"
53
+ ]
54
+ s.homepage = %q{http://github.com/datamapper/dm-rails}
55
+ s.rdoc_options = ["--charset=UTF-8"]
56
+ s.require_paths = ["lib"]
57
+ s.rubygems_version = %q{1.3.6}
58
+ s.summary = %q{Use DataMapper with Rails 3}
59
+
60
+ if s.respond_to? :specification_version then
61
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
62
+ s.specification_version = 3
63
+
64
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
65
+ s.add_runtime_dependency(%q<dm-core>, ["~> 1.0.0.rc1"])
66
+ s.add_runtime_dependency(%q<dm-active_model>, ["~> 0.4"])
67
+ s.add_runtime_dependency(%q<activesupport>, ["~> 3.0.0.beta3"])
68
+ s.add_runtime_dependency(%q<actionpack>, ["~> 3.0.0.beta3"])
69
+ s.add_runtime_dependency(%q<railties>, ["~> 3.0.0.beta3"])
70
+ else
71
+ s.add_dependency(%q<dm-core>, ["~> 1.0.0.rc1"])
72
+ s.add_dependency(%q<dm-active_model>, ["~> 0.4"])
73
+ s.add_dependency(%q<activesupport>, ["~> 3.0.0.beta3"])
74
+ s.add_dependency(%q<actionpack>, ["~> 3.0.0.beta3"])
75
+ s.add_dependency(%q<railties>, ["~> 3.0.0.beta3"])
76
+ end
77
+ else
78
+ s.add_dependency(%q<dm-core>, ["~> 1.0.0.rc1"])
79
+ s.add_dependency(%q<dm-active_model>, ["~> 0.4"])
80
+ s.add_dependency(%q<activesupport>, ["~> 3.0.0.beta3"])
81
+ s.add_dependency(%q<actionpack>, ["~> 3.0.0.beta3"])
82
+ s.add_dependency(%q<railties>, ["~> 3.0.0.beta3"])
83
+ end
84
+ end
85
+
data/lib/dm-rails.rb ADDED
@@ -0,0 +1 @@
1
+ require 'dm-rails/railtie'
@@ -0,0 +1,70 @@
1
+ require 'active_support/core_ext/hash/except'
2
+ require 'active_support/core_ext/class/attribute_accessors'
3
+
4
+ module Rails
5
+ module DataMapper
6
+
7
+ mattr_accessor :configuration
8
+
9
+ class Configuration
10
+
11
+ def self.for(root, database_yml_hash)
12
+ Rails::DataMapper.configuration ||= new(root, database_yml_hash)
13
+ end
14
+
15
+ attr_reader :root, :raw
16
+
17
+ def environments
18
+ config.keys
19
+ end
20
+
21
+ def repositories
22
+ @repositories ||= @raw.reject { |k,v| k =~ /defaults/ }.inject({}) do |repositories, pair|
23
+ environment, config = pair.first, pair.last
24
+ repositories[environment] = begin
25
+ c = config['repositories'] || {}
26
+ c['default'] = config.except('repositories') if config.except('repositories')
27
+ normalize_repository_config(c)
28
+ end
29
+ repositories
30
+ end
31
+ end
32
+
33
+
34
+ def identity_map=(value)
35
+ @identity_map = value
36
+ end
37
+
38
+ def identity_map
39
+ @identity_map ||= true
40
+ end
41
+
42
+
43
+ private
44
+
45
+ def initialize(root, database_yml_hash)
46
+ @root, @raw = root, database_yml_hash
47
+ end
48
+
49
+ def normalize_repository_config(hash)
50
+ config = {}
51
+ hash.each do |key, value|
52
+ config[key] = if value.kind_of?(Hash)
53
+ normalize_repository_config(value)
54
+ elsif key == 'port'
55
+ value.to_i
56
+ elsif key == 'adapter' && value == 'postgresql'
57
+ 'postgres'
58
+ elsif key == 'database' && hash['adapter'] == 'sqlite3'
59
+ value == ':memory:' ? value : File.expand_path(hash['database'], root)
60
+ else
61
+ value
62
+ end
63
+ end
64
+ config
65
+ end
66
+
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,19 @@
1
+ module Rails
2
+ module DataMapper
3
+ module Middleware
4
+
5
+ class IdentityMap
6
+ def initialize(app)
7
+ @app = app
8
+ end
9
+
10
+ def call(env)
11
+ ::DataMapper.repository do
12
+ @app.call(env)
13
+ end
14
+ end
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,103 @@
1
+ require 'dm-core'
2
+ require 'dm-active_model'
3
+
4
+ require 'rails'
5
+ require 'active_model/railtie'
6
+
7
+ # Comment taken from active_record/railtie.rb
8
+ #
9
+ # For now, action_controller must always be present with
10
+ # rails, so let's make sure that it gets required before
11
+ # here. This is needed for correctly setting up the middleware.
12
+ # In the future, this might become an optional require.
13
+ require 'action_controller/railtie'
14
+
15
+ require 'dm-rails/setup'
16
+ require "dm-rails/railties/log_subscriber"
17
+ require "dm-rails/railties/i18n_support"
18
+
19
+
20
+ module Rails
21
+ module DataMapper
22
+
23
+ class Railtie < Rails::Railtie
24
+
25
+ log_subscriber :data_mapper, ::DataMapper::Railties::LogSubscriber.new
26
+
27
+ config.generators.orm :data_mapper, :migration => true
28
+
29
+
30
+ # Support overwriting crucial steps in subclasses
31
+
32
+ def configure_data_mapper(app)
33
+ app.config.data_mapper = Rails::DataMapper::Configuration.for(
34
+ Rails.root, app.config.database_configuration
35
+ )
36
+ end
37
+
38
+ def setup_i18n_support(app)
39
+ ::DataMapper::Model.append_inclusions(Rails::DataMapper::I18nSupport)
40
+ end
41
+
42
+ def setup_controller_runtime(app)
43
+ 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
51
+ end
52
+ end
53
+
54
+ def setup_logger(app, logger)
55
+ Rails::DataMapper.setup_logger(logger)
56
+ end
57
+
58
+
59
+ initializer 'data_mapper.configuration' do |app|
60
+ configure_data_mapper(app)
61
+ end
62
+
63
+ initializer 'data_mapper.logger' do |app|
64
+ setup_logger(app, Rails.logger)
65
+ end
66
+
67
+ initializer 'data_mapper.i18n_support' do |app|
68
+ setup_i18n_support(app)
69
+ end
70
+
71
+ # Expose database runtime to controller for logging.
72
+ initializer "data_mapper.log_runtime" do |app|
73
+ setup_controller_runtime(app)
74
+ end
75
+
76
+ initializer 'data_mapper.setup_identity_map' do |app|
77
+ setup_identity_map(app)
78
+ end
79
+
80
+ # Preload all models once in production mode,
81
+ # and before every request in development mode
82
+ initializer "datamapper.add_to_prepare" do |app|
83
+ config.to_prepare { Rails::DataMapper.preload_models(app) }
84
+ end
85
+
86
+ # Run setup code once in after_initialize to make sure all initializers
87
+ # are in effect once we setup the connection. Also, this will make sure
88
+ # that the connection gets set up after all models have been loaded,
89
+ # because #after_initialize is guaranteed to run after #to_prepare.
90
+ # Both production and development environment will execute the setup
91
+ # code only once.
92
+ config.after_initialize do |app|
93
+ Rails::DataMapper.setup(Rails.env)
94
+ end
95
+
96
+ rake_tasks do
97
+ load 'dm-rails/railties/database.rake'
98
+ end
99
+
100
+ end
101
+
102
+ end
103
+ end
@@ -0,0 +1,23 @@
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
@@ -0,0 +1,45 @@
1
+ require 'active_support/core_ext/module/attr_internal'
2
+
3
+ module Rails
4
+ module DataMapper
5
+ module Railties
6
+
7
+ module ControllerRuntime
8
+
9
+ extend ActiveSupport::Concern
10
+
11
+ protected
12
+
13
+ attr_internal :db_runtime
14
+
15
+ def cleanup_view_runtime
16
+ # TODO add checks if DataMapper is connected to a repository.
17
+ # If it is, do this, if it isn't, just delegate to super
18
+ db_rt_before_render = ::DataMapper.repository.adapter.reset_runtime
19
+ runtime = super
20
+ db_rt_after_render = ::DataMapper.repository.adapter.reset_runtime
21
+ self.db_runtime = db_rt_before_render + db_rt_after_render
22
+ runtime - db_rt_after_render
23
+ end
24
+
25
+ def append_info_to_payload(payload)
26
+ super
27
+ payload[:db_runtime] = db_runtime
28
+ end
29
+
30
+
31
+ module ClassMethods
32
+
33
+ def log_process_action(payload)
34
+ messages, db_runtime = super, payload[:db_runtime]
35
+ messages << ("Models: %.1fms" % db_runtime.to_f) if db_runtime
36
+ messages
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,124 @@
1
+ require 'dm-rails/setup'
2
+ require 'dm-rails/storage'
3
+
4
+ namespace :db do
5
+
6
+ task :load_models => :environment do
7
+ FileList["app/models/**/*.rb"].each { |model| load model }
8
+ end
9
+
10
+ desc 'Create the database, load the schema, and initialize with the seed data'
11
+ task :setup => [ 'db:create', 'db:automigrate', 'db:seed' ]
12
+
13
+ namespace :test do
14
+ task :prepare => ['db:setup']
15
+ end
16
+
17
+ namespace :create do
18
+ desc 'Create all the local databases defined in config/database.yml'
19
+ task :all => :environment do
20
+ Rails::DataMapper.storage.create_all
21
+ end
22
+ end
23
+
24
+ desc "Create the database(s) defined in config/database.yml for the current Rails.env - also creates the test database(s) if Rails.env.development?"
25
+ task :create => :environment do
26
+ Rails::DataMapper.storage.create_environment(Rails::DataMapper.configuration.repositories[Rails.env])
27
+ if Rails.env.development? && Rails::DataMapper.configuration.repositories['test']
28
+ Rails::DataMapper.storage.create_environment(Rails::DataMapper.configuration.repositories['test'])
29
+ end
30
+ end
31
+
32
+ namespace :drop do
33
+ desc 'Drop all the local databases defined in config/database.yml'
34
+ task :all => :environment do
35
+ Rails::DataMapper.storage.drop_all
36
+ end
37
+ end
38
+
39
+ desc "Drops the database(s) for the current Rails.env - also drops the test database(s) if Rails.env.development?"
40
+ task :drop => :environment do
41
+ Rails::DataMapper.storage.drop_environment(Rails::DataMapper.configuration.repositories[Rails.env])
42
+ if Rails.env.development? && Rails::DataMapper.configuration.repositories['test']
43
+ Rails::DataMapper.storage.drop_environment(Rails::DataMapper.configuration.repositories['test'])
44
+ end
45
+ end
46
+
47
+
48
+ desc 'Perform destructive automigration of all repositories in the current Rails.env'
49
+ task :automigrate => :load_models do
50
+ require 'dm-migrations'
51
+ Rails::DataMapper.configuration.repositories[Rails.env].each do |repository, config|
52
+ ::DataMapper.auto_migrate!(repository.to_sym)
53
+ puts "[datamapper] Finished auto_migrate! for :#{repository} repository '#{config['database']}'"
54
+ end
55
+ if Rails.env.development? && Rails::DataMapper.configuration.repositories['test']
56
+ Rails::DataMapper.setup('test')
57
+ Rails::DataMapper.configuration.repositories['test'].each do |repository, config|
58
+ ::DataMapper.auto_migrate!(repository.to_sym)
59
+ puts "[datamapper] Finished auto_migrate! for :#{repository} repository '#{config['database']}'"
60
+ end
61
+ end
62
+ end
63
+
64
+ desc 'Perform non destructive automigration of all repositories in the current Rails.env'
65
+ task :autoupgrade => :load_models do
66
+ require 'dm-migrations'
67
+ Rails::DataMapper.configuration.repositories[Rails.env].each do |repository, config|
68
+ ::DataMapper.auto_upgrade!(repository.to_sym)
69
+ puts "[datamapper] Finished auto_upgrade! for :#{repository} repository '#{config['database']}'"
70
+ end
71
+ if Rails.env.development? && Rails::DataMapper.configuration.repositories['test']
72
+ Rails::DataMapper.setup('test')
73
+ Rails::DataMapper.configuration.repositories['test'].each do |repository, config|
74
+ ::DataMapper.auto_upgrade!(repository.to_sym)
75
+ puts "[datamapper] Finished auto_upgrade! for :#{repository} repository '#{config['database']}'"
76
+ end
77
+ end
78
+ end
79
+
80
+ desc 'Load the seed data from db/seeds.rb'
81
+ task :seed => :environment do
82
+ seed_file = File.join(Rails.root, 'db', 'seeds.rb')
83
+ load(seed_file) if File.exist?(seed_file)
84
+ end
85
+
86
+ namespace :migrate do
87
+ task :load => :environment do
88
+ require 'dm-migrations/migration_runner'
89
+ FileList['db/migrate/*.rb'].each do |migration|
90
+ load migration
91
+ end
92
+ end
93
+
94
+ desc 'Migrate up using migrations'
95
+ task :up, :version, :needs => :load do |t, args|
96
+ ::DataMapper::MigrationRunner.migrate_up!(args[:version])
97
+ end
98
+
99
+ desc 'Migrate down using migrations'
100
+ task :down, :version, :needs => :load do |t, args|
101
+ ::DataMapper::MigrationRunner.migrate_down!(args[:version])
102
+ end
103
+ end
104
+
105
+ desc 'Migrate the database to the latest version'
106
+ task :migrate => 'db:migrate:up'
107
+
108
+ namespace :sessions do
109
+ desc "Creates the sessions table for DataMapperStore"
110
+ task :create => :environment do
111
+ require 'dm-rails/session_store'
112
+ Rails::DataMapper::SessionStore::Session.auto_migrate!
113
+ puts "Created '#{Rails::DataMapper.configurations[Rails.env]['database']}.sessions'"
114
+ end
115
+
116
+ desc "Clear the sessions table for DataMapperStore"
117
+ task :clear => :environment do
118
+ require 'dm-rails/session_store'
119
+ Rails::DataMapper::SessionStore::Session.all.destroy!
120
+ puts "Deleted entries from '#{Rails::DataMapper.configurations[Rails.env]['database']}.sessions'"
121
+ end
122
+ end
123
+
124
+ end