snusnu-dm-accepts_nested_attributes 0.0.6 → 0.10.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.
- data/Manifest.txt +13 -8
- data/Rakefile +2 -3
- data/TODO +5 -0
- data/lib/dm-accepts_nested_attributes.rb +7 -14
- data/lib/dm-accepts_nested_attributes/error_collecting.rb +35 -0
- data/lib/dm-accepts_nested_attributes/model.rb +132 -0
- data/lib/dm-accepts_nested_attributes/resource.rb +218 -29
- data/lib/dm-accepts_nested_attributes/save.rb +13 -0
- data/lib/dm-accepts_nested_attributes/transactional_save.rb +15 -0
- data/lib/dm-accepts_nested_attributes/version.rb +1 -1
- data/spec/fixtures/person.rb +8 -0
- data/spec/fixtures/profile.rb +9 -0
- data/spec/fixtures/project.rb +8 -0
- data/spec/fixtures/project_membership.rb +8 -0
- data/spec/fixtures/task.rb +9 -0
- data/spec/integration/belongs_to_spec.rb +10 -134
- data/spec/integration/has_1_spec.rb +9 -121
- data/spec/integration/has_n_spec.rb +10 -149
- data/spec/integration/has_n_through_spec.rb +10 -162
- data/spec/{shared → lib}/rspec_tmbundle_support.rb +1 -1
- data/spec/shared/belongs_to_spec.rb +127 -0
- data/spec/shared/has_1_spec.rb +103 -0
- data/spec/shared/has_n_spec.rb +114 -0
- data/spec/shared/has_n_through_spec.rb +139 -0
- data/spec/spec_helper.rb +12 -9
- data/spec/unit/accepts_nested_attributes_for_spec.rb +39 -118
- data/tasks/changelog.rb +18 -0
- data/tasks/spec.rb +0 -1
- metadata +19 -14
- data/lib/dm-accepts_nested_attributes/association_proxies.rb +0 -55
- data/lib/dm-accepts_nested_attributes/association_validation.rb +0 -49
- data/lib/dm-accepts_nested_attributes/nested_attributes.rb +0 -350
- data/spec/unit/resource_spec.rb +0 -174
@@ -0,0 +1,103 @@
|
|
1
|
+
describe "every accessible has(1) association", :shared => true do
|
2
|
+
|
3
|
+
it "should allow to update an existing profile via Person#profile_attributes" do
|
4
|
+
@person.save.should be_true
|
5
|
+
profile = Profile.create(:person_id => @person.id, :nick => 'snusnu')
|
6
|
+
@person.reload
|
7
|
+
|
8
|
+
@person.profile_attributes = { :id => profile.id, :nick => 'still snusnu somehow' }
|
9
|
+
@person.save.should be_true
|
10
|
+
|
11
|
+
Person.all.size.should == 1
|
12
|
+
Profile.all.size.should == 1
|
13
|
+
Profile.first.nick.should == 'still snusnu somehow'
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should perform atomic commits" do
|
17
|
+
|
18
|
+
# related resource is invalid
|
19
|
+
@person.profile_attributes = { :nick => nil } # will fail because of validations
|
20
|
+
@person.save.should be_false
|
21
|
+
|
22
|
+
Person.all.size.should == 0
|
23
|
+
Profile.all.size.should == 0
|
24
|
+
|
25
|
+
# self is invalid
|
26
|
+
@person.name = nil # will fail because of validations
|
27
|
+
@person.profile_attributes = { :nick => 'snusnu' }
|
28
|
+
@person.save.should be_false
|
29
|
+
|
30
|
+
Person.all.size.should == 0
|
31
|
+
Profile.all.size.should == 0
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should return the attributes written to Person#profile_attributes from the Person#profile_attributes reader" do
|
35
|
+
@person.profile_attributes.should be_nil
|
36
|
+
@person.profile_attributes = { :nick => 'snusnu' }
|
37
|
+
@person.profile_attributes.should == { :nick => 'snusnu' }
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "every accessible has(1) association with a valid reject_if proc", :shared => true do
|
43
|
+
|
44
|
+
it "should not allow to create a new profile via Person#profile_attributes" do
|
45
|
+
Person.all.size.should == 0
|
46
|
+
Profile.all.size.should == 0
|
47
|
+
|
48
|
+
@person.profile_attributes = { :nick => 'snusnu' }
|
49
|
+
@person.save
|
50
|
+
|
51
|
+
Person.all.size.should == 1
|
52
|
+
Profile.all.size.should == 0
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "every accessible has(1) association with no reject_if proc", :shared => true do
|
58
|
+
|
59
|
+
it "should allow to create a new profile via Person#profile_attributes" do
|
60
|
+
Person.all.size.should == 0
|
61
|
+
Profile.all.size.should == 0
|
62
|
+
|
63
|
+
@person.profile_attributes = { :nick => 'snusnu' }
|
64
|
+
@person.save.should be_true
|
65
|
+
|
66
|
+
Person.all.size.should == 1
|
67
|
+
Profile.all.size.should == 1
|
68
|
+
Profile.first.nick.should == 'snusnu'
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
describe "every accessible has(1) association with :allow_destroy => false", :shared => true do
|
74
|
+
|
75
|
+
it "should not allow to delete an existing profile via Person#profile_attributes" do
|
76
|
+
@person.save
|
77
|
+
profile = Profile.create(:person_id => @person.id, :nick => 'snusnu')
|
78
|
+
@person.reload
|
79
|
+
|
80
|
+
@person.profile_attributes = { :id => profile.id, :_delete => true }
|
81
|
+
@person.save
|
82
|
+
Person.all.size.should == 1
|
83
|
+
Profile.all.size.should == 1
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
describe "every accessible has(1) association with :allow_destroy => true", :shared => true do
|
89
|
+
|
90
|
+
it "should allow to delete an existing profile via Person#profile_attributes" do
|
91
|
+
@person.save
|
92
|
+
profile = Profile.create(:person_id => @person.id, :nick => 'snusnu')
|
93
|
+
|
94
|
+
@person.profile = profile
|
95
|
+
@person.profile_attributes = { :id => profile.id, :_delete => true }
|
96
|
+
|
97
|
+
@person.save
|
98
|
+
|
99
|
+
Person.all.size.should == 1
|
100
|
+
Profile.all.size.should == 0
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
describe "every accessible has(n) association", :shared => true do
|
2
|
+
|
3
|
+
it "should allow to update an existing task via Project#tasks_attributes" do
|
4
|
+
@project.save
|
5
|
+
task = Task.create(:project => @project, :name => 'write specs')
|
6
|
+
Project.all.size.should == 1
|
7
|
+
Task.all.size.should == 1
|
8
|
+
|
9
|
+
@project.tasks_attributes = { task.id.to_s => { :id => task.id, :name => 'write more specs' } }
|
10
|
+
@project.save.should be_true
|
11
|
+
|
12
|
+
Project.all.size.should == 1
|
13
|
+
Task.all.size.should == 1
|
14
|
+
Task.first.name.should == 'write more specs'
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should perform atomic commits" do
|
18
|
+
Project.all.size.should == 0
|
19
|
+
Task.all.size.should == 0
|
20
|
+
|
21
|
+
# self is invalid
|
22
|
+
@project.name = nil # will fail because of validations
|
23
|
+
@project.tasks_attributes = { 'new_1' => { :name => 'write specs' } }
|
24
|
+
@project.save
|
25
|
+
|
26
|
+
Project.all.size.should == 0
|
27
|
+
Task.all.size.should == 0
|
28
|
+
|
29
|
+
# related resource is invalid
|
30
|
+
@project.name = 'dm-accepts_nested_attributes'
|
31
|
+
@project.tasks_attributes = { 'new_1' => { :name => nil } } # will fail because of validations
|
32
|
+
@project.save
|
33
|
+
|
34
|
+
Project.all.size.should == 0
|
35
|
+
Task.all.size.should == 0
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should return the attributes written to Project#task_attributes from the Project#task_attributes reader" do
|
39
|
+
@project.tasks_attributes.should be_nil
|
40
|
+
@project.tasks_attributes = { 'new_1' => { :name => 'write specs' } }
|
41
|
+
@project.tasks_attributes.should == { 'new_1' => { :name => 'write specs' } }
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "every accessible has(n) association with a valid reject_if proc", :shared => true do
|
47
|
+
|
48
|
+
it "should not allow to create a new task via Project#tasks_attributes" do
|
49
|
+
@project.save
|
50
|
+
Project.all.size.should == 1
|
51
|
+
Task.all.size.should == 0
|
52
|
+
|
53
|
+
@project.tasks_attributes = { 'new_1' => { :name => 'write specs' } }
|
54
|
+
@project.save.should be_true
|
55
|
+
|
56
|
+
Project.all.size.should == 1
|
57
|
+
Task.all.size.should == 0
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "every accessible has(n) association with no reject_if proc", :shared => true do
|
63
|
+
|
64
|
+
it "should allow to create a new task via Project#tasks_attributes" do
|
65
|
+
@project.save
|
66
|
+
Project.all.size.should == 1
|
67
|
+
Task.all.size.should == 0
|
68
|
+
|
69
|
+
@project.tasks_attributes = { 'new_1' => { :name => 'write specs' } }
|
70
|
+
@project.save.should be_true
|
71
|
+
|
72
|
+
Project.all.size.should == 1
|
73
|
+
Task.all.size.should == 1
|
74
|
+
Task.first.name.should == 'write specs'
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "every accessible has(n) association with :allow_destroy => false", :shared => true do
|
80
|
+
|
81
|
+
it "should not allow to delete an existing task via Profile#tasks_attributes" do
|
82
|
+
@project.save
|
83
|
+
task = Task.create(:project => @project, :name => 'write specs')
|
84
|
+
|
85
|
+
Project.all.size.should == 1
|
86
|
+
Task.all.size.should == 1
|
87
|
+
|
88
|
+
@project.tasks_attributes = { '1' => { :id => task.id, :_delete => true } }
|
89
|
+
@project.save
|
90
|
+
|
91
|
+
Project.all.size.should == 1
|
92
|
+
Task.all.size.should == 1
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "every accessible has(n) association with :allow_destroy => true", :shared => true do
|
98
|
+
|
99
|
+
it "should allow to delete an existing task via Profile#tasks_attributes" do
|
100
|
+
@project.save
|
101
|
+
task = Task.create(:project => @project, :name => 'write specs')
|
102
|
+
|
103
|
+
Project.all.size.should == 1
|
104
|
+
Task.all.size.should == 1
|
105
|
+
|
106
|
+
@project.tasks << task
|
107
|
+
@project.tasks_attributes = { '1' => { :id => task.id, :_delete => true } }
|
108
|
+
@project.save
|
109
|
+
|
110
|
+
Project.all.size.should == 1
|
111
|
+
Task.all.size.should == 0
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
describe "every accessible has(n, :through) association", :shared => true do
|
2
|
+
|
3
|
+
it "should allow to update an existing project via Person#projects_attributes" do
|
4
|
+
Person.all.size.should == 0
|
5
|
+
Project.all.size.should == 0
|
6
|
+
ProjectMembership.all.size.should == 0
|
7
|
+
|
8
|
+
@person.save
|
9
|
+
project = Project.create(:name => 'dm-accepts_nested_attributes')
|
10
|
+
project_membership = ProjectMembership.create(:person => @person, :project => project)
|
11
|
+
|
12
|
+
Person.all.size.should == 1
|
13
|
+
Project.all.size.should == 1
|
14
|
+
ProjectMembership.all.size.should == 1
|
15
|
+
|
16
|
+
@person.projects_attributes = { project.id.to_s => { :id => project.id, :name => 'still dm-accepts_nested_attributes' } }
|
17
|
+
@person.save
|
18
|
+
|
19
|
+
Person.all.size.should == 1
|
20
|
+
ProjectMembership.all.size.should == 1
|
21
|
+
Project.all.size.should == 1
|
22
|
+
|
23
|
+
Project.first.name.should == 'still dm-accepts_nested_attributes'
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should perform atomic commits" do
|
27
|
+
|
28
|
+
@person.projects_attributes = { 'new_1' => { :name => nil } } # should fail because of validations
|
29
|
+
@person.save
|
30
|
+
|
31
|
+
Person.all.size.should == 0 # TODO think more if this should be '1'
|
32
|
+
ProjectMembership.all.size.should == 0
|
33
|
+
Project.all.size.should == 0
|
34
|
+
|
35
|
+
@person.name = nil # should fail because of validations
|
36
|
+
@person.projects_attributes = { 'new_1' => { :name => nil } }
|
37
|
+
@person.save
|
38
|
+
|
39
|
+
Person.all.size.should == 0
|
40
|
+
ProjectMembership.all.size.should == 0
|
41
|
+
Project.all.size.should == 0
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should return the attributes written to Person#projects_attributes from the Person#projects_attributes reader" do
|
46
|
+
@person.projects_attributes.should be_nil
|
47
|
+
@person.projects_attributes = { 'new_1' => { :name => 'write specs' } }
|
48
|
+
@person.projects_attributes.should == { 'new_1' => { :name => 'write specs' } }
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "every accessible has(n, :through) association with a valid reject_if proc", :shared => true do
|
54
|
+
|
55
|
+
it "should not allow to create a new project via Person#projects_attributes" do
|
56
|
+
Person.all.size.should == 0
|
57
|
+
Project.all.size.should == 0
|
58
|
+
ProjectMembership.all.size.should == 0
|
59
|
+
|
60
|
+
@person.save
|
61
|
+
Person.all.size.should == 1
|
62
|
+
ProjectMembership.all.size.should == 0
|
63
|
+
Project.all.size.should == 0
|
64
|
+
|
65
|
+
@person.projects_attributes = { 'new_1' => { :name => 'dm-accepts_nested_attributes' } }
|
66
|
+
@person.save
|
67
|
+
|
68
|
+
Person.all.size.should == 1
|
69
|
+
ProjectMembership.all.size.should == 0
|
70
|
+
Project.all.size.should == 0
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
describe "every accessible has(n, :through) association with no reject_if proc", :shared => true do
|
76
|
+
|
77
|
+
it "should allow to create a new project via Person#projects_attributes" do
|
78
|
+
Person.all.size.should == 0
|
79
|
+
Project.all.size.should == 0
|
80
|
+
ProjectMembership.all.size.should == 0
|
81
|
+
|
82
|
+
@person.save
|
83
|
+
Person.all.size.should == 1
|
84
|
+
ProjectMembership.all.size.should == 0
|
85
|
+
Project.all.size.should == 0
|
86
|
+
|
87
|
+
@person.projects_attributes = { 'new_1' => { :name => 'dm-accepts_nested_attributes' } }
|
88
|
+
@person.save
|
89
|
+
|
90
|
+
Person.all.size.should == 1
|
91
|
+
ProjectMembership.all.size.should == 1
|
92
|
+
Project.all.size.should == 1
|
93
|
+
|
94
|
+
Project.first.name.should == 'dm-accepts_nested_attributes'
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
describe "every accessible has(n, :through) association with :allow_destroy => false", :shared => true do
|
100
|
+
|
101
|
+
it "should not allow to delete an existing project via Person#projects_attributes" do
|
102
|
+
@person.save
|
103
|
+
project = Project.create(:name => 'dm-accepts_nested_attributes')
|
104
|
+
project_membership = ProjectMembership.create(:person => @person, :project => project)
|
105
|
+
|
106
|
+
Person.all.size.should == 1
|
107
|
+
ProjectMembership.all.size.should == 1
|
108
|
+
Project.all.size.should == 1
|
109
|
+
|
110
|
+
@person.projects_attributes = { '1' => { :id => project.id, :_delete => true } }
|
111
|
+
@person.save
|
112
|
+
|
113
|
+
Person.all.size.should == 1
|
114
|
+
ProjectMembership.all.size.should == 1
|
115
|
+
Project.all.size.should == 1
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
describe "every accessible has(n, :through) association with :allow_destroy => true", :shared => true do
|
121
|
+
|
122
|
+
it "should allow to delete an existing project via Person#projects_attributes" do
|
123
|
+
@person.save
|
124
|
+
project = Project.create(:name => 'dm-accepts_nested_attributes')
|
125
|
+
project_membership = ProjectMembership.create(:person => @person, :project => project)
|
126
|
+
|
127
|
+
Person.all.size.should == 1
|
128
|
+
ProjectMembership.all.size.should == 1
|
129
|
+
Project.all.size.should == 1
|
130
|
+
|
131
|
+
@person.projects_attributes = { '1' => { :id => project.id, :_delete => true } }
|
132
|
+
@person.save
|
133
|
+
|
134
|
+
Person.all.size.should == 1
|
135
|
+
ProjectMembership.all.size.should == 0
|
136
|
+
Project.all.size.should == 0
|
137
|
+
end
|
138
|
+
|
139
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
require 'pathname'
|
2
|
-
require 'rubygems'
|
3
|
-
|
4
|
-
gem 'rspec', '>=1.1.12'
|
5
2
|
require 'spec'
|
6
3
|
|
7
|
-
|
8
|
-
require 'dm-association_validator'
|
9
|
-
|
4
|
+
# require the plugin
|
10
5
|
require Pathname(__FILE__).dirname.parent.expand_path + 'lib/dm-accepts_nested_attributes'
|
11
|
-
|
6
|
+
|
7
|
+
# allow testing with dm-validations enabled
|
8
|
+
# must be required after the plugin, since
|
9
|
+
# dm-validations seems to need dm-core
|
10
|
+
require 'dm-validations'
|
11
|
+
|
12
12
|
ENV["SQLITE3_SPEC_URI"] ||= 'sqlite3::memory:'
|
13
13
|
ENV["MYSQL_SPEC_URI"] ||= 'mysql://localhost/dm-accepts_nested_attributes_test'
|
14
14
|
ENV["POSTGRES_SPEC_URI"] ||= 'postgres://postgres@localhost/dm-accepts_nested_attributes_test'
|
@@ -36,7 +36,7 @@ USE_TEXTMATE_RSPEC_BUNDLE = true # set to false if not using textmate
|
|
36
36
|
|
37
37
|
if USE_TEXTMATE_RSPEC_BUNDLE
|
38
38
|
|
39
|
-
require Pathname(__FILE__).dirname.expand_path + '
|
39
|
+
require Pathname(__FILE__).dirname.expand_path + 'lib/rspec_tmbundle_support'
|
40
40
|
|
41
41
|
# use the tmbundle logger
|
42
42
|
RSpecTmBundleHelpers::TextmateRspecLogger.new(STDOUT, :off)
|
@@ -50,7 +50,10 @@ end
|
|
50
50
|
|
51
51
|
ENV['ADAPTER'] ||= 'sqlite3'
|
52
52
|
setup_adapter(:default)
|
53
|
-
|
53
|
+
|
54
|
+
spec_dir = Pathname(__FILE__).dirname.to_s
|
55
|
+
Dir[ spec_dir + "/fixtures/**/*.rb" ].each { |rb| require(rb) }
|
56
|
+
Dir[ spec_dir + "/shared/**/*.rb" ].each { |rb| require(rb) }
|
54
57
|
|
55
58
|
|
56
59
|
module XToOneHelpers
|
@@ -58,36 +58,8 @@ describe "DataMapper::Model.accepts_nested_attributes_for" do
|
|
58
58
|
|
59
59
|
describe "no association_name" do
|
60
60
|
|
61
|
-
|
62
|
-
|
63
|
-
it "should raise" do
|
64
|
-
lambda { Branch.accepts_nested_attributes_for }.should raise_error(ArgumentError)
|
65
|
-
end
|
66
|
-
|
67
|
-
end
|
68
|
-
|
69
|
-
describe "and empty options" do
|
70
|
-
|
71
|
-
it "should raise" do
|
72
|
-
lambda { Branch.accepts_nested_attributes_for({}) }.should raise_error(ArgumentError)
|
73
|
-
end
|
74
|
-
|
75
|
-
end
|
76
|
-
|
77
|
-
describe "and invalid options" do
|
78
|
-
|
79
|
-
it "should raise" do
|
80
|
-
lambda { Branch.accepts_nested_attributes_for({ :foo => :bar}) }.should raise_error(ArgumentError)
|
81
|
-
end
|
82
|
-
|
83
|
-
end
|
84
|
-
|
85
|
-
describe "and valid options" do
|
86
|
-
|
87
|
-
it "should raise" do
|
88
|
-
lambda { Branch.accepts_nested_attributes_for({ :allow_destroy => true}) }.should raise_error(ArgumentError)
|
89
|
-
end
|
90
|
-
|
61
|
+
it "should raise" do
|
62
|
+
lambda { Branch.accepts_nested_attributes_for }.should raise_error(ArgumentError)
|
91
63
|
end
|
92
64
|
|
93
65
|
end
|
@@ -174,16 +146,18 @@ describe "DataMapper::Model.accepts_nested_attributes_for" do
|
|
174
146
|
lambda { @model.accepts_nested_attributes_for @association }.should_not raise_error
|
175
147
|
end
|
176
148
|
|
177
|
-
it "should store the accessible association in .
|
178
|
-
@model.
|
149
|
+
it "should store the accessible association in .options_for_nested_attributes" do
|
150
|
+
@model.options_for_nested_attributes[@association].should be_nil
|
179
151
|
@model.accepts_nested_attributes_for @association
|
180
|
-
@model.
|
152
|
+
relationship = @model.relationships[@association]
|
153
|
+
@model.options_for_nested_attributes[relationship].should_not be_nil
|
181
154
|
end
|
182
155
|
|
183
|
-
it "should store the default options under the association_name in .
|
184
|
-
@model.
|
156
|
+
it "should store the default options under the association_name in .options_for_nested_attributes" do
|
157
|
+
@model.options_for_nested_attributes[@association].should be_nil
|
185
158
|
@model.accepts_nested_attributes_for @association
|
186
|
-
@model.
|
159
|
+
relationship = @model.relationships[@association]
|
160
|
+
@model.options_for_nested_attributes[relationship].should == { :allow_destroy => false }
|
187
161
|
end
|
188
162
|
|
189
163
|
it "should create a \#{association_name}_attributes instance reader" do
|
@@ -211,15 +185,17 @@ describe "DataMapper::Model.accepts_nested_attributes_for" do
|
|
211
185
|
end
|
212
186
|
|
213
187
|
it "should store the accessible association in .autosave_associations" do
|
214
|
-
@model.
|
188
|
+
@model.options_for_nested_attributes[@association].should be_nil
|
215
189
|
@model.accepts_nested_attributes_for @association, {}
|
216
|
-
@model.
|
190
|
+
relationship = @model.relationships[@association]
|
191
|
+
@model.options_for_nested_attributes[relationship].should_not be_nil
|
217
192
|
end
|
218
193
|
|
219
194
|
it "should store the default options under the association_name in .autosave_associations" do
|
220
|
-
@model.
|
195
|
+
@model.options_for_nested_attributes[@association].should be_nil
|
221
196
|
@model.accepts_nested_attributes_for @association, {}
|
222
|
-
@model.
|
197
|
+
relationship = @model.relationships[@association]
|
198
|
+
@model.options_for_nested_attributes[relationship].should == { :allow_destroy => false }
|
223
199
|
end
|
224
200
|
|
225
201
|
it "should create a \#{association_name}_attributes instance reader" do
|
@@ -243,19 +219,19 @@ describe "DataMapper::Model.accepts_nested_attributes_for" do
|
|
243
219
|
describe "and invalid options" do
|
244
220
|
|
245
221
|
it "should raise" do
|
246
|
-
lambda { @model.accepts_nested_attributes_for @association, { :foo => :bar } }.should raise_error
|
222
|
+
lambda { @model.accepts_nested_attributes_for @association, { :foo => :bar } }.should raise_error(DataMapper::NestedAttributes::InvalidOptions)
|
247
223
|
end
|
248
224
|
|
249
225
|
it "should not store the accessible association in .autosave_associations" do
|
250
|
-
@model.
|
251
|
-
lambda { @model.accepts_nested_attributes_for @association, { :foo => :bar } }.should raise_error
|
252
|
-
@model.
|
226
|
+
@model.options_for_nested_attributes[@association].should be_nil
|
227
|
+
lambda { @model.accepts_nested_attributes_for @association, { :foo => :bar } }.should raise_error(DataMapper::NestedAttributes::InvalidOptions)
|
228
|
+
@model.options_for_nested_attributes[@association].should be_nil
|
253
229
|
end
|
254
230
|
|
255
231
|
it "should not create a \#{association_name}_attributes instance reader" do
|
256
232
|
p = @model.new
|
257
233
|
p.respond_to?("#{@association}_attributes").should be_false
|
258
|
-
lambda { @model.accepts_nested_attributes_for @association, { :foo => :bar } }.should raise_error
|
234
|
+
lambda { @model.accepts_nested_attributes_for @association, { :foo => :bar } }.should raise_error(DataMapper::NestedAttributes::InvalidOptions)
|
259
235
|
p = @model.new
|
260
236
|
p.respond_to?("#{@association}_attributes").should be_false
|
261
237
|
end
|
@@ -263,7 +239,7 @@ describe "DataMapper::Model.accepts_nested_attributes_for" do
|
|
263
239
|
it "should not create a \#{association_name}_attributes instance writer" do
|
264
240
|
p = @model.new
|
265
241
|
p.respond_to?("#{@association}_attributes=").should be_false
|
266
|
-
lambda { @model.accepts_nested_attributes_for @association, { :foo => :bar } }.should raise_error
|
242
|
+
lambda { @model.accepts_nested_attributes_for @association, { :foo => :bar } }.should raise_error(DataMapper::NestedAttributes::InvalidOptions)
|
267
243
|
p = @model.new
|
268
244
|
p.respond_to?("#{@association}_attributes=").should be_false
|
269
245
|
end
|
@@ -277,33 +253,35 @@ describe "DataMapper::Model.accepts_nested_attributes_for" do
|
|
277
253
|
end
|
278
254
|
|
279
255
|
it "should store the accessible association in .autosave_associations" do
|
280
|
-
@model.
|
256
|
+
@model.options_for_nested_attributes[@association].should be_nil
|
281
257
|
@model.accepts_nested_attributes_for @association, :allow_destroy => true
|
282
|
-
@model.
|
258
|
+
relationship = @model.relationships[@association]
|
259
|
+
@model.options_for_nested_attributes[relationship].should_not be_nil
|
283
260
|
end
|
284
261
|
|
285
262
|
it "should accept :allow_destroy as the only option (and thus overwrite the default option)" do
|
286
|
-
@model.
|
263
|
+
@model.options_for_nested_attributes[@association].should be_nil
|
287
264
|
@model.accepts_nested_attributes_for @association, :allow_destroy => true
|
288
|
-
@model.
|
265
|
+
relationship = @model.relationships[@association]
|
266
|
+
@model.options_for_nested_attributes[relationship].should == { :allow_destroy => true }
|
289
267
|
end
|
290
268
|
|
291
269
|
it "should accept :reject_if as the only option (and add :allow_destroy => false)" do
|
292
|
-
@model.
|
270
|
+
@model.options_for_nested_attributes[@association].should be_nil
|
293
271
|
@model.accepts_nested_attributes_for @association, :reject_if => lambda { |attributes| nil }
|
294
|
-
@model.
|
295
|
-
@model.
|
296
|
-
@model.
|
297
|
-
@model.
|
272
|
+
relationship = @model.relationships[@association]
|
273
|
+
@model.options_for_nested_attributes[relationship].should_not be_nil
|
274
|
+
@model.options_for_nested_attributes[relationship][:allow_destroy].should be_false
|
275
|
+
@model.options_for_nested_attributes[relationship][:reject_if].should be_kind_of(Proc)
|
298
276
|
end
|
299
277
|
|
300
278
|
it "should accept both :allow_destroy and :reject_if as options" do
|
301
|
-
@model.
|
279
|
+
@model.options_for_nested_attributes[@association].should be_nil
|
302
280
|
@model.accepts_nested_attributes_for @association, :allow_destroy => true, :reject_if => lambda { |attributes| nil }
|
303
|
-
@model.
|
304
|
-
@model.
|
305
|
-
@model.
|
306
|
-
@model.
|
281
|
+
relationship = @model.relationships[@association]
|
282
|
+
@model.options_for_nested_attributes[relationship].should_not be_nil
|
283
|
+
@model.options_for_nested_attributes[relationship][:allow_destroy].should be_true
|
284
|
+
@model.options_for_nested_attributes[relationship][:reject_if].should be_kind_of(Proc)
|
307
285
|
end
|
308
286
|
|
309
287
|
it "should create a \#{association_name}_attributes instance reader" do
|
@@ -326,61 +304,6 @@ describe "DataMapper::Model.accepts_nested_attributes_for" do
|
|
326
304
|
|
327
305
|
end
|
328
306
|
|
329
|
-
describe "a valid association_name pointing to a single resource", :shared => true do
|
330
|
-
|
331
|
-
describe "and no options" do
|
332
|
-
|
333
|
-
it "should create a get_\#{association_name} instance reader" do
|
334
|
-
p = @model.new
|
335
|
-
p.respond_to?("get_#{@association}").should be_false
|
336
|
-
@model.accepts_nested_attributes_for @association
|
337
|
-
p = @model.new
|
338
|
-
p.respond_to?("get_#{@association}").should be_true
|
339
|
-
p.send("get_#{@association}").should_not be_nil
|
340
|
-
end
|
341
|
-
|
342
|
-
end
|
343
|
-
|
344
|
-
describe "and empty options" do
|
345
|
-
|
346
|
-
it "should create a get_\#{association_name} instance reader" do
|
347
|
-
p = @model.new
|
348
|
-
p.respond_to?("get_#{@association}").should be_false
|
349
|
-
@model.accepts_nested_attributes_for @association, {}
|
350
|
-
p = @model.new
|
351
|
-
p.respond_to?("get_#{@association}").should be_true
|
352
|
-
p.send("get_#{@association}").should_not be_nil
|
353
|
-
end
|
354
|
-
|
355
|
-
end
|
356
|
-
|
357
|
-
describe "and invalid options" do
|
358
|
-
|
359
|
-
it "should not create a get_\#{association_name} instance reader" do
|
360
|
-
p = @model.new
|
361
|
-
p.respond_to?("get_#{@association}").should be_false
|
362
|
-
lambda { @model.accepts_nested_attributes_for @association, { :foo => :bar } }.should raise_error
|
363
|
-
p = @model.new
|
364
|
-
p.respond_to?("get_#{@association}").should be_false
|
365
|
-
end
|
366
|
-
|
367
|
-
end
|
368
|
-
|
369
|
-
describe "and valid options" do
|
370
|
-
|
371
|
-
it "should create a get_\#{association_name} instance reader" do
|
372
|
-
p = @model.new
|
373
|
-
p.respond_to?("get_#{@association}").should be_false
|
374
|
-
@model.accepts_nested_attributes_for @association, :allow_destroy => true
|
375
|
-
p = @model.new
|
376
|
-
p.respond_to?("get_#{@association}").should be_true
|
377
|
-
p.send("get_#{@association}").should_not be_nil
|
378
|
-
end
|
379
|
-
|
380
|
-
end
|
381
|
-
|
382
|
-
end
|
383
|
-
|
384
307
|
describe "a valid association_name pointing to multiple resources", :shared => true do
|
385
308
|
|
386
309
|
describe "and no options" do
|
@@ -412,7 +335,7 @@ describe "DataMapper::Model.accepts_nested_attributes_for" do
|
|
412
335
|
it "should not create a get_\#{association_name} instance reader" do
|
413
336
|
p = @model.new
|
414
337
|
p.respond_to?("get_#{@association}").should be_false
|
415
|
-
lambda { @model.accepts_nested_attributes_for @association, { :foo => :bar } }.should raise_error
|
338
|
+
lambda { @model.accepts_nested_attributes_for @association, { :foo => :bar } }.should raise_error(DataMapper::NestedAttributes::InvalidOptions)
|
416
339
|
p = @model.new
|
417
340
|
p.respond_to?("get_#{@association}").should be_false
|
418
341
|
end
|
@@ -446,7 +369,6 @@ describe "DataMapper::Model.accepts_nested_attributes_for" do
|
|
446
369
|
end
|
447
370
|
|
448
371
|
it_should_behave_like "a valid association_name"
|
449
|
-
it_should_behave_like "a valid association_name pointing to a single resource"
|
450
372
|
|
451
373
|
end
|
452
374
|
|
@@ -458,7 +380,6 @@ describe "DataMapper::Model.accepts_nested_attributes_for" do
|
|
458
380
|
end
|
459
381
|
|
460
382
|
it_should_behave_like "a valid association_name"
|
461
|
-
it_should_behave_like "a valid association_name pointing to a single resource"
|
462
383
|
|
463
384
|
end
|
464
385
|
|