acts_has_many 0.3.3 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +37 -78
- data/acts_has_many.gemspec +1 -1
- data/lib/acts_has_many/active_record/acts_has_many/child.rb +1 -1
- data/lib/acts_has_many/version.rb +1 -1
- data/spec/has_many_common.rb +2 -2
- metadata +18 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4be5faf9f25faf7eb2a05a645da6b8be3addb69a
|
4
|
+
data.tar.gz: c09f0e97267de785247bd3f55ed9dc8038c0ec89
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 68219717483de612402fef8b5bc9a707206b7f98063d02859ef59d16a086753a9a3bc4805203d0a568339f323513e0b8cbffca1b6122ade1e22bdfd5c12500fd
|
7
|
+
data.tar.gz: 98dd7e5cf2958f2cc0d8ecbcb929b2f7b1a8f9d1472b875cdfecac1dc513e411a5646a971650d83d082cf3930a12da44f708c5fe4a5796b8d4d276a3c8d0d030
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
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
|
4
|
-
|
5
|
-
|
3
|
+
Acts_has_many gem gives functional for useing common resources with `has_many` relation
|
4
|
+
or `has_many :trhough`. The aim is common using records with has_many relation without duplications
|
5
|
+
and gerbage, every erecord will be used
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -19,37 +19,18 @@ Or install it yourself as:
|
|
19
19
|
$ gem install acts_has_many
|
20
20
|
|
21
21
|
## Usage
|
22
|
-
1. To initialize gem you
|
23
|
-
2. In model where you set `acts_has_many` will work clearly (all records used, no duplications)
|
24
|
-
3. Should use `dependent: :destroy`
|
25
|
-
|
26
|
-
4. If only `acts_has_many` is used:
|
22
|
+
1. To initialize gem you should add `acts_has_many` to your common resource model
|
27
23
|
|
28
24
|
```ruby
|
29
|
-
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
class Tag < ActiveRecord::Base
|
34
|
-
has_many :postings
|
25
|
+
class Tag < ActiveRecord::Base
|
26
|
+
has_many :postings
|
35
27
|
|
36
|
-
|
37
|
-
|
28
|
+
acts_has_many :postings
|
29
|
+
end
|
38
30
|
```
|
39
31
|
|
40
|
-
|
32
|
+
2. And add `acts_has_many_for` to model which will use common resource like:
|
41
33
|
|
42
|
-
```ruby
|
43
|
-
new_record, delete_record = Tag.first.has_many_update {title: 'ruby'}
|
44
|
-
```
|
45
|
-
|
46
|
-
if you use `acts_has_many` with `through: true` parameters:
|
47
|
-
|
48
|
-
```ruby
|
49
|
-
new_records, delete_ids = Tag.has_many_through_update(update: data, new: date)
|
50
|
-
```
|
51
|
-
|
52
|
-
5. If you use `acts_has_many` with `acts_has_many_for`
|
53
34
|
|
54
35
|
```ruby
|
55
36
|
class Posting < ActiveRecord::Base
|
@@ -57,66 +38,32 @@ Or install it yourself as:
|
|
57
38
|
|
58
39
|
acts_has_many_for :tag
|
59
40
|
end
|
60
|
-
|
61
|
-
class Tag < ActiveRecord::Base
|
62
|
-
has_many :postings
|
63
|
-
|
64
|
-
acts_has_many :postings
|
65
|
-
end
|
66
41
|
```
|
67
42
|
|
68
|
-
|
43
|
+
3. Then you could use new functionality
|
69
44
|
|
70
45
|
```ruby
|
71
46
|
Posting.first.tag_attributes = {title: 'ruby'}
|
72
47
|
```
|
73
48
|
|
74
|
-
|
49
|
+
in case with relation `has_many through` you could write next:
|
75
50
|
|
76
51
|
```ruby
|
77
52
|
Posting.first.tags_collection = [{title: 'ruby'}, {title: 'python'}]
|
78
53
|
```
|
79
54
|
|
80
|
-
New in v0.3.2
|
81
|
-
--------------
|
82
|
-
|
83
|
-
Use `block` to change condition, default search is `where :compare => :value`
|
84
|
-
|
85
|
-
```ruby
|
86
|
-
class Tag < ActiveRecord::Base
|
87
|
-
has_many :postings
|
88
|
-
|
89
|
-
acts_has_many :postings do |params|
|
90
|
-
where arel_table[:title].matches(params[:title])
|
91
|
-
end
|
92
|
-
end
|
93
|
-
```
|
94
|
-
|
95
|
-
Replace `compare` method to `condition`
|
96
|
-
|
97
|
-
Notice, if block is defined:
|
98
|
-
>* `:compare` argument will be ignored
|
99
|
-
>* auto `validates :compare, uniqueness: true` is off
|
100
|
-
|
101
|
-
|
102
55
|
More
|
103
56
|
----
|
104
57
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
58
|
+
`acts_has_many` options:
|
59
|
+
>* list relations or after necessary relations
|
60
|
+
>* :compare (string or symbol; default: :title) - name column with unique elements in table
|
61
|
+
>* :through (boolean; default: false) - if you use has_many :through
|
62
|
+
>* &block(should return ActiveRecord::Relation; by default :compare option is used)
|
63
|
+
>* example: do |params| where arel_table[:title].matches(params[:title]) end
|
110
64
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
`has_many_update` options:
|
115
|
-
>* data: data for update
|
116
|
-
|
117
|
-
`has_many_through_update` options:
|
118
|
-
>* :update - array data, data include :id record for update
|
119
|
-
>* :new - array data for new record
|
65
|
+
`acts_has_many_for` options:
|
66
|
+
>* list necessary relations
|
120
67
|
|
121
68
|
`<relation>_attributes` options:
|
122
69
|
>* data - Hash (other data use standart way)
|
@@ -124,6 +71,13 @@ More
|
|
124
71
|
`<relation>_collection` options:
|
125
72
|
>* data - Array (Records, Hash, Empty)
|
126
73
|
|
74
|
+
`has_many_update` options:
|
75
|
+
>* data: data for update
|
76
|
+
|
77
|
+
`has_many_through_update` options:
|
78
|
+
>* :update - array data, data include :id record for update
|
79
|
+
>* :new - array data for new record
|
80
|
+
|
127
81
|
Additional
|
128
82
|
>* `depend_relations` - show depend relations(Array)
|
129
83
|
>* `actual?` - check actuality(Boolean)
|
@@ -152,23 +106,26 @@ Use with `has_manay`:
|
|
152
106
|
tag_attributes: {name: 'ruby'}
|
153
107
|
|
154
108
|
posting.tag # => #<Tag id: 1, title: "ruby">
|
155
|
-
Tag.all
|
109
|
+
Tag.all # => [#<Tag id: 1, title: "ruby">]
|
156
110
|
|
157
111
|
posting = Posting.create title: 'Second posting',
|
158
112
|
tag_attributes: {name: 'ruby'}
|
159
113
|
|
114
|
+
#NO DUPLICATIONS
|
160
115
|
posting.tag # => #<Tag id: 1, title: "ruby">
|
161
|
-
Tag.all
|
116
|
+
Tag.all # => [#<Tag id: 1, title: "ruby">]
|
162
117
|
|
163
118
|
posting.update_attributes tag_attributes: {name: 'python'}
|
164
119
|
|
120
|
+
#COORRECT UPDATING
|
165
121
|
posting.tag # => #<Tag id: 2, title: "python">
|
166
|
-
Tag.all
|
122
|
+
Tag.all # => [#<Tag id: 1, title: "ruby">, #<Tag id: 2, title: "python">]
|
167
123
|
|
168
124
|
posting.tag_attributes = Tag.first
|
169
125
|
posting.save
|
170
126
|
|
171
|
-
|
127
|
+
#NO UNUSED RECORDS
|
128
|
+
Tag.all # => [#<Tag id: 1, title: "ruby">]
|
172
129
|
```
|
173
130
|
|
174
131
|
Use with `has_many :through`
|
@@ -211,13 +168,15 @@ Use with `has_many :through`
|
|
211
168
|
|
212
169
|
posting.update_attributes tags_collection: [Tag.first]
|
213
170
|
|
214
|
-
posting.tags
|
171
|
+
posting.tags
|
172
|
+
# => [#<Tag id: 2, title: "ruby">]
|
215
173
|
Tag.all
|
216
174
|
# => [#<Tag id: 1, title: "ruby">, #<Tag id: 2, title: "python">]
|
217
175
|
|
218
176
|
Posting.first.destroy
|
219
177
|
|
220
|
-
Tag.all
|
178
|
+
Tag.all
|
179
|
+
# => [#<Tag id: 1, title: "ruby">]
|
221
180
|
```
|
222
181
|
|
223
182
|
Contributing
|
data/acts_has_many.gemspec
CHANGED
@@ -22,6 +22,6 @@ Gem::Specification.new do |gem|
|
|
22
22
|
gem.add_development_dependency 'rake', '~> 10'
|
23
23
|
gem.add_development_dependency 'rspec', '~> 2.14'
|
24
24
|
gem.add_development_dependency 'sqlite3', '~> 1.3'
|
25
|
-
gem.add_development_dependency 'activerecord', '~>
|
25
|
+
gem.add_development_dependency 'activerecord', '~> 4.2'
|
26
26
|
end
|
27
27
|
|
@@ -27,7 +27,7 @@ module ActiveRecord
|
|
27
27
|
def actual? exclude = true
|
28
28
|
actual = 0
|
29
29
|
self.class.dependent_relations.each do |dependent_relation|
|
30
|
-
actual += self.send(dependent_relation).
|
30
|
+
actual += self.send(dependent_relation).count
|
31
31
|
end
|
32
32
|
|
33
33
|
exclude ? actual > 1 : actual > 0
|
data/spec/has_many_common.rb
CHANGED
@@ -23,7 +23,7 @@ shared_examples_for 'acts_has_many' do
|
|
23
23
|
expect(location).not_to eq add_loc
|
24
24
|
expect(del_loc).to be nil
|
25
25
|
|
26
|
-
experience.location = Location.find(add_loc)
|
26
|
+
experience.location = Location.find(add_loc.id)
|
27
27
|
expect(experience.location.title).to eq "italy"
|
28
28
|
|
29
29
|
add_loc, del_loc = experience.location.has_many_update({"title" => "ukraine"})
|
@@ -35,7 +35,7 @@ shared_examples_for 'acts_has_many' do
|
|
35
35
|
it "parent.child_attributes= Hash" do
|
36
36
|
experience = Experience.create location_attributes: {title: "ukraine"}, title: "test experience2"
|
37
37
|
|
38
|
-
Location.all.size.
|
38
|
+
expect(Location.all.size).to be 1
|
39
39
|
experience.location.title.should eq "ukraine"
|
40
40
|
|
41
41
|
Experience.create location_attributes: {title: "ukraine"}, title: "test experience2"
|
metadata
CHANGED
@@ -1,92 +1,92 @@
|
|
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.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Igor IS04
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.5'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.5'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '10'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ~>
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - ~>
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '2.14'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - ~>
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '2.14'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: sqlite3
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - ~>
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '1.3'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - ~>
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '1.3'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: activerecord
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - ~>
|
73
|
+
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
75
|
+
version: '4.2'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - ~>
|
80
|
+
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
82
|
+
version: '4.2'
|
83
83
|
description: This gem gives functional for update elements has_many relation
|
84
84
|
email: igor.s04g@gmail.com
|
85
85
|
executables: []
|
86
86
|
extensions: []
|
87
87
|
extra_rdoc_files: []
|
88
88
|
files:
|
89
|
-
- .travis.yml
|
89
|
+
- ".travis.yml"
|
90
90
|
- Gemfile
|
91
91
|
- LICENSE
|
92
92
|
- README.md
|
@@ -112,17 +112,17 @@ require_paths:
|
|
112
112
|
- lib
|
113
113
|
required_ruby_version: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
|
-
- -
|
115
|
+
- - ">="
|
116
116
|
- !ruby/object:Gem::Version
|
117
117
|
version: '0'
|
118
118
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
119
|
requirements:
|
120
|
-
- -
|
120
|
+
- - ">="
|
121
121
|
- !ruby/object:Gem::Version
|
122
122
|
version: '0'
|
123
123
|
requirements: []
|
124
124
|
rubyforge_project:
|
125
|
-
rubygems_version: 2.
|
125
|
+
rubygems_version: 2.5.1
|
126
126
|
signing_key:
|
127
127
|
specification_version: 4
|
128
128
|
summary: All records must be used, otherwise they will be deleted. Clear logic with
|