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
- #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 |
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb', line 15 def associated_class reflection.klass end |
#association_foreign_key ⇒ Object
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.[: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() 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
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_name ⇒ Object
69 70 71 |
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb', line 69 def has_and_belongs_to_many_name reflection.[:through] end |
#join_table_name ⇒ Object
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
19 20 21 |
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb', line 19 def polymorphic? reflection.[:polymorphic] end |
#through? ⇒ Boolean
23 24 25 |
# File 'lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb', line 23 def through? reflection.[: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 |