Class: Formed::Reflection::ThroughReflection
- Inherits:
-
AbstractReflection
- Object
- AbstractReflection
- Formed::Reflection::ThroughReflection
- Defined in:
- lib/formed/reflection.rb
Overview
Holds all the metadata about a :through association as it was specified in the Active Record class.
Instance Method Summary collapse
- #check_validity! ⇒ Object
-
#initialize(delegate_reflection) ⇒ ThroughReflection
constructor
A new instance of ThroughReflection.
- #klass ⇒ Object
-
#source_reflection_names ⇒ Object
Gets an array of possible
:through
source reflection names in both singular and plural form. - #through_reflection? ⇒ Boolean
Methods inherited from AbstractReflection
#alias_candidate, #build_association, #build_scope, #chain, #check_validity_of_inverse!, #class_name, #constraints, #inverse_of, #scopes, #strict_loading?, #strict_loading_violation_message, #table_name
Constructor Details
#initialize(delegate_reflection) ⇒ ThroughReflection
Returns a new instance of ThroughReflection.
657 658 659 660 661 662 663 |
# File 'lib/formed/reflection.rb', line 657 def initialize(delegate_reflection) @delegate_reflection = delegate_reflection @klass = delegate_reflection.[:anonymous_class] @source_reflection_name = delegate_reflection.[:source] ensure_option_not_given_as_class!(:source_type) end |
Instance Method Details
#check_validity! ⇒ Object
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 |
# File 'lib/formed/reflection.rb', line 688 def check_validity! raise HasManyThroughAssociationNotFoundError.new(active_record, self) if through_reflection.nil? if through_reflection.polymorphic? raise HasOneAssociationPolymorphicThroughError.new(active_record.name, self) if has_one? raise HasManyThroughAssociationPolymorphicThroughError.new(active_record.name, self) end raise HasManyThroughSourceAssociationNotFoundError, self if source_reflection.nil? if [:source_type] && !source_reflection.polymorphic? raise HasManyThroughAssociationPointlessSourceTypeError.new(active_record.name, self, source_reflection) end if source_reflection.polymorphic? && [:source_type].nil? raise HasManyThroughAssociationPolymorphicSourceError.new(active_record.name, self, source_reflection) end if has_one? && through_reflection.collection? raise HasOneThroughCantAssociateThroughCollection.new(active_record.name, self, through_reflection) end if parent_reflection.nil? reflections = active_record.reflections.keys.map(&:to_sym) if reflections.index(through_reflection.name) > reflections.index(name) raise HasManyThroughOrderError.new(active_record.name, self, through_reflection) end end check_validity_of_inverse! end |
#klass ⇒ Object
669 670 671 |
# File 'lib/formed/reflection.rb', line 669 def klass @klass ||= delegate_reflection.compute_class(class_name) end |
#source_reflection_names ⇒ Object
Gets an array of possible :through
source reflection names in both singular and plural form.
class Post < ActiveRecord::Base
has_many :taggings
has_many :tags, through: :taggings
end
= Post.reflect_on_association(:tags)
.source_reflection_names
# => [:tag, :tags]
684 685 686 |
# File 'lib/formed/reflection.rb', line 684 def source_reflection_names [:source] ? [[:source]] : [name.to_s.singularize, name].uniq end |
#through_reflection? ⇒ Boolean
665 666 667 |
# File 'lib/formed/reflection.rb', line 665 def through_reflection? true end |