Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding :if / :unless for conditional counter caches #389

Closed
tatethurston opened this issue Feb 14, 2024 · 1 comment 路 May be fixed by #390
Closed

Adding :if / :unless for conditional counter caches #389

tatethurston opened this issue Feb 14, 2024 · 1 comment 路 May be fixed by #390

Comments

@tatethurston
Copy link

tatethurston commented Feb 14, 2024

馃憢 Hey thanks for the great gem.

I'd like to propose supporting a conditional API similar to ActiveRecord's Callbacks.

Today, a conditional counter cache is implemented as follows:

class Product < ActiveRecord::Base
  belongs_to :category
  counter_culture :category, column_name: proc {|model| model.special? ? 'special_count' : nil }
end

class Category < ActiveRecord::Base
  has_many :products
end

I would like to propose the following shorthand:

class Product < ActiveRecord::Base
  belongs_to :category
  counter_culture :category, if: :special?
end

class Category < ActiveRecord::Base
  has_many :products
end

If you're amenable to that, I'd be happy to look into implementing the if/unless functionality (proposal: implementing the same API/semantics as ActiveRecord Callbacks), relevant specs, and interplay with the existing column_name. If not, no worries.

@tatethurston tatethurston changed the title Adding :if / :unless with predicate method symbol for conditional counter caches Adding :if / :unless for conditional counter caches Feb 14, 2024
@magnusvk
Copy link
Owner

That looks like a neat short-hand, yeah鈥擨'd be happy to look at a PR for that. Thanks!

tatethurston added a commit to tatethurston/counter_culture that referenced this issue Feb 17, 2024
Adds a conditional API similar to [ActiveRecord's Callbacks](https://guides.rubyonrails.org/active_record_callbacks.html#conditional-callbacks).

Resolves magnusvk#389.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants