{"id":13427704,"url":"https://github.com/cedric/custom_counter_cache","last_synced_at":"2025-03-16T00:31:56.532Z","repository":{"id":640415,"uuid":"1408200","full_name":"cedric/custom_counter_cache","owner":"cedric","description":"Custom counter_cache functionality that supports conditions and multiple models.","archived":false,"fork":false,"pushed_at":"2023-08-16T17:13:31.000Z","size":120,"stargazers_count":60,"open_issues_count":10,"forks_count":20,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-10T21:49:58.774Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://github.com/cedric/custom_counter_cache","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cedric.png","metadata":{"files":{"readme":"README.rdoc","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2011-02-24T20:34:12.000Z","updated_at":"2024-07-26T00:23:36.000Z","dependencies_parsed_at":"2024-04-10T05:38:35.962Z","dependency_job_id":"22ab3db2-6704-4342-8726-67d9eb81680f","html_url":"https://github.com/cedric/custom_counter_cache","commit_stats":{"total_commits":168,"total_committers":18,"mean_commits":9.333333333333334,"dds":0.4285714285714286,"last_synced_commit":"1c987d197db6969fa865a7e1e253284410ce701b"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cedric%2Fcustom_counter_cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cedric%2Fcustom_counter_cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cedric%2Fcustom_counter_cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cedric%2Fcustom_counter_cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cedric","download_url":"https://codeload.github.com/cedric/custom_counter_cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243809865,"owners_count":20351403,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T01:00:38.776Z","updated_at":"2025-03-16T00:31:56.241Z","avatar_url":"https://github.com/cedric.png","language":"Ruby","readme":"{\u003cimg src=\"https://badge.fury.io/rb/custom_counter_cache.svg\" alt=\"Gem Version\" /\u003e}[http://badge.fury.io/rb/custom_counter_cache]\n{\u003cimg src=\"https://travis-ci.org/cedric/custom_counter_cache.svg?branch=master\" alt=\"Build Status\" /\u003e}[https://travis-ci.org/cedric/custom_counter_cache]\n\n== Custom Counter Cache\n\nThis is a simple approach to creating a custom counter cache in Rails that can be used across multiple models.\n\n=== Installation\n\nAdd the following to your Gemfile:\n\n  gem 'custom_counter_cache'\n\n=== Example\n\n== Class with counter cache\n\nThis 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.\n\n  include CustomCounterCache::Model\n  define_counter_cache :articles_count do |user|\n    user.articles.where(state: 'published').count\n  end\n\n== Class with callbacks\n\nThis will define the after_create, after_update and after_destroy callbacks. An :if option can be provided to limit when these callbacks get triggered.\n\n  include CustomCounterCache::Model\n  update_counter_cache :user, :articles_count, if: -\u003e (article) { article.state_changed? }\n\nThese callbacks can be added to any number of models that might need to change the counter cache.\n\n== Counter Cache\n\nTo store the counter cache you need to create a column for the model with the counter cache (example: articles_count).\n\nIf you would like to store all of your counter caches in a single table, you can use this migration:\n\n  create_table :counters do |t|\n    t.references :countable, polymorphic: true\n    t.string :key, null: false\n    t.integer :value, null: false, default: 0\n    t.timestamps\n  end\n  add_index :counters, [ :countable_id, :countable_type, :key ], unique: true\n\nHere is the example model to go with:\n\n  class Counter \u003c ActiveRecord::Base\n    belongs_to :countable, polymorphic: true\n    validates :countable, presence: true\n  end\n\nIf you would like to store your counter cache in an existing table, you can use this migration:\n\n  def change\n    add_column :users, :articles_count, :integer, default: 0, null: false\n  end\n\nTo backfill your counters, run something like this either in a migration or in the console:\n\n  User.select(:id).find_each(batch_size: 100) { |u| u.update_articles_count }\n","funding_links":[],"categories":["Active Record","模型"],"sub_categories":["Omniauth"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcedric%2Fcustom_counter_cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcedric%2Fcustom_counter_cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcedric%2Fcustom_counter_cache/lists"}