dm-accepts_nested_attributes_for 1.2.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.
Files changed (58) hide show
  1. data/CHANGELOG +970 -0
  2. data/Gemfile +84 -0
  3. data/LICENSE +20 -0
  4. data/README.textile +94 -0
  5. data/Rakefile +25 -0
  6. data/TODO +6 -0
  7. data/VERSION +1 -0
  8. data/dm-accepts_nested_attributes.gemspec +114 -0
  9. data/lib/dm-accepts_nested_attributes.rb +13 -0
  10. data/lib/dm-accepts_nested_attributes/model.rb +144 -0
  11. data/lib/dm-accepts_nested_attributes/relationship.rb +82 -0
  12. data/lib/dm-accepts_nested_attributes/resource.rb +382 -0
  13. data/lib/dm-accepts_nested_attributes/version.rb +7 -0
  14. data/spec/accepts_nested_attributes_for_spec.rb +408 -0
  15. data/spec/assign_nested_attributes_for_spec.rb +101 -0
  16. data/spec/comb/1-1_disjoint_spec.rb +67 -0
  17. data/spec/comb/1-1_overlapping_spec.rb +66 -0
  18. data/spec/comb/1-1_subset_spec.rb +65 -0
  19. data/spec/comb/1-1_superset_spec.rb +67 -0
  20. data/spec/comb/1-m_disjoint_spec.rb +71 -0
  21. data/spec/comb/1-m_overlapping_spec.rb +70 -0
  22. data/spec/comb/1-m_subset_spec.rb +65 -0
  23. data/spec/comb/1-m_superset_spec.rb +71 -0
  24. data/spec/comb/m-1_disjoint_spec.rb +71 -0
  25. data/spec/comb/m-1_overlapping_spec.rb +70 -0
  26. data/spec/comb/m-1_subset_spec.rb +65 -0
  27. data/spec/comb/m-1_superset_spec.rb +71 -0
  28. data/spec/comb/n-m_composite_spec.rb +141 -0
  29. data/spec/comb/n-m_surrogate_spec.rb +154 -0
  30. data/spec/many_to_many_composite_spec.rb +120 -0
  31. data/spec/many_to_many_spec.rb +129 -0
  32. data/spec/many_to_one_composite_spec.rb +120 -0
  33. data/spec/many_to_one_spec.rb +101 -0
  34. data/spec/one_to_many_composite_spec.rb +120 -0
  35. data/spec/one_to_many_spec.rb +100 -0
  36. data/spec/one_to_one_composite_spec.rb +150 -0
  37. data/spec/one_to_one_spec.rb +115 -0
  38. data/spec/rcov.opts +6 -0
  39. data/spec/resource_spec.rb +65 -0
  40. data/spec/shared/many_to_many_composite_spec.rb +149 -0
  41. data/spec/shared/many_to_many_spec.rb +146 -0
  42. data/spec/shared/many_to_one_composite_spec.rb +160 -0
  43. data/spec/shared/many_to_one_spec.rb +130 -0
  44. data/spec/shared/one_to_many_composite_spec.rb +159 -0
  45. data/spec/shared/one_to_many_spec.rb +107 -0
  46. data/spec/shared/one_to_one_composite_spec.rb +114 -0
  47. data/spec/shared/one_to_one_spec.rb +111 -0
  48. data/spec/spec.opts +4 -0
  49. data/spec/spec_helper.rb +50 -0
  50. data/spec/update_dirty_spec.rb +113 -0
  51. data/spec/update_multiple_spec.rb +79 -0
  52. data/tasks/changelog.rake +20 -0
  53. data/tasks/ci.rake +1 -0
  54. data/tasks/local_gemfile.rake +18 -0
  55. data/tasks/spec.rake +22 -0
  56. data/tasks/yard.rake +9 -0
  57. data/tasks/yardstick.rake +19 -0
  58. metadata +216 -0
@@ -0,0 +1,160 @@
1
+ describe "every accessible many_to_one composite association", :shared => true do
2
+
3
+ it "should allow to update an existing person via Membership#person_attributes" do
4
+ Person.all.size.should == 0
5
+ Project.all.size.should == 0
6
+ Membership.all.size.should == 0
7
+
8
+ person = Person.create(:audit_id => 1, :name => 'Martin')
9
+ project = Project.create(:audit_id => 2, :name => 'trippings')
10
+ membership = Membership.new(:role => 'maintainer')
11
+ membership.person = person
12
+ membership.project = project
13
+ membership.save.should be_true
14
+
15
+ Person.all.size.should == 1
16
+ Project.all.size.should == 1
17
+ Membership.all.size.should == 1
18
+ Person.first.name.should == 'Martin'
19
+
20
+ membership.person_attributes = { :id => person.id, :audit_id => person.audit_id, :name => 'Martin Gamsjaeger' }
21
+ membership.save.should be_true
22
+
23
+ Person.all.size.should == 1
24
+ Project.all.size.should == 1
25
+ Membership.all.size.should == 1
26
+ Person.first.name.should == 'Martin Gamsjaeger'
27
+ end
28
+
29
+ it "should return the attributes written to Membership#person_attributes from the Membership#person_attributes reader" do
30
+ membership = Membership.new(:role => 'maintainer')
31
+ membership.person_attributes.should be_nil
32
+ membership.person_attributes = { :name => 'Martin' }
33
+ membership.person_attributes.should == { :name => 'Martin' }
34
+ end
35
+
36
+ end
37
+
38
+ describe "every accessible many_to_one composite association with a valid reject_if proc", :shared => true do
39
+
40
+ it "should not allow to create a new person via Membership#person_attributes" do
41
+ project = Project.create(:audit_id => 2, :name => 'trippings')
42
+
43
+ Person.all.size.should == 0
44
+ Project.all.size.should == 1
45
+ Membership.all.size.should == 0
46
+
47
+ membership = Membership.new(:project_id => project.id, :project_audit_id => project.audit_id, :role => 'maintainer')
48
+ membership.person_attributes = { :name => 'Martin' }
49
+
50
+ Person.all.size.should == 0
51
+ Project.all.size.should == 1
52
+ Membership.all.size.should == 0
53
+
54
+ begin
55
+ membership.save.should be(false)
56
+ rescue
57
+ # swallow native FK errors which is basically like expecting save to be false
58
+ end
59
+ end
60
+
61
+ end
62
+
63
+ describe "every accessible many_to_one composite association with no reject_if proc", :shared => true do
64
+
65
+ it "should allow to create a new person via Membership#person_attributes" do
66
+ project = Project.create(:audit_id => 2, :name => 'trippings')
67
+
68
+ Person.all.size.should == 0
69
+ Project.all.size.should == 1
70
+ Membership.all.size.should == 0
71
+
72
+ membership = Membership.new(:project_id => project.id, :project_audit_id => project.audit_id, :role => 'maintainer')
73
+ membership.person_attributes = { :audit_id => 1, :name => 'Martin' }
74
+
75
+ Person.all.size.should == 0
76
+ Project.all.size.should == 1
77
+ Membership.all.size.should == 0
78
+
79
+ membership.save.should be_true
80
+
81
+ Person.all.size.should == 1
82
+ Project.all.size.should == 1
83
+ Membership.all.size.should == 1
84
+ Person.first.name.should == 'Martin'
85
+ end
86
+
87
+ end
88
+
89
+ describe "every accessible many_to_one composite association with :allow_destroy => false", :shared => true do
90
+
91
+ it "should not allow to delete an existing person via Membership#person_attributes" do
92
+ person = Person.create(:audit_id => 1, :name => 'Martin')
93
+ project = Project.create(:audit_id => 2, :name => 'trippings')
94
+ membership = Membership.new(:role => 'maintainer')
95
+ membership.person = person
96
+ membership.project = project
97
+ membership.save
98
+
99
+ Person.all.size.should == 1
100
+ Project.all.size.should == 1
101
+ Membership.all.size.should == 1
102
+
103
+ membership.person_attributes = {
104
+ :id => person.id,
105
+ :audit_id => person.audit_id,
106
+ :_delete => true
107
+ }
108
+
109
+ Person.all.size.should == 1
110
+ Project.all.size.should == 1
111
+ Membership.all.size.should == 1
112
+
113
+ membership.save
114
+
115
+ Person.all.size.should == 1
116
+ Project.all.size.should == 1
117
+ Membership.all.size.should == 1
118
+ end
119
+
120
+ end
121
+
122
+ describe "every accessible many_to_one composite association with :allow_destroy => true", :shared => true do
123
+
124
+ it "should allow to delete an existing person via Membership#person_attributes" do
125
+ Person.all.size.should == 0
126
+ Project.all.size.should == 0
127
+ Membership.all.size.should == 0
128
+
129
+ person = Person.create(:audit_id => 1, :name => 'Martin')
130
+ project = Project.create(:audit_id => 2, :name => 'trippings')
131
+
132
+ membership = Membership.new(:role => 'maintainer')
133
+ membership.person = person
134
+ membership.project = project
135
+ membership.save.should be_true
136
+
137
+ Person.all.size.should == 1
138
+ Project.all.size.should == 1
139
+ Membership.all.size.should == 1
140
+
141
+ membership.person_attributes = {
142
+ :id => person.id,
143
+ :audit_id => person.audit_id,
144
+ :_delete => true
145
+ }
146
+
147
+ Person.all.size.should == 1
148
+ Project.all.size.should == 1
149
+ Membership.all.size.should == 1
150
+
151
+ membership.save
152
+
153
+ Person.all.size.should == 0
154
+ Project.all.size.should == 1
155
+
156
+ # TODO also test this behavior in situations where setting the FK to nil is allowed
157
+
158
+ end
159
+
160
+ end
@@ -0,0 +1,130 @@
1
+ describe "every accessible many_to_one association", :shared => true do
2
+
3
+ it "should allow to update an existing person via Profile#person_attributes" do
4
+ Profile.all.size.should == 0
5
+ Person.all.size.should == 0
6
+
7
+ profile = Profile.new :nick => 'snusnu'
8
+ person = Person.create(:name => 'Martin')
9
+ profile.person = person
10
+ profile.save.should be_true
11
+
12
+ Profile.all.size.should == 1
13
+ Person.all.size.should == 1
14
+ Person.first.name.should == 'Martin'
15
+
16
+ profile.person_attributes = { :id => person.id, :name => 'Martin Gamsjaeger' }
17
+ profile.save.should be_true
18
+
19
+ Profile.all.size.should == 1
20
+ Person.all.size.should == 1
21
+ Person.first.name.should == 'Martin Gamsjaeger'
22
+ end
23
+
24
+ it "should return the attributes written to Profile#person_attributes from the Profile#person_attributes reader" do
25
+ profile = Profile.new :nick => 'snusnu'
26
+ profile.person_attributes.should be_nil
27
+ profile.person_attributes = { :name => 'Martin' }
28
+ profile.person_attributes.should == { :name => 'Martin' }
29
+ end
30
+
31
+ end
32
+
33
+ describe "every accessible many_to_one association with a valid reject_if proc", :shared => true do
34
+
35
+ it "should not allow to create a new person via Profile#person_attributes" do
36
+ Profile.all.size.should == 0
37
+ Person.all.size.should == 0
38
+
39
+ profile = Profile.new :nick => 'snusnu'
40
+ profile.person_attributes = { :name => 'Martin' }
41
+
42
+ Profile.all.size.should == 0
43
+ Person.all.size.should == 0
44
+
45
+ begin
46
+ profile.save.should be(false)
47
+ rescue
48
+ # swallow native FK errors which is basically like expecting save to be false
49
+ end
50
+ end
51
+
52
+ end
53
+
54
+ describe "every accessible many_to_one association with no reject_if proc", :shared => true do
55
+
56
+ it "should allow to create a new person via Profile#person_attributes" do
57
+ Profile.all.size.should == 0
58
+ Person.all.size.should == 0
59
+
60
+ profile = Profile.new :nick => 'snusnu'
61
+ profile.person_attributes = { :name => 'Martin' }
62
+
63
+ Profile.all.size.should == 0
64
+ Person.all.size.should == 0
65
+
66
+ profile.save.should be_true
67
+
68
+ Profile.all.size.should == 1
69
+ Person.all.size.should == 1
70
+ Person.first.name.should == 'Martin'
71
+ end
72
+
73
+ end
74
+
75
+ describe "every accessible many_to_one association with :allow_destroy => false", :shared => true do
76
+
77
+ it "should not allow to delete an existing person via Profile#person_attributes" do
78
+ Profile.all.size.should == 0
79
+ Person.all.size.should == 0
80
+
81
+ person = Person.create(:name => 'Martin')
82
+ profile = Profile.new :nick => 'snusnu'
83
+ profile.person = person
84
+ profile.save
85
+
86
+ Profile.all.size.should == 1
87
+ Person.all.size.should == 1
88
+
89
+ profile.person_attributes = { :id => person.id, :_delete => true }
90
+
91
+ Profile.all.size.should == 1
92
+ Person.all.size.should == 1
93
+
94
+ profile.save
95
+
96
+ Profile.all.size.should == 1
97
+ Person.all.size.should == 1
98
+ end
99
+
100
+ end
101
+
102
+ describe "every accessible many_to_one association with :allow_destroy => true", :shared => true do
103
+
104
+ it "should allow to delete an existing person via Profile#person_attributes" do
105
+ Profile.all.size.should == 0
106
+ Person.all.size.should == 0
107
+
108
+ person = Person.create(:name => 'Martin')
109
+
110
+ profile = Profile.new :nick => 'snusnu'
111
+ profile.person = person
112
+ profile.save.should be_true
113
+
114
+ Profile.all.size.should == 1
115
+ Person.all.size.should == 1
116
+
117
+ profile.person_attributes = { :id => person.id, :_delete => true }
118
+
119
+ Profile.all.size.should == 1
120
+ Person.all.size.should == 1
121
+
122
+ profile.save
123
+
124
+ Person.all.size.should == 0
125
+
126
+ # TODO also test this behavior in situations where setting the FK to nil is allowed
127
+
128
+ end
129
+
130
+ end
@@ -0,0 +1,159 @@
1
+ describe "every accessible one_to_many composite association", :shared => true do
2
+
3
+ it "should allow to update an existing membership via Person#memberships_attributes" do
4
+ Person.all.size.should == 0
5
+ Project.all.size.should == 0
6
+ Membership.all.size.should == 0
7
+
8
+ person = Person.create(:audit_id => 1, :name => 'snusnu')
9
+ project = Project.create(:audit_id => 2, :name => 'dm-accepts_nested_attributes')
10
+ membership = Membership.create(:person => person, :project => project, :role => 'maintainer')
11
+
12
+ Person.all.size.should == 1
13
+ Project.all.size.should == 1
14
+ Membership.all.size.should == 1
15
+
16
+ person.memberships_attributes = [{ :project_id => project.id, :project_audit_id => project.audit_id, :role => 'still maintainer' }]
17
+ person.save
18
+
19
+ Person.all.size.should == 1
20
+ Project.all.size.should == 1
21
+ Membership.all.size.should == 1
22
+
23
+ Membership.first.role.should == 'still maintainer'
24
+ end
25
+
26
+ it "should return the attributes written with Person#memberships_attributes= from the Person#memberships_attributes reader" do
27
+ person = Person.create(:audit_id => 1, :name => 'snusnu')
28
+ person.memberships_attributes.should be_nil
29
+ person.memberships_attributes = [{ :role => 'maintainer' }]
30
+ person.memberships_attributes.should == [{ :role => 'maintainer' }]
31
+ end
32
+
33
+ end
34
+
35
+ describe "every accessible one_to_many composite association with a valid reject_if proc", :shared => true do
36
+
37
+ it "should not allow to create a new membership via Project#memberships_attributes" do
38
+ person = Person.create(:audit_id => 1, :name => 'snusnu')
39
+ project = Project.create(:audit_id => 2, :name => 'trippings')
40
+
41
+ Person.all.size.should == 1
42
+ Project.all.size.should == 1
43
+ Membership.all.size.should == 0
44
+
45
+ person.memberships_attributes = [{
46
+ :project_id => project.id,
47
+ :project_audit_id => project.audit_id,
48
+ :role => 'contributor'
49
+ }]
50
+ project.save.should be_true
51
+
52
+ Person.all.size.should == 1
53
+ Project.all.size.should == 1
54
+ Membership.all.size.should == 0
55
+ end
56
+
57
+ end
58
+
59
+ describe "every accessible one_to_many composite association with no reject_if proc", :shared => true do
60
+
61
+ it "should allow to create a new membership via Person#memberships_attributes" do
62
+ person = Person.create(:audit_id => 1, :name => 'snusnu')
63
+ project = Project.create(:audit_id => 2, :name => 'trippings')
64
+
65
+ Person.all.size.should == 1
66
+ Project.all.size.should == 1
67
+ Membership.all.size.should == 0
68
+
69
+ person.memberships_attributes = [{
70
+ :project_id => project.id,
71
+ :project_audit_id => project.audit_id,
72
+ :role => 'contributor'
73
+ }]
74
+
75
+ Person.all.size.should == 1
76
+ Project.all.size.should == 1
77
+ Membership.all.size.should == 0
78
+
79
+ person.save.should be_true
80
+
81
+ Person.all.size.should == 1
82
+ Project.all.size.should == 1
83
+ Membership.all.size.should == 1
84
+
85
+ membership = Membership.first
86
+ membership.person.should == person
87
+ membership.project.should == project
88
+ membership.role.should == 'contributor'
89
+ end
90
+
91
+ end
92
+
93
+ describe "every accessible one_to_many composite association with :allow_destroy => false", :shared => true do
94
+
95
+ it "should not allow to delete an existing membership via Person#memberships_attributes" do
96
+ person = Person.create(:audit_id => 1, :name => 'snusnu')
97
+ project = Project.create(:audit_id => 2, :name => 'trippings')
98
+
99
+ membership = Membership.create(:person => person, :project => project, :role => 'contributor')
100
+
101
+ Person.all.size.should == 1
102
+ Project.all.size.should == 1
103
+ Membership.all.size.should == 1
104
+
105
+ person.memberships_attributes = [{
106
+ :project_id => project.id,
107
+ :project_audit_id => project.audit_id,
108
+ :_delete => true
109
+ }]
110
+ person.save
111
+
112
+ Person.all.size.should == 1
113
+ Project.all.size.should == 1
114
+ Membership.all.size.should == 1
115
+
116
+ Person.first.attributes.should == { :id => person.id, :audit_id => 1, :name => 'snusnu' }
117
+ Project.first.attributes.should == { :id => project.id, :audit_id => 2, :name => 'trippings' }
118
+ Membership.first.attributes.should == {
119
+ :person_id => person.id,
120
+ :person_audit_id => person.audit_id,
121
+ :project_id => project.id,
122
+ :project_audit_id => project.audit_id,
123
+ :role => 'contributor'
124
+ }
125
+ end
126
+
127
+ end
128
+
129
+ describe "every accessible one_to_many composite association with :allow_destroy => true", :shared => true do
130
+
131
+ it "should allow to delete an existing membership via Person#membership_attributes" do
132
+ person = Person.create(:audit_id => 1, :name => 'snusnu')
133
+ project = Project.create(:audit_id => 2, :name => 'trippings')
134
+
135
+ membership = Membership.create(:person => person, :project => project, :role => 'maintainer')
136
+ person.memberships.reload
137
+
138
+ Person.all.size.should == 1
139
+ Project.all.size.should == 1
140
+ Membership.all.size.should == 1
141
+
142
+ person.memberships_attributes = [{
143
+ :project_id => project.id,
144
+ :project_audit_id => project.audit_id,
145
+ :_delete => true
146
+ }]
147
+
148
+ Person.all.size.should == 1
149
+ Project.all.size.should == 1
150
+ Membership.all.size.should == 1
151
+
152
+ person.save
153
+
154
+ Person.all.size.should == 1
155
+ Project.all.size.should == 1
156
+ Membership.all.size.should == 0
157
+ end
158
+
159
+ end