snusnu-dm-accepts_nested_attributes 0.0.2 → 0.0.3

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.
@@ -1,7 +1,7 @@
1
1
  module DataMapper
2
2
  module NestedAttributes
3
3
 
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.3"
5
5
 
6
6
  end
7
7
  end
@@ -1,7 +1,7 @@
1
1
  class Person
2
2
  include DataMapper::Resource
3
3
  property :id, Serial
4
- property :name, String
4
+ property :name, String, :nullable => false
5
5
  has 1, :profile
6
6
  has n, :project_memberships
7
7
  has n, :projects, :through => :project_memberships
@@ -2,6 +2,6 @@ class Profile
2
2
  include DataMapper::Resource
3
3
  property :id, Serial
4
4
  property :person_id, Integer, :nullable => false
5
- property :nick, String
5
+ property :nick, String, :nullable => false
6
6
  belongs_to :person
7
7
  end
@@ -1,7 +1,7 @@
1
1
  class Project
2
2
  include DataMapper::Resource
3
3
  property :id, Serial
4
- property :name, String
4
+ property :name, String, :nullable => false
5
5
  has n, :tasks
6
6
  has n, :project_memberships
7
7
  has n, :people, :through => :project_memberships
@@ -1,7 +1,7 @@
1
1
  class Task
2
2
  include DataMapper::Resource
3
3
  property :id, Serial
4
- property :name, String
5
4
  property :project_id, Integer, :nullable => false
5
+ property :name, String, :nullable => false
6
6
  belongs_to :project
7
7
  end
@@ -2,37 +2,12 @@ require 'pathname'
2
2
  require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
3
 
4
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.get_person.should_not be_nil
11
- @profile.get_person.should be_new_record
12
- end
13
-
14
- end
15
-
16
- describe "every accessible belongs_to association with an associated parent model", :shared => true do
17
-
18
- it "should return an already existing record from get_\#{association_name}" do
19
- @profile.person_attributes = { :name => 'Martin' }
20
- @profile.save
21
- @profile.get_person.should_not be_nil
22
- @profile.get_person.should_not be_new_record
23
- @profile.get_person.should be_kind_of(Person)
24
- end
25
-
26
- end
27
5
 
28
6
  describe "every accessible belongs_to association with a valid reject_if proc", :shared => true do
29
7
 
30
- it_should_behave_like "every accessible belongs_to association with no associated parent model"
31
-
32
8
  it "should not allow to create a new person via Profile#person_attributes" do
33
9
  @profile.person_attributes = { :name => 'Martin' }
34
- @profile.person.should be_nil
35
- @profile.save # fails because of validations (not null on belongs_to)
10
+ @profile.save.should be_false # fails because of validations (not null on belongs_to)
36
11
  Profile.all.size.should == 0
37
12
  Person.all.size.should == 0
38
13
  end
@@ -40,9 +15,6 @@ describe DataMapper::NestedAttributes do
40
15
  end
41
16
 
42
17
  describe "every accessible belongs_to association with no reject_if proc", :shared => true do
43
-
44
- it_should_behave_like "every accessible belongs_to association with no associated parent model"
45
- it_should_behave_like "every accessible belongs_to association with an associated parent model"
46
18
 
47
19
  it "should allow to create a new person via Profile#person_attributes" do
48
20
  @profile.person_attributes = { :name => 'Martin' }
@@ -74,6 +46,26 @@ describe DataMapper::NestedAttributes do
74
46
  Profile.all.size.should == 1
75
47
  end
76
48
 
49
+ it "should perform atomic commits" do
50
+ @profile.person_attributes = { :name => nil } # will fail because of validations
51
+ @profile.person.should_not be_nil
52
+ @profile.person.name.should be_nil
53
+ @profile.save.should be_false
54
+
55
+ Profile.all.size.should == 0
56
+ Person.all.size.should == 0
57
+
58
+ @profile.nick = nil # will fail because of validations
59
+ @profile.should_not be_valid
60
+ @profile.person_attributes = { :name => 'Martin' }
61
+ @profile.person.should_not be_nil
62
+ @profile.person.name.should == 'Martin'
63
+ @profile.save.should be_false
64
+
65
+ Profile.all.size.should == 0
66
+ Person.all.size.should == 0
67
+ end
68
+
77
69
  end
78
70
 
79
71
  describe "every accessible belongs_to association with :allow_destroy => false", :shared => true do
@@ -127,11 +119,17 @@ describe DataMapper::NestedAttributes do
127
119
  end
128
120
 
129
121
  describe "Profile.belongs_to(:person)" do
122
+
123
+ include XToOneHelpers
124
+
125
+ before(:all) do
126
+ DataMapper.auto_migrate!
127
+ end
130
128
 
131
129
  describe "accepts_nested_attributes_for(:person)" do
132
130
 
133
131
  before(:each) do
134
- DataMapper.auto_migrate!
132
+ clear_data
135
133
  Profile.accepts_nested_attributes_for :person
136
134
  @profile = Profile.new :nick => 'snusnu'
137
135
  end
@@ -145,7 +143,7 @@ describe DataMapper::NestedAttributes do
145
143
  describe "accepts_nested_attributes_for(:person, :allow_destroy => false)" do
146
144
 
147
145
  before(:each) do
148
- DataMapper.auto_migrate!
146
+ clear_data
149
147
  Profile.accepts_nested_attributes_for :person, :allow_destroy => false
150
148
  @profile = Profile.new :nick => 'snusnu'
151
149
  end
@@ -158,7 +156,7 @@ describe DataMapper::NestedAttributes do
158
156
  describe "accepts_nested_attributes_for(:person, :allow_destroy = true)" do
159
157
 
160
158
  before(:each) do
161
- DataMapper.auto_migrate!
159
+ clear_data
162
160
  Profile.accepts_nested_attributes_for :person, :allow_destroy => true
163
161
  @profile = Profile.new :nick => 'snusnu'
164
162
  end
@@ -173,7 +171,7 @@ describe DataMapper::NestedAttributes do
173
171
  describe ":reject_if => :foo" do
174
172
 
175
173
  before(:each) do
176
- DataMapper.auto_migrate!
174
+ clear_data
177
175
  Profile.accepts_nested_attributes_for :person, :reject_if => :foo
178
176
  @profile = Profile.new :nick => 'snusnu'
179
177
  end
@@ -186,7 +184,7 @@ describe DataMapper::NestedAttributes do
186
184
  describe ":reject_if => lambda { |attrs| true }" do
187
185
 
188
186
  before(:each) do
189
- DataMapper.auto_migrate!
187
+ clear_data
190
188
  Profile.accepts_nested_attributes_for :person, :reject_if => lambda { |attrs| true }
191
189
  @profile = Profile.new :nick => 'snusnu'
192
190
  end
@@ -199,7 +197,7 @@ describe DataMapper::NestedAttributes do
199
197
  describe ":reject_if => lambda { |attrs| false }" do
200
198
 
201
199
  before(:each) do
202
- DataMapper.auto_migrate!
200
+ clear_data
203
201
  Profile.accepts_nested_attributes_for :person, :reject_if => lambda { |attrs| false }
204
202
  @profile = Profile.new :nick => 'snusnu'
205
203
  end
@@ -3,31 +3,7 @@ require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
3
 
4
4
  describe DataMapper::NestedAttributes do
5
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
6
  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
7
 
32
8
  it "should not allow to create a new profile via Person#profile_attributes" do
33
9
  @person.profile_attributes = { :nick => 'snusnu' }
@@ -41,35 +17,51 @@ describe DataMapper::NestedAttributes do
41
17
 
42
18
  describe "every accessible has(1) association with no reject_if proc", :shared => true do
43
19
 
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
20
  it "should allow to create a new profile via Person#profile_attributes" do
48
21
  @person.profile_attributes = { :nick => 'snusnu' }
49
22
  @person.profile.should_not be_nil
50
23
  @person.profile.nick.should == 'snusnu'
51
- @person.save
52
- @person.profile.should == Profile.first
24
+ @person.save.should be_true
25
+
53
26
  Person.all.size.should == 1
54
27
  Profile.all.size.should == 1
55
28
  Profile.first.nick.should == 'snusnu'
56
29
  end
57
30
 
58
31
  it "should allow to update an existing profile via Person#profile_attributes" do
59
- @person.save
32
+ @person.save.should be_true
60
33
  profile = Profile.create(:person_id => @person.id, :nick => 'snusnu')
61
34
  @person.reload
62
35
 
63
36
  @person.profile.should == profile
64
37
  @person.profile_attributes = { :id => profile.id, :nick => 'still snusnu somehow' }
65
38
  @person.profile.nick.should == 'still snusnu somehow'
66
- @person.save
39
+ @person.save.should be_true
67
40
  @person.profile.should == Profile.first
68
41
  Person.all.size.should == 1
69
42
  Profile.all.size.should == 1
70
43
  Profile.first.nick.should == 'still snusnu somehow'
71
44
  end
72
45
 
46
+ it "should perform atomic commits" do
47
+ @person.profile_attributes = { :nick => nil } # will fail because of validations
48
+ @person.profile.should_not be_nil
49
+ @person.profile.nick.should be_nil
50
+ @person.save.should be_false
51
+
52
+ Person.all.size.should == 0
53
+ Profile.all.size.should == 0
54
+
55
+ @person.profile_attributes = { :nick => 'snusnu' }
56
+ @person.profile.should_not be_nil
57
+ @person.profile.nick.should == 'snusnu'
58
+ @person.name = nil # will fail because of validations
59
+ @person.save.should be_false
60
+
61
+ Person.all.size.should == 0
62
+ Profile.all.size.should == 0
63
+ end
64
+
73
65
  end
74
66
 
75
67
  describe "every accessible has(1) association with :allow_destroy => false", :shared => true do
@@ -116,14 +108,20 @@ describe DataMapper::NestedAttributes do
116
108
 
117
109
  describe "Person.has(1, :profile)" do
118
110
 
111
+ include XToOneHelpers
112
+
113
+ before(:all) do
114
+ DataMapper.auto_migrate!
115
+ end
116
+
119
117
  describe "accepts_nested_attributes_for(:profile)" do
120
118
 
121
119
  before(:each) do
122
- DataMapper.auto_migrate!
120
+ clear_data
123
121
  Person.accepts_nested_attributes_for :profile
124
122
  @person = Person.new :name => 'Martin'
125
123
  end
126
-
124
+
127
125
  it_should_behave_like "every accessible has(1) association with no reject_if proc"
128
126
  it_should_behave_like "every accessible has(1) association with :allow_destroy => false"
129
127
  it_should_behave_like "every accessible has(1) association with a nested attributes reader"
@@ -133,7 +131,7 @@ describe DataMapper::NestedAttributes do
133
131
  describe "accepts_nested_attributes_for(:profile, :allow_destroy => false)" do
134
132
 
135
133
  before(:each) do
136
- DataMapper.auto_migrate!
134
+ clear_data
137
135
  Person.accepts_nested_attributes_for :profile, :allow_destroy => false
138
136
  @person = Person.new :name => 'Martin'
139
137
  end
@@ -146,7 +144,7 @@ describe DataMapper::NestedAttributes do
146
144
  describe "accepts_nested_attributes_for(:profile, :allow_destroy => true)" do
147
145
 
148
146
  before(:each) do
149
- DataMapper.auto_migrate!
147
+ clear_data
150
148
  Person.accepts_nested_attributes_for :profile, :allow_destroy => true
151
149
  @person = Person.new :name => 'Martin'
152
150
  end
@@ -162,7 +160,7 @@ describe DataMapper::NestedAttributes do
162
160
  describe ":reject_if => :foo" do
163
161
 
164
162
  before(:each) do
165
- DataMapper.auto_migrate!
163
+ clear_data
166
164
  Person.accepts_nested_attributes_for :profile, :reject_if => :foo
167
165
  @person = Person.new :name => 'Martin'
168
166
  end
@@ -175,7 +173,7 @@ describe DataMapper::NestedAttributes do
175
173
  describe ":reject_if => lambda { |attrs| true }" do
176
174
 
177
175
  before(:each) do
178
- DataMapper.auto_migrate!
176
+ clear_data
179
177
  Person.accepts_nested_attributes_for :profile, :reject_if => lambda { |attrs| true }
180
178
  @person = Person.new :name => 'Martin'
181
179
  end
@@ -184,11 +182,11 @@ describe DataMapper::NestedAttributes do
184
182
  it_should_behave_like "every accessible has(1) association with :allow_destroy => false"
185
183
 
186
184
  end
187
-
185
+
188
186
  describe ":reject_if => lambda { |attrs| false }" do
189
-
187
+
190
188
  before(:each) do
191
- DataMapper.auto_migrate!
189
+ clear_data
192
190
  Person.accepts_nested_attributes_for :profile, :reject_if => lambda { |attrs| false }
193
191
  @person = Person.new :name => 'Martin'
194
192
  end
@@ -52,6 +52,35 @@ describe DataMapper::NestedAttributes do
52
52
  Task.first.name.should == 'write more specs'
53
53
  end
54
54
 
55
+ it "should perform atomic commits" do
56
+ Project.all.size.should == 0
57
+ Task.all.size.should == 0
58
+
59
+ @project.name = nil # will fail because of validations
60
+ @project.tasks_attributes = { 'new_1' => { :name => 'write specs' } }
61
+ @project.tasks.should_not be_empty
62
+ @project.tasks.first.name.should == 'write specs'
63
+ @project.save
64
+ @project.should be_new_record
65
+ @project.tasks.all? { |t| t.should be_new_record }
66
+ Project.all.size.should == 0
67
+ Task.all.size.should == 0
68
+
69
+ # TODO write specs for xxx_attributes= method that test
70
+ # if same hash keys properly get overwritten and not end up being multiple records
71
+ # (which obviously is the case right now)
72
+
73
+ @project.name = 'dm-accepts_nested_attributes'
74
+ @project.tasks_attributes = { 'new_1' => { :name => nil } } # will fail because of validations
75
+ @project.tasks.should_not be_empty
76
+ @project.tasks.first.name.should be_nil
77
+ @project.save
78
+ @project.should be_new_record
79
+ @project.tasks.all? { |t| t.should be_new_record }
80
+ Project.all.size.should == 0
81
+ Task.all.size.should == 0
82
+ end
83
+
55
84
  end
56
85
 
57
86
  describe "every accessible has(n) association with :allow_destroy => false", :shared => true do
@@ -105,11 +134,17 @@ describe DataMapper::NestedAttributes do
105
134
  end
106
135
 
107
136
  describe "Project.has(n, :tasks)" do
137
+
138
+ include OneToManyHelpers
139
+
140
+ before(:all) do
141
+ DataMapper.auto_migrate!
142
+ end
108
143
 
109
144
  describe "accepts_nested_attributes_for(:tasks)" do
110
145
 
111
146
  before(:each) do
112
- DataMapper.auto_migrate!
147
+ clear_data
113
148
  Project.accepts_nested_attributes_for :tasks
114
149
  @project = Project.new :name => 'trippings'
115
150
  end
@@ -123,7 +158,7 @@ describe DataMapper::NestedAttributes do
123
158
  describe "accepts_nested_attributes_for(:tasks, :allow_destroy => false)" do
124
159
 
125
160
  before(:each) do
126
- DataMapper.auto_migrate!
161
+ clear_data
127
162
  Project.accepts_nested_attributes_for :tasks, :allow_destroy => false
128
163
  @project = Project.new :name => 'trippings'
129
164
  end
@@ -136,7 +171,7 @@ describe DataMapper::NestedAttributes do
136
171
  describe "accepts_nested_attributes_for(:tasks, :allow_destroy => true)" do
137
172
 
138
173
  before(:each) do
139
- DataMapper.auto_migrate!
174
+ clear_data
140
175
  Project.accepts_nested_attributes_for :tasks, :allow_destroy => true
141
176
  @project = Project.new :name => 'trippings'
142
177
  end
@@ -151,7 +186,7 @@ describe DataMapper::NestedAttributes do
151
186
  describe ":reject_if => :foo" do
152
187
 
153
188
  before(:each) do
154
- DataMapper.auto_migrate!
189
+ clear_data
155
190
  Project.accepts_nested_attributes_for :tasks, :reject_if => :foo
156
191
  @project = Project.new :name => 'trippings'
157
192
  end
@@ -164,7 +199,7 @@ describe DataMapper::NestedAttributes do
164
199
  describe ":reject_if => lambda { |attrs| true }" do
165
200
 
166
201
  before(:each) do
167
- DataMapper.auto_migrate!
202
+ clear_data
168
203
  Project.accepts_nested_attributes_for :tasks, :reject_if => lambda { |attrs| true }
169
204
  @project = Project.new :name => 'trippings'
170
205
  end
@@ -177,7 +212,7 @@ describe DataMapper::NestedAttributes do
177
212
  describe ":reject_if => lambda { |attrs| false }" do
178
213
 
179
214
  before(:each) do
180
- DataMapper.auto_migrate!
215
+ clear_data
181
216
  Project.accepts_nested_attributes_for :tasks, :reject_if => lambda { |attrs| false }
182
217
  @project = Project.new :name => 'trippings'
183
218
  end
@@ -65,6 +65,29 @@ describe DataMapper::NestedAttributes do
65
65
  Project.first.name.should == 'still dm-accepts_nested_attributes'
66
66
  end
67
67
 
68
+ it "should perform atomic commits" do
69
+
70
+ @person.projects_attributes = { 'new_1' => { :name => nil } } # should fail because of validations
71
+ @person.projects.should be_empty
72
+ @person.save
73
+ @person.projects.should be_empty
74
+
75
+ Person.all.size.should == 1
76
+ ProjectMembership.all.size.should == 0
77
+ Project.all.size.should == 0
78
+
79
+ @person.name = nil # should fail because of validations
80
+ @person.projects_attributes = { 'new_1' => { :name => nil } }
81
+ @person.projects.should be_empty
82
+ @person.save
83
+ @person.projects.should be_empty
84
+
85
+ Person.all.size.should == 0
86
+ ProjectMembership.all.size.should == 0
87
+ Project.all.size.should == 0
88
+
89
+ end
90
+
68
91
  end
69
92
 
70
93
  describe "every accessible has(n, :through) association with :allow_destroy => false", :shared => true do
@@ -124,11 +147,17 @@ describe DataMapper::NestedAttributes do
124
147
  end
125
148
 
126
149
  describe "Person.has(n, :projects, :through => :project_memberships)" do
150
+
151
+ include ManyToManyHelpers
152
+
153
+ before(:all) do
154
+ DataMapper.auto_migrate!
155
+ end
127
156
 
128
157
  describe "accepts_nested_attributes_for(:projects)" do
129
158
 
130
159
  before(:each) do
131
- DataMapper.auto_migrate!
160
+ clear_data
132
161
  Person.accepts_nested_attributes_for :projects
133
162
  @person = Person.new :name => 'snusnu'
134
163
  end
@@ -142,7 +171,7 @@ describe DataMapper::NestedAttributes do
142
171
  describe "accepts_nested_attributes_for(:projects, :allow_destroy => false)" do
143
172
 
144
173
  before(:each) do
145
- DataMapper.auto_migrate!
174
+ clear_data
146
175
  Person.accepts_nested_attributes_for :projects, :allow_destroy => false
147
176
  @person = Person.new :name => 'snusnu'
148
177
  end
@@ -155,7 +184,7 @@ describe DataMapper::NestedAttributes do
155
184
  describe "accepts_nested_attributes_for(:projects, :allow_destroy = true)" do
156
185
 
157
186
  before(:each) do
158
- DataMapper.auto_migrate!
187
+ clear_data
159
188
  Person.accepts_nested_attributes_for :projects, :allow_destroy => true
160
189
  @person = Person.new :name => 'snusnu'
161
190
  end
@@ -170,7 +199,7 @@ describe DataMapper::NestedAttributes do
170
199
  describe ":reject_if => :foo" do
171
200
 
172
201
  before(:each) do
173
- DataMapper.auto_migrate!
202
+ clear_data
174
203
  Person.accepts_nested_attributes_for :projects, :reject_if => :foo
175
204
  @person = Person.new :name => 'snusnu'
176
205
  end
@@ -183,7 +212,7 @@ describe DataMapper::NestedAttributes do
183
212
  describe ":reject_if => lambda { |attrs| true }" do
184
213
 
185
214
  before(:each) do
186
- DataMapper.auto_migrate!
215
+ clear_data
187
216
  Person.accepts_nested_attributes_for :projects, :reject_if => lambda { |attrs| true }
188
217
  @person = Person.new :name => 'snusnu'
189
218
  end
@@ -196,7 +225,7 @@ describe DataMapper::NestedAttributes do
196
225
  describe ":reject_if => lambda { |attrs| false }" do
197
226
 
198
227
  before(:each) do
199
- DataMapper.auto_migrate!
228
+ clear_data
200
229
  Person.accepts_nested_attributes_for :projects, :reject_if => lambda { |attrs| false }
201
230
  @person = Person.new :name => 'snusnu'
202
231
  end