Class: Shoulda::Matchers::ActiveRecord::AssociationMatchers::ModelReflector

Inherits:
Object
  • Object
show all
Defined in:
lib/shoulda/matchers/active_record/association_matchers/model_reflector.rb

Instance Method Summary collapse

Constructor Details

#initialize(subject, name) ⇒ ModelReflector



24
25
26
27
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflector.rb', line 24

def initialize(subject, name)
  @subject = subject
  @name = name
end

Instance Method Details

#association_relationObject



29
30
31
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflector.rb', line 29

def association_relation
  reflection.association_relation(subject)
end

#build_relation_with_clause(name, value) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflector.rb', line 49

def build_relation_with_clause(name, value)
  case name
  when :conditions
    associated_class.where(value)
  when :order
    associated_class.order(value)
  else
    raise ArgumentError, "Unknown clause '#{name}'"
  end
end

#extract_relation_clause_from(relation, name) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflector.rb', line 60

def extract_relation_clause_from(relation, name)
  case name
  when :conditions
    relation.where_values_hash
  when :order
    relation.order_values.map do |value|
      value_as_sql(value)
    end.join(', ')
  else
    raise ArgumentError, "Unknown clause '#{name}'"
  end
end

#model_classObject



45
46
47
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflector.rb', line 45

def model_class
  subject.class
end

#reflect_on_association(name) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflector.rb', line 37

def reflect_on_association(name)
  reflection = model_class.reflect_on_association(name)

  if reflection
    ModelReflection.new(reflection)
  end
end

#reflectionObject



33
34
35
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflector.rb', line 33

def reflection
  @_reflection ||= reflect_on_association(name)
end