census 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (29) hide show
  1. data/README.rdoc +21 -0
  2. data/VERSION +1 -1
  3. data/generators/census/templates/migrations/with_users.rb +8 -8
  4. data/generators/census/templates/migrations/without_users.rb +8 -8
  5. data/lib/census/user.rb +14 -0
  6. data/lib/census/user_data.rb +14 -2
  7. data/test/models/user_test.rb +76 -0
  8. data/test/rails_root/config/environment.rb +37 -4
  9. data/test/rails_root/config/environments/test.rb +3 -3
  10. data/test/rails_root/config/initializers/cookie_verification_secret.rb +7 -0
  11. data/test/rails_root/config/initializers/session_store.rb +1 -1
  12. data/test/rails_root/db/migrate/{20100421154808_create_census_tables.rb → 20100526201135_create_census_tables.rb} +8 -8
  13. data/test/rails_root/db/schema.rb +5 -5
  14. metadata +48 -20
  15. data/test/rails_root/vendor/gems/acts_as_list-0.1.2/lib/acts_as_list.rb +0 -254
  16. data/test/rails_root/vendor/gems/acts_as_list-0.1.2/test/list_test.rb +0 -369
  17. data/test/rails_root/vendor/gems/inverse_of-0.0.1/install.rb +0 -1
  18. data/test/rails_root/vendor/gems/inverse_of-0.0.1/lib/inverse_of.rb +0 -293
  19. data/test/rails_root/vendor/gems/inverse_of-0.0.1/rails/init.rb +0 -1
  20. data/test/rails_root/vendor/gems/inverse_of-0.0.1/test/cases/helper.rb +0 -28
  21. data/test/rails_root/vendor/gems/inverse_of-0.0.1/test/cases/inverse_associations_test.rb +0 -567
  22. data/test/rails_root/vendor/gems/inverse_of-0.0.1/test/models/club.rb +0 -13
  23. data/test/rails_root/vendor/gems/inverse_of-0.0.1/test/models/face.rb +0 -7
  24. data/test/rails_root/vendor/gems/inverse_of-0.0.1/test/models/interest.rb +0 -5
  25. data/test/rails_root/vendor/gems/inverse_of-0.0.1/test/models/man.rb +0 -9
  26. data/test/rails_root/vendor/gems/inverse_of-0.0.1/test/models/sponsor.rb +0 -4
  27. data/test/rails_root/vendor/gems/inverse_of-0.0.1/test/models/zine.rb +0 -3
  28. data/test/rails_root/vendor/gems/inverse_of-0.0.1/test/schema/schema.rb +0 -32
  29. data/test/rails_root/vendor/gems/inverse_of-0.0.1/uninstall.rb +0 -1
@@ -1 +0,0 @@
1
- # Install hook code here
@@ -1,293 +0,0 @@
1
- if ([ActiveRecord::VERSION::MAJOR, ActiveRecord::VERSION::MINOR, ActiveRecord::VERSION::TINY] <=> [2, 3, 6]) < 0
2
- module InverseOf
3
- class InverseOfAssociationNotFoundError < ActiveRecord::ActiveRecordError #:nodoc:
4
- def initialize(reflection, associated_class = nil)
5
- super("Could not find the inverse association for #{reflection.name} (#{reflection.options[:inverse_of].inspect} in #{associated_class.nil? ? reflection.class_name : associated_class.name})")
6
- end
7
- end
8
-
9
- module Reflection
10
- def self.included(base)
11
- base::AssociationReflection.send :include, AssociationReflection
12
- base::ThroughReflection.send :include, ThroughReflection
13
- end
14
-
15
- module AssociationReflection
16
- def self.included(base)
17
- base.alias_method_chain :check_validity!, :inverse_of
18
- end
19
-
20
- def check_validity_with_inverse_of!
21
- check_validity_of_inverse!
22
- check_validity_without_inverse_of!
23
- end
24
-
25
- def check_validity_of_inverse!
26
- unless options[:polymorphic]
27
- if has_inverse? && inverse_of.nil?
28
- raise InverseOfAssociationNotFoundError.new(self)
29
- end
30
- end
31
- end
32
-
33
- def has_inverse?
34
- !@options[:inverse_of].nil?
35
- end
36
-
37
- def inverse_of
38
- if has_inverse?
39
- @inverse_of ||= klass.reflect_on_association(options[:inverse_of])
40
- end
41
- end
42
-
43
- def polymorphic_inverse_of(associated_class)
44
- if has_inverse?
45
- if inverse_relationship = associated_class.reflect_on_association(options[:inverse_of])
46
- inverse_relationship
47
- else
48
- raise InverseOfAssociationNotFoundError.new(self, associated_class)
49
- end
50
- end
51
- end
52
- end
53
-
54
- module ThroughReflection
55
- def self.included(base)
56
- base.alias_method_chain :check_validity!, :inverse_of
57
- end
58
-
59
- def check_validity_with_inverse_of!
60
- check_validity_of_inverse!
61
- check_validity_without_inverse_of!
62
- end
63
- end
64
- end
65
-
66
- module Associations
67
- module AssociationCollection
68
- def self.included(base)
69
- base.alias_method_chain :find_target, :inverse_of
70
- base.alias_method_chain :add_record_to_target_with_callbacks, :inverse_of
71
- end
72
-
73
- def find_target_with_inverse_of
74
- records = find_target_without_inverse_of
75
- records.each do |record|
76
- set_inverse_instance(record, @owner)
77
- end
78
- records
79
- end
80
-
81
- def add_record_to_target_with_callbacks_with_inverse_of(record, &block)
82
- record = add_record_to_target_with_callbacks_without_inverse_of(record, &block)
83
- set_inverse_instance(record, @owner)
84
- record
85
- end
86
- end
87
-
88
- module AssociationProxy
89
- def self.included(base)
90
- base.alias_method_chain :initialize, :inverse_of
91
- end
92
-
93
- def initialize_with_inverse_of(owner, reflection)
94
- reflection.check_validity!
95
- initialize_without_inverse_of(owner, reflection)
96
- end
97
-
98
- private
99
-
100
- def set_inverse_instance(record, instance)
101
- return if record.nil? || !we_can_set_the_inverse_on_this?(record)
102
- inverse_relationship = @reflection.inverse_of
103
- unless inverse_relationship.nil?
104
- record.send(:"set_#{inverse_relationship.name}_target", instance)
105
- end
106
- end
107
-
108
- # Override in subclasses
109
- def we_can_set_the_inverse_on_this?(record)
110
- false
111
- end
112
- end
113
-
114
- module BelongsToAssociation
115
- def self.included(base)
116
- base.alias_method_chain :replace, :inverse_of
117
- base.alias_method_chain :find_target, :inverse_of
118
- end
119
-
120
- def replace_with_inverse_of(record)
121
- replace_without_inverse_of(record)
122
- set_inverse_instance(record, @owner)
123
- record
124
- end
125
-
126
- def find_target_with_inverse_of
127
- target = find_target_without_inverse_of and
128
- set_inverse_instance(target, @owner)
129
- target
130
- end
131
-
132
- # NOTE - for now, we're only supporting inverse setting from belongs_to back onto
133
- # has_one associations.
134
- def we_can_set_the_inverse_on_this?(record)
135
- @reflection.has_inverse? && @reflection.inverse_of.macro == :has_one
136
- end
137
- end
138
-
139
- module BelongsToPolymorphicAssociation
140
- def self.included(base)
141
- base.alias_method_chain :replace, :inverse_of
142
- base.alias_method_chain :find_target, :inverse_of
143
- end
144
-
145
- def replace_with_inverse_of(record)
146
- replace_without_inverse_of(record)
147
- set_inverse_instance(record, @owner)
148
- record
149
- end
150
-
151
- def find_target_with_inverse_of
152
- target = find_target_without_inverse_of
153
- set_inverse_instance(target, @owner)
154
- target
155
- end
156
-
157
- # NOTE - for now, we're only supporting inverse setting from belongs_to back onto
158
- # has_one associations.
159
- def we_can_set_the_inverse_on_this?(record)
160
- if @reflection.has_inverse?
161
- inverse_association = @reflection.polymorphic_inverse_of(record.class)
162
- inverse_association && inverse_association.macro == :has_one
163
- else
164
- false
165
- end
166
- end
167
-
168
- def set_inverse_instance(record, instance)
169
- return if record.nil? || !we_can_set_the_inverse_on_this?(record)
170
- inverse_relationship = @reflection.polymorphic_inverse_of(record.class)
171
- unless inverse_relationship.nil?
172
- record.send(:"set_#{inverse_relationship.name}_target", instance)
173
- end
174
- end
175
- end
176
-
177
- module HasManyAssociation
178
- def we_can_set_the_inverse_on_this?(record)
179
- inverse = @reflection.inverse_of
180
- return !inverse.nil?
181
- end
182
- end
183
-
184
- module HasManyThroughAssociation
185
- def initialize(owner, reflection)
186
- super
187
- end
188
-
189
- # NOTE - not sure that we can actually cope with inverses here
190
- def we_can_set_the_inverse_on_this?(record)
191
- false
192
- end
193
- end
194
-
195
- module HasOneAssociation
196
- def self.included(base)
197
- base.alias_method_chain :find_target, :inverse_of
198
- base.alias_method_chain :new_record, :inverse_of
199
- base.alias_method_chain :replace, :inverse_of
200
- end
201
-
202
- def find_target_with_inverse_of
203
- target = find_target_without_inverse_of
204
- set_inverse_instance(target, @owner)
205
- target
206
- end
207
-
208
- def replace_with_inverse_of(record, dont_save = false)
209
- value = replace_without_inverse_of(record, dont_save)
210
- set_inverse_instance(record, @owner)
211
- value
212
- end
213
-
214
- private
215
-
216
- def new_record_with_inverse_of(replace_existing, &block)
217
- record = new_record_without_inverse_of(replace_existing, &block)
218
- set_inverse_instance(record, @owner) unless replace_existing
219
- record
220
- end
221
-
222
- def we_can_set_the_inverse_on_this?(record)
223
- inverse = @reflection.inverse_of
224
- return !inverse.nil?
225
- end
226
- end
227
-
228
- module ClassMethods
229
- module JoinDependency
230
- def self.included(base)
231
- base.alias_method_chain :construct_association, :inverse_of
232
- end
233
-
234
- def construct_association_with_inverse_of(record, join, row)
235
- association = construct_association_without_inverse_of(record, join, row) or
236
- return nil
237
- association_proxy = record.send(join.reflection.name)
238
- association_proxy.__send__(:set_inverse_instance, association, record)
239
- end
240
- end
241
- end
242
- end
243
-
244
- module AssociationPreload
245
- def self.included(base)
246
- base.extend ClassMethods
247
- base.metaclass.alias_method_chain :add_preloaded_records_to_collection, :inverse_of
248
- base.metaclass.alias_method_chain :set_association_single_records, :inverse_of
249
- end
250
-
251
- module ClassMethods
252
- def add_preloaded_records_to_collection_with_inverse_of(parent_records, reflection_name, associated_record)
253
- value = add_preloaded_records_to_collection_without_inverse_of(parent_records, reflection_name, associated_record)
254
- parent_records.each do |parent_record|
255
- association_proxy = parent_record.send(reflection_name)
256
- association_proxy.__send__(:set_inverse_instance, associated_record, parent_record)
257
- end
258
- value
259
- end
260
-
261
- def set_association_single_records_with_inverse_of(id_to_record_map, reflection_name, associated_records, key)
262
- value = set_association_single_records_without_inverse_of(id_to_record_map, reflection_name, associated_records, key)
263
- associated_records.each do |associated_record|
264
- mapped_records = id_to_record_map[associated_record[key].to_s]
265
- mapped_records.each do |mapped_record|
266
- association_proxy = mapped_record.send(reflection_name)
267
- association_proxy.__send__(:set_inverse_instance, associated_record, mapped_record)
268
- end
269
- end
270
- value
271
- end
272
- end
273
- end
274
- end
275
-
276
- ActiveRecord::InverseOfAssociationNotFoundError = InverseOf::InverseOfAssociationNotFoundError
277
- ActiveRecord::Associations::AssociationCollection.send :include, InverseOf::Associations::AssociationCollection
278
- ActiveRecord::Associations::AssociationProxy.send :include, InverseOf::Associations::AssociationProxy
279
- ActiveRecord::Associations::BelongsToAssociation.send :include, InverseOf::Associations::BelongsToAssociation
280
- ActiveRecord::Associations::BelongsToPolymorphicAssociation.send :include, InverseOf::Associations::BelongsToPolymorphicAssociation
281
- ActiveRecord::Associations::HasManyAssociation.send :include, InverseOf::Associations::HasManyAssociation
282
- ActiveRecord::Associations::HasManyThroughAssociation.send :include, InverseOf::Associations::HasManyThroughAssociation
283
- ActiveRecord::Associations::HasOneAssociation.send :include, InverseOf::Associations::HasOneAssociation
284
- ActiveRecord::Associations::ClassMethods::JoinDependency.send :include, InverseOf::Associations::ClassMethods::JoinDependency
285
- ActiveRecord::Reflection.send :include, InverseOf::Reflection
286
- ActiveRecord::Base.send :include, InverseOf::AssociationPreload
287
-
288
- module ActiveRecord::Associations::ClassMethods
289
- @@valid_keys_for_has_many_association << :inverse_of
290
- @@valid_keys_for_has_one_association << :inverse_of
291
- @@valid_keys_for_belongs_to_association << :inverse_of
292
- end
293
- end
@@ -1 +0,0 @@
1
- require 'inverse_of'
@@ -1,28 +0,0 @@
1
- require 'test/unit'
2
- require 'ruby-debug'
3
- require 'active_record'
4
- require 'active_record/test_case'
5
- require 'active_record/fixtures'
6
-
7
- ROOT = File.dirname(File.dirname(File.dirname(__FILE__)))
8
-
9
- require "#{ROOT}/rails/init"
10
-
11
- class ActiveSupport::TestCase
12
- include ActiveRecord::TestFixtures
13
-
14
- self.fixture_path = "#{ROOT}/test/fixtures"
15
- self.use_instantiated_fixtures = false
16
- self.use_transactional_fixtures = true
17
-
18
- def create_fixtures(*table_names, &block)
19
- Fixtures.create_fixtures(ActiveSupport::TestCase.fixture_path, table_names, {}, &block)
20
- end
21
- end
22
-
23
- ActiveRecord::Base.configurations = {'test' => {'adapter' => "sqlite3", 'database' => ":memory:"}}
24
- ActiveRecord::Base.establish_connection('test')
25
- ActiveRecord::Base.connection.instance_eval do
26
- eval File.read("#{ROOT}/test/schema/schema.rb")
27
- end
28
- Dir["#{ROOT}/test/models/*.rb"].each{|path| require path}
@@ -1,567 +0,0 @@
1
- require "cases/helper"
2
- require 'models/man'
3
- require 'models/face'
4
- require 'models/interest'
5
- require 'models/zine'
6
- require 'models/club'
7
- require 'models/sponsor'
8
-
9
- class InverseAssociationTests < ActiveRecord::TestCase
10
- def test_should_allow_for_inverse_of_options_in_associations
11
- assert_nothing_raised(ArgumentError, 'ActiveRecord should allow the inverse_of options on has_many') do
12
- Class.new(ActiveRecord::Base).has_many(:wheels, :inverse_of => :car)
13
- end
14
-
15
- assert_nothing_raised(ArgumentError, 'ActiveRecord should allow the inverse_of options on has_one') do
16
- Class.new(ActiveRecord::Base).has_one(:engine, :inverse_of => :car)
17
- end
18
-
19
- assert_nothing_raised(ArgumentError, 'ActiveRecord should allow the inverse_of options on belongs_to') do
20
- Class.new(ActiveRecord::Base).belongs_to(:car, :inverse_of => :driver)
21
- end
22
- end
23
-
24
- def test_should_be_able_to_ask_a_reflection_if_it_has_an_inverse
25
- has_one_with_inverse_ref = Man.reflect_on_association(:face)
26
- assert has_one_with_inverse_ref.respond_to?(:has_inverse?)
27
- assert has_one_with_inverse_ref.has_inverse?
28
-
29
- has_many_with_inverse_ref = Man.reflect_on_association(:interests)
30
- assert has_many_with_inverse_ref.respond_to?(:has_inverse?)
31
- assert has_many_with_inverse_ref.has_inverse?
32
-
33
- belongs_to_with_inverse_ref = Face.reflect_on_association(:man)
34
- assert belongs_to_with_inverse_ref.respond_to?(:has_inverse?)
35
- assert belongs_to_with_inverse_ref.has_inverse?
36
-
37
- has_one_without_inverse_ref = Club.reflect_on_association(:sponsor)
38
- assert has_one_without_inverse_ref.respond_to?(:has_inverse?)
39
- assert !has_one_without_inverse_ref.has_inverse?
40
-
41
- has_many_without_inverse_ref = Club.reflect_on_association(:memberships)
42
- assert has_many_without_inverse_ref.respond_to?(:has_inverse?)
43
- assert !has_many_without_inverse_ref.has_inverse?
44
-
45
- belongs_to_without_inverse_ref = Sponsor.reflect_on_association(:sponsor_club)
46
- assert belongs_to_without_inverse_ref.respond_to?(:has_inverse?)
47
- assert !belongs_to_without_inverse_ref.has_inverse?
48
- end
49
-
50
- def test_should_be_able_to_ask_a_reflection_what_it_is_the_inverse_of
51
- has_one_ref = Man.reflect_on_association(:face)
52
- assert has_one_ref.respond_to?(:inverse_of)
53
-
54
- has_many_ref = Man.reflect_on_association(:interests)
55
- assert has_many_ref.respond_to?(:inverse_of)
56
-
57
- belongs_to_ref = Face.reflect_on_association(:man)
58
- assert belongs_to_ref.respond_to?(:inverse_of)
59
- end
60
-
61
- def test_inverse_of_method_should_supply_the_actual_reflection_instance_it_is_the_inverse_of
62
- has_one_ref = Man.reflect_on_association(:face)
63
- assert_equal Face.reflect_on_association(:man), has_one_ref.inverse_of
64
-
65
- has_many_ref = Man.reflect_on_association(:interests)
66
- assert_equal Interest.reflect_on_association(:man), has_many_ref.inverse_of
67
-
68
- belongs_to_ref = Face.reflect_on_association(:man)
69
- assert_equal Man.reflect_on_association(:face), belongs_to_ref.inverse_of
70
- end
71
-
72
- def test_associations_with_no_inverse_of_should_return_nil
73
- has_one_ref = Club.reflect_on_association(:sponsor)
74
- assert_nil has_one_ref.inverse_of
75
-
76
- has_many_ref = Club.reflect_on_association(:memberships)
77
- assert_nil has_many_ref.inverse_of
78
-
79
- belongs_to_ref = Sponsor.reflect_on_association(:sponsor_club)
80
- assert_nil belongs_to_ref.inverse_of
81
- end
82
- end
83
-
84
- class InverseHasOneTests < ActiveRecord::TestCase
85
- fixtures :men, :faces
86
-
87
- def test_parent_instance_should_be_shared_with_child_on_find
88
- m = men(:gordon)
89
- f = m.face
90
- assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
91
- m.name = 'Bongo'
92
- assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
93
- f.man.name = 'Mungo'
94
- assert_equal m.name, f.man.name, "Name of man should be the same after changes to child-owned instance"
95
- end
96
-
97
-
98
- def test_parent_instance_should_be_shared_with_eager_loaded_child_on_find
99
- m = Man.find(:first, :conditions => {:name => 'Gordon'}, :include => :face)
100
- f = m.face
101
- assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
102
- m.name = 'Bongo'
103
- assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
104
- f.man.name = 'Mungo'
105
- assert_equal m.name, f.man.name, "Name of man should be the same after changes to child-owned instance"
106
-
107
- m = Man.find(:first, :conditions => {:name => 'Gordon'}, :include => :face, :order => 'faces.id')
108
- f = m.face
109
- assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
110
- m.name = 'Bongo'
111
- assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
112
- f.man.name = 'Mungo'
113
- assert_equal m.name, f.man.name, "Name of man should be the same after changes to child-owned instance"
114
- end
115
-
116
- def test_parent_instance_should_be_shared_with_newly_built_child
117
- m = men(:gordon)
118
- f = m.build_face(:description => 'haunted')
119
- assert_not_nil f.man
120
- assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
121
- m.name = 'Bongo'
122
- assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
123
- f.man.name = 'Mungo'
124
- assert_equal m.name, f.man.name, "Name of man should be the same after changes to just-built-child-owned instance"
125
- end
126
-
127
- def test_parent_instance_should_be_shared_with_newly_created_child
128
- m = men(:gordon)
129
- f = m.create_face(:description => 'haunted')
130
- assert_not_nil f.man
131
- assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
132
- m.name = 'Bongo'
133
- assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
134
- f.man.name = 'Mungo'
135
- assert_equal m.name, f.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
136
- end
137
-
138
- def test_parent_instance_should_be_shared_with_newly_created_child_via_bang_method
139
- m = Man.find(:first)
140
- f = m.face.create!(:description => 'haunted')
141
- assert_not_nil f.man
142
- assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
143
- m.name = 'Bongo'
144
- assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
145
- f.man.name = 'Mungo'
146
- assert_equal m.name, f.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
147
- end
148
-
149
- def test_parent_instance_should_be_shared_with_newly_built_child_when_we_dont_replace_existing
150
- m = Man.find(:first)
151
- f = m.build_face({:description => 'haunted'}, false)
152
- assert_not_nil f.man
153
- assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
154
- m.name = 'Bongo'
155
- assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
156
- f.man.name = 'Mungo'
157
- assert_equal m.name, f.man.name, "Name of man should be the same after changes to just-built-child-owned instance"
158
- end
159
-
160
- def test_parent_instance_should_be_shared_with_newly_created_child_when_we_dont_replace_existing
161
- m = Man.find(:first)
162
- f = m.create_face({:description => 'haunted'}, false)
163
- assert_not_nil f.man
164
- assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
165
- m.name = 'Bongo'
166
- assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
167
- f.man.name = 'Mungo'
168
- assert_equal m.name, f.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
169
- end
170
-
171
- def test_parent_instance_should_be_shared_with_newly_created_child_via_bang_method_when_we_dont_replace_existing
172
- m = Man.find(:first)
173
- f = m.face.create!({:description => 'haunted'}, false)
174
- assert_not_nil f.man
175
- assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
176
- m.name = 'Bongo'
177
- assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
178
- f.man.name = 'Mungo'
179
- assert_equal m.name, f.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
180
- end
181
-
182
- def test_parent_instance_should_be_shared_with_replaced_via_accessor_child
183
- m = Man.find(:first)
184
- f = Face.new(:description => 'haunted')
185
- m.face = f
186
- assert_not_nil f.man
187
- assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
188
- m.name = 'Bongo'
189
- assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
190
- f.man.name = 'Mungo'
191
- assert_equal m.name, f.man.name, "Name of man should be the same after changes to replaced-child-owned instance"
192
- end
193
-
194
- def test_parent_instance_should_be_shared_with_replaced_via_method_child
195
- m = Man.find(:first)
196
- f = Face.new(:description => 'haunted')
197
- m.face.replace(f)
198
- assert_not_nil f.man
199
- assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
200
- m.name = 'Bongo'
201
- assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
202
- f.man.name = 'Mungo'
203
- assert_equal m.name, f.man.name, "Name of man should be the same after changes to replaced-child-owned instance"
204
- end
205
-
206
- def test_parent_instance_should_be_shared_with_replaced_via_method_child_when_we_dont_replace_existing
207
- m = Man.find(:first)
208
- f = Face.new(:description => 'haunted')
209
- m.face.replace(f, false)
210
- assert_not_nil f.man
211
- assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance"
212
- m.name = 'Bongo'
213
- assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance"
214
- f.man.name = 'Mungo'
215
- assert_equal m.name, f.man.name, "Name of man should be the same after changes to replaced-child-owned instance"
216
- end
217
-
218
- def test_trying_to_use_inverses_that_dont_exist_should_raise_an_error
219
- assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Man.find(:first).dirty_face }
220
- end
221
- end
222
-
223
- class InverseHasManyTests < ActiveRecord::TestCase
224
- fixtures :men, :interests
225
-
226
- def test_parent_instance_should_be_shared_with_every_child_on_find
227
- m = men(:gordon)
228
- is = m.interests
229
- is.each do |i|
230
- assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
231
- m.name = 'Bongo'
232
- assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
233
- i.man.name = 'Mungo'
234
- assert_equal m.name, i.man.name, "Name of man should be the same after changes to child-owned instance"
235
- end
236
- end
237
-
238
- def test_parent_instance_should_be_shared_with_eager_loaded_children
239
- m = Man.find(:first, :conditions => {:name => 'Gordon'}, :include => :interests)
240
- is = m.interests
241
- is.each do |i|
242
- assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
243
- m.name = 'Bongo'
244
- assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
245
- i.man.name = 'Mungo'
246
- assert_equal m.name, i.man.name, "Name of man should be the same after changes to child-owned instance"
247
- end
248
-
249
- m = Man.find(:first, :conditions => {:name => 'Gordon'}, :include => :interests, :order => 'interests.id')
250
- is = m.interests
251
- is.each do |i|
252
- assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
253
- m.name = 'Bongo'
254
- assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
255
- i.man.name = 'Mungo'
256
- assert_equal m.name, i.man.name, "Name of man should be the same after changes to child-owned instance"
257
- end
258
- end
259
-
260
- def test_parent_instance_should_be_shared_with_newly_built_child
261
- m = men(:gordon)
262
- i = m.interests.build(:topic => 'Industrial Revolution Re-enactment')
263
- assert_not_nil i.man
264
- assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
265
- m.name = 'Bongo'
266
- assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
267
- i.man.name = 'Mungo'
268
- assert_equal m.name, i.man.name, "Name of man should be the same after changes to just-built-child-owned instance"
269
- end
270
-
271
- def test_parent_instance_should_be_shared_with_newly_block_style_built_child
272
- m = Man.find(:first)
273
- i = m.interests.build {|ii| ii.topic = 'Industrial Revolution Re-enactment'}
274
- assert_not_nil i.topic, "Child attributes supplied to build via blocks should be populated"
275
- assert_not_nil i.man
276
- assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
277
- m.name = 'Bongo'
278
- assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
279
- i.man.name = 'Mungo'
280
- assert_equal m.name, i.man.name, "Name of man should be the same after changes to just-built-child-owned instance"
281
- end
282
-
283
- def test_parent_instance_should_be_shared_with_newly_created_child
284
- m = men(:gordon)
285
- i = m.interests.create(:topic => 'Industrial Revolution Re-enactment')
286
- assert_not_nil i.man
287
- assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
288
- m.name = 'Bongo'
289
- assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
290
- i.man.name = 'Mungo'
291
- assert_equal m.name, i.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
292
- end
293
-
294
- def test_parent_instance_should_be_shared_with_newly_created_via_bang_method_child
295
- m = Man.find(:first)
296
- i = m.interests.create!(:topic => 'Industrial Revolution Re-enactment')
297
- assert_not_nil i.man
298
- assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
299
- m.name = 'Bongo'
300
- assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
301
- i.man.name = 'Mungo'
302
- assert_equal m.name, i.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
303
- end
304
-
305
- def test_parent_instance_should_be_shared_with_newly_block_style_created_child
306
- m = Man.find(:first)
307
- i = m.interests.create {|ii| ii.topic = 'Industrial Revolution Re-enactment'}
308
- assert_not_nil i.topic, "Child attributes supplied to create via blocks should be populated"
309
- assert_not_nil i.man
310
- assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
311
- m.name = 'Bongo'
312
- assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
313
- i.man.name = 'Mungo'
314
- assert_equal m.name, i.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
315
- end
316
-
317
- def test_parent_instance_should_be_shared_with_poked_in_child
318
- m = men(:gordon)
319
- i = Interest.create(:topic => 'Industrial Revolution Re-enactment')
320
- m.interests << i
321
- assert_not_nil i.man
322
- assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
323
- m.name = 'Bongo'
324
- assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
325
- i.man.name = 'Mungo'
326
- assert_equal m.name, i.man.name, "Name of man should be the same after changes to newly-created-child-owned instance"
327
- end
328
-
329
- def test_parent_instance_should_be_shared_with_replaced_via_accessor_children
330
- m = Man.find(:first)
331
- i = Interest.new(:topic => 'Industrial Revolution Re-enactment')
332
- m.interests = [i]
333
- assert_not_nil i.man
334
- assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
335
- m.name = 'Bongo'
336
- assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
337
- i.man.name = 'Mungo'
338
- assert_equal m.name, i.man.name, "Name of man should be the same after changes to replaced-child-owned instance"
339
- end
340
-
341
- def test_parent_instance_should_be_shared_with_replaced_via_method_children
342
- m = Man.find(:first)
343
- i = Interest.new(:topic => 'Industrial Revolution Re-enactment')
344
- m.interests.replace([i])
345
- assert_not_nil i.man
346
- assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance"
347
- m.name = 'Bongo'
348
- assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance"
349
- i.man.name = 'Mungo'
350
- assert_equal m.name, i.man.name, "Name of man should be the same after changes to replaced-child-owned instance"
351
- end
352
-
353
- def test_trying_to_use_inverses_that_dont_exist_should_raise_an_error
354
- assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Man.find(:first).secret_interests }
355
- end
356
- end
357
-
358
- class InverseBelongsToTests < ActiveRecord::TestCase
359
- fixtures :men, :faces, :interests
360
-
361
- def test_child_instance_should_be_shared_with_parent_on_find
362
- f = faces(:trusting)
363
- m = f.man
364
- assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance"
365
- f.description = 'gormless'
366
- assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance"
367
- m.face.description = 'pleasing'
368
- assert_equal f.description, m.face.description, "Description of face should be the same after changes to parent-owned instance"
369
- end
370
-
371
- def test_eager_loaded_child_instance_should_be_shared_with_parent_on_find
372
- f = Face.find(:first, :include => :man, :conditions => {:description => 'trusting'})
373
- m = f.man
374
- assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance"
375
- f.description = 'gormless'
376
- assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance"
377
- m.face.description = 'pleasing'
378
- assert_equal f.description, m.face.description, "Description of face should be the same after changes to parent-owned instance"
379
-
380
- f = Face.find(:first, :include => :man, :order => 'men.id', :conditions => {:description => 'trusting'})
381
- m = f.man
382
- assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance"
383
- f.description = 'gormless'
384
- assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance"
385
- m.face.description = 'pleasing'
386
- assert_equal f.description, m.face.description, "Description of face should be the same after changes to parent-owned instance"
387
- end
388
-
389
- def test_child_instance_should_be_shared_with_newly_built_parent
390
- f = faces(:trusting)
391
- m = f.build_man(:name => 'Charles')
392
- assert_not_nil m.face
393
- assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance"
394
- f.description = 'gormless'
395
- assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance"
396
- m.face.description = 'pleasing'
397
- assert_equal f.description, m.face.description, "Description of face should be the same after changes to just-built-parent-owned instance"
398
- end
399
-
400
- def test_child_instance_should_be_shared_with_newly_created_parent
401
- f = faces(:trusting)
402
- m = f.create_man(:name => 'Charles')
403
- assert_not_nil m.face
404
- assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance"
405
- f.description = 'gormless'
406
- assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance"
407
- m.face.description = 'pleasing'
408
- assert_equal f.description, m.face.description, "Description of face should be the same after changes to newly-created-parent-owned instance"
409
- end
410
-
411
- def test_should_not_try_to_set_inverse_instances_when_the_inverse_is_a_has_many
412
- i = interests(:trainspotting)
413
- m = i.man
414
- assert_not_nil m.interests
415
- iz = m.interests.detect {|iz| iz.id == i.id}
416
- assert_not_nil iz
417
- assert_equal i.topic, iz.topic, "Interest topics should be the same before changes to child"
418
- i.topic = 'Eating cheese with a spoon'
419
- assert_not_equal i.topic, iz.topic, "Interest topics should not be the same after changes to child"
420
- iz.topic = 'Cow tipping'
421
- assert_not_equal i.topic, iz.topic, "Interest topics should not be the same after changes to parent-owned instance"
422
- end
423
-
424
- def test_child_instance_should_be_shared_with_replaced_via_accessor_parent
425
- f = Face.find(:first)
426
- m = Man.new(:name => 'Charles')
427
- f.man = m
428
- assert_not_nil m.face
429
- assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance"
430
- f.description = 'gormless'
431
- assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance"
432
- m.face.description = 'pleasing'
433
- assert_equal f.description, m.face.description, "Description of face should be the same after changes to replaced-parent-owned instance"
434
- end
435
-
436
- def test_child_instance_should_be_shared_with_replaced_via_method_parent
437
- f = faces(:trusting)
438
- assert_not_nil f.man
439
- m = Man.new(:name => 'Charles')
440
- f.man.replace(m)
441
- assert_not_nil m.face
442
- assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance"
443
- f.description = 'gormless'
444
- assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance"
445
- m.face.description = 'pleasing'
446
- assert_equal f.description, m.face.description, "Description of face should be the same after changes to replaced-parent-owned instance"
447
- end
448
-
449
- def test_trying_to_use_inverses_that_dont_exist_should_raise_an_error
450
- assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.find(:first).horrible_man }
451
- end
452
- end
453
-
454
- class InversePolymorphicBelongsToTests < ActiveRecord::TestCase
455
- fixtures :men, :faces, :interests
456
-
457
- def test_child_instance_should_be_shared_with_parent_on_find
458
- f = Face.find(:first, :conditions => {:description => 'confused'})
459
- m = f.polymorphic_man
460
- assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same before changes to child instance"
461
- f.description = 'gormless'
462
- assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same after changes to child instance"
463
- m.polymorphic_face.description = 'pleasing'
464
- assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same after changes to parent-owned instance"
465
- end
466
-
467
- def test_eager_loaded_child_instance_should_be_shared_with_parent_on_find
468
- f = Face.find(:first, :conditions => {:description => 'confused'}, :include => :man)
469
- m = f.polymorphic_man
470
- assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same before changes to child instance"
471
- f.description = 'gormless'
472
- assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same after changes to child instance"
473
- m.polymorphic_face.description = 'pleasing'
474
- assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same after changes to parent-owned instance"
475
-
476
- f = Face.find(:first, :conditions => {:description => 'confused'}, :include => :man, :order => 'men.id')
477
- m = f.polymorphic_man
478
- assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same before changes to child instance"
479
- f.description = 'gormless'
480
- assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same after changes to child instance"
481
- m.polymorphic_face.description = 'pleasing'
482
- assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same after changes to parent-owned instance"
483
- end
484
-
485
- def test_child_instance_should_be_shared_with_replaced_via_accessor_parent
486
- face = faces(:confused)
487
- old_man = face.polymorphic_man
488
- new_man = Man.new
489
-
490
- assert_not_nil face.polymorphic_man
491
- face.polymorphic_man = new_man
492
-
493
- assert_equal face.description, new_man.polymorphic_face.description, "Description of face should be the same before changes to parent instance"
494
- face.description = 'Bongo'
495
- assert_equal face.description, new_man.polymorphic_face.description, "Description of face should be the same after changes to parent instance"
496
- new_man.polymorphic_face.description = 'Mungo'
497
- assert_equal face.description, new_man.polymorphic_face.description, "Description of face should be the same after changes to replaced-parent-owned instance"
498
- end
499
-
500
- def test_child_instance_should_be_shared_with_replaced_via_method_parent
501
- face = faces(:confused)
502
- old_man = face.polymorphic_man
503
- new_man = Man.new
504
-
505
- assert_not_nil face.polymorphic_man
506
- face.polymorphic_man.replace(new_man)
507
-
508
- assert_equal face.description, new_man.polymorphic_face.description, "Description of face should be the same before changes to parent instance"
509
- face.description = 'Bongo'
510
- assert_equal face.description, new_man.polymorphic_face.description, "Description of face should be the same after changes to parent instance"
511
- new_man.polymorphic_face.description = 'Mungo'
512
- assert_equal face.description, new_man.polymorphic_face.description, "Description of face should be the same after changes to replaced-parent-owned instance"
513
- end
514
-
515
- def test_should_not_try_to_set_inverse_instances_when_the_inverse_is_a_has_many
516
- i = interests(:llama_wrangling)
517
- m = i.polymorphic_man
518
- assert_not_nil m.polymorphic_interests
519
- iz = m.polymorphic_interests.detect {|iz| iz.id == i.id}
520
- assert_not_nil iz
521
- assert_equal i.topic, iz.topic, "Interest topics should be the same before changes to child"
522
- i.topic = 'Eating cheese with a spoon'
523
- assert_not_equal i.topic, iz.topic, "Interest topics should not be the same after changes to child"
524
- iz.topic = 'Cow tipping'
525
- assert_not_equal i.topic, iz.topic, "Interest topics should not be the same after changes to parent-owned instance"
526
- end
527
-
528
- def test_trying_to_access_inverses_that_dont_exist_shouldnt_raise_an_error
529
- # Ideally this would, if only for symmetry's sake with other association types
530
- assert_nothing_raised(ActiveRecord::InverseOfAssociationNotFoundError) { Face.find(:first).horrible_polymorphic_man }
531
- end
532
-
533
- def test_trying_to_set_polymorphic_inverses_that_dont_exist_at_all_should_raise_an_error
534
- # fails because no class has the correct inverse_of for horrible_polymorphic_man
535
- assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.find(:first).horrible_polymorphic_man = Man.first }
536
- end
537
-
538
- def test_trying_to_set_polymorphic_inverses_that_dont_exist_on_the_instance_being_set_should_raise_an_error
539
- # passes because Man does have the correct inverse_of
540
- assert_nothing_raised(ActiveRecord::InverseOfAssociationNotFoundError) { Face.find(:first).polymorphic_man = Man.first }
541
- # fails because Interest does have the correct inverse_of
542
- assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.find(:first).polymorphic_man = Interest.first }
543
- end
544
- end
545
-
546
- # NOTE - these tests might not be meaningful, ripped as they were from the parental_control plugin
547
- # which would guess the inverse rather than look for an explicit configuration option.
548
- class InverseMultipleHasManyInversesForSameModel < ActiveRecord::TestCase
549
- fixtures :men, :interests, :zines
550
-
551
- def test_that_we_can_load_associations_that_have_the_same_reciprocal_name_from_different_models
552
- assert_nothing_raised(ActiveRecord::AssociationTypeMismatch) do
553
- i = Interest.find(:first)
554
- z = i.zine
555
- m = i.man
556
- end
557
- end
558
-
559
- def test_that_we_can_create_associations_that_have_the_same_reciprocal_name_from_different_models
560
- assert_nothing_raised(ActiveRecord::AssociationTypeMismatch) do
561
- i = Interest.find(:first)
562
- i.build_zine(:title => 'Get Some in Winter! 2008')
563
- i.build_man(:name => 'Gordon')
564
- i.save!
565
- end
566
- end
567
- end