Class: Shoulda::Matchers::ActiveRecord::AssociationMatchers::ModelReflection
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Shoulda::Matchers::ActiveRecord::AssociationMatchers::ModelReflection
- Defined in:
- lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb
Instance Method Summary collapse
- #associated_class ⇒ Object
- #association_foreign_key ⇒ Object
- #association_relation(related_instance) ⇒ Object
- #foreign_key ⇒ Object
- #has_and_belongs_to_many_name ⇒ Object
-
#initialize(reflection) ⇒ ModelReflection
constructor
A new instance of ModelReflection.
- #join_table_name ⇒ Object
- #polymorphic? ⇒ Boolean
- #strict_loading? ⇒ Boolean
- #through? ⇒ Boolean
- #validate_inverse_of_through_association! ⇒ Object
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_class ⇒ Object
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_key ⇒ Object
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.[: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() relation = associated_class.all if reflection.scope # Source: AR::Associations::AssociationScope#eval_scope relation.instance_exec(, &reflection.scope) else relation end end |
#foreign_key ⇒ Object
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_name ⇒ Object
73 74 75 |
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb', line 73 def has_and_belongs_to_many_name reflection.[:through] end |
#join_table_name ⇒ Object
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
23 24 25 |
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb', line 23 def polymorphic? reflection.[:polymorphic] end |
#strict_loading? ⇒ Boolean
77 78 79 |
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb', line 77 def strict_loading? reflection..fetch(:strict_loading, subject.strict_loading_by_default) end |
#through? ⇒ Boolean
27 28 29 |
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb', line 27 def through? reflection.[: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 |