Class: Shoulda::Matchers::ActiveRecord::AssociationMatchers::ModelReflection

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

Instance Method Summary collapse

Constructor Details

#initialize(reflection) ⇒ ModelReflection

Returns a new instance of ModelReflection.



9
10
11
12
13
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb', line 9

def initialize(reflection)
  super(reflection)
  @reflection = reflection
  @subject = reflection.active_record
end

Instance Method Details

#associated_classObject



15
16
17
18
19
20
21
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb', line 15

def associated_class
  if polymorphic?
    subject
  else
    reflection.klass
  end
end

#association_foreign_keyObject



58
59
60
61
62
63
64
65
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb', line 58

def association_foreign_key
  if has_and_belongs_to_many_reflection
    join_model = has_and_belongs_to_many_reflection.options[:class]
    join_model.right_reflection.foreign_key
  else
    reflection.association_foreign_key
  end
end

#association_relation(related_instance) ⇒ Object



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

def association_relation(related_instance)
  relation = associated_class.all

  if reflection.scope
    # Source: AR::Associations::AssociationScope#eval_scope
    relation.instance_exec(related_instance, &reflection.scope)
  else
    relation
  end
end

#foreign_keyObject



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

def foreign_key
  if has_and_belongs_to_many_reflection
    has_and_belongs_to_many_reflection.foreign_key
  elsif reflection.respond_to?(:foreign_key)
    reflection.foreign_key
  else
    reflection.primary_key_name
  end
end

#has_and_belongs_to_many_nameObject



73
74
75
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb', line 73

def has_and_belongs_to_many_name
  reflection.options[:through]
end

#join_table_nameObject



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

def join_table_name
  join_table_name =
    has_and_belongs_to_many_name_table_name || reflection.join_table
  join_table_name.to_s
end

#polymorphic?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb', line 23

def polymorphic?
  reflection.options[:polymorphic]
end

#strict_loading?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb', line 77

def strict_loading?
  reflection.options.fetch(:strict_loading, subject.strict_loading_by_default)
end

#through?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb', line 27

def through?
  reflection.options[:through]
end

#validate_inverse_of_through_association!Object



67
68
69
70
71
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb', line 67

def validate_inverse_of_through_association!
  if through?
    reflection.check_validity!
  end
end