no_fly_list 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: da62f66e9694575999da6c6864650b05036fe0f32ed762255d4552e11fc53ce4
4
- data.tar.gz: 965687001501fec670c74c1a89d95340776b3b8ffa061517d34c430439471572
3
+ metadata.gz: 8e5fd47ff5074ba83503a9c1633bada42f7348aafb9a914d2c5f51a70820427d
4
+ data.tar.gz: a337a25cdce43d598fd501266ba0d99665c3adb183f994121fd201082e4e874b
5
5
  SHA512:
6
- metadata.gz: 3c347ef635cb9c1adeb14df10b7ce8a6b6220104f93b0fc4b7debccf30c0537da86e232a4b62000fb3498253c8ae5535c5ca9ddb9290371d3afd4dd021c894ab
7
- data.tar.gz: 3b1685788f672eadfe558f2bbceacdfd287dbcb865b7e2bbd8b718e710cf3870a575900bcae69452ebc9a720f8ca77c4c0f2b4488d56431e6c5a30946f4d630b
6
+ metadata.gz: a0063a48bd720527540437b156136c704bd9deea0c4985fb34a9c4a7595c3c932d3c9b13ab5b8032daf277b5e547b358b5343410bdb6bedae1e466bc081edecd
7
+ data.tar.gz: 2f430e49bc7a6cc8ed09b1aa5686121c8d90d2262f7a6cce48de48e208804ab77899227e2b48ace8a6b017a9147fd368136ce42b99eeaab9668361e1bf10acfc
@@ -24,7 +24,8 @@ module NoFlyList
24
24
  restrict_to_existing: options.fetch(:restrict_to_existing, false),
25
25
  limit: options.fetch(:limit, nil),
26
26
  case_sensitive: options.fetch(:case_sensitive, true),
27
- adapter: @adapter
27
+ adapter: @adapter,
28
+ counter_cache: options.fetch(:counter_cache, false)
28
29
  }
29
30
  end
30
31
 
@@ -94,7 +94,8 @@ module NoFlyList
94
94
 
95
95
  belongs_to :taggable,
96
96
  class_name: setup.taggable_klass.name,
97
- foreign_key: "taggable_id"
97
+ foreign_key: "taggable_id",
98
+ counter_cache: setup.counter_cache ? "#{setup.context}_count" : nil
98
99
 
99
100
  include NoFlyList::TaggingRecord
100
101
  end
@@ -156,12 +157,14 @@ module NoFlyList
156
157
 
157
158
  # Sets up associations for local (non-global) tags
158
159
  def setup_local_tag_associations(setup, singular_name)
160
+ plural_name = setup.context.to_s
159
161
  # Set up tag class associations
160
162
  setup.tag_class_name.constantize.class_eval do
161
163
  has_many :"#{singular_name}_taggings",
162
164
  -> { where(context: singular_name) },
163
165
  class_name: setup.tagging_class_name,
164
166
  foreign_key: "tag_id",
167
+ counter_cache: setup.counter_cache ? "#{plural_name}_count" : nil,
165
168
  dependent: :destroy
166
169
 
167
170
  has_many :"#{singular_name}_taggables",
@@ -4,7 +4,7 @@ module NoFlyList
4
4
  module TaggableRecord
5
5
  class TagSetup
6
6
  attr_reader :taggable_klass, :context, :transformer, :polymorphic,
7
- :restrict_to_existing, :limit,
7
+ :restrict_to_existing, :limit, :counter_cache,
8
8
  :tag_class_name, :tagging_class_name, :adapter
9
9
 
10
10
  def initialize(taggable_klass, context, options = {})
@@ -13,6 +13,8 @@ module NoFlyList
13
13
  @transformer = options.fetch(:transformer, ApplicationTagTransformer)
14
14
  @polymorphic = options.fetch(:polymorphic, false)
15
15
  @restrict_to_existing = options.fetch(:restrict_to_existing, false)
16
+ @counter_cache = options.fetch(:counter_cache, false)
17
+ @counter_cache_column = "#{context}_count"
16
18
  @limit = options.fetch(:limit, nil)
17
19
  @tag_class_name = determine_tag_class_name(taggable_klass, options)
18
20
  @tagging_class_name = determine_tagging_class_name(taggable_klass, options)
@@ -74,8 +74,11 @@ module NoFlyList
74
74
  end
75
75
 
76
76
  # Clear existing tags
77
+ old_count = model.send(context_taggings).count
77
78
  model.send(context_taggings).delete_all
78
79
 
80
+ # Update counter
81
+ model.update_column("#{@context}_count", 0) if setup[:counter_cache]
79
82
  # Create new tags
80
83
  @pending_changes.each do |tag_name|
81
84
  tag = find_or_create_tag(tag_name)
@@ -95,6 +98,8 @@ module NoFlyList
95
98
  model.send(context_taggings).create!(attributes)
96
99
  end
97
100
  end
101
+ # Update counter to match the actual count
102
+ model.update_column("#{@context}_count", @pending_changes.size) if setup[:counter_cache]
98
103
 
99
104
  refresh_from_database
100
105
  true
@@ -147,10 +152,10 @@ module NoFlyList
147
152
  "#<#{self.class.name} tags=#{current_list.inspect} transformer_with=#{transformer_name} >"
148
153
  end
149
154
 
150
- def add(tag)
155
+ def add(*tags)
151
156
  return self if limit_reached?
152
157
 
153
- new_tags = transformer.parse_tags(tag)
158
+ new_tags = tags.flatten.map { |tag| transformer.parse_tags(tag) }.flatten
154
159
  return self if new_tags.empty?
155
160
 
156
161
  @pending_changes = current_list + new_tags
@@ -158,8 +163,8 @@ module NoFlyList
158
163
  self
159
164
  end
160
165
 
161
- def add!(tag)
162
- add(tag)
166
+ def add!(*tags)
167
+ add(*tags)
163
168
  save
164
169
  end
165
170
 
@@ -179,12 +184,14 @@ module NoFlyList
179
184
  old_list = current_list.dup
180
185
  @pending_changes = []
181
186
  mark_record_dirty if @pending_changes != old_list
187
+ model.write_attribute("#{@context}_count", 0) if setup[:counter_cache]
182
188
  self
183
189
  end
184
190
 
185
191
  def clear!
186
192
  @model.send(@context.to_s).destroy_all
187
193
  @pending_changes = []
194
+ @model.update_column("#{@context}_count", 0) if setup[:counter_cache]
188
195
  self
189
196
  end
190
197
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NoFlyList
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: no_fly_list
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abdelkader Boudih