snusnu-dm-accepts_nested_attributes 0.0.6 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. data/Manifest.txt +13 -8
  2. data/Rakefile +2 -3
  3. data/TODO +5 -0
  4. data/lib/dm-accepts_nested_attributes.rb +7 -14
  5. data/lib/dm-accepts_nested_attributes/error_collecting.rb +35 -0
  6. data/lib/dm-accepts_nested_attributes/model.rb +132 -0
  7. data/lib/dm-accepts_nested_attributes/resource.rb +218 -29
  8. data/lib/dm-accepts_nested_attributes/save.rb +13 -0
  9. data/lib/dm-accepts_nested_attributes/transactional_save.rb +15 -0
  10. data/lib/dm-accepts_nested_attributes/version.rb +1 -1
  11. data/spec/fixtures/person.rb +8 -0
  12. data/spec/fixtures/profile.rb +9 -0
  13. data/spec/fixtures/project.rb +8 -0
  14. data/spec/fixtures/project_membership.rb +8 -0
  15. data/spec/fixtures/task.rb +9 -0
  16. data/spec/integration/belongs_to_spec.rb +10 -134
  17. data/spec/integration/has_1_spec.rb +9 -121
  18. data/spec/integration/has_n_spec.rb +10 -149
  19. data/spec/integration/has_n_through_spec.rb +10 -162
  20. data/spec/{shared → lib}/rspec_tmbundle_support.rb +1 -1
  21. data/spec/shared/belongs_to_spec.rb +127 -0
  22. data/spec/shared/has_1_spec.rb +103 -0
  23. data/spec/shared/has_n_spec.rb +114 -0
  24. data/spec/shared/has_n_through_spec.rb +139 -0
  25. data/spec/spec_helper.rb +12 -9
  26. data/spec/unit/accepts_nested_attributes_for_spec.rb +39 -118
  27. data/tasks/changelog.rb +18 -0
  28. data/tasks/spec.rb +0 -1
  29. metadata +19 -14
  30. data/lib/dm-accepts_nested_attributes/association_proxies.rb +0 -55
  31. data/lib/dm-accepts_nested_attributes/association_validation.rb +0 -49
  32. data/lib/dm-accepts_nested_attributes/nested_attributes.rb +0 -350
  33. data/spec/unit/resource_spec.rb +0 -174
@@ -0,0 +1,13 @@
1
+ module DataMapper
2
+ module NestedAttributes
3
+
4
+ module Save
5
+
6
+ def save(*)
7
+ marked_for_destruction? ? destroy : super
8
+ end
9
+
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ module DataMapper
2
+ module NestedAttributes
3
+
4
+ module TransactionalSave
5
+
6
+ def save(*args)
7
+ saved = false
8
+ transaction { |t| t.rollback unless saved = super }
9
+ saved
10
+ end
11
+
12
+ end
13
+
14
+ end
15
+ end
@@ -1,7 +1,7 @@
1
1
  module DataMapper
2
2
  module NestedAttributes
3
3
 
4
- VERSION = "0.0.6"
4
+ VERSION = '0.10.0'.freeze
5
5
 
6
6
  end
7
7
  end
@@ -1,8 +1,16 @@
1
1
  class Person
2
+
2
3
  include DataMapper::Resource
4
+
5
+ # properties
6
+
3
7
  property :id, Serial
4
8
  property :name, String, :nullable => false
9
+
10
+ # associations
11
+
5
12
  has 1, :profile
6
13
  has n, :project_memberships
7
14
  has n, :projects, :through => :project_memberships
15
+
8
16
  end
@@ -1,7 +1,16 @@
1
1
  class Profile
2
+
2
3
  include DataMapper::Resource
4
+
5
+ # properties
6
+
3
7
  property :id, Serial
4
8
  property :person_id, Integer, :nullable => false
9
+
5
10
  property :nick, String, :nullable => false
11
+
12
+ # associations
13
+
6
14
  belongs_to :person
15
+
7
16
  end
@@ -1,8 +1,16 @@
1
1
  class Project
2
+
2
3
  include DataMapper::Resource
4
+
5
+ # properties
6
+
3
7
  property :id, Serial
4
8
  property :name, String, :nullable => false
9
+
10
+ # associations
11
+
5
12
  has n, :tasks
6
13
  has n, :project_memberships
7
14
  has n, :people, :through => :project_memberships
15
+
8
16
  end
@@ -1,8 +1,16 @@
1
1
  class ProjectMembership
2
+
2
3
  include DataMapper::Resource
4
+
5
+ # properties
6
+
3
7
  property :id, Serial
4
8
  property :person_id, Integer, :nullable => false
5
9
  property :project_id, Integer, :nullable => false
10
+
11
+ # associations
12
+
6
13
  belongs_to :person
7
14
  belongs_to :project
15
+
8
16
  end
@@ -1,7 +1,16 @@
1
1
  class Task
2
+
2
3
  include DataMapper::Resource
4
+
5
+ # properties
6
+
3
7
  property :id, Serial
4
8
  property :project_id, Integer, :nullable => false
9
+
5
10
  property :name, String, :nullable => false
11
+
12
+ # associations
13
+
6
14
  belongs_to :project
15
+
7
16
  end
@@ -2,121 +2,6 @@ require 'pathname'
2
2
  require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
3
 
4
4
  describe DataMapper::NestedAttributes do
5
-
6
- describe "every accessible belongs_to association with a valid reject_if proc", :shared => true do
7
-
8
- it "should not allow to create a new person via Profile#person_attributes" do
9
- @profile.person_attributes = { :name => 'Martin' }
10
- @profile.save.should be_false # fails because of validations (not null on belongs_to)
11
- Profile.all.size.should == 0
12
- Person.all.size.should == 0
13
- end
14
-
15
- end
16
-
17
- describe "every accessible belongs_to association with no reject_if proc", :shared => true do
18
-
19
- it "should allow to create a new person via Profile#person_attributes" do
20
- @profile.person_attributes = { :name => 'Martin' }
21
- @profile.person.should_not be_nil
22
- @profile.person.name.should == 'Martin'
23
- @profile.save
24
- @profile.person.should == Person.first
25
- Profile.all.size.should == 1
26
- Person.all.size.should == 1
27
- Person.first.name.should == 'Martin'
28
- end
29
-
30
- it "should allow to update an existing person via Profile#person_attributes" do
31
- person = Person.create(:name => 'Martin')
32
- @profile.person = person
33
- @profile.save
34
-
35
- Person.all.size.should == 1
36
- Person.first.name.should == 'Martin'
37
- Profile.all.size.should == 1
38
-
39
- @profile.person.should == person
40
- @profile.person_attributes = { :id => person.id, :name => 'Martin Gamsjaeger' }
41
- @profile.person.name.should == 'Martin Gamsjaeger'
42
- @profile.save
43
-
44
- Person.all.size.should == 1
45
- Person.first.name.should == 'Martin Gamsjaeger'
46
- Profile.all.size.should == 1
47
- end
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
-
69
- end
70
-
71
- describe "every accessible belongs_to association with :allow_destroy => false", :shared => true do
72
-
73
- it "should not allow to delete an existing person via Profile#person_attributes" do
74
- person = Person.create(:name => 'Martin')
75
- @profile.person = person
76
- @profile.save
77
-
78
- Profile.all.size.should == 1
79
- Person.all.size.should == 1
80
-
81
- @profile.person_attributes = { :id => person.id, :_delete => true }
82
- @profile.save
83
-
84
- Profile.all.size.should == 1
85
- Person.all.size.should == 1
86
- end
87
-
88
- end
89
-
90
- describe "every accessible belongs_to association with :allow_destroy => true", :shared => true do
91
-
92
- it "should allow to delete an existing person via Profile#person_attributes" do
93
- person = Person.create(:name => 'Martin')
94
- @profile.person = person
95
- @profile.save
96
-
97
- Profile.all.size.should == 1
98
- Person.all.size.should == 1
99
-
100
- @profile.person_attributes = { :id => person.id, :_delete => true }
101
- @profile.save
102
-
103
- Profile.all.size.should == 1
104
- Person.all.size.should == 0
105
- end
106
-
107
- end
108
-
109
- describe "every accessible belongs_to association with a nested attributes reader", :shared => true do
110
-
111
- it "should return the attributes written to Profile#person_attributes from the Profile#person_attributes reader" do
112
- @profile.person_attributes.should be_nil
113
-
114
- @profile.person_attributes = { :name => 'Martin' }
115
-
116
- @profile.person_attributes.should == { :name => 'Martin' }
117
- end
118
-
119
- end
120
5
 
121
6
  describe "Profile.belongs_to(:person)" do
122
7
 
@@ -133,10 +18,10 @@ describe DataMapper::NestedAttributes do
133
18
  Profile.accepts_nested_attributes_for :person
134
19
  @profile = Profile.new :nick => 'snusnu'
135
20
  end
136
-
21
+
22
+ it_should_behave_like "every accessible belongs_to association"
137
23
  it_should_behave_like "every accessible belongs_to association with no reject_if proc"
138
24
  it_should_behave_like "every accessible belongs_to association with :allow_destroy => false"
139
- it_should_behave_like "every accessible belongs_to association with a nested attributes reader"
140
25
 
141
26
  end
142
27
 
@@ -147,7 +32,8 @@ describe DataMapper::NestedAttributes do
147
32
  Profile.accepts_nested_attributes_for :person, :allow_destroy => false
148
33
  @profile = Profile.new :nick => 'snusnu'
149
34
  end
150
-
35
+
36
+ it_should_behave_like "every accessible belongs_to association"
151
37
  it_should_behave_like "every accessible belongs_to association with no reject_if proc"
152
38
  it_should_behave_like "every accessible belongs_to association with :allow_destroy => false"
153
39
 
@@ -160,26 +46,14 @@ describe DataMapper::NestedAttributes do
160
46
  Profile.accepts_nested_attributes_for :person, :allow_destroy => true
161
47
  @profile = Profile.new :nick => 'snusnu'
162
48
  end
163
-
49
+
50
+ it_should_behave_like "every accessible belongs_to association"
164
51
  it_should_behave_like "every accessible belongs_to association with no reject_if proc"
165
52
  it_should_behave_like "every accessible belongs_to association with :allow_destroy => true"
166
53
 
167
54
  end
168
55
 
169
56
  describe "accepts_nested_attributes_for :person, " do
170
-
171
- describe ":reject_if => :foo" do
172
-
173
- before(:each) do
174
- clear_data
175
- Profile.accepts_nested_attributes_for :person, :reject_if => :foo
176
- @profile = Profile.new :nick => 'snusnu'
177
- end
178
-
179
- it_should_behave_like "every accessible belongs_to association with no reject_if proc"
180
- it_should_behave_like "every accessible belongs_to association with :allow_destroy => false"
181
-
182
- end
183
57
 
184
58
  describe ":reject_if => lambda { |attrs| true }" do
185
59
 
@@ -188,7 +62,8 @@ describe DataMapper::NestedAttributes do
188
62
  Profile.accepts_nested_attributes_for :person, :reject_if => lambda { |attrs| true }
189
63
  @profile = Profile.new :nick => 'snusnu'
190
64
  end
191
-
65
+
66
+ it_should_behave_like "every accessible belongs_to association"
192
67
  it_should_behave_like "every accessible belongs_to association with a valid reject_if proc"
193
68
  it_should_behave_like "every accessible belongs_to association with :allow_destroy => false"
194
69
 
@@ -201,7 +76,8 @@ describe DataMapper::NestedAttributes do
201
76
  Profile.accepts_nested_attributes_for :person, :reject_if => lambda { |attrs| false }
202
77
  @profile = Profile.new :nick => 'snusnu'
203
78
  end
204
-
79
+
80
+ it_should_behave_like "every accessible belongs_to association"
205
81
  it_should_behave_like "every accessible belongs_to association with no reject_if proc"
206
82
  it_should_behave_like "every accessible belongs_to association with :allow_destroy => false"
207
83
 
@@ -2,110 +2,7 @@ require 'pathname'
2
2
  require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
3
3
 
4
4
  describe DataMapper::NestedAttributes do
5
-
6
- describe "every accessible has(1) association with a valid reject_if proc", :shared => true do
7
-
8
- it "should not allow to create a new profile via Person#profile_attributes" do
9
- @person.profile_attributes = { :nick => 'snusnu' }
10
- @person.profile.should be_nil
11
- @person.save
12
- Person.all.size.should == 1
13
- Profile.all.size.should == 0
14
- end
15
-
16
- end
17
-
18
- describe "every accessible has(1) association with no reject_if proc", :shared => true do
19
-
20
- it "should allow to create a new profile via Person#profile_attributes" do
21
- @person.profile_attributes = { :nick => 'snusnu' }
22
- @person.profile.should_not be_nil
23
- @person.profile.nick.should == 'snusnu'
24
- @person.save.should be_true
25
-
26
- Person.all.size.should == 1
27
- Profile.all.size.should == 1
28
- Profile.first.nick.should == 'snusnu'
29
- end
30
-
31
- it "should allow to update an existing profile via Person#profile_attributes" do
32
- @person.save.should be_true
33
- profile = Profile.create(:person_id => @person.id, :nick => 'snusnu')
34
- @person.reload
35
-
36
- @person.profile.should == profile
37
- @person.profile_attributes = { :id => profile.id, :nick => 'still snusnu somehow' }
38
- @person.profile.nick.should == 'still snusnu somehow'
39
- @person.save.should be_true
40
- @person.profile.should == Profile.first
41
- Person.all.size.should == 1
42
- Profile.all.size.should == 1
43
- Profile.first.nick.should == 'still snusnu somehow'
44
- end
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
-
65
- end
66
-
67
- describe "every accessible has(1) association with :allow_destroy => false", :shared => true do
68
-
69
- it "should not allow to delete an existing profile via Person#profile_attributes" do
70
- @person.save
71
- profile = Profile.create(:person_id => @person.id, :nick => 'snusnu')
72
- @person.reload
73
-
74
- @person.profile_attributes = { :id => profile.id, :_delete => true }
75
- @person.save
76
- Person.all.size.should == 1
77
- Profile.all.size.should == 1
78
- end
79
-
80
- end
81
-
82
- describe "every accessible has(1) association with :allow_destroy => true", :shared => true do
83
-
84
- it "should allow to delete an existing profile via Person#profile_attributes" do
85
- @person.save
86
- profile = Profile.create(:person_id => @person.id, :nick => 'snusnu')
87
- @person.reload
88
5
 
89
- @person.profile_attributes = { :id => profile.id, :_delete => true }
90
- @person.save
91
- Person.all.size.should == 1
92
- Profile.all.size.should == 0
93
- end
94
-
95
- end
96
-
97
- describe "every accessible has(1) association with a nested attributes reader", :shared => true do
98
-
99
- it "should return the attributes written to Person#profile_attributes from the Person#profile_attributes reader" do
100
- @person.profile_attributes.should be_nil
101
-
102
- @person.profile_attributes = { :nick => 'snusnu' }
103
-
104
- @person.profile_attributes.should == { :nick => 'snusnu' }
105
- end
106
-
107
- end
108
-
109
6
  describe "Person.has(1, :profile)" do
110
7
 
111
8
  include XToOneHelpers
@@ -121,10 +18,10 @@ describe DataMapper::NestedAttributes do
121
18
  Person.accepts_nested_attributes_for :profile
122
19
  @person = Person.new :name => 'Martin'
123
20
  end
124
-
21
+
22
+ it_should_behave_like "every accessible has(1) association"
125
23
  it_should_behave_like "every accessible has(1) association with no reject_if proc"
126
24
  it_should_behave_like "every accessible has(1) association with :allow_destroy => false"
127
- it_should_behave_like "every accessible has(1) association with a nested attributes reader"
128
25
 
129
26
  end
130
27
 
@@ -135,7 +32,8 @@ describe DataMapper::NestedAttributes do
135
32
  Person.accepts_nested_attributes_for :profile, :allow_destroy => false
136
33
  @person = Person.new :name => 'Martin'
137
34
  end
138
-
35
+
36
+ it_should_behave_like "every accessible has(1) association"
139
37
  it_should_behave_like "every accessible has(1) association with no reject_if proc"
140
38
  it_should_behave_like "every accessible has(1) association with :allow_destroy => false"
141
39
 
@@ -149,6 +47,7 @@ describe DataMapper::NestedAttributes do
149
47
  @person = Person.new :name => 'Martin'
150
48
  end
151
49
 
50
+ it_should_behave_like "every accessible has(1) association"
152
51
  it_should_behave_like "every accessible has(1) association with no reject_if proc"
153
52
  it_should_behave_like "every accessible has(1) association with :allow_destroy => true"
154
53
 
@@ -156,19 +55,6 @@ describe DataMapper::NestedAttributes do
156
55
 
157
56
 
158
57
  describe "accepts_nested_attributes_for :profile, " do
159
-
160
- describe ":reject_if => :foo" do
161
-
162
- before(:each) do
163
- clear_data
164
- Person.accepts_nested_attributes_for :profile, :reject_if => :foo
165
- @person = Person.new :name => 'Martin'
166
- end
167
-
168
- it_should_behave_like "every accessible has(1) association with no reject_if proc"
169
- it_should_behave_like "every accessible has(1) association with :allow_destroy => false"
170
-
171
- end
172
58
 
173
59
  describe ":reject_if => lambda { |attrs| true }" do
174
60
 
@@ -177,7 +63,8 @@ describe DataMapper::NestedAttributes do
177
63
  Person.accepts_nested_attributes_for :profile, :reject_if => lambda { |attrs| true }
178
64
  @person = Person.new :name => 'Martin'
179
65
  end
180
-
66
+
67
+ it_should_behave_like "every accessible has(1) association"
181
68
  it_should_behave_like "every accessible has(1) association with a valid reject_if proc"
182
69
  it_should_behave_like "every accessible has(1) association with :allow_destroy => false"
183
70
 
@@ -190,7 +77,8 @@ describe DataMapper::NestedAttributes do
190
77
  Person.accepts_nested_attributes_for :profile, :reject_if => lambda { |attrs| false }
191
78
  @person = Person.new :name => 'Martin'
192
79
  end
193
-
80
+
81
+ it_should_behave_like "every accessible has(1) association"
194
82
  it_should_behave_like "every accessible has(1) association with no reject_if proc"
195
83
  it_should_behave_like "every accessible has(1) association with :allow_destroy => false"
196
84