acts_has_many 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -99,7 +99,7 @@ delete_id # => 1
99
99
 
100
100
  company.destroy # => true
101
101
 
102
- # this situation with delete_id best way is "company.delete" because you miss recheck actuality
102
+ # this situation with delete_id best way is "company.delete" because you miss unnecessary check actuality
103
103
  ```
104
104
 
105
105
  ### has_many_update_through used with has_many :through and get array parameters
@@ -129,7 +129,5 @@ new_rows, delete_ids = Company.has_many_update_through( update: data, new: date,
129
129
 
130
130
  user.companies = new_rows # update user companies
131
131
 
132
- delete_ids.each do |id|
133
- Company.finde(id).destroy # necessary for correct delete record ( will be fixed)
134
- end
132
+ Company.delete(delete_ids) # for delete_ids from has_many_update_through best way is to use "delete" and miss unnecessary check
135
133
  ```
@@ -75,10 +75,11 @@ module ActiveRecord
75
75
  unless options[:new].nil?
76
76
  options[:new].uniq!
77
77
  options[:new].each do |data|
78
- date = data.symbolize_keys
78
+ data = data.symbolize_keys
79
79
  record_add << #{self}
80
80
  .where('#{options[:compare]}' => data['#{options[:compare]}'.to_sym])
81
81
  .first_or_create(data)
82
+ record_del.delete record_add.last.id
82
83
  end
83
84
  end
84
85
 
@@ -117,7 +118,7 @@ module ActiveRecord
117
118
 
118
119
  def has_many_update(options)
119
120
  options_default = {
120
- :data => { :title => ""}
121
+ :data => { title: "" }
121
122
  }
122
123
 
123
124
  options = options_default.merge options
@@ -127,7 +128,7 @@ module ActiveRecord
127
128
  object_id = id
128
129
  delete_id = nil
129
130
 
130
- if actuale? :relation => options[:relation]
131
+ if actuale? relation: options[:relation]
131
132
  # create new object and finish
132
133
  object = model.where(full_compare).first_or_create(options[:data])
133
134
  object_id = object.id
@@ -161,7 +162,7 @@ module ActiveRecord
161
162
  # :relation( string, symbol) - exclude current relation
162
163
  #
163
164
 
164
- def actuale? (options = { :relation => "" })
165
+ def actuale? (options = { relation: "" })
165
166
  actuale = false
166
167
 
167
168
  depend_relations.each do |relation|
@@ -1,3 +1,3 @@
1
1
  module ActsHasMany
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -1,70 +1,122 @@
1
1
  require 'helper'
2
2
 
3
- describe 'acts_has_many' do
4
- it 'update' do
5
- location = Location.create :title => "italy"
6
- experience = Experience.create :location => location, :title => "test experience1"
7
-
8
- add_loc, del_loc = experience.location.has_many_update(
9
- :data => {"title" => "ukraine"}, :relation => "experiences")
10
- location = Location.find(add_loc)
11
- experience.location = location
12
- Location.delete(del_loc) unless del_loc.nil?
13
-
14
- experience.location.title.should == "ukraine"
15
- Location.all.size.should == 1
16
-
17
- Experience.create :location => location, :title => "test experience2"
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
18
11
 
19
- add_loc, del_loc = experience.location.has_many_update(
20
- :data => {"title" => "italy"}, :relation => "experiences")
21
- experience.location = Location.find(add_loc)
22
- Location.delete(del_loc) unless del_loc.nil?
23
-
24
- experience.location.id.should == add_loc
25
- Location.all.size.should == 2
26
-
27
- add_loc, del_loc = experience.location.has_many_update(
28
- :data => {"title" => "ukraine"}, :relation => "experiences")
29
- experience.location = Location.find(add_loc)
30
- Location.delete(del_loc) unless del_loc.nil?
31
-
32
- experience.location.should == location
33
- Location.all.size.should == 1
34
- end
12
+ acts_has_many
13
+ end
35
14
 
36
- it 'destroy' do
37
- Location.delete_all
38
- Experience.delete_all
39
-
40
- location = Location.create(:title => "ukraine")
41
-
42
- Experience.create( :location => location, :title => "test" )
43
-
44
- location.destroy
45
- Location.all.size.should == 1
46
-
47
- Experience.all[0].destroy
48
- location.destroy
49
- Location.all.size.should == 0
50
- end
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
22
+ end
51
23
 
52
- it 'actuale?' do
53
- Location.delete_all
54
-
55
- location = Location.create(:title => "ukraine")
56
-
57
- location.actuale?.should == false
58
- location.actuale?(:relation => "experiences").should == false
59
-
60
- Experience.create( :title => 'test', :location => location )
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
31
+ end
32
+ end
33
+
34
+ describe "Initialization tipe #{c}" do
35
+ describe 'acts_has_many' do
36
+ it 'update' do
37
+ Location.delete_all
38
+ Experience.delete_all
39
+
40
+ location = Location.create :title => "italy"
41
+ experience = Experience.create :location => location, :title => "test experience1"
42
+
43
+ add_loc, del_loc = experience.location.has_many_update(
44
+ :data => {"title" => "ukraine"}, :relation => "experiences")
45
+
46
+ Location.all.size.should == 1
47
+ location.id.should == add_loc
48
+ del_loc.should == nil
49
+ experience.location.title.should == "ukraine"
50
+
51
+ Experience.create :location => location, :title => "test experience2"
52
+
53
+ add_loc, del_loc = experience.location.has_many_update(
54
+ :data => {"title" => "italy"}, :relation => "experiences")
55
+
56
+ Location.all.size.should == 2
57
+ location.id.should_not == add_loc
58
+ del_loc.should == nil
59
+ experience.location = Location.find(add_loc)
60
+ experience.location.title.should == "italy"
61
+
62
+ add_loc, del_loc = experience.location.has_many_update(
63
+ :data => {"title" => "ukraine"}, :relation => "experiences")
64
+
65
+ location.id.should == add_loc
66
+ experience.location.id.should == del_loc
67
+ end
68
+
69
+ it 'destroy' do
70
+ Location.delete_all
71
+ Experience.delete_all
72
+
73
+ location = Location.create(:title => "ukraine")
74
+
75
+ Experience.create( :location => location, :title => "test" )
76
+
77
+ location.destroy
78
+ Location.all.size.should == 1
79
+
80
+ Experience.all[0].destroy
81
+ location.destroy
82
+ Location.all.size.should == 0
83
+ end
84
+
85
+ it 'actuale?' do
86
+ Location.delete_all
87
+
88
+ location = Location.create(:title => "ukraine")
89
+
90
+ location.actuale?.should == false
91
+ location.actuale?(:relation => "experiences").should == false
61
92
 
62
- location.actuale?.should == true
63
- location.actuale?(:relation => "experiences").should == false
93
+ Experience.create( :title => 'test', :location => location )
64
94
 
65
- Experience.create( :title => 'test', :location => location )
66
-
67
- location.actuale?.should == true
68
- location.actuale?(:relation => "experiences").should == true
69
- end
95
+ location.actuale?.should == true
96
+ location.actuale?(:relation => "experiences").should == false
97
+ location.actuale?(:relation => :experiences).should == false
98
+ location.actuale?(:relation => "Experience").should == false
99
+
100
+ Experience.create( :title => 'test', :location => location )
101
+
102
+ location.actuale?.should == true
103
+ location.actuale?(:relation => "experiences").should == true
104
+ end
105
+
106
+ it 'compare' do
107
+ location = Location.create(:title => "ukraine")
108
+ location.compare.should == :title
109
+ end
110
+
111
+ it 'model' do
112
+ location = Location.create(:title => "ukraine")
113
+ location.model.should == Location
114
+ end
115
+
116
+ it 'depend_relations' do
117
+ location = Location.create(:title => "ukraine")
118
+ location.depend_relations.should == ['experiences']
119
+ end
120
+ end
121
+ end
70
122
  end
@@ -0,0 +1,106 @@
1
+ require 'helper'
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
36
+
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
105
+ end
106
+ end
data/spec/helper.rb CHANGED
@@ -23,6 +23,17 @@ ActiveRecord::Schema.define(:version => 1) do
23
23
  create_table :locations do |t|
24
24
  t.string :title
25
25
  end
26
+
27
+ create_table :locals do |t|
28
+ t.string :title
29
+ end
30
+ create_table :company_locals do |t|
31
+ t.references :local
32
+ t.references :company
33
+ end
34
+ create_table :companies do |t|
35
+ t.string :title
36
+ end
26
37
  end
27
38
 
28
39
  class Experience < ActiveRecord::Base
@@ -30,10 +41,14 @@ class Experience < ActiveRecord::Base
30
41
  belongs_to :location, :dependent => :destroy
31
42
  end
32
43
 
33
- class Location < ActiveRecord::Base
34
- self.table_name = 'locations'
35
- has_many :experiences
36
- validates :title, :presence => true
37
-
38
- acts_has_many
44
+ class CompanyLocal < ActiveRecord::Base
45
+ self.table_name = 'company_locals'
46
+ belongs_to :local
47
+ belongs_to :company
48
+ end
49
+
50
+ class Company < ActiveRecord::Base
51
+ self.table_name = 'companies'
52
+ has_many :company_locals, :dependent => :destroy
53
+ has_many :locals, :through => :company_locals
39
54
  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.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -91,6 +91,7 @@ files:
91
91
  - lib/acts_has_many/active_record/acts/has_many.rb
92
92
  - lib/acts_has_many/version.rb
93
93
  - spec/has_many_spec.rb
94
+ - spec/has_many_through_spec.rb
94
95
  - spec/helper.rb
95
96
  homepage: https://github.com/igor04/acts_has_many
96
97
  licenses: []