conditional_counter_cache 0.0.1 → 0.0.2
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 +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +10 -3
- data/lib/conditional_counter_cache/reflection.rb +17 -20
- data/lib/conditional_counter_cache/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 00a4bcb0f63a7936b1f0ccf93a62f88ab2636172
|
4
|
+
data.tar.gz: ee002eeb7f874e9ca7f31f309ed7fa2437cebcf8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 13441a35f7f0c13dfe7b8b30386edadf10244178dc5b44f08eeb1fe7115d3444375e2d48f41904462f88b04781a75ebd02fa550f73776169ce536f43c5564a39
|
7
|
+
data.tar.gz: bd069a40e093f2785586083894279ef8dd379a2024e7131c3205b3dcedab72178da50fab757cc65041baf1b69df715f33f88cf26cf93deb49a3310a340e0cd03
|
data/CHANGELOG.md
ADDED
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: -> { !
|
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: -> { !
|
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: -> { !
|
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(
|
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(
|
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 [
|
36
|
-
def initialize(
|
37
|
-
@
|
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
|
44
|
-
|
45
|
-
when
|
46
|
-
|
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
|
-
|
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
|
60
|
-
@
|
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
|
-
|
66
|
+
cache[:condition] if cache.is_a?(Hash)
|
70
67
|
end
|
71
68
|
end
|
72
69
|
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.
|
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
|