listable_collections 5.1.0 → 5.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +22 -19
- data/Rakefile +1 -0
- data/lib/listable_collections/builder.rb +33 -14
- data/lib/listable_collections/extensions/active_record/base.rb +1 -1
- data/lib/listable_collections/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a930da030243d9469bca798a65dd6ef3ddb4dbe4fd6e1ed685bfe1352d11cf9f
|
4
|
+
data.tar.gz: 30fd2504c7059326fb97bf262daf7909eefc815d74c12257828c0974d32e59b1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 95fbc6c124e898592d753e45c821897f089259edfba8b7a1f988559acf4fc3f79ede7fd757b846a4c868e2e44edb8b7fdfaf5e01d657b90e906f3e2698c30262
|
7
|
+
data.tar.gz: c5bd31e8ae2588e8ac763588de147685fa34da6c7eb5e869c1fd27ccc28886589d67b9296d266c2f88fe90bbcb7d4564b68b265f16690102800ed8054c0a6ee1
|
data/README.md
CHANGED
@@ -9,11 +9,12 @@ Makes collections accessible from a string list in rails.
|
|
9
9
|
|
10
10
|
## Why
|
11
11
|
|
12
|
-
|
12
|
+
We did this gem to:
|
13
13
|
|
14
14
|
- Easily manage collections from input text field.
|
15
15
|
- Track changes like dirty module in activerecord.
|
16
16
|
- Have callbacks like has_many associations.
|
17
|
+
- Automatically handle join models creation/deletion.
|
17
18
|
|
18
19
|
## Install
|
19
20
|
|
@@ -33,28 +34,30 @@ $ bundle
|
|
33
34
|
|
34
35
|
If you want to list a has_many association:
|
35
36
|
```ruby
|
36
|
-
class
|
37
|
+
class Product < ApplicationRecord
|
37
38
|
|
38
|
-
has_many :
|
39
|
+
has_many :tagizations
|
40
|
+
has_many :tags, through: :tagizations
|
39
41
|
|
40
|
-
|
42
|
+
listify :tags, by: :name
|
41
43
|
|
42
44
|
end
|
43
45
|
```
|
44
46
|
|
45
|
-
Associated records
|
47
|
+
Associated records will be synced and chances will be tracked using the following helpers:
|
46
48
|
```ruby
|
47
|
-
|
48
|
-
|
49
|
+
product.tags.map(&:name) => ['New']
|
50
|
+
product.tag_list => 'New'
|
49
51
|
|
50
|
-
|
51
|
-
|
52
|
+
product.tag_list = 'Natural'
|
53
|
+
product.tagizations.reject(&:marked_for_destruction?).map(&:tag).map(&:name) => ['Natural']
|
54
|
+
product.tagizations.select(&:marked_for_destruction?).map(&:tag).map(&:name) => ['New']
|
52
55
|
|
53
|
-
|
54
|
-
|
56
|
+
product.added_tags_to_list => ['Natural']
|
57
|
+
product.removed_tags_from_list => ['New']
|
55
58
|
```
|
56
59
|
|
57
|
-
NOTE:
|
60
|
+
NOTE: The gem will handle automatically creation/deletion if you use a has many through association.
|
58
61
|
|
59
62
|
### Attributes
|
60
63
|
|
@@ -64,7 +67,7 @@ class Product < ActiveRecord::Base
|
|
64
67
|
|
65
68
|
serialize :sizes, Array
|
66
69
|
|
67
|
-
|
70
|
+
listify :sizes
|
68
71
|
|
69
72
|
end
|
70
73
|
```
|
@@ -83,17 +86,17 @@ product.removed_sizes_from_list => ['64GB']
|
|
83
86
|
|
84
87
|
In some cases you may need to run some logic after changes, you can use callbacks for it:
|
85
88
|
```ruby
|
86
|
-
class
|
89
|
+
class Product < ActiveRecord::Base
|
87
90
|
|
88
|
-
|
91
|
+
serialize :sizes, Array
|
89
92
|
|
90
|
-
|
93
|
+
listify :sizes, after_add: :size_added, after_remove: :size_removed
|
91
94
|
|
92
|
-
def
|
95
|
+
def size_added(name)
|
93
96
|
# Some logic
|
94
97
|
end
|
95
98
|
|
96
|
-
def
|
99
|
+
def size_removed(name)
|
97
100
|
# Some logic
|
98
101
|
end
|
99
102
|
|
@@ -104,7 +107,7 @@ end
|
|
104
107
|
|
105
108
|
Any issue, pull request, comment of any kind is more than welcome!
|
106
109
|
|
107
|
-
|
110
|
+
We will mainly ensure compatibility to Rails, AWS, PostgreSQL, Redis, Elasticsearch and FreeBSD.
|
108
111
|
|
109
112
|
## Credits
|
110
113
|
|
data/Rakefile
CHANGED
@@ -29,23 +29,42 @@ module ListableCollections
|
|
29
29
|
define_method "#{name}=" do |value|
|
30
30
|
current_values = send(name).split(',')
|
31
31
|
new_values = value.split(',').reject(&:blank?).map(&:strip)
|
32
|
-
value = ((current_values & new_values) + (new_values - current_values))
|
33
32
|
if current_values.sort != new_values.sort
|
34
|
-
|
35
|
-
|
36
|
-
|
33
|
+
attribute_will_change! name
|
34
|
+
instance_variable_set variable, new_values.join(',')
|
35
|
+
added_values = (new_values - current_values)
|
36
|
+
removed_values = (current_values - new_values)
|
37
|
+
if relation_attribute = options[:by]
|
38
|
+
reflection = self.class.reflections[attribute.to_s]
|
39
|
+
scope = options[:scope]
|
40
|
+
relation = (scope ? send(scope).send(attribute) : reflection.klass)
|
41
|
+
added_values.each do |added_value|
|
42
|
+
send(attribute) << relation.find_or_initialize_by(
|
43
|
+
relation_attribute => added_value
|
44
|
+
)
|
45
|
+
end
|
46
|
+
through = reflection.options[:through]
|
47
|
+
source = reflection.source_reflection.name
|
48
|
+
removed_values.each do |removed_value|
|
49
|
+
send(through).each do |record|
|
50
|
+
if record.send(source).send(relation_attribute) == removed_value
|
51
|
+
record.mark_for_destruction
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
37
55
|
else
|
38
56
|
attribute_will_change! name
|
39
|
-
|
57
|
+
new_value = ((current_values & new_values) + (new_values - current_values))
|
58
|
+
send "#{attribute}=", new_value
|
40
59
|
end
|
41
|
-
if options
|
42
|
-
|
43
|
-
send
|
60
|
+
if add_callback = options[:after_add]
|
61
|
+
added_values.each do |added_value|
|
62
|
+
send add_callback, added_value
|
44
63
|
end
|
45
64
|
end
|
46
|
-
if options
|
47
|
-
|
48
|
-
send
|
65
|
+
if remove_callback = options[:after_remove]
|
66
|
+
removed_values.each do |removed_value|
|
67
|
+
send remove_callback, removed_value
|
49
68
|
end
|
50
69
|
end
|
51
70
|
end
|
@@ -59,12 +78,12 @@ module ListableCollections
|
|
59
78
|
if list = instance_variable_get(variable)
|
60
79
|
list
|
61
80
|
elsif values = send(attribute)
|
62
|
-
if options
|
81
|
+
if relation_attribute = options[:by]
|
63
82
|
values = values.map do |record|
|
64
|
-
record.send
|
83
|
+
record.send relation_attribute
|
65
84
|
end
|
66
85
|
end
|
67
|
-
values.join
|
86
|
+
values.join ','
|
68
87
|
end
|
69
88
|
end
|
70
89
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: listable_collections
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.1.
|
4
|
+
version: 5.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mmontossi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-01-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -87,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
87
87
|
version: '0'
|
88
88
|
requirements: []
|
89
89
|
rubyforge_project:
|
90
|
-
rubygems_version: 2.
|
90
|
+
rubygems_version: 2.7.3
|
91
91
|
signing_key:
|
92
92
|
specification_version: 4
|
93
93
|
summary: Listable collections for rails.
|