acts_has_many 0.1.5 → 0.2.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.
@@ -1,198 +1,165 @@
1
1
  require 'helper'
2
2
 
3
- (1..3).each do |c|
4
-
5
- case c
6
- when 1
7
- class Location < ActiveRecord::Base
8
- self.table_name = 'locations'
9
- has_many :experiences
10
- validates :title, :presence => true
11
-
12
- acts_has_many
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
- when 2
16
- class Location < ActiveRecord::Base
17
- self.table_name = 'locations'
18
- acts_has_many relations: [:experiences], compare: :title
19
-
20
- has_many :experiences
21
- validates :title, :presence => true
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
- when 3
25
- class Location < ActiveRecord::Base
26
- self.table_name = 'locations'
27
- acts_has_many relations: ['experiences'], compare: 'title'
28
-
29
- has_many :experiences
30
- validates :title, :presence => true
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
- describe "Initialization tipe #{c}" do
35
- describe 'acts_has_many' do
36
- context 'update method' do
37
- let(:location){Location.create :title => "italy"}
38
- let(:experience){Experience.create :location => location, :title => "test experience1"}
39
- before :each do
40
- Location.delete_all
41
- Experience.delete_all
42
- end
43
-
44
- it 'has_many_update(data: data, relation: relation)' do
45
- add_loc, del_loc = experience.location.has_many_update(
46
- :data => {"title" => "ukraine"}, :relation => "experiences")
47
-
48
- Location.all.size.should == 1
49
- location.id.should == add_loc
50
- del_loc.should == nil
51
- experience.location.title.should == "ukraine"
52
-
53
- Experience.create :location => location, :title => "test experience2"
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
- (1..3).each do |c|
4
-
5
- case c
6
- when 1
7
- class Local < ActiveRecord::Base
8
- self.table_name = 'locals'
9
- validates :title, :presence => true
10
-
11
- has_many :companies, :through => :company_locals
12
- acts_has_many :through => true
13
- has_many :company_locals
14
- end
15
-
16
- when 2
17
- class Local < ActiveRecord::Base
18
- self.table_name = 'locals'
19
- validates :title, :presence => true
20
-
21
- has_many :companies, :through => :company_locals
22
- has_many :company_locals
23
- acts_has_many through: true, relations: [:companies], compare: :title
24
- end
25
-
26
- when 3
27
- class Local < ActiveRecord::Base
28
- self.table_name = 'locals'
29
- validates :title, :presence => true
30
-
31
- has_many :companies, :through => :company_locals
32
- has_many :company_locals
33
- acts_has_many :through => true, relations: ['companies'], compare: 'title'
34
- end
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
- describe "Initialization tipe #{c}" do
38
- describe 'acts_has_many with :through' do
39
- it 'update' do
40
- Local.delete_all
41
-
42
- Local.all.size.should == 0
43
-
44
- new_row, del_ids = Local.has_many_through_update( :new => [
45
- { title: 'test0'},
46
- { title: 'test1'},
47
- { title: 'test2'},
48
- { title: 'test3'},
49
- { title: 'test0'},
50
- ], relation: :companies)
51
-
52
- new_row.count.should == 4
53
- del_ids.should == []
54
- Local.all.size.should == 4
55
-
56
- new_row1, del_ids1 = Local.has_many_through_update(
57
- :new => [
58
- { title: 'test0'},
59
- { 'title' => 'test1'}],
60
- :update => {
61
- new_row[2].id => {title: 'test2s'},
62
- new_row[3].id.to_s => {'title' => 'test3s'}
63
- }, relation: 'companies')
64
-
65
- new_row1[0].id.should == new_row[0].id
66
- new_row1[1].id.should == new_row[1].id
67
- new_row1[2].id.should == new_row[2].id
68
- new_row1[3].id.should == new_row[3].id
69
- del_ids1.should == []
70
- Local.find(new_row[2].id).title.should == 'test2s'
71
- Local.find(new_row[3].id).title.should == 'test3s'
72
- Local.all.size.should == 4
73
-
74
-
75
- new_row, del_ids = Local.has_many_through_update(
76
- :new => [
77
- { title: 'test0'},
78
- { title: 'test3s'}],
79
- :update => {
80
- new_row1[2].id => {title: 'test2s'},
81
- new_row1[3].id => {title: ''}
82
- }, relation: :companies)
83
-
84
- del_ids.should == []
85
- new_row.size.should == 3
86
-
87
- new_row, del_ids = Local.has_many_through_update(
88
- :update => {
89
- new_row1[2].id => {title: 'test2s'},
90
- new_row1[3].id => {title: ''}
91
- }, relation: :companies)
92
-
93
- del_ids.should == [new_row1[3].id]
94
- new_row.size.should == 1
95
-
96
- new_row, del_ids = Local.has_many_through_update(
97
- :new => [{ title: 'test0'}],
98
- :update => { new_row1[2].id => {title: 'test0'} }, relation: :companies)
99
-
100
- new_row.size.should == 1
101
-
102
- Local.all.size.should == 4
103
- end
104
- end
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
@@ -53,4 +53,5 @@ class Company < ActiveRecord::Base
53
53
  self.table_name = 'companies'
54
54
  has_many :company_locals, :dependent => :destroy
55
55
  has_many :locals, :through => :company_locals
56
+ acts_has_many_for :locals
56
57
  end
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.1.5
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-10-29 00:00:00.000000000 Z
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/acts/has_many.rb
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