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
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb', line 15

def associated_class
  reflection.klass
end

#association_foreign_keyObject



54
55
56
57
58
59
60
61
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb', line 54

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



33
34
35
36
37
38
39
40
41
42
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb', line 33

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



44
45
46
47
48
49
50
51
52
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb', line 44

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



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

def has_and_belongs_to_many_name
  reflection.options[:through]
end

#join_table_nameObject



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

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)


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

def polymorphic?
  reflection.options[:polymorphic]
end

#through?Boolean

Returns:

  • (Boolean)


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

def through?
  reflection.options[:through]
end

#validate_inverse_of_through_association!Object



63
64
65
66
67
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb', line 63

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