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,101 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Profile.belongs_to(:person);" do
4
+
5
+ before(:all) do
6
+
7
+ class ::Person
8
+
9
+ include DataMapper::Resource
10
+ extend ConstraintSupport
11
+
12
+ property :id, Serial
13
+ property :name, String, :required => true
14
+
15
+ has 1, :profile, constraint(:destroy)
16
+
17
+ end
18
+
19
+ class ::Profile
20
+
21
+ include DataMapper::Resource
22
+
23
+ property :id, Serial
24
+ property :person_id, Integer, :required => true, :min => 0
25
+ property :nick, String, :required => true
26
+
27
+ belongs_to :person
28
+
29
+ end
30
+
31
+ DataMapper.auto_migrate!
32
+
33
+ end
34
+
35
+ before(:each) do
36
+ Profile.all.destroy!
37
+ Person.all.destroy!
38
+ end
39
+
40
+
41
+ describe "Profile.accepts_nested_attributes_for(:person)" do
42
+
43
+ before(:all) do
44
+ Profile.accepts_nested_attributes_for :person
45
+ end
46
+
47
+ it_should_behave_like "every accessible many_to_one association"
48
+ it_should_behave_like "every accessible many_to_one association with no reject_if proc"
49
+ it_should_behave_like "every accessible many_to_one association with :allow_destroy => false"
50
+
51
+ end
52
+
53
+ describe "Profile.accepts_nested_attributes_for(:person, :allow_destroy => false)" do
54
+
55
+ before(:all) do
56
+ Profile.accepts_nested_attributes_for :person, :allow_destroy => false
57
+ end
58
+
59
+ it_should_behave_like "every accessible many_to_one association"
60
+ it_should_behave_like "every accessible many_to_one association with no reject_if proc"
61
+ it_should_behave_like "every accessible many_to_one association with :allow_destroy => false"
62
+
63
+ end
64
+
65
+ describe "Profile.accepts_nested_attributes_for(:person, :allow_destroy = true)" do
66
+
67
+ before(:all) do
68
+ Profile.accepts_nested_attributes_for :person, :allow_destroy => true
69
+ end
70
+
71
+ it_should_behave_like "every accessible many_to_one association"
72
+ it_should_behave_like "every accessible many_to_one association with no reject_if proc"
73
+ it_should_behave_like "every accessible many_to_one association with :allow_destroy => true"
74
+
75
+ end
76
+
77
+ describe "Profile.accepts_nested_attributes_for(:person, :reject_if => lambda { |attrs| true })" do
78
+
79
+ before(:all) do
80
+ Profile.accepts_nested_attributes_for :person, :reject_if => lambda { |attrs| true }
81
+ end
82
+
83
+ it_should_behave_like "every accessible many_to_one association"
84
+ it_should_behave_like "every accessible many_to_one association with a valid reject_if proc"
85
+ it_should_behave_like "every accessible many_to_one association with :allow_destroy => false"
86
+
87
+ end
88
+
89
+ describe "Profile.accepts_nested_attributes_for(:person, :reject_if => lambda { |attrs| false })" do
90
+
91
+ before(:all) do
92
+ Profile.accepts_nested_attributes_for :person, :reject_if => lambda { |attrs| false }
93
+ end
94
+
95
+ it_should_behave_like "every accessible many_to_one association"
96
+ it_should_behave_like "every accessible many_to_one association with no reject_if proc"
97
+ it_should_behave_like "every accessible many_to_one association with :allow_destroy => false"
98
+
99
+ end
100
+
101
+ end
@@ -0,0 +1,120 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Person.has(n, :memberships) with CPK;" do
4
+
5
+ before(:all) do
6
+ class ::Person
7
+
8
+ include DataMapper::Resource
9
+ extend ConstraintSupport
10
+
11
+ property :id, Serial, :key => true
12
+ property :audit_id, Integer, :key => true, :min => 0
13
+ property :name, String, :required => true
14
+
15
+ has n, :memberships, constraint(:destroy)
16
+ has n, :projects, :through => :memberships
17
+
18
+ end
19
+
20
+ class ::Project
21
+
22
+ include DataMapper::Resource
23
+ extend ConstraintSupport
24
+
25
+ property :id, Serial, :key => true
26
+ property :audit_id, Integer, :key => true, :min => 0
27
+ property :name, String, :required => true
28
+
29
+ has n, :memberships, constraint(:destroy)
30
+ has n, :people, :through => :memberships
31
+
32
+ end
33
+
34
+ class ::Membership
35
+
36
+ include DataMapper::Resource
37
+
38
+ property :person_id, Integer, :key => true, :min => 0
39
+ property :person_audit_id, Integer, :key => true, :min => 0
40
+ property :project_id, Integer, :key => true, :min => 0
41
+ property :project_audit_id, Integer, :key => true, :min => 0
42
+ property :role, String, :required => false
43
+
44
+ belongs_to :person, :key => true
45
+ belongs_to :project, :key => true
46
+
47
+ end
48
+
49
+ DataMapper.auto_migrate!
50
+
51
+ end
52
+
53
+ before(:each) do
54
+ Membership.all.destroy!
55
+ Project.all.destroy!
56
+ Person.all.destroy!
57
+ end
58
+
59
+
60
+ describe "Person.accepts_nested_attributes_for(:memberships)" do
61
+
62
+ before(:all) do
63
+ Person.accepts_nested_attributes_for :memberships
64
+ end
65
+
66
+ it_should_behave_like "every accessible one_to_many composite association"
67
+ it_should_behave_like "every accessible one_to_many composite association with no reject_if proc"
68
+ it_should_behave_like "every accessible one_to_many composite association with :allow_destroy => false"
69
+
70
+ end
71
+
72
+ describe "Person.accepts_nested_attributes_for(:memberships, :allow_destroy => false)" do
73
+
74
+ before(:all) do
75
+ Person.accepts_nested_attributes_for :memberships, :allow_destroy => false
76
+ end
77
+
78
+ it_should_behave_like "every accessible one_to_many composite association"
79
+ it_should_behave_like "every accessible one_to_many composite association with no reject_if proc"
80
+ it_should_behave_like "every accessible one_to_many composite association with :allow_destroy => false"
81
+
82
+ end
83
+
84
+ describe "Person.accepts_nested_attributes_for(:memberships, :allow_destroy => true)" do
85
+
86
+ before(:all) do
87
+ Person.accepts_nested_attributes_for :memberships, :allow_destroy => true
88
+ end
89
+
90
+ it_should_behave_like "every accessible one_to_many composite association"
91
+ it_should_behave_like "every accessible one_to_many composite association with no reject_if proc"
92
+ it_should_behave_like "every accessible one_to_many composite association with :allow_destroy => true"
93
+
94
+ end
95
+
96
+ describe "Person.accepts_nested_attributes_for(:memberships, :reject_if => lambda { |attrs| true })" do
97
+
98
+ before(:all) do
99
+ Person.accepts_nested_attributes_for :memberships, :reject_if => lambda { |attrs| true }
100
+ end
101
+
102
+ it_should_behave_like "every accessible one_to_many composite association"
103
+ it_should_behave_like "every accessible one_to_many composite association with a valid reject_if proc"
104
+ it_should_behave_like "every accessible one_to_many composite association with :allow_destroy => false"
105
+
106
+ end
107
+
108
+ describe "Person.accepts_nested_attributes_for(:memberships, :reject_if => lambda { |attrs| false })" do
109
+
110
+ before(:all) do
111
+ Person.accepts_nested_attributes_for :memberships, :reject_if => lambda { |attrs| false }
112
+ end
113
+
114
+ it_should_behave_like "every accessible one_to_many composite association"
115
+ it_should_behave_like "every accessible one_to_many composite association with no reject_if proc"
116
+ it_should_behave_like "every accessible one_to_many composite association with :allow_destroy => false"
117
+
118
+ end
119
+
120
+ end
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Project.has(n, :tasks);" do
4
+
5
+ before(:all) do
6
+
7
+ class ::Project
8
+
9
+ include DataMapper::Resource
10
+ extend ConstraintSupport
11
+
12
+ property :id, Serial
13
+ property :name, String, :required => true
14
+
15
+ has n, :tasks, constraint(:destroy)
16
+
17
+ end
18
+
19
+ class ::Task
20
+
21
+ include DataMapper::Resource
22
+
23
+ property :id, Serial
24
+ property :name, String, :required => true
25
+
26
+ belongs_to :project
27
+
28
+ end
29
+
30
+ DataMapper.auto_migrate!
31
+
32
+ end
33
+
34
+ before(:each) do
35
+ Task.all.destroy!
36
+ Project.all.destroy!
37
+ end
38
+
39
+
40
+ describe "Project.accepts_nested_attributes_for(:tasks)" do
41
+
42
+ before(:all) do
43
+ Project.accepts_nested_attributes_for :tasks
44
+ end
45
+
46
+ it_should_behave_like "every accessible one_to_many association"
47
+ it_should_behave_like "every accessible one_to_many association with no reject_if proc"
48
+ it_should_behave_like "every accessible one_to_many association with :allow_destroy => false"
49
+
50
+ end
51
+
52
+ describe "Project.accepts_nested_attributes_for(:tasks, :allow_destroy => false)" do
53
+
54
+ before(:all) do
55
+ Project.accepts_nested_attributes_for :tasks, :allow_destroy => false
56
+ end
57
+
58
+ it_should_behave_like "every accessible one_to_many association"
59
+ it_should_behave_like "every accessible one_to_many association with no reject_if proc"
60
+ it_should_behave_like "every accessible one_to_many association with :allow_destroy => false"
61
+
62
+ end
63
+
64
+ describe "Project.accepts_nested_attributes_for(:tasks, :allow_destroy => true)" do
65
+
66
+ before(:all) do
67
+ Project.accepts_nested_attributes_for :tasks, :allow_destroy => true
68
+ end
69
+
70
+ it_should_behave_like "every accessible one_to_many association"
71
+ it_should_behave_like "every accessible one_to_many association with no reject_if proc"
72
+ it_should_behave_like "every accessible one_to_many association with :allow_destroy => true"
73
+
74
+ end
75
+
76
+ describe "Project.accepts_nested_attributes_for(:tasks, :reject_if => lambda { |attrs| true })" do
77
+
78
+ before(:all) do
79
+ Project.accepts_nested_attributes_for :tasks, :reject_if => lambda { |attrs| true }
80
+ end
81
+
82
+ it_should_behave_like "every accessible one_to_many association"
83
+ it_should_behave_like "every accessible one_to_many association with a valid reject_if proc"
84
+ it_should_behave_like "every accessible one_to_many association with :allow_destroy => false"
85
+
86
+ end
87
+
88
+ describe "Project.accepts_nested_attributes_for(:tasks, :reject_if => lambda { |attrs| false })" do
89
+
90
+ before(:all) do
91
+ Project.accepts_nested_attributes_for :tasks, :reject_if => lambda { |attrs| false }
92
+ end
93
+
94
+ it_should_behave_like "every accessible one_to_many association"
95
+ it_should_behave_like "every accessible one_to_many association with no reject_if proc"
96
+ it_should_behave_like "every accessible one_to_many association with :allow_destroy => false"
97
+
98
+ end
99
+
100
+ end
@@ -0,0 +1,150 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Person.has(1, :profile) with CPK;" do
4
+
5
+ before(:all) do
6
+
7
+ class ::Person
8
+
9
+ include DataMapper::Resource
10
+ extend ConstraintSupport
11
+
12
+ property :id, Serial, :key => true
13
+ property :audit_id, Integer, :key => true, :min => 0
14
+ property :name, String, :required => true
15
+
16
+ has 1, :profile, constraint(:destroy)
17
+ has 1, :address, :through => :profile
18
+
19
+ end
20
+
21
+ class ::Profile
22
+
23
+ include DataMapper::Resource
24
+
25
+ property :person_id, Integer, :key => true, :min => 0
26
+ property :person_audit_id, Integer, :key => true, :min => 0
27
+ property :nick, String, :required => true
28
+
29
+ belongs_to :person
30
+ has 1, :address
31
+
32
+ end
33
+
34
+ class ::Address
35
+
36
+ include DataMapper::Resource
37
+
38
+ property :id, Serial
39
+ property :audit_id, Integer, :key => true, :min => 0
40
+ property :profile_id, Integer, :required => true, :unique => :profile, :unique_index => true, :min => 0
41
+ property :profile_audit_id, Integer, :required => true, :unique => :profile, :unique_index => true, :min => 0
42
+ property :body, String, :required => true
43
+
44
+ belongs_to :profile
45
+
46
+ end
47
+
48
+ DataMapper.auto_migrate!
49
+
50
+ end
51
+
52
+ before(:each) do
53
+ Address.all.destroy!
54
+ Profile.all.destroy!
55
+ Person.all.destroy!
56
+ end
57
+
58
+
59
+ describe "Person.accepts_nested_attributes_for(:profile)" do
60
+
61
+ before(:all) do
62
+ Person.accepts_nested_attributes_for :profile
63
+ end
64
+
65
+ it_should_behave_like "every accessible one_to_one composite association"
66
+ it_should_behave_like "every accessible one_to_one composite association with no reject_if proc"
67
+ it_should_behave_like "every accessible one_to_one composite association with :allow_destroy => false"
68
+
69
+ end
70
+
71
+ describe "Person.accepts_nested_attributes_for(:profile, :allow_destroy => false)" do
72
+
73
+ before(:all) do
74
+ Person.accepts_nested_attributes_for :profile, :allow_destroy => false
75
+ end
76
+
77
+ it_should_behave_like "every accessible one_to_one composite association"
78
+ it_should_behave_like "every accessible one_to_one composite association with no reject_if proc"
79
+ it_should_behave_like "every accessible one_to_one composite association with :allow_destroy => false"
80
+
81
+ end
82
+
83
+ describe "Person.accepts_nested_attributes_for(:profile, :allow_destroy => true)" do
84
+
85
+ before(:all) do
86
+ Person.accepts_nested_attributes_for :profile, :allow_destroy => true
87
+ end
88
+
89
+ it_should_behave_like "every accessible one_to_one composite association"
90
+ it_should_behave_like "every accessible one_to_one composite association with no reject_if proc"
91
+ it_should_behave_like "every accessible one_to_one composite association with :allow_destroy => true"
92
+
93
+ end
94
+
95
+ describe "Person.accepts_nested_attributes_for(:profile, :reject_if => lambda { |attrs| true })" do
96
+
97
+ before(:all) do
98
+ Person.accepts_nested_attributes_for :profile, :reject_if => lambda { |attrs| true }
99
+ end
100
+
101
+ it_should_behave_like "every accessible one_to_one composite association"
102
+ it_should_behave_like "every accessible one_to_one composite association with a valid reject_if proc"
103
+ it_should_behave_like "every accessible one_to_one composite association with :allow_destroy => false"
104
+
105
+ end
106
+
107
+ describe "Person.accepts_nested_attributes_for(:profile, :reject_if => lambda { |attrs| false })" do
108
+
109
+ before(:all) do
110
+ Person.accepts_nested_attributes_for :profile, :reject_if => lambda { |attrs| false }
111
+ end
112
+
113
+ it_should_behave_like "every accessible one_to_one composite association"
114
+ it_should_behave_like "every accessible one_to_one composite association with no reject_if proc"
115
+ it_should_behave_like "every accessible one_to_one composite association with :allow_destroy => false"
116
+
117
+ end
118
+
119
+ describe "Profile.accepts_nested_attributes_for(:person)" do
120
+
121
+ before(:all) do
122
+ Profile.accepts_nested_attributes_for :person
123
+ end
124
+
125
+ it "should allow to update an existing profile via Person#profile_attributes" do
126
+ person = Person.create(:audit_id => 1, :name => 'Martin')
127
+ profile = Profile.create(:person => person, :nick => 'snusnu')
128
+ person.reload
129
+
130
+ Person.all.size.should == 1
131
+ Profile.all.size.should == 1
132
+
133
+ profile.person_attributes = { :id => person.id, :audit_id => 1, :name => 'Martin Gamsjaeger' }
134
+ profile.save.should be_true
135
+
136
+ Person.all.size.should == 1
137
+ Profile.all.size.should == 1
138
+ Person.first.name.should == 'Martin Gamsjaeger'
139
+ end
140
+
141
+ it "should return the attributes written to Person#profile_attributes from the Person#profile_attributes reader" do
142
+ profile = Profile.new :nick => 'snusnu'
143
+ profile.person_attributes.should be_nil
144
+ profile.person_attributes = { :name => 'Martin' }
145
+ profile.person_attributes.should == { :name => 'Martin' }
146
+ end
147
+
148
+ end
149
+
150
+ end