snusnu-dm-accepts_nested_attributes 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ module DataMapper
2
+ module NestedAttributes
3
+
4
+ VERSION = "0.0.1"
5
+
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ class Person
2
+ include DataMapper::Resource
3
+ property :id, Serial
4
+ property :name, String
5
+ has 1, :profile
6
+ has n, :project_memberships
7
+ has n, :projects, :through => :project_memberships
8
+ end
@@ -0,0 +1,7 @@
1
+ class Profile
2
+ include DataMapper::Resource
3
+ property :id, Serial
4
+ property :person_id, Integer
5
+ property :nick, String
6
+ belongs_to :person
7
+ end
@@ -0,0 +1,8 @@
1
+ class Project
2
+ include DataMapper::Resource
3
+ property :id, Serial
4
+ property :name, String
5
+ has n, :tasks
6
+ has n, :project_memberships
7
+ has n, :people, :through => :project_memberships
8
+ end
@@ -0,0 +1,8 @@
1
+ class ProjectMembership
2
+ include DataMapper::Resource
3
+ property :id, Serial
4
+ property :person_id, Integer
5
+ property :project_id, Integer
6
+ belongs_to :person
7
+ belongs_to :project
8
+ end
@@ -0,0 +1,7 @@
1
+ class Task
2
+ include DataMapper::Resource
3
+ property :id, Serial
4
+ property :name, String
5
+ property :project_id, Integer
6
+ belongs_to :project
7
+ end
@@ -0,0 +1,204 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
+
4
+ describe DataMapper::NestedAttributes do
5
+
6
+
7
+ describe "every accessible belongs_to association with no associated parent model", :shared => true do
8
+
9
+ it "should return a new_record from get_\#{association_name}" do
10
+ @profile.person_attributes = { :name => 'Martin' }
11
+ @profile.get_person.should_not be_nil
12
+ @profile.get_person.should be_new_record
13
+ end
14
+
15
+ end
16
+
17
+ describe "every accessible belongs_to association with an associated parent model", :shared => true do
18
+
19
+ it "should return an already existing record from get_\#{association_name}" do
20
+ @profile.person_attributes = { :name => 'Martin' }
21
+ @profile.save
22
+ @profile.get_person.should_not be_nil
23
+ @profile.get_person.should_not be_new_record
24
+ @profile.get_person.should be_kind_of(Person)
25
+ end
26
+
27
+ end
28
+
29
+ describe "every accessible belongs_to association with a valid reject_if proc", :shared => true do
30
+
31
+ it_should_behave_like "every accessible belongs_to association with no associated parent model"
32
+
33
+ it "should not allow to create a new person via Profile#person_attributes" do
34
+ @profile.person_attributes = { :name => 'Martin' }
35
+ @profile.person.should be_nil
36
+ @profile.save
37
+ Profile.all.size.should == 1
38
+ Person.all.size.should == 0
39
+ end
40
+
41
+ end
42
+
43
+ describe "every accessible belongs_to association with no reject_if proc", :shared => true do
44
+
45
+ it_should_behave_like "every accessible belongs_to association with no associated parent model"
46
+ it_should_behave_like "every accessible belongs_to association with an associated parent model"
47
+
48
+ it "should allow to create a new person via Profile#person_attributes" do
49
+ @profile.person_attributes = { :name => 'Martin' }
50
+ @profile.person.should_not be_nil
51
+ @profile.person.name.should == 'Martin'
52
+ @profile.save
53
+ @profile.person.should == Person.first
54
+ Profile.all.size.should == 1
55
+ Person.all.size.should == 1
56
+ Person.first.name.should == 'Martin'
57
+ end
58
+
59
+ it "should allow to update an existing person via Profile#person_attributes" do
60
+ person = Person.create(:name => 'Martin')
61
+ @profile.person = person
62
+ @profile.save
63
+
64
+ Person.all.size.should == 1
65
+ Person.first.name.should == 'Martin'
66
+ Profile.all.size.should == 1
67
+
68
+ @profile.person.should == person
69
+ @profile.person_attributes = { :id => person.id, :name => 'Martin Gamsjaeger' }
70
+ @profile.person.name.should == 'Martin Gamsjaeger'
71
+ @profile.save
72
+
73
+ Person.all.size.should == 1
74
+ Person.first.name.should == 'Martin Gamsjaeger'
75
+ Profile.all.size.should == 1
76
+ end
77
+
78
+ end
79
+
80
+ describe "every accessible belongs_to association with :allow_destroy => false", :shared => true do
81
+
82
+ it "should not allow to delete an existing person via Profile#person_attributes" do
83
+ person = Person.create(:name => 'Martin')
84
+ @profile.person = person
85
+ @profile.save
86
+
87
+ Profile.all.size.should == 1
88
+ Person.all.size.should == 1
89
+
90
+ @profile.person_attributes = { :id => person.id, :_delete => true }
91
+ @profile.save
92
+
93
+ Profile.all.size.should == 1
94
+ Person.all.size.should == 1
95
+ end
96
+
97
+ end
98
+
99
+ describe "every accessible belongs_to association with :allow_destroy => true", :shared => true do
100
+
101
+ it "should allow to delete an existing person via Profile#person_attributes" do
102
+ person = Person.create(:name => 'Martin')
103
+ @profile.person = person
104
+ @profile.save
105
+
106
+ Profile.all.size.should == 1
107
+ Person.all.size.should == 1
108
+
109
+ @profile.person_attributes = { :id => person.id, :_delete => true }
110
+ @profile.save
111
+
112
+ Profile.all.size.should == 1
113
+ Person.all.size.should == 0
114
+ end
115
+
116
+ end
117
+
118
+ describe "Profile.belongs_to(:person)" do
119
+
120
+ describe "accepts_nested_attributes_for(:person)" do
121
+
122
+ before(:each) do
123
+ DataMapper.auto_migrate!
124
+ Profile.accepts_nested_attributes_for :person
125
+ @profile = Profile.new :nick => 'snusnu'
126
+ end
127
+
128
+ it_should_behave_like "every accessible belongs_to association with no reject_if proc"
129
+ it_should_behave_like "every accessible belongs_to association with :allow_destroy => false"
130
+
131
+ end
132
+
133
+ describe "accepts_nested_attributes_for(:person, :allow_destroy => false)" do
134
+
135
+ before(:each) do
136
+ DataMapper.auto_migrate!
137
+ Profile.accepts_nested_attributes_for :person, :allow_destroy => false
138
+ @profile = Profile.new :nick => 'snusnu'
139
+ end
140
+
141
+ it_should_behave_like "every accessible belongs_to association with no reject_if proc"
142
+ it_should_behave_like "every accessible belongs_to association with :allow_destroy => false"
143
+
144
+ end
145
+
146
+ describe "accepts_nested_attributes_for(:person, :allow_destroy = true)" do
147
+
148
+ before(:each) do
149
+ DataMapper.auto_migrate!
150
+ Profile.accepts_nested_attributes_for :person, :allow_destroy => true
151
+ @profile = Profile.new :nick => 'snusnu'
152
+ end
153
+
154
+ it_should_behave_like "every accessible belongs_to association with no reject_if proc"
155
+ it_should_behave_like "every accessible belongs_to association with :allow_destroy => true"
156
+
157
+ end
158
+
159
+ describe "accepts_nested_attributes_for :person, " do
160
+
161
+ describe ":reject_if => :foo" do
162
+
163
+ before(:each) do
164
+ DataMapper.auto_migrate!
165
+ Profile.accepts_nested_attributes_for :person, :reject_if => :foo
166
+ @profile = Profile.new :nick => 'snusnu'
167
+ end
168
+
169
+ it_should_behave_like "every accessible belongs_to association with no reject_if proc"
170
+ it_should_behave_like "every accessible belongs_to association with :allow_destroy => false"
171
+
172
+ end
173
+
174
+ describe ":reject_if => lambda { |attrs| true }" do
175
+
176
+ before(:each) do
177
+ DataMapper.auto_migrate!
178
+ Profile.accepts_nested_attributes_for :person, :reject_if => lambda { |attrs| true }
179
+ @profile = Profile.new :nick => 'snusnu'
180
+ end
181
+
182
+ it_should_behave_like "every accessible belongs_to association with a valid reject_if proc"
183
+ it_should_behave_like "every accessible belongs_to association with :allow_destroy => false"
184
+
185
+ end
186
+
187
+ describe ":reject_if => lambda { |attrs| false }" do
188
+
189
+ before(:each) do
190
+ DataMapper.auto_migrate!
191
+ Profile.accepts_nested_attributes_for :person, :reject_if => lambda { |attrs| false }
192
+ @profile = Profile.new :nick => 'snusnu'
193
+ end
194
+
195
+ it_should_behave_like "every accessible belongs_to association with no reject_if proc"
196
+ it_should_behave_like "every accessible belongs_to association with :allow_destroy => false"
197
+
198
+ end
199
+
200
+ end
201
+
202
+ end
203
+
204
+ end
@@ -0,0 +1,193 @@
1
+ require 'pathname'
2
+ require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
+
4
+ describe DataMapper::NestedAttributes do
5
+
6
+ describe "every accessible has(1) association with no associated parent model", :shared => true do
7
+
8
+ it "should return a new_record from get_\#{association_name}" do
9
+ @person.profile_attributes = { :nick => 'snusnu' }
10
+ @person.get_profile.should_not be_nil
11
+ @person.get_profile.should be_new_record
12
+ end
13
+
14
+ end
15
+
16
+ describe "every accessible has(1) association with an associated parent model", :shared => true do
17
+
18
+ it "should return an already existing record from get_\#{association_name}" do
19
+ @person.profile_attributes = { :nick => 'snusnu' }
20
+ @person.save
21
+ @person.get_profile.should_not be_nil
22
+ @person.get_profile.should_not be_new_record
23
+ @person.get_profile.should be_kind_of(Profile)
24
+ end
25
+
26
+ end
27
+
28
+ describe "every accessible has(1) association with a valid reject_if proc", :shared => true do
29
+
30
+ it_should_behave_like "every accessible has(1) association with no associated parent model"
31
+
32
+ it "should not allow to create a new profile via Person#profile_attributes" do
33
+ @person.profile_attributes = { :nick => 'snusnu' }
34
+ @person.profile.should be_nil
35
+ @person.save
36
+ Person.all.size.should == 1
37
+ Profile.all.size.should == 0
38
+ end
39
+
40
+ end
41
+
42
+ describe "every accessible has(1) association with no reject_if proc", :shared => true do
43
+
44
+ it_should_behave_like "every accessible has(1) association with no associated parent model"
45
+ it_should_behave_like "every accessible has(1) association with an associated parent model"
46
+
47
+ it "should allow to create a new profile via Person#profile_attributes" do
48
+ @person.profile_attributes = { :nick => 'snusnu' }
49
+ @person.profile.should_not be_nil
50
+ @person.profile.nick.should == 'snusnu'
51
+ @person.save
52
+ @person.profile.should == Profile.first
53
+ Person.all.size.should == 1
54
+ Profile.all.size.should == 1
55
+ Profile.first.nick.should == 'snusnu'
56
+ end
57
+
58
+ it "should allow to update an existing profile via Person#profile_attributes" do
59
+ @person.save
60
+ profile = Profile.create(:person_id => @person.id, :nick => 'snusnu')
61
+ @person.reload
62
+
63
+ @person.profile.should == profile
64
+ @person.profile_attributes = { :id => profile.id, :nick => 'still snusnu somehow' }
65
+ @person.profile.nick.should == 'still snusnu somehow'
66
+ @person.save
67
+ @person.profile.should == Profile.first
68
+ Person.all.size.should == 1
69
+ Profile.all.size.should == 1
70
+ Profile.first.nick.should == 'still snusnu somehow'
71
+ end
72
+
73
+ end
74
+
75
+ describe "every accessible has(1) association with :allow_destroy => false", :shared => true do
76
+
77
+ it "should not allow to delete an existing profile via Person#profile_attributes" do
78
+ @person.save
79
+ profile = Profile.create(:person_id => @person.id, :nick => 'snusnu')
80
+ @person.reload
81
+
82
+ @person.profile_attributes = { :id => profile.id, :_delete => true }
83
+ @person.save
84
+ Person.all.size.should == 1
85
+ Profile.all.size.should == 1
86
+ end
87
+
88
+ end
89
+
90
+ describe "every accessible has(1) association with :allow_destroy => true", :shared => true do
91
+
92
+ it "should allow to delete an existing profile via Person#profile_attributes" do
93
+ @person.save
94
+ profile = Profile.create(:person_id => @person.id, :nick => 'snusnu')
95
+ @person.reload
96
+
97
+ @person.profile_attributes = { :id => profile.id, :_delete => true }
98
+ @person.save
99
+ Person.all.size.should == 1
100
+ Profile.all.size.should == 0
101
+ end
102
+
103
+ end
104
+
105
+ describe "Person.has(1, :profile)" do
106
+
107
+ describe "accepts_nested_attributes_for(:profile)" do
108
+
109
+ before(:each) do
110
+ DataMapper.auto_migrate!
111
+ Person.accepts_nested_attributes_for :profile
112
+ @person = Person.new :name => 'Martin'
113
+ end
114
+
115
+ it_should_behave_like "every accessible has(1) association with no reject_if proc"
116
+ it_should_behave_like "every accessible has(1) association with :allow_destroy => false"
117
+
118
+ end
119
+
120
+ describe "accepts_nested_attributes_for(:profile, :allow_destroy => false)" do
121
+
122
+ before(:each) do
123
+ DataMapper.auto_migrate!
124
+ Person.accepts_nested_attributes_for :profile, :allow_destroy => false
125
+ @person = Person.new :name => 'Martin'
126
+ end
127
+
128
+ it_should_behave_like "every accessible has(1) association with no reject_if proc"
129
+ it_should_behave_like "every accessible has(1) association with :allow_destroy => false"
130
+
131
+ end
132
+
133
+ describe "accepts_nested_attributes_for(:profile, :allow_destroy => true)" do
134
+
135
+ before(:each) do
136
+ DataMapper.auto_migrate!
137
+ Person.accepts_nested_attributes_for :profile, :allow_destroy => true
138
+ @person = Person.new :name => 'Martin'
139
+ end
140
+
141
+ it_should_behave_like "every accessible has(1) association with no reject_if proc"
142
+ it_should_behave_like "every accessible has(1) association with :allow_destroy => true"
143
+
144
+ end
145
+
146
+
147
+ describe "accepts_nested_attributes_for :profile, " do
148
+
149
+ describe ":reject_if => :foo" do
150
+
151
+ before(:each) do
152
+ DataMapper.auto_migrate!
153
+ Person.accepts_nested_attributes_for :profile, :reject_if => :foo
154
+ @person = Person.new :name => 'Martin'
155
+ end
156
+
157
+ it_should_behave_like "every accessible has(1) association with no reject_if proc"
158
+ it_should_behave_like "every accessible has(1) association with :allow_destroy => false"
159
+
160
+ end
161
+
162
+ describe ":reject_if => lambda { |attrs| true }" do
163
+
164
+ before(:each) do
165
+ DataMapper.auto_migrate!
166
+ Person.accepts_nested_attributes_for :profile, :reject_if => lambda { |attrs| true }
167
+ @person = Person.new :name => 'Martin'
168
+ end
169
+
170
+ it_should_behave_like "every accessible has(1) association with a valid reject_if proc"
171
+ it_should_behave_like "every accessible has(1) association with :allow_destroy => false"
172
+
173
+ end
174
+
175
+ describe ":reject_if => lambda { |attrs| false }" do
176
+
177
+ before(:each) do
178
+ DataMapper.auto_migrate!
179
+ Person.accepts_nested_attributes_for :profile, :reject_if => lambda { |attrs| false }
180
+ @person = Person.new :name => 'Martin'
181
+ end
182
+
183
+ it_should_behave_like "every accessible has(1) association with no reject_if proc"
184
+ it_should_behave_like "every accessible has(1) association with :allow_destroy => false"
185
+
186
+ end
187
+
188
+ end
189
+
190
+
191
+ end
192
+
193
+ end