conditional_counter_cache 0.0.1 → 0.0.2

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
2
  SHA1:
3
- metadata.gz: 1a7dd773fc250091226da0a2a5efdedc39b8da51
4
- data.tar.gz: 1d9d5bcc9e6712100f44ef3894243c77bd67a12c
3
+ metadata.gz: 00a4bcb0f63a7936b1f0ccf93a62f88ab2636172
4
+ data.tar.gz: ee002eeb7f874e9ca7f31f309ed7fa2437cebcf8
5
5
  SHA512:
6
- metadata.gz: f6fc68c2c9700a7456d1f3de057010645f390b8ae4a9e608b6d4809ac84128f4d5788faad1d44f101d5b3aa274ad2987d0274c748ad3a6c73e0e3957d4938547
7
- data.tar.gz: 484034592c1b600f73ef671592ea2211c97da011451a872f4007890812c920c0e7ac2e09f7730b115615be484745752e23bae9810d0d5c88e9d562678cee2071
6
+ metadata.gz: 13441a35f7f0c13dfe7b8b30386edadf10244178dc5b44f08eeb1fe7115d3444375e2d48f41904462f88b04781a75ebd02fa550f73776169ce536f43c5564a39
7
+ data.tar.gz: bd069a40e093f2785586083894279ef8dd379a2024e7131c3205b3dcedab72178da50fab757cc65041baf1b69df715f33f88cf26cf93deb49a3310a340e0cd03
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## 0.0.2
2
+ * Fix non counter-cached belongs_to association
3
+
4
+ ## 0.0.1
5
+ * 1st release
data/README.md CHANGED
@@ -7,7 +7,7 @@ Customize condition via `:counter_cache` option:
7
7
  ```ruby
8
8
  class Tagging < ActiveRecord::Base
9
9
  belongs_to :item
10
- belongs_to :tag, counter_cache: { condition: -> { !tagging.item.private? } }
10
+ belongs_to :tag, counter_cache: { condition: -> { !item.private? } }
11
11
  end
12
12
  ```
13
13
 
@@ -16,7 +16,14 @@ Other examples:
16
16
  ```ruby
17
17
  belongs_to :tag, counter_cache: true
18
18
  belongs_to :tag, counter_cache: "items_count"
19
- belongs_to :tag, counter_cache: { condition: -> { !tagging.item.private? } }
19
+ belongs_to :tag, counter_cache: { condition: -> { !item.private? } }
20
+ belongs_to :tag, counter_cache: { condition: -> :your_favorite_method_name }
20
21
  belongs_to :tag, counter_cache: { column_name: "items_count" }
21
- belongs_to :tag, counter_cache: { column_name: "items_count", condition: -> { !tagging.item.private? } }
22
+ belongs_to :tag, counter_cache: { column_name: "items_count", condition: -> { !item.private? } }
22
23
  ```
24
+
25
+ ## See also
26
+ * [Active Record Associations — Ruby on Rails Guides](http://guides.rubyonrails.org/association_basics.html)
27
+ * [#23 Counter Cache Column - RailsCasts](http://railscasts.com/episodes/23-counter-cache-column)
28
+ * [magnusvk/counter_culture](https://github.com/magnusvk/counter_culture)
29
+ * [Counterculture - Wikipedia, the free encyclopedia](http://en.wikipedia.org/wiki/Counterculture)
@@ -2,13 +2,12 @@ module ConditionalCounterCache
2
2
  module Reflection
3
3
  # @note Overridden
4
4
  def counter_cache_column
5
- Option.new(options[:counter_cache]).column_name ||
6
- "#{active_record.name.demodulize.underscore.pluralize}_count"
5
+ Option.new(self).column_name
7
6
  end
8
7
 
9
8
  # @return [true, false]
10
9
  def has_countable?(record)
11
- Option.new(options[:counter_cache]).condition.call(record)
10
+ Option.new(self).condition.call(record)
12
11
  end
13
12
 
14
13
  # Utility wrapper of `option[:counter_cache][:condition]`.
@@ -30,22 +29,24 @@ module ConditionalCounterCache
30
29
  end
31
30
  end
32
31
 
33
- # Utility wrapper of `option[:counter_cache]`.
32
+ # Utility wrapper of reflection to process `option[:counter_cache]`.
34
33
  class Option
35
- # @param [Hash, String, true, nil]
36
- def initialize(value)
37
- @value = value
34
+ # @param [ActiveRecord::Reflection]
35
+ def initialize(reflection)
36
+ @reflection = reflection
38
37
  end
39
38
 
40
39
  # @return [String, nil] Specified column name, or nil meaning "use default column name".
41
40
  def column_name
42
- case
43
- when has_hash_value?
44
- @value[:column_name]
45
- when has_true_value?
46
- nil
41
+ case cache
42
+ when Hash
43
+ cache[:column_name]
44
+ when String, Symbol
45
+ cache.to_s
46
+ when true
47
+ "#{@reflection.active_record.name.demodulize.underscore.pluralize}_count"
47
48
  else
48
- @value.to_s
49
+ nil
49
50
  end
50
51
  end
51
52
 
@@ -56,17 +57,13 @@ module ConditionalCounterCache
56
57
 
57
58
  private
58
59
 
59
- def has_hash_value?
60
- @value.is_a?(Hash)
61
- end
62
-
63
- def has_true_value?
64
- @value == true
60
+ def cache
61
+ @reflection.options[:counter_cache]
65
62
  end
66
63
 
67
64
  # @return [Proc, String, Symbol, nil]
68
65
  def raw_condition
69
- @value[:condition] if has_hash_value?
66
+ cache[:condition] if cache.is_a?(Hash)
70
67
  end
71
68
  end
72
69
  end
@@ -1,3 +1,3 @@
1
1
  module ConditionalCounterCache
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: conditional_counter_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
@@ -122,6 +122,7 @@ extensions: []
122
122
  extra_rdoc_files: []
123
123
  files:
124
124
  - ".gitignore"
125
+ - CHANGELOG.md
125
126
  - Gemfile
126
127
  - LICENSE.txt
127
128
  - README.md