Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cedric/custom_counter_cache
Custom counter_cache functionality that supports conditions and multiple models.
https://github.com/cedric/custom_counter_cache
Last synced: 13 days ago
JSON representation
Custom counter_cache functionality that supports conditions and multiple models.
- Host: GitHub
- URL: https://github.com/cedric/custom_counter_cache
- Owner: cedric
- License: mit
- Created: 2011-02-24T20:34:12.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2023-08-16T17:13:31.000Z (about 1 year ago)
- Last Synced: 2024-10-01T10:46:54.969Z (about 1 month ago)
- Language: Ruby
- Homepage: https://github.com/cedric/custom_counter_cache
- Size: 117 KB
- Stars: 60
- Watchers: 1
- Forks: 20
- Open Issues: 10
-
Metadata Files:
- Readme: README.rdoc
- License: LICENSE
Awesome Lists containing this project
- awesome-rails-gem - custom_counter_cache - A simple approach to creating a custom counter cache that can be used across multiple models. (Active Record / Omniauth)
README
{}[http://badge.fury.io/rb/custom_counter_cache]
{}[https://travis-ci.org/cedric/custom_counter_cache]== Custom Counter Cache
This is a simple approach to creating a custom counter cache in Rails that can be used across multiple models.
=== Installation
Add the following to your Gemfile:
gem 'custom_counter_cache'
=== Example
== Class with counter cache
This is the block that will be used to calculate the value for the counter cache. It will be called by other models through their association via an after_save or after_destroy callback.
include CustomCounterCache::Model
define_counter_cache :articles_count do |user|
user.articles.where(state: 'published').count
end== Class with callbacks
This will define the after_create, after_update and after_destroy callbacks. An :if option can be provided to limit when these callbacks get triggered.
include CustomCounterCache::Model
update_counter_cache :user, :articles_count, if: -> (article) { article.state_changed? }These callbacks can be added to any number of models that might need to change the counter cache.
== Counter Cache
To store the counter cache you need to create a column for the model with the counter cache (example: articles_count).
If you would like to store all of your counter caches in a single table, you can use this migration:
create_table :counters do |t|
t.references :countable, polymorphic: true
t.string :key, null: false
t.integer :value, null: false, default: 0
t.timestamps
end
add_index :counters, [ :countable_id, :countable_type, :key ], unique: trueHere is the example model to go with:
class Counter < ActiveRecord::Base
belongs_to :countable, polymorphic: true
validates :countable, presence: true
endIf you would like to store your counter cache in an existing table, you can use this migration:
def change
add_column :users, :articles_count, :integer, default: 0, null: false
endTo backfill your counters, run something like this either in a migration or in the console:
User.select(:id).find_each(batch_size: 100) { |u| u.update_articles_count }