Skip to content

Commit

Permalink
Support Rails 7.2 counter_cache configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorton committed May 28, 2024
1 parent 030f7f5 commit cd16340
Showing 1 changed file with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ def description
def matches?(subject)
self.subject = ModelReflector.new(subject, name)

if option_verifier.correct_for_string?(
:counter_cache,
counter_cache,
)
if correct_value?
true
else
self.missing_option = "#{name} should have #{description}"
Expand All @@ -34,9 +31,42 @@ def matches?(subject)

attr_accessor :subject, :counter_cache, :name

def correct_value?
expected = normalize_value

if expected.is_a?(Hash)
option_verifier.correct_for_hash?(
:counter_cache,
expected,
)
else
option_verifier.correct_for_string?(
:counter_cache,
expected,
)
end
end

def option_verifier
@_option_verifier ||= OptionVerifier.new(subject)
end

def normalize_value
if Rails::VERSION::STRING >= '7.2'
case counter_cache
when true
{ active: true, column: nil }
when String, Symbol
{ active: true, column: counter_cache.to_s }
when Hash
({ active: true }).merge(counter_cache)
else
raise ArgumentError, 'Invalid counter_cache option'
end
else
counter_cache
end
end
end
end
end
Expand Down

0 comments on commit cd16340

Please sign in to comment.