Coder Social home page Coder Social logo

counter-cache's Introduction

CounterCache

Counter Cache Rails gem provides an easy way to find and auto-update the number of belonging objects efficiently by caching counters in the parent model.

Motivation

CounterCache is an alternative for using native Rails counter cache.

My motivation behind this gem is an attempt to separate counting objects from belongs_to association. If you prefer counting objects and associations separated, this gem gives you a clear distinction between defining counter caches and its associations.

This gem also tends to encapsulate defining counters on the parent model, rather its belonging, so it's clear which counters are specified in every model.

Getting Started

Prerequisites

Rails > 5

Installation

Add this line to your application's Gemfile:

gem 'counter_cache'

And then execute:

$ bundle

Or install it yourself as:

$ gem install counter_cache

Usage

Adding counter_for macro in Active Record models will generate update associated counters from belongings models.

Ex:

class Invoice < ApplicationRecord
  has_many :line_items
  counter_for :line_items
end

counter_for :line_items assumes the that there is a LineItem model, and it will associate the counters in line_items_count column.

Adding counter columns to parent table

You can either add your counters to your parent table manually. Note: Your column name should end with _count Ex:

counter_for :comments
# column name should be comments_count

Or you can use the custom generator provided by this gem:

rails g counter_cache:add_counters [model name] [counter_for option]
rails g counter_cache:add_counters invoice line_items

This will create a new migration for adding an integer line_items_count column in invoices table with 0 as default value.

Then run rails db:migrate.

  • counter_cached_columns will return all the cached columns defined per model:
Invoice.counter_cached_columns
#=> [:line_items]
Using custom counter column name

If you want to use another name, you need to use class_name option passing the original model name

Ex:

class Invoice < ApplicationRecord
  has_many :line_items
  counter_for :items, class_name: :LineItem
end
Conditional counters
class Post < ApplicationRecord
  counter_for :comments
  counter_for :liked_comments, class_name: :Comment, scope: -> { liked? }
end

Roadmap

See the open issues for a list of proposed features

License

The gem is available as open source under the terms of the MIT License.

Tests

The gem is fully tested with minitest You can run the tests with:

rake test

Author

Contributing

Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

counter-cache's People

Contributors

dependabot[bot] avatar mosaaleb avatar

Stargazers

 avatar

Watchers

 avatar

counter-cache's Issues

counter caches recalculation

Public API method for recalculating cached_counters:

Ex:

# parent_model.recalculate(:defined_column_name)
invoice.recalculate(:line_items)

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.