module Traits::Association::Polymorphism

Public Instance Methods

accepted_classes_through_polymorphism() click to toggle source

Example 1:

class Picture
  belongs_to :imageable, polymorphic: true

class Toy
  has_one :picture, as: :imageable

picture_traits.associations[:imageable].accepted_classes_through_polymorphism
  => [Toy]

toy_traits.associations[:picture].accepted_classes_through_polymorphism
  => nil

Example 2:

class Picture
  belongs_to :imageable, polymorphic: true

class Present
  has_one :picture, as: :imageable

class Toy < Present
class VideoGame < Present
class Car < Present

picture_traits.associations[:imageable].accepted_classes_through_polymorphism
  => [Car, Present, Toy, VideoGame]

Note that items in list are sorted by class name

# File lib/traits/association/polymorphism.rb, line 51
def accepted_classes_through_polymorphism
  if polymorphic?
    classes     = []
    attr_name   = attribute_name_for_polymorphic_type
    this_class  = from_active_record

    Traits.active_record_descendants.each do |active_record|
      # Skip current model and models which are STI derived
      next if active_record <= this_class # Means is or derived from current model

      active_record.traits.associations.each do |assoc|
        if assoc.attribute_name_for_polymorphic_type == attr_name
          classes << assoc.from_active_record
        end
      end
    end
    classes.uniq.sort! { |l, r| l.to_s <=> r.to_s  }
  end
end
accepts_various_classes?()
Alias for: polymorphic?
attribute_for_polymorphic_type() click to toggle source
# File lib/traits/association/polymorphism.rb, line 101
def attribute_for_polymorphic_type
  if polymorphic?
    from.attributes[attribute_for_polymorphic_type]
  elsif paired_through_polymorphism?
    to.attributes[reflection.foreign_type]
  end
end
attribute_name_for_polymorphic_type() click to toggle source

class Picture

belongs_to :imageable, polymorphic: true

class Toy

has_one :picture, as: :imageable

picture_traits.associations.polymorphic_type_attribute_name => :imageable_type toy_traits.associations.polymorphic_type_attribute_name => :imageable_type

# File lib/traits/association/polymorphism.rb, line 93
def attribute_name_for_polymorphic_type
  if polymorphic?
    reflection.foreign_type.to_sym
  elsif paired_through_polymorphism?
    reflection.type.to_sym
  end
end
paired_through_polymorphism?() click to toggle source

class Picture

belongs_to :imageable, polymorphic: true

class Toy

has_one :picture, as: :imageable

picture_traits.associations.paired_through_polymorphism? => false toy_traits.associations.paired_through_polymorphism? => true

# File lib/traits/association/polymorphism.rb, line 80
def paired_through_polymorphism?
  reflection.type.present?
end
polymorphic?() click to toggle source

class Picture

belongs_to :imageable, polymorphic: true

class Toy

has_one :picture, as: :imageable

picture_traits.associations.polymorphic? => true toy_traits.associations.polymorphic? => false

# File lib/traits/association/polymorphism.rb, line 16
def polymorphic?
  belongs_to? && reflection.options[:polymorphic] == true
end
Also aliased as: accepts_various_classes?
to_hash() click to toggle source
Calls superclass method
# File lib/traits/association/polymorphism.rb, line 109
def to_hash
  accepted_classes = accepted_classes_through_polymorphism
  super.merge!(
    polymorphic:                  polymorphic?,
    paired_through_polymorphism:  paired_through_polymorphism?,

    attribute_name_for_polymorphic_type:   attribute_name_for_polymorphic_type,
    accepted_classes_through_polymorphism: accepted_classes.try(:map) { |el| el.traits.name }
  )
end