acts_has_many 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +3 -0
- data/README.md +62 -132
- data/acts_has_many.gemspec +1 -0
- data/lib/acts_has_many/active_record/acts_has_many/child.rb +36 -70
- data/lib/acts_has_many/active_record/acts_has_many/parent.rb +13 -27
- data/lib/acts_has_many/active_record/acts_has_many.rb +18 -34
- data/lib/acts_has_many/version.rb +1 -1
- data/spec/has_many_spec.rb +26 -44
- data/spec/has_many_through_spec.rb +4 -4
- data/spec/helper.rb +6 -6
- metadata +20 -3
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
# acts_has_many
|
1
|
+
# acts_has_many [![Build Status](https://travis-ci.org/igor04/acts_has_many.png?branch=master)](https://travis-ci.org/igor04/acts_has_many)
|
2
2
|
|
3
|
-
Acts_has_many gem gives functional for clean update elements
|
4
|
-
(additional is has_many :trhough). The aim is work with has_many relation without gerbage,
|
3
|
+
Acts_has_many gem gives functional for clean update elements `has_many` relation
|
4
|
+
(additional is `has_many :trhough`). The aim is work with has_many relation without gerbage,
|
5
5
|
every record must be used, othervise there will be no record.
|
6
6
|
|
7
7
|
## Installation
|
@@ -19,153 +19,83 @@ Or install it yourself as:
|
|
19
19
|
$ gem install acts_has_many
|
20
20
|
|
21
21
|
## Usage
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
class Company < ActiveRecord::Base
|
28
|
-
has_many :users
|
29
|
-
|
30
|
-
acts_has_many # after necessary relation
|
31
|
-
end
|
32
|
-
|
33
|
-
# OR
|
34
|
-
|
35
|
-
class Company < ActiveRecord::Base
|
36
|
-
has_many :users
|
37
|
-
|
38
|
-
acts_has_many :users # list necessary relations
|
39
|
-
end
|
40
|
-
|
41
|
-
# acts_has_many options:
|
42
|
-
# list relations(it maybe missed if you use has many relation which are written above)
|
43
|
-
# :compare( string or symbol; default: :title) - name column with unique elements in table
|
44
|
-
# :through( boolean; default: false) - if you use has_many :through
|
45
|
-
|
46
|
-
|
47
|
-
company = Company.create(:title => 'Microsoft')
|
48
|
-
Company.dependent_relations # => ["users"]
|
49
|
-
Company.compare # => :title
|
50
|
-
|
51
|
-
company.actuale? # => false
|
52
|
-
|
53
|
-
user = User.create do |user|
|
54
|
-
user.company = company
|
55
|
-
# ...
|
56
|
-
end
|
57
|
-
|
58
|
-
company.actuale? # => true
|
59
|
-
company.actuale? :users # => false ( exclude 1 record of current relation)
|
60
|
-
|
61
|
-
company # => <Company id: 1, title: "Microsoft">
|
62
|
-
|
63
|
-
update_record, delete_record = company.has_many_update({ title: 'Google'}, :users)
|
64
|
-
# or
|
65
|
-
# update_record, delete_record = company.update_with_users({ title: 'Google'})
|
66
|
-
|
67
|
-
update_record # => <Company id: 1, title: "Google">
|
68
|
-
delete_record # => nil
|
69
|
-
|
70
|
-
user2 = User.create do |user|
|
71
|
-
user.company = company
|
72
|
-
# ...
|
73
|
-
end
|
74
|
-
|
75
|
-
company.actuale? # => true
|
76
|
-
company.actuale? :users # => true
|
77
|
-
|
78
|
-
# if you want to destroy user
|
79
|
-
# user2.destroy # => user will be destroyed, company will rollback (because company is used by other user)
|
80
|
-
|
81
|
-
company # => <Company id: 1, title: "Google">
|
82
|
-
update_record, delete_id = company.has_many_update({ title: 'Apple'}, :users)
|
83
|
-
update_record # => <Company id: 2, title: "Apple">
|
84
|
-
delete_record # => nil
|
85
|
-
|
86
|
-
user2.company = update_record
|
87
|
-
user2.save
|
88
|
-
|
89
|
-
# if you want to destroy user now
|
90
|
-
# user2.destroy # => user and company will be destroyed (because company is used only by user2)
|
91
|
-
|
92
|
-
Company.all # [#<Company id: 1, title: "Google">, #<Company id: 2, title: "Apple"]
|
22
|
+
1. To initialize gem you can use `acts_has_many` only, or `acts_has_many` with `acts_has_many_for` together
|
23
|
+
2. In model where you set `acts_has_many` will work clearly (all records used, no duplications)
|
24
|
+
3. Should use `dependent: :destroy`
|
93
25
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
delete_record # => <Company id: 1, title: "Google">
|
100
|
-
|
101
|
-
user2.company = update_record
|
102
|
-
user2.save
|
103
|
-
|
104
|
-
company.destroy # => true
|
26
|
+
4. If only `acts_has_many` is used:
|
27
|
+
```ruby
|
28
|
+
class User < ActiveRecord::Base
|
29
|
+
belongs_to :company, dependent: :destroy
|
30
|
+
end
|
105
31
|
|
106
|
-
|
32
|
+
class Company < ActiveRecord::Base
|
33
|
+
has_many :users
|
107
34
|
|
35
|
+
acts_has_many :users
|
36
|
+
end
|
108
37
|
```
|
109
|
-
|
110
|
-
### has_many_update_through used with has_many :through and get array parameters
|
111
|
-
has_many_update_through is helpful method for has_many_update and use it, you can use has_many_update
|
112
|
-
here too, and to do without has_many_update_through
|
113
|
-
|
114
|
-
For example:
|
115
|
-
|
38
|
+
In this case you have `has_many_update` method:
|
116
39
|
```ruby
|
117
|
-
|
118
|
-
belongs_to :user
|
119
|
-
belongs_to :company
|
120
|
-
|
121
|
-
class Company < ActiveRecord::Base
|
122
|
-
has_many :users, :through => :user_company
|
123
|
-
has_many :user_company
|
124
|
-
|
125
|
-
acts_has_many :users, :through => true
|
126
|
-
end
|
127
|
-
|
128
|
-
class User < ActiveRecord::Base
|
129
|
-
has_many :user_company, :dependent => :destroy
|
130
|
-
has_many :companies, :through => :user_company
|
131
|
-
end
|
132
|
-
|
133
|
-
new_rows, delete_ids = Company.has_many_update_through( update: data, new: date, relation: :users)
|
134
|
-
|
135
|
-
user.companies = new_rows # update user companies
|
136
|
-
|
137
|
-
Company.delete(delete_ids) # for delete_ids from has_many_update_through best way is to use "delete" and miss unnecessary check
|
40
|
+
Company.first.has_many_update {title: 'Google'}
|
138
41
|
```
|
139
|
-
|
140
|
-
### Use acts_has_many_for with acts_has_many
|
141
|
-
When use acts_has_many_for you can user attributes
|
42
|
+
if you use `acts_has_many` with `through: true` paramters:
|
142
43
|
```ruby
|
44
|
+
new_records, delete_ids = Company.has_many_through_update(update: data, new: date)
|
45
|
+
```
|
143
46
|
|
144
|
-
|
145
|
-
|
146
|
-
|
47
|
+
5. If you use `acts_has_many` with `acts_has_many_for`
|
48
|
+
```ruby
|
49
|
+
class User < ActiveRecord::Base
|
50
|
+
belongs_to :company, dependent: :destroy
|
147
51
|
|
148
|
-
|
52
|
+
acts_has_many_for :company
|
53
|
+
end
|
149
54
|
|
150
|
-
|
55
|
+
class Company < ActiveRecord::Base
|
56
|
+
has_many :users
|
151
57
|
|
152
|
-
|
153
|
-
|
58
|
+
acts_has_many :users
|
59
|
+
end
|
60
|
+
```
|
61
|
+
In this case you can use the same that is in 4-th point and also:
|
62
|
+
```ruby
|
63
|
+
User.first.company_attributes = {title: 'Google'}
|
64
|
+
```
|
65
|
+
if you use `acts_has_many` with `through: true` paramters
|
66
|
+
```ruby
|
67
|
+
User.first.companies_collection = [{title: 'Google'}, {title: 'Apple'}]
|
68
|
+
```
|
154
69
|
|
155
|
-
|
70
|
+
More
|
71
|
+
----
|
72
|
+
`acts_has_many` options:
|
73
|
+
>* list relations or after necessary relations
|
74
|
+
>* :compare( string or symbol; default: :title) - name column with unique elements in table
|
75
|
+
>* :through( boolean; default: false) - if you use has_many :through
|
156
76
|
|
157
|
-
|
77
|
+
`acts_has_many_for` options:
|
78
|
+
>* list necessary relations
|
158
79
|
|
159
|
-
|
80
|
+
`has_many_update` options:
|
81
|
+
>* data: data for update
|
160
82
|
|
161
|
-
|
83
|
+
`has_many_through_update` options:
|
84
|
+
>* :update - array data, data include :id record for update
|
85
|
+
>* :new - array data for new record
|
162
86
|
|
163
|
-
|
87
|
+
`<relation>_attributes` options:
|
88
|
+
>* data - Hash (other data use standart way)
|
164
89
|
|
165
|
-
|
166
|
-
|
90
|
+
`<relation>_collection` options:
|
91
|
+
>* data - Array (Records, Hash, Empty)
|
167
92
|
|
168
|
-
|
93
|
+
additionl
|
94
|
+
>* `depend_relations` - show depend relations(Array)
|
95
|
+
>* `actual?` - check actuality(Boolean)
|
96
|
+
>* `compare` - return compare column(String)
|
97
|
+
>* `destroy!` - standart destroy
|
98
|
+
>* `destroy` - destroy with cheking actuality record
|
169
99
|
|
170
100
|
Contributing
|
171
101
|
------------
|
data/acts_has_many.gemspec
CHANGED
@@ -19,6 +19,7 @@ Gem::Specification.new do |gem|
|
|
19
19
|
gem.require_paths = ['lib']
|
20
20
|
|
21
21
|
gem.add_development_dependency 'bundler'
|
22
|
+
gem.add_development_dependency 'rake'
|
22
23
|
gem.add_development_dependency 'rspec'
|
23
24
|
gem.add_development_dependency 'sqlite3'
|
24
25
|
gem.add_development_dependency 'activerecord'
|
@@ -1,96 +1,62 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
module ActsHasMany
|
3
3
|
|
4
|
-
#
|
5
|
-
#
|
6
|
-
# +has_many_through_update+
|
4
|
+
# Class methods:
|
5
|
+
# <tt>has_many_through_update</tt>
|
7
6
|
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
|
7
|
+
# Instance methods:
|
8
|
+
# <tt>has_many_update</tt>
|
9
|
+
# <tt>actual?</tt>
|
10
|
+
# <tt>destroy</tt>
|
11
|
+
# <tt>destroy!</tt>
|
13
12
|
module Child
|
14
13
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
# update_with_<relation>(data)
|
19
|
-
|
20
|
-
base.dependent_relations.each do |relation|
|
21
|
-
define_method("update_with_#{relation}") do |data|
|
22
|
-
has_many_update data, relation
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
#
|
28
|
-
# +has_many_update+ (return: array) - [new_record, delete_record]
|
29
|
-
# options: (Hash is deprecated, list parameters)
|
30
|
-
# data (type: hash) - data for updte
|
31
|
-
# relation (type: str, symbol) - current relation for check
|
32
|
-
#
|
33
|
-
|
34
|
-
def has_many_update data, relation
|
14
|
+
# <tt>has_many_update</tt> (return: array) - [new_record, delete_record]
|
15
|
+
# options: data (type: hash) - data for updte
|
16
|
+
def has_many_update data
|
35
17
|
data = { model.compare => ''}.merge data
|
36
18
|
|
37
|
-
|
38
|
-
warn "[ARRGUMENT MISSING]: 'has_many_update' don't know about current relation, and check all relations"
|
39
|
-
end
|
40
|
-
|
41
|
-
has_many_cleaner data.symbolize_keys, relation
|
19
|
+
has_many_cleaner data.symbolize_keys
|
42
20
|
end
|
43
21
|
|
44
|
-
#
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
22
|
+
# destroy with check actuality
|
23
|
+
def destroy
|
24
|
+
return false if actual? false
|
25
|
+
super
|
26
|
+
end
|
49
27
|
|
50
|
-
|
51
|
-
|
28
|
+
# original destroy
|
29
|
+
def destroy!
|
30
|
+
self.class.superclass.instance_method(:destroy).bind(self).call
|
31
|
+
end
|
52
32
|
|
53
|
-
|
33
|
+
# <tt>actual?</tt> - check the acutuality of element in has_many table
|
34
|
+
# options: exclude (boolean, default: true) - ignore one record or no
|
35
|
+
def actual? exclude = true
|
36
|
+
actual = 0
|
54
37
|
model.dependent_relations.each do |dependent_relation|
|
55
38
|
tmp = self.send dependent_relation
|
56
|
-
|
57
|
-
actuale ||= tmp.all.size > 1
|
58
|
-
else
|
59
|
-
actuale ||= tmp.exists?
|
60
|
-
end
|
39
|
+
actual += tmp.all.size
|
61
40
|
end
|
62
|
-
|
41
|
+
|
42
|
+
exclude ? actual > 1 : actual > 0
|
63
43
|
end
|
64
44
|
end
|
65
45
|
|
66
46
|
private
|
67
47
|
|
68
|
-
#
|
69
|
-
|
70
|
-
#
|
71
|
-
|
72
|
-
def destroy_filter
|
73
|
-
not actuale?
|
74
|
-
end
|
75
|
-
|
76
|
-
#
|
77
|
-
# +has_many_cleaner+ - base mothod
|
78
|
-
#
|
79
|
-
|
80
|
-
def has_many_cleaner data, relation
|
48
|
+
# <tt>has_many_cleaner</tt> - base mothod
|
49
|
+
def has_many_cleaner data
|
81
50
|
compare = { model.compare => data[model.compare] }
|
82
51
|
|
83
52
|
new_record = self
|
84
53
|
del_record = nil
|
85
54
|
|
86
|
-
if
|
55
|
+
if actual?
|
87
56
|
new_record = model.where(compare).first_or_create data
|
88
57
|
else
|
89
58
|
object_tmp = model.where(compare).first
|
90
|
-
|
91
|
-
del_record = (new_record.id == object_tmp.id) ? nil : new_record
|
92
|
-
new_record = object_tmp
|
93
|
-
else
|
59
|
+
if object_tmp.nil?
|
94
60
|
if new_record.id.nil?
|
95
61
|
new_record = model.where(compare).first_or_create data
|
96
62
|
else
|
@@ -100,6 +66,9 @@ module ActiveRecord
|
|
100
66
|
update_attributes data
|
101
67
|
end
|
102
68
|
end
|
69
|
+
else
|
70
|
+
del_record = (new_record.id == object_tmp.id) ? nil : new_record
|
71
|
+
new_record = object_tmp
|
103
72
|
end
|
104
73
|
end
|
105
74
|
[new_record, del_record]
|
@@ -108,21 +77,18 @@ module ActiveRecord
|
|
108
77
|
|
109
78
|
module ChildThrough
|
110
79
|
|
111
|
-
#
|
112
|
-
# +has_many_through_update+ (return array) [ 1 - array new records, 2 - array delete records ]
|
80
|
+
# <tt>has_many_through_update</tt> (return array) [ 1 - array new records, 2 - array delete records ]
|
113
81
|
# options
|
114
82
|
# :update (array) - data for update (id and data)
|
115
83
|
# :new (array) - data for create record (data)
|
116
84
|
#
|
117
85
|
# +for delete records need use method destroy !!!+
|
118
|
-
#
|
119
|
-
|
120
86
|
def has_many_through_update(options)
|
121
87
|
record_add = []
|
122
88
|
record_del = []
|
123
89
|
|
124
90
|
options[:update].each do |id, data|
|
125
|
-
add, del = find(id).has_many_update data
|
91
|
+
add, del = find(id).has_many_update data
|
126
92
|
record_add << add unless add.nil?
|
127
93
|
record_del << del unless del.nil?
|
128
94
|
end unless options[:update].nil?
|
@@ -1,35 +1,24 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
module ActsHasMany
|
3
3
|
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# or
|
8
|
-
# +<relation>_collection=+
|
9
|
-
#
|
10
|
-
|
4
|
+
# Class methods:
|
5
|
+
# <tt><relation>_attributes=</tt>
|
6
|
+
# <tt><relation>_collection=</tt>
|
11
7
|
module Parent
|
12
8
|
|
13
9
|
def self.included base
|
14
10
|
|
15
|
-
#
|
16
11
|
# Generate class methods
|
17
|
-
#
|
18
|
-
# options:
|
19
|
-
# data (type: Hash, existing recod, NilClass)
|
12
|
+
# <tt><relation>_attributes=</tt> - set one element (use with belogns_to)
|
13
|
+
# options: data (type: Hash, existing recod, NilClass)
|
20
14
|
#
|
21
15
|
# Create or update child record and set to parent when `data` is Hash
|
22
16
|
# Change data with delating or leaving child record when `data` is exists record, or NilClass
|
23
17
|
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
# +<relation>_collection=+ - set many elements (use with has_many :through)
|
27
|
-
# options:
|
28
|
-
# data (type: Array with (exist records or Hash)
|
18
|
+
# <tt><relation>_collection=</tt> - set many elements (use with has_many :through)
|
19
|
+
# options: data (type: Array with (exist records or Hash)
|
29
20
|
#
|
30
21
|
# Create or select exists records and set to parent, unused record try destory
|
31
|
-
#
|
32
|
-
|
33
22
|
base.dependent_relations.each do |relation|
|
34
23
|
unless base.reflect_on_association relation.to_sym
|
35
24
|
raise ArgumentError, "No association found for name `#{relation}`. Has it been defined yet?"
|
@@ -37,16 +26,16 @@ module ActiveRecord
|
|
37
26
|
|
38
27
|
relation = relation.to_s
|
39
28
|
unless base.reflect_on_association(relation.to_sym).collection?
|
40
|
-
base.class_eval <<-EOV, __FILE__ , __LINE__
|
29
|
+
base.class_eval <<-EOV, __FILE__ , __LINE__ + 1
|
41
30
|
def #{relation}_attributes= data
|
42
31
|
self.tmp_garbage ||= {}
|
43
32
|
current = self.#{relation}
|
44
33
|
|
45
34
|
if data.is_a? Hash
|
46
35
|
if current
|
47
|
-
new, del = current.has_many_update data
|
36
|
+
new, del = current.has_many_update data
|
48
37
|
else
|
49
|
-
new, del = #{relation.classify}.new.has_many_update data
|
38
|
+
new, del = #{relation.classify}.new.has_many_update data
|
50
39
|
end
|
51
40
|
|
52
41
|
self.#{relation} = new
|
@@ -58,13 +47,13 @@ module ActiveRecord
|
|
58
47
|
end
|
59
48
|
EOV
|
60
49
|
else
|
61
|
-
base.class_eval <<-EOV, __FILE__ , __LINE__
|
50
|
+
base.class_eval <<-EOV, __FILE__ , __LINE__ + 1
|
62
51
|
def #{relation}_collection= data
|
63
52
|
self.tmp_garbage ||= {}
|
64
53
|
|
65
54
|
if data.is_a? Array
|
66
55
|
if data.first.is_a? Hash
|
67
|
-
new, del = #{relation.classify}.has_many_through_update new: data
|
56
|
+
new, del = #{relation.classify}.has_many_through_update new: data
|
68
57
|
elsif data.first.is_a? #{relation.classify}
|
69
58
|
new, del = data, []
|
70
59
|
elsif data.empty?
|
@@ -87,10 +76,7 @@ module ActiveRecord
|
|
87
76
|
|
88
77
|
private
|
89
78
|
|
90
|
-
#
|
91
|
-
# +clear_garbage+ work with after_save and try destroied records from tmp_garbage
|
92
|
-
#
|
93
|
-
|
79
|
+
# <tt>clear_garbage</tt> work with after_save and try destroied records from tmp_garbage
|
94
80
|
def clear_garbage
|
95
81
|
self.tmp_garbage.each do |relation, record|
|
96
82
|
if record.is_a? Array
|
@@ -1,34 +1,25 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
module ActsHasMany
|
3
3
|
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# +acts_has_many+
|
8
|
-
#
|
9
|
-
|
4
|
+
# Class methods: (for use in your model)
|
5
|
+
# <tt>acts_has_many_for</tt>
|
6
|
+
# <tt>acts_has_many</tt>
|
10
7
|
def self.included base
|
11
8
|
base.extend ClassMethods
|
12
9
|
end
|
13
10
|
|
14
11
|
module ClassMethods
|
15
12
|
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
# +has_many_update+
|
25
|
-
# +update_with_<relation>+
|
26
|
-
#
|
13
|
+
# Class methods:
|
14
|
+
# <tt>dependent_relations</tt>
|
15
|
+
# <tt>compare</tt>
|
16
|
+
# <tt>model</tt>
|
17
|
+
# <tt>has_many_through_update</tt>
|
18
|
+
# Instance mothods:
|
19
|
+
# <tt>actual?</tt>
|
20
|
+
# <tt>has_many_update</tt>
|
27
21
|
# Set:
|
28
22
|
# validates for <compare_element> (uniqueness: true, presence: true)
|
29
|
-
# before destroy filter
|
30
|
-
#
|
31
|
-
|
32
23
|
def acts_has_many *opt
|
33
24
|
options = { compare: :title, through: false }
|
34
25
|
options.update opt.extract_options!
|
@@ -47,7 +38,7 @@ module ActiveRecord
|
|
47
38
|
end
|
48
39
|
end
|
49
40
|
|
50
|
-
class_eval <<-EOV, __FILE__ , __LINE__
|
41
|
+
class_eval <<-EOV, __FILE__ , __LINE__ + 1
|
51
42
|
def self.dependent_relations
|
52
43
|
#{dependent_relations}
|
53
44
|
end
|
@@ -64,26 +55,19 @@ module ActiveRecord
|
|
64
55
|
end
|
65
56
|
|
66
57
|
validates :#{options[:compare]}, uniqueness: true, presence: true
|
67
|
-
before_destroy :destroy_filter
|
68
58
|
EOV
|
69
59
|
end
|
70
60
|
|
71
|
-
#
|
72
|
-
#
|
73
|
-
#
|
74
|
-
#
|
75
|
-
# +<relation>_attributes=+
|
76
|
-
# or
|
77
|
-
# +<relation>_collection=+
|
78
|
-
#
|
61
|
+
# Class methods:
|
62
|
+
# <tt>dependent_relations</tt>
|
63
|
+
# <tt><relation>_attributes=</tt>
|
64
|
+
# <tt><relation>_collection=</tt>
|
79
65
|
# Set:
|
80
66
|
# after save filter
|
81
67
|
# attribut accessor tmp_garbage
|
82
|
-
#
|
83
|
-
|
84
68
|
def acts_has_many_for *relations
|
85
|
-
class_eval <<-EOV, __FILE__ , __LINE__
|
86
|
-
def self.dependent_relations
|
69
|
+
class_eval <<-EOV, __FILE__ , __LINE__ + 1
|
70
|
+
def self.dependent_relations
|
87
71
|
#{relations}
|
88
72
|
end
|
89
73
|
|
data/spec/has_many_spec.rb
CHANGED
@@ -16,17 +16,17 @@ describe 'acts_has_many' do
|
|
16
16
|
Experience.delete_all
|
17
17
|
end
|
18
18
|
|
19
|
-
it 'has_many_update data
|
20
|
-
add_loc, del_loc = experience.location.has_many_update({"title" => "ukraine"}
|
19
|
+
it 'has_many_update data' do
|
20
|
+
add_loc, del_loc = experience.location.has_many_update({"title" => "ukraine"})
|
21
21
|
|
22
22
|
expect(Location.all.size).to be 1
|
23
23
|
expect(location).to eq add_loc
|
24
24
|
expect(del_loc).to be nil
|
25
25
|
expect(experience.location.title).to eq "ukraine"
|
26
26
|
|
27
|
-
Experience.create :location => location, :title => "test experience2"
|
27
|
+
Experience.create :location => location, :title => "test experience2"
|
28
28
|
|
29
|
-
add_loc, del_loc = experience.location.has_many_update({"title" => "italy"}
|
29
|
+
add_loc, del_loc = experience.location.has_many_update({"title" => "italy"})
|
30
30
|
|
31
31
|
expect(Location.all.size).to be 2
|
32
32
|
expect(location).not_to eq add_loc
|
@@ -35,32 +35,7 @@ describe 'acts_has_many' do
|
|
35
35
|
experience.location = Location.find(add_loc)
|
36
36
|
expect(experience.location.title).to eq "italy"
|
37
37
|
|
38
|
-
add_loc, del_loc = experience.location.has_many_update({"title" => "ukraine"}
|
39
|
-
|
40
|
-
expect(location).to eq add_loc
|
41
|
-
expect(experience.location).to eq del_loc
|
42
|
-
end
|
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"})
|
38
|
+
add_loc, del_loc = experience.location.has_many_update({"title" => "ukraine"})
|
64
39
|
|
65
40
|
expect(location).to eq add_loc
|
66
41
|
expect(experience.location).to eq del_loc
|
@@ -123,32 +98,39 @@ describe 'acts_has_many' do
|
|
123
98
|
Location.all.size.should == 1
|
124
99
|
|
125
100
|
Experience.all[0].destroy
|
126
|
-
location.destroy
|
127
101
|
Location.all.size.should == 0
|
128
|
-
end
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'destroy' do
|
105
|
+
Location.delete_all
|
106
|
+
Experience.delete_all
|
107
|
+
|
108
|
+
location = Location.create(:title => "ukraine")
|
109
|
+
|
110
|
+
Experience.create( :location => location, :title => "test" )
|
111
|
+
|
112
|
+
location.destroy!
|
113
|
+
Location.all.size.should == 0
|
114
|
+
end
|
129
115
|
|
130
|
-
it '
|
116
|
+
it 'actual?' do
|
131
117
|
Location.delete_all
|
132
118
|
|
133
119
|
location = Location.create(:title => "ukraine")
|
134
120
|
|
135
|
-
location.
|
136
|
-
location.
|
121
|
+
location.actual?.should == false
|
122
|
+
location.actual?(false).should == false
|
137
123
|
|
138
124
|
Experience.create( :title => 'test', :location => location )
|
139
125
|
|
140
|
-
location.
|
141
|
-
location.
|
142
|
-
location.actuale?(:experiences).should == false
|
143
|
-
location.actuale?("Experience").should == false
|
144
|
-
location.actuale?("Experience").should == false
|
126
|
+
location.actual?.should == false
|
127
|
+
location.actual?(false).should == true
|
145
128
|
|
146
129
|
Experience.create( :title => 'test', :location => location )
|
147
130
|
|
148
|
-
location.
|
149
|
-
location.
|
150
|
-
|
151
|
-
end
|
131
|
+
location.actual?.should == true
|
132
|
+
location.actual?(false).should == true
|
133
|
+
end
|
152
134
|
|
153
135
|
it 'compare' do
|
154
136
|
Location.compare.should == :title
|
@@ -69,7 +69,7 @@ describe 'acts_has_many with :through' do
|
|
69
69
|
{ title: 'test2'},
|
70
70
|
{ title: 'test3'},
|
71
71
|
{ title: 'test0'},
|
72
|
-
]
|
72
|
+
])
|
73
73
|
|
74
74
|
expect(new_records.count).to be 4
|
75
75
|
expect(del_records).to eq []
|
@@ -83,7 +83,7 @@ describe 'acts_has_many with :through' do
|
|
83
83
|
:update => {
|
84
84
|
new_records[2].id => {title: 'test2s'},
|
85
85
|
new_records[3].id.to_s => {'title' => 'test3s'}
|
86
|
-
}
|
86
|
+
})
|
87
87
|
|
88
88
|
expect(new_records_1.map(&:id).sort!).to eq new_records.map(&:id)
|
89
89
|
expect(Local.find(new_records[2].id).title).to eq 'test2s'
|
@@ -91,14 +91,14 @@ describe 'acts_has_many with :through' do
|
|
91
91
|
expect(Local.all.size).to be 4
|
92
92
|
|
93
93
|
|
94
|
-
new, del = Local.has_many_through_update(
|
94
|
+
new, del = Local.has_many_through_update(
|
95
95
|
:new => [
|
96
96
|
{ title: 'test0'},
|
97
97
|
{ title: 'test3s'}],
|
98
98
|
:update => {
|
99
99
|
new_records_1[2].id => {title: 'test2s'},
|
100
100
|
new_records_1[3].id => {title: ''}
|
101
|
-
}
|
101
|
+
})
|
102
102
|
|
103
103
|
expect(del.size).to be 1
|
104
104
|
expect(new.size).to be 3
|
data/spec/helper.rb
CHANGED
@@ -19,21 +19,21 @@ ActiveRecord::Schema.define(:version => 1) do
|
|
19
19
|
create_table :experiences do |t|
|
20
20
|
t.string :title
|
21
21
|
t.references :location
|
22
|
-
end
|
22
|
+
end
|
23
23
|
create_table :locations do |t|
|
24
24
|
t.string :title
|
25
|
-
end
|
26
|
-
|
25
|
+
end
|
26
|
+
|
27
27
|
create_table :locals do |t|
|
28
28
|
t.string :title
|
29
|
-
end
|
29
|
+
end
|
30
30
|
create_table :company_locals do |t|
|
31
31
|
t.references :local
|
32
32
|
t.references :company
|
33
|
-
end
|
33
|
+
end
|
34
34
|
create_table :companies do |t|
|
35
35
|
t.string :title
|
36
|
-
end
|
36
|
+
end
|
37
37
|
end
|
38
38
|
|
39
39
|
class Experience < ActiveRecord::Base
|
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.3.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:
|
12
|
+
date: 2013-03-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -27,6 +27,22 @@ dependencies:
|
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
30
46
|
- !ruby/object:Gem::Dependency
|
31
47
|
name: rspec
|
32
48
|
requirement: !ruby/object:Gem::Requirement
|
@@ -81,6 +97,7 @@ executables: []
|
|
81
97
|
extensions: []
|
82
98
|
extra_rdoc_files: []
|
83
99
|
files:
|
100
|
+
- .travis.yml
|
84
101
|
- Gemfile
|
85
102
|
- LICENSE
|
86
103
|
- README.md
|
@@ -115,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
115
132
|
version: '0'
|
116
133
|
requirements: []
|
117
134
|
rubyforge_project:
|
118
|
-
rubygems_version: 1.8.
|
135
|
+
rubygems_version: 1.8.25
|
119
136
|
signing_key:
|
120
137
|
specification_version: 3
|
121
138
|
summary: All records must be used, otherwise they will be deleted. Clear logic with
|