Module: Traits::Association::Polymorphism
- Included in:
- Traits::Association
- Defined in:
- lib/traits/concerns/association/polymorphism.rb
Instance Method Summary collapse
-
#accepted_classes_through_polymorphism ⇒ Object
Example 1: class Picture belongs_to :imageable, polymorphic: true.
- #attribute_for_polymorphic_type ⇒ Object
-
#attribute_name_for_polymorphic_type ⇒ Object
class Picture belongs_to :imageable, polymorphic: true.
-
#paired_through_polymorphism? ⇒ Boolean
class Picture belongs_to :imageable, polymorphic: true.
-
#polymorphic? ⇒ Boolean
(also: #accepts_various_classes?)
class Picture belongs_to :imageable, polymorphic: true.
- #to_hash ⇒ Object
Instance Method Details
#accepted_classes_through_polymorphism ⇒ Object
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
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/traits/concerns/association/polymorphism.rb', line 48 def accepted_classes_through_polymorphism if polymorphic? classes = [] attr_name = attribute_name_for_polymorphic_type this_class = from_class Traits.active_record_descendants.each do |model_class| # Skip current model and models which are STI derived next if model_class <= this_class # Means is or derived from current model model_class.traits.associations.each do |assoc| if assoc.attribute_name_for_polymorphic_type == attr_name classes << assoc.from_class end end end classes.uniq.sort! { |l, r| l.to_s <=> r.to_s } end end |
#attribute_for_polymorphic_type ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/traits/concerns/association/polymorphism.rb', line 98 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 ⇒ Object
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
90 91 92 93 94 95 96 |
# File 'lib/traits/concerns/association/polymorphism.rb', line 90 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? ⇒ Boolean
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
77 78 79 |
# File 'lib/traits/concerns/association/polymorphism.rb', line 77 def paired_through_polymorphism? reflection.type.present? end |
#polymorphic? ⇒ Boolean Also known as: accepts_various_classes?
class Picture
belongs_to :imageable, polymorphic: true
class Toy
has_one :picture, as: :imageable
picture_traits.associations.polymorphic? => true toy_traits.associations.polymorphic? => false
13 14 15 |
# File 'lib/traits/concerns/association/polymorphism.rb', line 13 def polymorphic? belongs_to? && reflection.[:polymorphic] == true end |
#to_hash ⇒ Object
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/traits/concerns/association/polymorphism.rb', line 106 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 |