acts_has_many 0.1.5 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +46 -67
- data/lib/acts_has_many/active_record/acts_has_many/child.rb +143 -0
- data/lib/acts_has_many/active_record/acts_has_many/parent.rb +106 -0
- data/lib/acts_has_many/active_record/acts_has_many.rb +100 -0
- data/lib/acts_has_many/version.rb +1 -1
- data/lib/acts_has_many.rb +5 -2
- data/spec/has_many_spec.rb +157 -190
- data/spec/has_many_through_spec.rb +102 -102
- data/spec/helper.rb +1 -0
- metadata +5 -3
- data/lib/acts_has_many/active_record/acts/has_many.rb +0 -358
data/spec/has_many_spec.rb
CHANGED
@@ -1,198 +1,165 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
3
|
+
class Location < ActiveRecord::Base
|
4
|
+
self.table_name = 'locations'
|
5
|
+
has_many :experiences
|
6
|
+
|
7
|
+
acts_has_many :experiences, compare: 'title'
|
8
|
+
end
|
9
|
+
|
10
|
+
describe 'acts_has_many' do
|
11
|
+
context 'update method' do
|
12
|
+
let(:location){Location.create :title => "italy"}
|
13
|
+
let(:experience){Experience.create :location => location, :title => "test experience1"}
|
14
|
+
before :each do
|
15
|
+
Location.delete_all
|
16
|
+
Experience.delete_all
|
13
17
|
end
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
|
19
|
+
it 'has_many_update data, relation' do
|
20
|
+
add_loc, del_loc = experience.location.has_many_update({"title" => "ukraine"}, "experiences")
|
21
|
+
|
22
|
+
expect(Location.all.size).to be 1
|
23
|
+
expect(location).to eq add_loc
|
24
|
+
expect(del_loc).to be nil
|
25
|
+
expect(experience.location.title).to eq "ukraine"
|
26
|
+
|
27
|
+
Experience.create :location => location, :title => "test experience2"
|
28
|
+
|
29
|
+
add_loc, del_loc = experience.location.has_many_update({"title" => "italy"}, "experiences")
|
30
|
+
|
31
|
+
expect(Location.all.size).to be 2
|
32
|
+
expect(location).not_to eq add_loc
|
33
|
+
expect(del_loc).to be nil
|
34
|
+
|
35
|
+
experience.location = Location.find(add_loc)
|
36
|
+
expect(experience.location.title).to eq "italy"
|
37
|
+
|
38
|
+
add_loc, del_loc = experience.location.has_many_update({"title" => "ukraine"}, :experiences)
|
39
|
+
|
40
|
+
expect(location).to eq add_loc
|
41
|
+
expect(experience.location).to eq del_loc
|
22
42
|
end
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
43
|
+
|
44
|
+
it 'update_with_<relation> data' do
|
45
|
+
add_loc, del_loc = experience.location.update_with_experiences({"title" => "ukraine"})
|
46
|
+
|
47
|
+
expect(Location.all.size).to be 1
|
48
|
+
expect(location).to eq add_loc
|
49
|
+
expect(del_loc).to be nil
|
50
|
+
expect(experience.location.title).to eq "ukraine"
|
51
|
+
|
52
|
+
Experience.create :location => location, :title => "test experience2"
|
53
|
+
|
54
|
+
add_loc, del_loc = experience.location.update_with_experiences({"title" => "italy"})
|
55
|
+
|
56
|
+
expect(Location.all.size).to be 2
|
57
|
+
expect(location.id).not_to eq add_loc
|
58
|
+
expect(del_loc).to be nil
|
59
|
+
|
60
|
+
experience.location = Location.find(add_loc)
|
61
|
+
expect(experience.location.title).to eq "italy"
|
62
|
+
|
63
|
+
add_loc, del_loc = experience.location.update_with_experiences({"title" => "ukraine"})
|
64
|
+
|
65
|
+
expect(location).to eq add_loc
|
66
|
+
expect(experience.location).to eq del_loc
|
31
67
|
end
|
32
|
-
end
|
33
68
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
add_loc, del_loc = experience.location.has_many_update({"title" => "italy"}, "experiences")
|
56
|
-
|
57
|
-
Location.all.size.should == 2
|
58
|
-
location.id.should_not == add_loc
|
59
|
-
del_loc.should == nil
|
60
|
-
experience.location = Location.find(add_loc)
|
61
|
-
experience.location.title.should == "italy"
|
62
|
-
|
63
|
-
add_loc, del_loc = experience.location.has_many_update({"title" => "ukraine"})
|
64
|
-
|
65
|
-
location.id.should == add_loc
|
66
|
-
experience.location.id.should == del_loc
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'has_many_update!(data)' do
|
70
|
-
# update independent Location
|
71
|
-
new_row = experience.location.has_many_update!({"title" => "ukraine"})
|
72
|
-
|
73
|
-
Location.all.size.should == 1
|
74
|
-
location.id.should == new_row.id
|
75
|
-
experience.location.title.should == "ukraine"
|
76
|
-
|
77
|
-
Experience.create :location => location, :title => "test experience2"
|
78
|
-
|
79
|
-
# don't update and create new Location
|
80
|
-
new_row = experience.location.has_many_update!({"title" => "italy"})
|
81
|
-
|
82
|
-
experience.reload
|
83
|
-
Location.all.size.should == 2
|
84
|
-
location.id.should_not == new_row.id
|
85
|
-
experience.location.title.should == "italy"
|
86
|
-
|
87
|
-
# delete unnecessary Location
|
88
|
-
new_row = experience.location.has_many_update!({"title" => "ukraine"})
|
89
|
-
|
90
|
-
location.id.should == new_row.id
|
91
|
-
Location.all.size.should == 1
|
92
|
-
end
|
93
|
-
|
94
|
-
it 'has_many_update!(data, parent_row)' do
|
95
|
-
# update independent Location
|
96
|
-
new_row = location.has_many_update!({"title" => "ukraine"}, experience)
|
97
|
-
|
98
|
-
Location.all.size.should == 1
|
99
|
-
location.id.should == new_row.id
|
100
|
-
experience.location.title.should == "ukraine"
|
101
|
-
|
102
|
-
Experience.create :location => location, :title => "test experience2"
|
103
|
-
|
104
|
-
# don't update and create new Location
|
105
|
-
new_row = location.has_many_update!({"title" => "italy"}, experience)
|
106
|
-
|
107
|
-
experience.reload
|
108
|
-
Location.all.size.should == 2
|
109
|
-
location.id.should_not == new_row.id
|
110
|
-
experience.location.title.should == "italy"
|
111
|
-
|
112
|
-
# delete unnecessary Location
|
113
|
-
new_row = location.has_many_update!({"title" => "italy"}, Experience.where(location_id: location).first) #becaus experience wos reload
|
114
|
-
|
115
|
-
location.id.should_not == new_row.id
|
116
|
-
location.frozen?.should == true
|
117
|
-
Location.all.size.should == 1
|
118
|
-
end
|
119
|
-
|
120
|
-
it 'update_with_<relation>(data)' do
|
121
|
-
add_loc, del_loc = experience.location.update_with_experiences({"title" => "ukraine"})
|
122
|
-
|
123
|
-
Location.all.size.should == 1
|
124
|
-
location.id.should == add_loc
|
125
|
-
del_loc.should == nil
|
126
|
-
experience.location.title.should == "ukraine"
|
127
|
-
|
128
|
-
Experience.create :location => location, :title => "test experience2"
|
129
|
-
|
130
|
-
add_loc, del_loc = experience.location.update_with_experiences({"title" => "italy"})
|
131
|
-
|
132
|
-
Location.all.size.should == 2
|
133
|
-
location.id.should_not == add_loc
|
134
|
-
del_loc.should == nil
|
135
|
-
experience.location = Location.find(add_loc)
|
136
|
-
experience.location.title.should == "italy"
|
137
|
-
|
138
|
-
add_loc, del_loc = experience.location.update_with_experiences({"title" => "ukraine"})
|
139
|
-
|
140
|
-
location.id.should == add_loc
|
141
|
-
experience.location.id.should == del_loc
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
it 'destroy' do
|
146
|
-
Location.delete_all
|
147
|
-
Experience.delete_all
|
148
|
-
|
149
|
-
location = Location.create(:title => "ukraine")
|
150
|
-
|
151
|
-
Experience.create( :location => location, :title => "test" )
|
152
|
-
|
153
|
-
location.destroy
|
154
|
-
Location.all.size.should == 1
|
155
|
-
|
156
|
-
Experience.all[0].destroy
|
157
|
-
location.destroy
|
158
|
-
Location.all.size.should == 0
|
159
|
-
end
|
160
|
-
|
161
|
-
it 'actuale?' do
|
162
|
-
Location.delete_all
|
163
|
-
|
164
|
-
location = Location.create(:title => "ukraine")
|
165
|
-
|
166
|
-
location.actuale?.should == false
|
167
|
-
location.actuale?(:relation => "experiences").should == false
|
168
|
-
|
169
|
-
Experience.create( :title => 'test', :location => location )
|
170
|
-
|
171
|
-
location.actuale?.should == true
|
172
|
-
location.actuale?(:relation => "experiences").should == false
|
173
|
-
location.actuale?(:relation => :experiences).should == false
|
174
|
-
location.actuale?(:relation => "Experience").should == false
|
175
|
-
location.actuale?("Experience").should == false
|
176
|
-
|
177
|
-
Experience.create( :title => 'test', :location => location )
|
178
|
-
|
179
|
-
location.actuale?.should == true
|
180
|
-
location.actuale?(:relation => "experiences").should == true
|
181
|
-
location.actuale?(:experiences).should == true
|
182
|
-
end
|
183
|
-
|
184
|
-
it 'compare' do
|
185
|
-
Location.compare.should == :title
|
186
|
-
end
|
187
|
-
|
188
|
-
it 'model' do
|
189
|
-
location = Location.create(:title => "ukraine")
|
190
|
-
location.model.should == Location
|
191
|
-
end
|
192
|
-
|
193
|
-
it 'dependent_relations' do
|
194
|
-
Location.dependent_relations.should == ['experiences']
|
195
|
-
end
|
69
|
+
it "parent.child_attributes= Hash" do
|
70
|
+
experience = Experience.create location_attributes: {title: "ukraine"}, title: "test experience2"
|
71
|
+
|
72
|
+
Location.all.size.should be 1
|
73
|
+
experience.location.title.should eq "ukraine"
|
74
|
+
|
75
|
+
Experience.create location_attributes: {title: "ukraine"}, title: "test experience2"
|
76
|
+
Location.all.size.should be 1
|
77
|
+
|
78
|
+
experience.location_attributes = {"title" => "italy"}
|
79
|
+
experience.save
|
80
|
+
|
81
|
+
Location.all.size.should be 2
|
82
|
+
experience.location.title.should eq "italy"
|
83
|
+
|
84
|
+
experience.location_attributes = {"title" => "ukraine"}
|
85
|
+
experience.save
|
86
|
+
|
87
|
+
Location.all.size.should be 1
|
88
|
+
experience.location.title.should eq "ukraine"
|
196
89
|
end
|
90
|
+
|
91
|
+
it "parent.child_attributes= exist_record" do
|
92
|
+
experience = Experience.create location_attributes: {title: "ukraine"}, title: "test experience2"
|
93
|
+
|
94
|
+
Location.all.size.should be 1
|
95
|
+
experience.location.title.should eq "ukraine"
|
96
|
+
|
97
|
+
Experience.create location_attributes: Location.first, title: "test experience2"
|
98
|
+
Location.all.size.should be 1
|
99
|
+
|
100
|
+
experience.location_attributes = {"title" => "italy"}
|
101
|
+
experience.save
|
102
|
+
|
103
|
+
Location.all.size.should be 2
|
104
|
+
experience.location.title.should eq "italy"
|
105
|
+
|
106
|
+
experience.location_attributes = Location.first
|
107
|
+
experience.save
|
108
|
+
|
109
|
+
Location.all.size.should be 1
|
110
|
+
experience.location.title.should eq "ukraine"
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'destroy' do
|
115
|
+
Location.delete_all
|
116
|
+
Experience.delete_all
|
117
|
+
|
118
|
+
location = Location.create(:title => "ukraine")
|
119
|
+
|
120
|
+
Experience.create( :location => location, :title => "test" )
|
121
|
+
|
122
|
+
location.destroy
|
123
|
+
Location.all.size.should == 1
|
124
|
+
|
125
|
+
Experience.all[0].destroy
|
126
|
+
location.destroy
|
127
|
+
Location.all.size.should == 0
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'actuale?' do
|
131
|
+
Location.delete_all
|
132
|
+
|
133
|
+
location = Location.create(:title => "ukraine")
|
134
|
+
|
135
|
+
location.actuale?.should == false
|
136
|
+
location.actuale?("experiences").should == false
|
137
|
+
|
138
|
+
Experience.create( :title => 'test', :location => location )
|
139
|
+
|
140
|
+
location.actuale?.should == true
|
141
|
+
location.actuale?("experiences").should == false
|
142
|
+
location.actuale?(:experiences).should == false
|
143
|
+
location.actuale?("Experience").should == false
|
144
|
+
location.actuale?("Experience").should == false
|
145
|
+
|
146
|
+
Experience.create( :title => 'test', :location => location )
|
147
|
+
|
148
|
+
location.actuale?.should == true
|
149
|
+
location.actuale?("experiences").should == true
|
150
|
+
location.actuale?(:experiences).should == true
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'compare' do
|
154
|
+
Location.compare.should == :title
|
155
|
+
end
|
156
|
+
|
157
|
+
it 'model' do
|
158
|
+
location = Location.create(:title => "ukraine")
|
159
|
+
location.model.should == Location
|
160
|
+
end
|
161
|
+
|
162
|
+
it 'dependent_relations' do
|
163
|
+
Location.dependent_relations.should == ['experiences']
|
197
164
|
end
|
198
|
-
end
|
165
|
+
end
|
@@ -1,106 +1,106 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
3
|
+
class Local < ActiveRecord::Base
|
4
|
+
self.table_name = 'locals'
|
5
|
+
has_many :companies, :through => :company_locals
|
6
|
+
has_many :company_locals
|
7
|
+
|
8
|
+
acts_has_many :companies, :through => true, compare: 'title'
|
9
|
+
end
|
10
|
+
|
11
|
+
describe 'acts_has_many with :through' do
|
12
|
+
it 'parent.child_colletion=<data>' do
|
13
|
+
Local.delete_all
|
14
|
+
|
15
|
+
company = Company.create title: 'test', locals_collection: [{title: 'testx'}]
|
16
|
+
company.locals.first.title.should eq 'testx'
|
17
|
+
|
18
|
+
company.locals_collection = [{title: 'test2'}]
|
19
|
+
company.locals.first.title.should eq 'test2'
|
20
|
+
Local.all.size.should be 2
|
21
|
+
|
22
|
+
company.save # after save we clear garbage
|
23
|
+
company.locals.first.title.should eq 'test2'
|
24
|
+
Local.all.size.should be 1
|
25
|
+
|
26
|
+
company2 = Company.create title: 'test2', locals_collection: [{title: 'test2'},{title: 'test1'}]
|
27
|
+
company.locals_collection = Local.all
|
28
|
+
company.locals.size.should be 2
|
29
|
+
|
30
|
+
company.save
|
31
|
+
Local.all.size.should be 2
|
32
|
+
|
33
|
+
company.locals_collection = [{title: 'test3'}, {title: 'test2'}]
|
34
|
+
company.save
|
36
35
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
36
|
+
company.locals.size.should be 2
|
37
|
+
Local.all.size.should be 3
|
38
|
+
|
39
|
+
company.locals_collection = [{title: 'test1'}]
|
40
|
+
Local.all.size.should be 3
|
41
|
+
|
42
|
+
company.save
|
43
|
+
Local.all.size.should be 2
|
44
|
+
|
45
|
+
company2.locals_collection = [Local.last]
|
46
|
+
Local.all.size.should be 2
|
47
|
+
|
48
|
+
company2.save
|
49
|
+
Local.all.size.should be 1
|
50
|
+
company2.locals.should eq company.locals
|
51
|
+
|
52
|
+
company2.locals_collection = []
|
53
|
+
company2.save
|
54
|
+
company2.locals.should eq []
|
55
|
+
|
56
|
+
company.locals_collection = []
|
57
|
+
company.save
|
58
|
+
|
59
|
+
Local.all.should eq []
|
60
|
+
end
|
61
|
+
it 'update' do
|
62
|
+
Local.delete_all
|
63
|
+
|
64
|
+
Local.all.size.should == 0
|
65
|
+
|
66
|
+
new_records, del_records = Local.has_many_through_update( :new => [
|
67
|
+
{ title: 'test0'},
|
68
|
+
{ title: 'test1'},
|
69
|
+
{ title: 'test2'},
|
70
|
+
{ title: 'test3'},
|
71
|
+
{ title: 'test0'},
|
72
|
+
], relation: :companies)
|
73
|
+
|
74
|
+
expect(new_records.count).to be 4
|
75
|
+
expect(del_records).to eq []
|
76
|
+
expect(Local.all.size).to be 4
|
77
|
+
|
78
|
+
new_records_1, del_records_1 = Local.has_many_through_update(
|
79
|
+
:new => [
|
80
|
+
{ title: 'test0'},
|
81
|
+
{ 'title' => 'test1'}
|
82
|
+
],
|
83
|
+
:update => {
|
84
|
+
new_records[2].id => {title: 'test2s'},
|
85
|
+
new_records[3].id.to_s => {'title' => 'test3s'}
|
86
|
+
}, relation: 'companies')
|
87
|
+
|
88
|
+
expect(new_records_1.map(&:id).sort!).to eq new_records.map(&:id)
|
89
|
+
expect(Local.find(new_records[2].id).title).to eq 'test2s'
|
90
|
+
expect(Local.find(new_records[3].id).title).to eq 'test3s'
|
91
|
+
expect(Local.all.size).to be 4
|
92
|
+
|
93
|
+
|
94
|
+
new, del = Local.has_many_through_update(
|
95
|
+
:new => [
|
96
|
+
{ title: 'test0'},
|
97
|
+
{ title: 'test3s'}],
|
98
|
+
:update => {
|
99
|
+
new_records_1[2].id => {title: 'test2s'},
|
100
|
+
new_records_1[3].id => {title: ''}
|
101
|
+
}, relation: :companies)
|
102
|
+
|
103
|
+
expect(del.size).to be 1
|
104
|
+
expect(new.size).to be 3
|
105
105
|
end
|
106
|
-
end
|
106
|
+
end
|
data/spec/helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_has_many
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-11-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -88,7 +88,9 @@ files:
|
|
88
88
|
- acts_has_many.gemspec
|
89
89
|
- init.rb
|
90
90
|
- lib/acts_has_many.rb
|
91
|
-
- lib/acts_has_many/active_record/
|
91
|
+
- lib/acts_has_many/active_record/acts_has_many.rb
|
92
|
+
- lib/acts_has_many/active_record/acts_has_many/child.rb
|
93
|
+
- lib/acts_has_many/active_record/acts_has_many/parent.rb
|
92
94
|
- lib/acts_has_many/version.rb
|
93
95
|
- spec/has_many_spec.rb
|
94
96
|
- spec/has_many_through_spec.rb
|