Class: Formed::Reflection::ThroughReflection

Inherits:
AbstractReflection show all
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

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.options[:anonymous_class]
  @source_reflection_name = delegate_reflection.options[:source]

  ensure_option_not_given_as_class!(:source_type)
end

Instance Method Details

#check_validity!Object

Raises:

  • (HasManyThroughAssociationNotFoundError)


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 options[:source_type] && !source_reflection.polymorphic?
    raise HasManyThroughAssociationPointlessSourceTypeError.new(active_record.name, self, source_reflection)
  end

  if source_reflection.polymorphic? && options[: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

#klassObject



669
670
671
# File 'lib/formed/reflection.rb', line 669

def klass
  @klass ||= delegate_reflection.compute_class(class_name)
end

#source_reflection_namesObject

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

tags_reflection = Post.reflect_on_association(:tags)
tags_reflection.source_reflection_names
# => [:tag, :tags]


684
685
686
# File 'lib/formed/reflection.rb', line 684

def source_reflection_names
  options[:source] ? [options[:source]] : [name.to_s.singularize, name].uniq
end

#through_reflection?Boolean

Returns:

  • (Boolean)


665
666
667
# File 'lib/formed/reflection.rb', line 665

def through_reflection?
  true
end