has_counter_on 0.1.5 → 1.0.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 +4 -4
- data/README.md +85 -14
- data/db/migrate/create_counters_migration.rb +14 -0
- data/lib/has_counter_on/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7ee345c262aca2fe2c606864808605f4852f14b921a83cb6a529bd14d28b1a79
|
4
|
+
data.tar.gz: e31a406d000556d9f99de9ee5d12c04189b95bfebdbb677589dce5f4c30b3cc3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6bd2dc9c66d257b106b03e35437b8a9da96bcc4c02a2a150ae00b70249e7e4d0a070fc69d88c75776437b1b4abd43c39dc187722df640f21517e406eca6043cd
|
7
|
+
data.tar.gz: b0837c7d4e690467cd0125b44ee190dabddd21ef9504761f4262746fad3440fa745972aebcea75c6ad7dd606a5d74aa533bf648c609b61c9347f938010da0576
|
data/README.md
CHANGED
@@ -1,39 +1,110 @@
|
|
1
1
|
# HasCounterOn
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
ActiveRecord counter_cache has been reborn, with ability to specify conditions. Inspired by [counter_cache_with_conditions](https://github.com/skojin/counter_cache_with_conditions) gem.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
9
|
-
|
7
|
+
To use it, add it to your Gemfile:
|
10
8
|
|
11
9
|
```ruby
|
12
10
|
gem 'has_counter_on'
|
13
11
|
```
|
14
12
|
|
15
|
-
|
13
|
+
and bundle:
|
16
14
|
|
17
|
-
|
15
|
+
```
|
16
|
+
$ bundle install
|
17
|
+
```
|
18
18
|
|
19
|
-
|
19
|
+
### After Installation
|
20
20
|
|
21
|
-
|
21
|
+
Install migrations
|
22
|
+
|
23
|
+
```
|
24
|
+
rake has_counter_on:setup
|
25
|
+
```
|
26
|
+
|
27
|
+
Review the generated migrations then migrate:
|
28
|
+
|
29
|
+
```
|
30
|
+
rake db:migrate
|
31
|
+
```
|
22
32
|
|
23
33
|
## Usage
|
24
34
|
|
25
|
-
|
35
|
+
Let's say you have a User model and an Article model.
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
class User < ActiveRecord::Base
|
39
|
+
has_many :articles
|
40
|
+
has_many :published_articles, -> { where(has_published: true) }, class_name: :Article
|
41
|
+
end
|
42
|
+
|
43
|
+
class Article < ActiveRecord::Base
|
44
|
+
belongs_to :user
|
45
|
+
end
|
46
|
+
```
|
26
47
|
|
27
|
-
|
48
|
+
By using ActiveRecord's `counter_cache`, articles_count can be created as follows: (However, you cannot handle `published_articles_count`)
|
28
49
|
|
29
|
-
|
50
|
+
```ruby
|
51
|
+
class User < ActiveRecord::Base
|
52
|
+
has_many :articles
|
53
|
+
has_many :published_articles, -> { where(has_published: true) }, class_name: :Article
|
54
|
+
end
|
55
|
+
|
56
|
+
class Article < ActiveRecord::Base
|
57
|
+
belongs_to :user, counter_cache: true
|
58
|
+
end
|
59
|
+
```
|
30
60
|
|
31
|
-
|
61
|
+
**with has_counter_on**, you can handle both of counters like this:
|
32
62
|
|
33
|
-
|
63
|
+
```ruby
|
64
|
+
class User < ActiveRecord::Base
|
65
|
+
has_many :articles
|
66
|
+
has_many :published_articles, -> { where(has_published: true) }, class_name: :Article
|
34
67
|
|
35
|
-
|
68
|
+
has_counter_on :articles
|
69
|
+
has_counter_on :articles, :published_articles_count, has_published: true
|
70
|
+
end
|
71
|
+
|
72
|
+
class Article < ActiveRecord::Base
|
73
|
+
belongs_to :user
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
77
|
+
Each counter will be updated automatically like that of the ActiveRecord.
|
78
|
+
|
79
|
+
### Complex Conditions
|
80
|
+
|
81
|
+
Complex conditions can be expressed as lambda.
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
class User < ActiveRecord::Base
|
85
|
+
has_many :articles
|
86
|
+
has_many :published_articles, -> { where.not(published_at: nil) }, class_name: :Article
|
87
|
+
|
88
|
+
has_counter_on :articles
|
89
|
+
has_counter_on :articles, :published_articles_count, published_at: -> (value) { value.present? }
|
90
|
+
end
|
91
|
+
|
92
|
+
class Article < ActiveRecord::Base
|
93
|
+
belongs_to :user
|
94
|
+
end
|
95
|
+
```
|
96
|
+
|
97
|
+
Of course, multiple conditions are available.
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
class User < ActiveRecord::Base
|
101
|
+
has_counter_on :articles, :featured_articles_count, has_featured: true, has_published: true
|
102
|
+
end
|
103
|
+
```
|
104
|
+
|
105
|
+
## Contributing
|
36
106
|
|
107
|
+
Bug reports and pull requests are welcome on [GitHub](https://github.com/mu29/has_counter_on).
|
37
108
|
|
38
109
|
## License
|
39
110
|
|
@@ -0,0 +1,14 @@
|
|
1
|
+
AR_VERSION = ActiveRecord.gem_version.version.to_f
|
2
|
+
|
3
|
+
class CreateCounters < ActiveRecord::Migration[AR_VERSION]
|
4
|
+
def change
|
5
|
+
create_table :counters do |t|
|
6
|
+
t.string :countable_type, limit: 64
|
7
|
+
t.integer :countable_id, unsigned: true
|
8
|
+
t.string :countable_name
|
9
|
+
t.integer :value
|
10
|
+
end
|
11
|
+
|
12
|
+
add_index :counters, [:countable_id, :countable_type, :countable_name], name: :index_counters_on_countable_id_type_name
|
13
|
+
end
|
14
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_counter_on
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Injung Chung
|
@@ -36,6 +36,7 @@ files:
|
|
36
36
|
- LICENSE
|
37
37
|
- README.md
|
38
38
|
- Rakefile
|
39
|
+
- db/migrate/create_counters_migration.rb
|
39
40
|
- has_counter_on.gemspec
|
40
41
|
- lib/has_counter_on.rb
|
41
42
|
- lib/has_counter_on/countable.rb
|