inverse_of 0.0.1 → 0.1.0

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.
@@ -0,0 +1,11 @@
1
+ module InverseOf
2
+ VERSION = [0, 1, 0]
3
+
4
+ class << VERSION
5
+ include Comparable
6
+
7
+ def to_s
8
+ join('.')
9
+ end
10
+ end
11
+ end
metadata CHANGED
@@ -1,91 +1,110 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: inverse_of
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
5
6
  platform: ruby
6
- authors:
7
+ authors:
7
8
  - George Ogata
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
12
+ date: 2011-08-31 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: &70237185895300 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - <
20
+ - !ruby/object:Gem::Version
21
+ version: 2.3.6
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70237185895300
25
+ - !ruby/object:Gem::Dependency
26
+ name: ritual
27
+ requirement: &70237185894520 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70237185894520
36
+ - !ruby/object:Gem::Dependency
37
+ name: bundler
38
+ requirement: &70237185894040 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70237185894040
47
+ - !ruby/object:Gem::Dependency
48
+ name: sqlite3-ruby
49
+ requirement: &70237185893540 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70237185893540
58
+ - !ruby/object:Gem::Dependency
59
+ name: ruby-debug19
60
+ requirement: &70237185892840 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70237185892840
69
+ description: ! 'Adds the :inverse option to Active Record associations for Active
11
70
 
12
- date: 2010-02-05 00:00:00 -05:00
13
- default_executable:
14
- dependencies: []
71
+ Record 2.3.0 -- 2.3.5.
15
72
 
16
- description: Backport of ActiveRecord 2.3.6's inverse associations.
17
- email: george.ogata@gmail.com
73
+ '
74
+ email:
75
+ - george.ogata@gmail.com
18
76
  executables: []
19
-
20
77
  extensions: []
21
-
22
- extra_rdoc_files:
23
- - LICENSE
24
- - README.markdown
25
- files:
26
- - .document
27
- - .gitignore
78
+ extra_rdoc_files: []
79
+ files:
80
+ - lib/inverse_of/version.rb
81
+ - lib/inverse_of.rb
28
82
  - LICENSE
29
- - MIT-LICENSE
30
83
  - README.markdown
31
84
  - Rakefile
32
- - VERSION
33
- - install.rb
34
- - inverse_of.gemspec
35
- - lib/inverse_of.rb
36
- - rails/init.rb
37
- - tasks/inverse_of_tasks.rake
38
- - test/cases/helper.rb
39
- - test/cases/inverse_associations_test.rb
40
- - test/fixtures/clubs.yml
41
- - test/fixtures/faces.yml
42
- - test/fixtures/interests.yml
43
- - test/fixtures/men.yml
44
- - test/fixtures/sponsors.yml
45
- - test/fixtures/zines.yml
46
- - test/models/club.rb
47
- - test/models/face.rb
48
- - test/models/interest.rb
49
- - test/models/man.rb
50
- - test/models/sponsor.rb
51
- - test/models/zine.rb
52
- - test/schema/schema.rb
53
- - uninstall.rb
54
- has_rdoc: true
85
+ - CHANGELOG
55
86
  homepage: http://github.com/oggy/inverse_of
56
87
  licenses: []
57
-
58
88
  post_install_message:
59
- rdoc_options:
60
- - --charset=UTF-8
61
- require_paths:
89
+ rdoc_options: []
90
+ require_paths:
62
91
  - lib
63
- required_ruby_version: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - ">="
66
- - !ruby/object:Gem::Version
67
- version: "0"
68
- version:
69
- required_rubygems_version: !ruby/object:Gem::Requirement
70
- requirements:
71
- - - ">="
72
- - !ruby/object:Gem::Version
73
- version: "0"
74
- version:
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: 1.3.6
75
104
  requirements: []
76
-
77
105
  rubyforge_project:
78
- rubygems_version: 1.3.5
106
+ rubygems_version: 1.8.6
79
107
  signing_key:
80
108
  specification_version: 3
81
109
  summary: Backport of ActiveRecord 2.3.6's inverse associations.
82
- test_files:
83
- - test/cases/helper.rb
84
- - test/cases/inverse_associations_test.rb
85
- - test/models/club.rb
86
- - test/models/face.rb
87
- - test/models/interest.rb
88
- - test/models/man.rb
89
- - test/models/sponsor.rb
90
- - test/models/zine.rb
91
- - test/schema/schema.rb
110
+ test_files: []
data/.document DELETED
@@ -1,5 +0,0 @@
1
- README.rdoc
2
- lib/**/*.rb
3
- bin/*
4
- features/**/*.feature
5
- LICENSE
data/.gitignore DELETED
@@ -1,21 +0,0 @@
1
- ## MAC OS
2
- .DS_Store
3
-
4
- ## TEXTMATE
5
- *.tmproj
6
- tmtags
7
-
8
- ## EMACS
9
- *~
10
- \#*
11
- .\#*
12
-
13
- ## VIM
14
- *.swp
15
-
16
- ## PROJECT::GENERAL
17
- coverage
18
- rdoc
19
- pkg
20
-
21
- ## PROJECT::SPECIFIC
@@ -1,20 +0,0 @@
1
- Copyright (c) 2009 [name of plugin creator]
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.0.1
data/install.rb DELETED
@@ -1 +0,0 @@
1
- # Install hook code here
@@ -1,76 +0,0 @@
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{inverse_of}
8
- s.version = "0.0.1"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["George Ogata"]
12
- s.date = %q{2010-02-05}
13
- s.description = %q{Backport of ActiveRecord 2.3.6's inverse associations.}
14
- s.email = %q{george.ogata@gmail.com}
15
- s.extra_rdoc_files = [
16
- "LICENSE",
17
- "README.markdown"
18
- ]
19
- s.files = [
20
- ".document",
21
- ".gitignore",
22
- "LICENSE",
23
- "MIT-LICENSE",
24
- "README.markdown",
25
- "Rakefile",
26
- "VERSION",
27
- "install.rb",
28
- "inverse_of.gemspec",
29
- "lib/inverse_of.rb",
30
- "rails/init.rb",
31
- "tasks/inverse_of_tasks.rake",
32
- "test/cases/helper.rb",
33
- "test/cases/inverse_associations_test.rb",
34
- "test/fixtures/clubs.yml",
35
- "test/fixtures/faces.yml",
36
- "test/fixtures/interests.yml",
37
- "test/fixtures/men.yml",
38
- "test/fixtures/sponsors.yml",
39
- "test/fixtures/zines.yml",
40
- "test/models/club.rb",
41
- "test/models/face.rb",
42
- "test/models/interest.rb",
43
- "test/models/man.rb",
44
- "test/models/sponsor.rb",
45
- "test/models/zine.rb",
46
- "test/schema/schema.rb",
47
- "uninstall.rb"
48
- ]
49
- s.homepage = %q{http://github.com/oggy/inverse_of}
50
- s.rdoc_options = ["--charset=UTF-8"]
51
- s.require_paths = ["lib"]
52
- s.rubygems_version = %q{1.3.5}
53
- s.summary = %q{Backport of ActiveRecord 2.3.6's inverse associations.}
54
- s.test_files = [
55
- "test/cases/helper.rb",
56
- "test/cases/inverse_associations_test.rb",
57
- "test/models/club.rb",
58
- "test/models/face.rb",
59
- "test/models/interest.rb",
60
- "test/models/man.rb",
61
- "test/models/sponsor.rb",
62
- "test/models/zine.rb",
63
- "test/schema/schema.rb"
64
- ]
65
-
66
- if s.respond_to? :specification_version then
67
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
68
- s.specification_version = 3
69
-
70
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
71
- else
72
- end
73
- else
74
- end
75
- end
76
-
@@ -1 +0,0 @@
1
- require 'inverse_of'
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :inverse_of do
3
- # # Task goes here
4
- # end
@@ -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