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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: d47c9d32ec17aa0389a54c9e415ec1a380792809
4
- data.tar.gz: 3966124268625ebfa2be2455f44415b6ba0070d6
2
+ SHA256:
3
+ metadata.gz: a930da030243d9469bca798a65dd6ef3ddb4dbe4fd6e1ed685bfe1352d11cf9f
4
+ data.tar.gz: 30fd2504c7059326fb97bf262daf7909eefc815d74c12257828c0974d32e59b1
5
5
  SHA512:
6
- metadata.gz: ba45f4d4bd8a4e131314e2204184e20c459b63ed2738df2e38008efc0d82bf889d292d7005290e6b9a220440f0caaffce4b3d5cc31a74661a4748683483c95bc
7
- data.tar.gz: 55858a79e778ea6def559e5d720afec00b745559d31cd9fd266b96e0f4663fee09a34ca4c9c4887c655745e4866f581a3f21a4c697a124a32b28a4e23c82530c
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
- I did this gem to:
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 Shop < ActiveRecord::Base
37
+ class Product < ApplicationRecord
37
38
 
38
- has_many :products
39
+ has_many :tagizations
40
+ has_many :tags, through: :tagizations
39
41
 
40
- list :products, attribute: :name
42
+ listify :tags, by: :name
41
43
 
42
44
  end
43
45
  ```
44
46
 
45
- Associated records won't be touched but changes will be tracked using the following helpers:
47
+ Associated records will be synced and chances will be tracked using the following helpers:
46
48
  ```ruby
47
- shop.products.map(&:name) => ['iPhone']
48
- shop.product_list => 'iPhone'
49
+ product.tags.map(&:name) => ['New']
50
+ product.tag_list => 'New'
49
51
 
50
- shop.product_list = 'iPod,iMac'
51
- shop.products.map(&:name) => ['iPhone']
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
- shop.added_products_to_list => ['iMac']
54
- shop.removed_products_from_list => ['iPod']
56
+ product.added_tags_to_list => ['Natural']
57
+ product.removed_tags_from_list => ['New']
55
58
  ```
56
59
 
57
- NOTE: Is recommended to do this check before save all at once and not dynamically to avoid multiple queries.
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
- list :sizes
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 Shop < ActiveRecord::Base
89
+ class Product < ActiveRecord::Base
87
90
 
88
- has_many :product
91
+ serialize :sizes, Array
89
92
 
90
- list :products, attribute: :name, after_add: :product_added, after_remove: :product_removed
93
+ listify :sizes, after_add: :size_added, after_remove: :size_removed
91
94
 
92
- def product_added(name)
95
+ def size_added(name)
93
96
  # Some logic
94
97
  end
95
98
 
96
- def product_removed(name)
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
- I will mainly ensure compatibility to Rails, AWS, PostgreSQL, Redis, Elasticsearch and FreeBSD. 
110
+ We will mainly ensure compatibility to Rails, AWS, PostgreSQL, Redis, Elasticsearch and FreeBSD. 
108
111
 
109
112
  ## Credits
110
113
 
data/Rakefile CHANGED
@@ -12,6 +12,7 @@ Rake::TestTask.new(:test) do |t|
12
12
  t.libs << 'test'
13
13
  t.pattern = 'test/**/*_test.rb'
14
14
  t.verbose = false
15
+ t.warning = false
15
16
  end
16
17
 
17
18
  task default: :test
@@ -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
- if options.has_key?(:attribute)
35
- attribute_will_change! name
36
- instance_variable_set variable, value.join(',')
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
- send "#{attribute}=", value
57
+ new_value = ((current_values & new_values) + (new_values - current_values))
58
+ send "#{attribute}=", new_value
40
59
  end
41
- if options.has_key?(:after_add)
42
- (new_values - current_values).each do |added_value|
43
- send options[:after_add], added_value
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.has_key?(:after_remove)
47
- (current_values - new_values).each do |removed_value|
48
- send options[:after_remove], removed_value
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.has_key?(:attribute)
81
+ if relation_attribute = options[:by]
63
82
  values = values.map do |record|
64
- record.send options[:attribute]
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
@@ -6,7 +6,7 @@ module ListableCollections
6
6
 
7
7
  module ClassMethods
8
8
 
9
- def list(*args)
9
+ def listify(*args)
10
10
  options = args.extract_options!
11
11
  builder = Builder.new(self)
12
12
  args.each do |attribute|
@@ -1,5 +1,5 @@
1
1
  module ListableCollections
2
2
 
3
- VERSION = '5.1.0'
3
+ VERSION = '5.1.1'
4
4
 
5
5
  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.0
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: 2017-09-27 00:00:00.000000000 Z
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.6.13
90
+ rubygems_version: 2.7.3
91
91
  signing_key:
92
92
  specification_version: 4
93
93
  summary: Listable collections for rails.