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
@@ -0,0 +1,7 @@
1
+ class <%= class_name %>Observer
2
+
3
+ include DataMapper::Observer
4
+
5
+ observe <%= class_name %>
6
+
7
+ end
@@ -0,0 +1,82 @@
1
+ require 'rails/generators/named_base'
2
+ require 'rails/generators/migration'
3
+ require 'rails/generators/active_model'
4
+
5
+ module DataMapper
6
+ module Generators
7
+
8
+ class Base < ::Rails::Generators::NamedBase #:nodoc:
9
+
10
+ include ::Rails::Generators::Migration
11
+
12
+ def self.source_root
13
+ @_datamapper_source_root ||=
14
+ File.expand_path("../#{base_name}/#{generator_name}/templates", __FILE__)
15
+ end
16
+
17
+ protected
18
+
19
+ # Datamapper does not care if migrations have the same name as long as
20
+ # they have different ids.
21
+ #
22
+ def migration_exists?(dirname, file_name) #:nodoc:
23
+ false
24
+ end
25
+
26
+ # Implement the required interface for Rails::Generators::Migration.
27
+ #
28
+ def self.next_migration_number(dirname) #:nodoc:
29
+ "%.3d" % (current_migration_number(dirname) + 1)
30
+ end
31
+
32
+ end
33
+
34
+ class ActiveModel < ::Rails::Generators::ActiveModel #:nodoc:
35
+ def self.all(klass)
36
+ "#{klass}.all"
37
+ end
38
+
39
+ def self.find(klass, params=nil)
40
+ "#{klass}.get(#{params})"
41
+ end
42
+
43
+ def self.build(klass, params=nil)
44
+ if params
45
+ "#{klass}.new(#{params})"
46
+ else
47
+ "#{klass}.new"
48
+ end
49
+ end
50
+
51
+ def save
52
+ "#{name}.save"
53
+ end
54
+
55
+ def update_attributes(params=nil)
56
+ "#{name}.update(#{params})"
57
+ end
58
+
59
+ def errors
60
+ "#{name}.errors"
61
+ end
62
+
63
+ def destroy
64
+ "#{name}.destroy"
65
+ end
66
+ end
67
+
68
+ end
69
+ end
70
+
71
+ module Rails
72
+
73
+ module Generators
74
+ class GeneratedAttribute #:nodoc:
75
+ def type_class
76
+ return 'DateTime' if type.to_s == 'datetime'
77
+ return type.to_s.camelcase
78
+ end
79
+ end
80
+ end
81
+
82
+ end
@@ -0,0 +1,31 @@
1
+ module Rails; module DataMapper; module Models
2
+ class Composite
3
+ attr_accessor :args
4
+
5
+ def initialize(*args)
6
+ @args = args
7
+ end
8
+
9
+ def ==(other)
10
+ eql?(other)
11
+ end
12
+
13
+ def eql?(other)
14
+ !other.nil? && other.args == args
15
+ end
16
+ end
17
+
18
+ class Fake
19
+ super_module = Module.new do
20
+ def _super_attributes=(*args)
21
+ end
22
+
23
+ def attributes=(*args)
24
+ self.send(:_super_attributes=, *args)
25
+ end
26
+ end
27
+ include super_module
28
+
29
+ include ::Rails::DataMapper::MultiparameterAttributes
30
+ end
31
+ end; end; end
@@ -0,0 +1,11 @@
1
+ module Rails; module DataMapper; module Models
2
+ class Topic
3
+ include ::DataMapper::Resource
4
+ include ::Rails::DataMapper::MultiparameterAttributes
5
+
6
+ property :id, Serial
7
+ property :last_read, Date
8
+ property :written_on, Time
9
+ property :updated_at, DateTime
10
+ end
11
+ end; end; end
data/spec/spec.opts ADDED
@@ -0,0 +1,3 @@
1
+ --format progress
2
+ --color
3
+ --backtrace
@@ -0,0 +1,19 @@
1
+ $:.unshift File.expand_path File.dirname(__FILE__) + '../lib'
2
+ require 'dm-core/spec/setup'
3
+ require 'dm-core/spec/lib/adapter_helpers'
4
+ require 'dm-core/spec/lib/spec_helper'
5
+ require 'dm-core/spec/lib/pending_helpers'
6
+
7
+ DataMapper::Spec.setup
8
+ DataMapper.finalize
9
+
10
+ Spec::Runner.configure do |config|
11
+
12
+ config.extend(DataMapper::Spec::Adapters::Helpers)
13
+ config.include(DataMapper::Spec::PendingHelpers)
14
+
15
+ config.after :all do
16
+ DataMapper::Spec.cleanup_models
17
+ end
18
+
19
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+ require 'dm-rails/mass_assignment_security'
3
+
4
+ # Because mass-assignment security is based on ActiveModel we just have to
5
+ # ensure that ActiveModel is called.
6
+ describe DataMapper::MassAssignmentSecurity do
7
+ before :all do
8
+ class Fake
9
+ super_module = Module.new do
10
+ def _super_attributes=(*args)
11
+ end
12
+
13
+ def attributes=(*args)
14
+ self.send(:_super_attributes=, *args)
15
+ end
16
+ end
17
+ include super_module
18
+
19
+ include ::DataMapper::MassAssignmentSecurity
20
+ end
21
+ end
22
+
23
+ describe '#attributes=' do
24
+ it 'calls super with sanitized attributes' do
25
+ attributes = { :name => 'John', :is_admin => true }
26
+ sanitized_attributes = { :name => 'John' }
27
+ model = Fake.new
28
+ model.should_receive(:sanitize_for_mass_assignment).with(attributes).and_return(sanitized_attributes)
29
+ model.should_receive(:_super_attributes=).with(sanitized_attributes)
30
+
31
+ model.attributes = attributes
32
+ end
33
+
34
+ it 'skips sanitation when called with true' do
35
+ attributes = { :name => 'John', :is_admin => true }
36
+ sanitized_attributes = { :name => 'John' }
37
+ model = Fake.new
38
+ model.should_receive(:_super_attributes=).with(attributes)
39
+
40
+ model.send(:attributes=, attributes, true)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,168 @@
1
+ require 'spec_helper'
2
+ require 'dm-rails/multiparameter_attributes'
3
+
4
+ # Since multiparameters are a feature of Rails some tests are based on the test
5
+ # suite of Rails.
6
+ describe Rails::DataMapper::MultiparameterAttributes do
7
+ before :all do
8
+ load Pathname(__FILE__).dirname.parent.join('models/topic.rb').expand_path
9
+ load Pathname(__FILE__).dirname.parent.join('models/fake.rb').expand_path
10
+ DataMapper.finalize
11
+ model = ::Rails::DataMapper::Models::Topic
12
+ model.auto_migrate! if model.respond_to?(:auto_migrate!)
13
+ end
14
+
15
+ describe '#attributes=' do
16
+ date_inputs = [
17
+ [ 'date',
18
+ { 'last_read(1i)' => '2004', 'last_read(2i)' => '6', 'last_read(3i)' => '24' },
19
+ Date.new(2004, 6, 24) ],
20
+ [ 'date with empty year',
21
+ { 'last_read(1i)' => '', 'last_read(2i)' => '6', 'last_read(3i)' => '24' },
22
+ Date.new(1, 6, 24) ],
23
+ [ 'date with empty month',
24
+ { 'last_read(1i)' => '2004', 'last_read(2i)' => '', 'last_read(3i)' => '24' },
25
+ Date.new(2004, 1, 24) ],
26
+ [ 'date with empty day',
27
+ { 'last_read(1i)' => '2004', 'last_read(2i)' => '6', 'last_read(3i)' => '' },
28
+ Date.new(2004, 6, 1) ],
29
+ [ 'date with empty day and year',
30
+ { 'last_read(1i)' => '', 'last_read(2i)' => '6', 'last_read(3i)' => '' },
31
+ Date.new(1, 6, 1) ],
32
+ [ 'date with empty day and month',
33
+ { 'last_read(1i)' => '2004', 'last_read(2i)' => '', 'last_read(3i)' => '' },
34
+ Date.new(2004, 1, 1) ],
35
+ [ 'date with empty year and month',
36
+ { 'last_read(1i)' => '', 'last_read(2i)' => '', 'last_read(3i)' => '24' },
37
+ Date.new(1, 1, 24) ],
38
+ [ 'date with all empty',
39
+ { 'last_read(1i)' => '', 'last_read(2i)' => '', 'last_read(3i)' => '' },
40
+ nil ],
41
+ ]
42
+
43
+ date_inputs.each do |(name, attributes, date)|
44
+ it "converts #{name}" do
45
+ topic = ::Rails::DataMapper::Models::Topic.new
46
+ topic.attributes = attributes
47
+ topic.last_read.should == date
48
+ end
49
+ end
50
+
51
+ time_inputs = [
52
+ [ 'time',
53
+ { 'written_on(1i)' => '2004', 'written_on(2i)' => '6', 'written_on(3i)' => '24',
54
+ 'written_on(4i)' => '16', 'written_on(5i)' => '24', 'written_on(6i)' => '00' },
55
+ Time.local(2004, 6, 24, 16, 24, 0) ],
56
+ [ 'time with old date',
57
+ { 'written_on(1i)' => '1901', 'written_on(2i)' => '12', 'written_on(3i)' => '31',
58
+ 'written_on(4i)' => '23', 'written_on(5i)' => '59', 'written_on(6i)' => '59' },
59
+ Time.local(1901, 12, 31, 23, 59, 59) ],
60
+ [ 'time with all empty',
61
+ { 'written_on(1i)' => '', 'written_on(2i)' => '', 'written_on(3i)' => '',
62
+ 'written_on(4i)' => '', 'written_on(5i)' => '', 'written_on(6i)' => '' },
63
+ nil ],
64
+ [ 'time with empty seconds',
65
+ { 'written_on(1i)' => '2004', 'written_on(2i)' => '6', 'written_on(3i)' => '24',
66
+ 'written_on(4i)' => '16', 'written_on(5i)' => '24', 'written_on(6i)' => '' },
67
+ Time.local(2004, 6, 24, 16, 24, 0) ],
68
+ ]
69
+
70
+ time_inputs.each do |(name, attributes, time)|
71
+ it "converts #{name}" do
72
+ topic = ::Rails::DataMapper::Models::Topic.new
73
+ topic.attributes = attributes
74
+ topic.written_on.should == time
75
+ end
76
+ end
77
+
78
+ date_time_inputs = [
79
+ [ 'datetime',
80
+ { 'updated_at(1i)' => '2004', 'updated_at(2i)' => '6', 'updated_at(3i)' => '24',
81
+ 'updated_at(4i)' => '16', 'updated_at(5i)' => '24', 'updated_at(6i)' => '00' },
82
+ DateTime.new(2004, 6, 24, 16, 24, 0) ],
83
+ ]
84
+
85
+ date_time_inputs.each do |(name, attributes, time)|
86
+ it "converts #{name}" do
87
+ topic = ::Rails::DataMapper::Models::Topic.new
88
+ topic.attributes = attributes
89
+ topic.updated_at.should == time
90
+ end
91
+ end
92
+
93
+ it 'calls super with merged multiparameters' do
94
+ multiparameter_hash = {
95
+ 'composite(1)' => 'a string',
96
+ 'composite(2)' => '1.5',
97
+ 'composite(3i)' => '1.5',
98
+ 'composite(4f)' => '1.5',
99
+ 'composite(5)' => '',
100
+ 'composite(6i)' => '',
101
+ 'composite(7f)' => '',
102
+ }
103
+ attributes = { 'composite' => Object.new }
104
+
105
+ ::Rails::DataMapper::Models::Composite.
106
+ should_receive(:new).
107
+ with('a string', '1.5', '1.5'.to_i, '1.5'.to_f).
108
+ and_return(attributes['composite'])
109
+
110
+ composite_property = mock(::DataMapper::Property)
111
+ composite_property.stub!(:dump_class).and_return(::Rails::DataMapper::Models::Composite)
112
+
113
+ resource = ::Rails::DataMapper::Models::Fake.new
114
+ resource.stub!(:properties).and_return('composite' => composite_property)
115
+
116
+ resource.should_receive(:_super_attributes=).with(attributes)
117
+
118
+ resource.attributes = multiparameter_hash
119
+ end
120
+
121
+ it 'raises exception on failure' do
122
+ multiparameter_hash = { 'composite(1)' => 'a string' }
123
+ attributes = { 'composite' => Object.new }
124
+
125
+ composite_exception = StandardError.new('foo')
126
+ ::Rails::DataMapper::Models::Composite.
127
+ should_receive(:new).with('a string').and_raise(composite_exception)
128
+
129
+ composite_property = mock(::DataMapper::Property)
130
+ composite_property.stub!(:dump_class).and_return(::Rails::DataMapper::Models::Composite)
131
+
132
+ resource = ::Rails::DataMapper::Models::Fake.new
133
+ resource.stub!(:properties).and_return('composite' => composite_property)
134
+
135
+ lambda { resource.attributes = multiparameter_hash }.
136
+ should raise_error(::Rails::DataMapper::MultiparameterAssignmentErrors) { |ex|
137
+ ex.errors.size.should == 1
138
+
139
+ error = ex.errors[0]
140
+ error.attribute.should == 'composite'
141
+ error.values.should == ['a string']
142
+ error.exception.should == composite_exception
143
+ }
144
+ end
145
+ end
146
+
147
+ describe 'new' do
148
+ it "merges multiparameters" do
149
+ attributes = {
150
+ 'updated_at(1i)' => '2004', 'updated_at(2i)' => '6', 'updated_at(3i)' => '24',
151
+ 'updated_at(4i)' => '16', 'updated_at(5i)' => '24', 'updated_at(6i)' => '00' }
152
+
153
+ topic = ::Rails::DataMapper::Models::Topic.new(attributes)
154
+ topic.updated_at.should == DateTime.new(2004, 6, 24, 16, 24, 0)
155
+ end
156
+ end
157
+
158
+ describe 'create' do
159
+ it "merges multiparameters" do
160
+ attributes = {
161
+ 'updated_at(1i)' => '2004', 'updated_at(2i)' => '6', 'updated_at(3i)' => '24',
162
+ 'updated_at(4i)' => '16', 'updated_at(5i)' => '24', 'updated_at(6i)' => '00' }
163
+
164
+ topic = ::Rails::DataMapper::Models::Topic.create(attributes)
165
+ topic.updated_at.should == DateTime.new(2004, 6, 24, 16, 24, 0)
166
+ end
167
+ end
168
+ end
data/tasks/clean.rake ADDED
@@ -0,0 +1,6 @@
1
+ require 'rake/clean'
2
+
3
+ File.foreach('.gitignore') do |line|
4
+ line.strip!
5
+ CLOBBER << line unless line.empty? || line[0, 1] == '#'
6
+ end
data/tasks/yard.rake ADDED
@@ -0,0 +1,9 @@
1
+ begin
2
+ require 'yard'
3
+
4
+ YARD::Rake::YardocTask.new
5
+ rescue LoadError
6
+ task :yard do
7
+ abort 'YARD is not available. In order to run yard, you must: gem install yard'
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+ begin
2
+ require 'pathname'
3
+ require 'yardstick'
4
+ require 'yardstick/rake/measurement'
5
+ require 'yardstick/rake/verify'
6
+
7
+ # yardstick_measure task
8
+ Yardstick::Rake::Measurement.new
9
+
10
+ # verify_measurements task
11
+ Yardstick::Rake::Verify.new do |verify|
12
+ verify.threshold = 100
13
+ end
14
+ rescue LoadError
15
+ %w[ yardstick_measure verify_measurements ].each do |name|
16
+ task name.to_s do
17
+ abort "Yardstick is not available. In order to run #{name}, you must: gem install yardstick"
18
+ end
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tpitale-dm-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Martin Gamsjaeger (snusnu)
9
+ - Dan Kubb
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2011-10-09 00:00:00.000000000Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: dm-core
17
+ requirement: &70167497981900 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 1.2.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *70167497981900
26
+ - !ruby/object:Gem::Dependency
27
+ name: dm-active_model
28
+ requirement: &70167497981360 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *70167497981360
37
+ - !ruby/object:Gem::Dependency
38
+ name: actionpack
39
+ requirement: &70167497980780 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 3.1.0
45
+ type: :runtime
46
+ prerelease: false
47
+ version_requirements: *70167497980780
48
+ - !ruby/object:Gem::Dependency
49
+ name: railties
50
+ requirement: &70167497980240 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: 3.1.0
56
+ type: :runtime
57
+ prerelease: false
58
+ version_requirements: *70167497980240
59
+ - !ruby/object:Gem::Dependency
60
+ name: jeweler
61
+ requirement: &70167497979600 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ~>
65
+ - !ruby/object:Gem::Version
66
+ version: 1.6.4
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *70167497979600
70
+ - !ruby/object:Gem::Dependency
71
+ name: rake
72
+ requirement: &70167497979020 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 0.9.2
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: *70167497979020
81
+ - !ruby/object:Gem::Dependency
82
+ name: rspec
83
+ requirement: &70167497978480 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ~>
87
+ - !ruby/object:Gem::Version
88
+ version: 1.3.2
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: *70167497978480
92
+ description: Integrate DataMapper with Rails 3
93
+ email: gamsnjaga@gmail.com
94
+ executables: []
95
+ extensions: []
96
+ extra_rdoc_files:
97
+ - LICENSE
98
+ - README.rdoc
99
+ files:
100
+ - .document
101
+ - Gemfile
102
+ - LICENSE
103
+ - README.rdoc
104
+ - Rakefile
105
+ - VERSION
106
+ - dm-rails.gemspec
107
+ - lib/dm-rails.rb
108
+ - lib/dm-rails/configuration.rb
109
+ - lib/dm-rails/mass_assignment_security.rb
110
+ - lib/dm-rails/middleware/identity_map.rb
111
+ - lib/dm-rails/multiparameter_attributes.rb
112
+ - lib/dm-rails/railtie.rb
113
+ - lib/dm-rails/railties/controller_runtime.rb
114
+ - lib/dm-rails/railties/database.rake
115
+ - lib/dm-rails/railties/i18n_support.rb
116
+ - lib/dm-rails/railties/log_listener.rb
117
+ - lib/dm-rails/railties/log_subscriber.rb
118
+ - lib/dm-rails/session_store.rb
119
+ - lib/dm-rails/setup.rb
120
+ - lib/dm-rails/storage.rb
121
+ - lib/generators/data_mapper.rb
122
+ - lib/generators/data_mapper/migration/migration_generator.rb
123
+ - lib/generators/data_mapper/migration/templates/migration.rb
124
+ - lib/generators/data_mapper/model/model_generator.rb
125
+ - lib/generators/data_mapper/model/templates/model.rb
126
+ - lib/generators/data_mapper/observer/observer_generator.rb
127
+ - lib/generators/data_mapper/observer/templates/observer.rb
128
+ - spec/models/fake.rb
129
+ - spec/models/topic.rb
130
+ - spec/spec.opts
131
+ - spec/spec_helper.rb
132
+ - spec/unit/mass_assignment_security_spec.rb
133
+ - spec/unit/multiparameter_attributes_spec.rb
134
+ - tasks/clean.rake
135
+ - tasks/yard.rake
136
+ - tasks/yardstick.rake
137
+ homepage: http://github.com/datamapper/dm-rails
138
+ licenses: []
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ none: false
145
+ requirements:
146
+ - - ! '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ none: false
151
+ requirements:
152
+ - - ! '>='
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project: datamapper
157
+ rubygems_version: 1.8.6
158
+ signing_key:
159
+ specification_version: 3
160
+ summary: Use DataMapper with Rails 3
161
+ test_files: []