Custom counter_cache functionality that supports conditions and multiple models.
Ruby
Switch branches/tags
Pull request Compare This branch is 145 commits behind cedric:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
.gitignore
Gemfile
README.rdoc
Rakefile
custom_counter_cache.gemspec

README.rdoc

Custom Counter Cache

This is a simple approach to creating a custom counter cache that can be used across multiple models.

Installation

In Rails 2.3, add the following to your environment.rb:

config.gem 'custom_counter_cache'

If using Bundler, 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.

define_counter_cache :articles_count do |user|
  user.articles.count(:conditions => "articles.state = 'published'")
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.

update_counter_cache :user, :articles_count, :if => Proc.new { |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 => true

Here is the example model to go with:

class Counter < ActiveRecord::Base
  belongs_to :countable, :polymorphic => true
  validates_presence_of :countable
end

License

(The MIT License)

Copyright © 2011 Cedric Howe

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.