class Shoulda::Matchers::ActiveRecord::AssociationMatchers::CounterCacheMatcher

@private

Attributes

counter_cache[RW]
missing_option[RW]
name[RW]
subject[RW]

Public Class Methods

new(counter_cache, name) click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/counter_cache_matcher.rb, line 9
def initialize(counter_cache, name)
  @counter_cache = counter_cache
  @name = name
  @missing_option = ''
end

Public Instance Methods

description() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/counter_cache_matcher.rb, line 15
def description
  "counter_cache => #{counter_cache}"
end
matches?(subject) click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/counter_cache_matcher.rb, line 19
def matches?(subject)
  self.subject = ModelReflector.new(subject, name)

  if correct_value?
    true
  else
    self.missing_option = "#{name} should have #{description}"
    false
  end
end

Protected Instance Methods

correct_value?() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/counter_cache_matcher.rb, line 34
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
normalize_value() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/counter_cache_matcher.rb, line 54
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, column: nil }.merge(counter_cache)
    else
      raise ArgumentError, 'Invalid counter_cache option'
    end
  else
    counter_cache
  end
end
option_verifier() click to toggle source
# File lib/shoulda/matchers/active_record/association_matchers/counter_cache_matcher.rb, line 50
def option_verifier
  @_option_verifier ||= OptionVerifier.new(subject)
end