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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f216a569999e552a8a811c2be20123f629cb42f4b4a83316fa8f5ae19d1a139f
4
- data.tar.gz: b39fb98410d0fd86109126cd4cd94b8c0f36f10e96d81691cc9032564b9c4501
3
+ metadata.gz: 7ee345c262aca2fe2c606864808605f4852f14b921a83cb6a529bd14d28b1a79
4
+ data.tar.gz: e31a406d000556d9f99de9ee5d12c04189b95bfebdbb677589dce5f4c30b3cc3
5
5
  SHA512:
6
- metadata.gz: 0753b5ed10479ffa680b95ea35a14c23b98c5bc4f38c22b122304523b55143f2112b643f9d34dc719fa17618cb06882d667700d6715a179b0fb0d5587999b87d
7
- data.tar.gz: 121ed866cbddc6e7bf8415dbcac8037a10590297f97a44ed28c469e531b80218a85688702a3b8e8a7bbff7921f5006b09d056b09162255e2c43259e11bf92042
6
+ metadata.gz: 6bd2dc9c66d257b106b03e35437b8a9da96bcc4c02a2a150ae00b70249e7e4d0a070fc69d88c75776437b1b4abd43c39dc187722df640f21517e406eca6043cd
7
+ data.tar.gz: b0837c7d4e690467cd0125b44ee190dabddd21ef9504761f4262746fad3440fa745972aebcea75c6ad7dd606a5d74aa533bf648c609b61c9347f938010da0576
data/README.md CHANGED
@@ -1,39 +1,110 @@
1
1
  # HasCounterOn
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/has_counter_on`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- Add this line to your application's Gemfile:
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
- And then execute:
13
+ and bundle:
16
14
 
17
- $ bundle install
15
+ ```
16
+ $ bundle install
17
+ ```
18
18
 
19
- Or install it yourself as:
19
+ ### After Installation
20
20
 
21
- $ gem install has_counter_on
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
- TODO: Write usage instructions here
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
- ## Development
48
+ By using ActiveRecord's `counter_cache`, articles_count can be created as follows: (However, you cannot handle `published_articles_count`)
28
49
 
29
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
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
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
61
+ **with has_counter_on**, you can handle both of counters like this:
32
62
 
33
- ## Contributing
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
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/has_counter_on.
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
@@ -1,3 +1,3 @@
1
1
  module HasCounterOn
2
- VERSION = '0.1.5'
2
+ VERSION = '1.0.0'
3
3
  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.1.5
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