Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Counter Cache With Conditions

Build Status

Replacement for ActiveRecord belongs_to :counter_cache with ability to specify conditions.

Installation

Just include in your Gemfile:

gem 'counter_cache_with_conditions'

Usage

When additional counter cache need

class Message < ActiveRecord::Base
  belongs_to :folder, :counter_cache => true # rails default counter cache
  # hash syntax
  # counter_cache_with_conditions :association, :association_column_name, attribute: value
  counter_cache_with_conditions :folder, :unread_messages_count, unread: true

  # lambda syntax
  # counter_cache_with_conditions :association, :association_column_name, [:attribute], lambda{|attribute| ... }
  # lambda syntax need [:used_attributes_array] because it need to track changes, and lambda will be called only when attributes are changed
  counter_cache_with_conditions :folder, :unread_messages_count, [:read_at], lambda{|read_at| !read_at }

end

More examples.

# hash syntax
counter_cache_with_conditions :folder, :unread_messages_count, :unread => true
counter_cache_with_conditions :folder, :archived_messages_count, status: 'archived'

# lambda syntax
counter_cache_with_conditions :folder, :unread_messages_count, [:read, :source], lambda{|read, source| read == false && source == 'message'}
counter_cache_with_conditions :folder, :published_events_count, [:published_at], lambda{|published_at| published_at != nil }

Or even as replacement for rails build in solution.

belongs_to :folder
# same as rails default counter cache
counter_cache_with_conditions :folder, :messages_count

See Also

Copyright

Copyright (c) 2010-2017 Sergey Kojin, released under the MIT license

About

Replacement for ActiveRecord belongs_to :counter_cache with ability to specify conditions.

Resources

Stars

23 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages