Coder Social home page Coder Social logo

gargron / counter_cache-rails Goto Github PK

View Code? Open in Web Editor NEW

This project forked from iguchi1124/counter_cache-rails

0.0 2.0 0.0 41 KB

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

License: MIT License

Ruby 83.64% JavaScript 0.55% CSS 2.40% HTML 13.41%

counter_cache-rails's Introduction

CounterCacheRails

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

Installation

Add this line to your rails application's Gemfile:

gem 'counter_cache-rails'

Usage

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.

counter_cache-rails's People

Contributors

gargron avatar iguchi1124 avatar masarakki avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.