Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/iguchi1124/counter_cache-rails

ActiveRecord child records counter caching on your rails application cache store.
https://github.com/iguchi1124/counter_cache-rails

Last synced: 17 days ago
JSON representation

ActiveRecord child records counter caching on your rails application cache store.

Awesome Lists containing this project

README

        

# CounterCacheRails

ActiveRecord child records counter caching on your rails application cache store.

## Installation

Add this line to your rails application's Gemfile:

```ruby
gem 'counter_cache-rails'
```

## Usage

```rb
class Post
has_many :comments

counter_cache :comments,
if: ->(comment) { comment.visible? },
scope: ->(comments) { comments.visible }

after_update_comments_count do
# you can use callbacks on update counter cache
end

after_increment_comments_count do
Redis.current.increment('service:total:comments')
end

after_decrement_comments_count do
Redis.current.decrement('service:total:comments')
end

after_cache_comments_count do
update(comments_count: comments_count)
end
end

class Comment
belong_to :post
end

post = Post.create(title: 'sample title', body: 'post body')
post.comments_count # => 0

post.comments.create(body: 'comment body')
post.comments_count # => 1

post.comments_count(force: true) # force reload counter cache
```

## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).