Class: Syncify::Association::PolymorphicAssociation
- Inherits:
-
Object
- Object
- Syncify::Association::PolymorphicAssociation
- Defined in:
- lib/syncify/association/polymorphic_association.rb
Instance Attribute Summary collapse
-
#destination ⇒ Object
Returns the value of attribute destination.
-
#from_class ⇒ Object
Returns the value of attribute from_class.
-
#name ⇒ Object
Returns the value of attribute name.
-
#to_classes ⇒ Object
Returns the value of attribute to_classes.
-
#traversed ⇒ Object
Returns the value of attribute traversed.
Class Method Summary collapse
Instance Method Summary collapse
- #create_destination(association_name) ⇒ Object
- #eql?(other_association) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(from_class:, association:, destination:) ⇒ PolymorphicAssociation
constructor
A new instance of PolymorphicAssociation.
- #inverse_of?(association) ⇒ Boolean
- #polymorphic? ⇒ Boolean
- #traversed? ⇒ Boolean
Constructor Details
#initialize(from_class:, association:, destination:) ⇒ PolymorphicAssociation
Returns a new instance of PolymorphicAssociation.
19 20 21 22 23 24 25 |
# File 'lib/syncify/association/polymorphic_association.rb', line 19 def initialize(from_class:, association:, destination:) @from_class = from_class @to_classes = Syncify::Association::PolymorphicAssociation.identify_to_classes(from_class, association.name) @name = association.name @destination = destination @traversed = false end |
Instance Attribute Details
#destination ⇒ Object
Returns the value of attribute destination.
4 5 6 |
# File 'lib/syncify/association/polymorphic_association.rb', line 4 def destination @destination end |
#from_class ⇒ Object
Returns the value of attribute from_class.
4 5 6 |
# File 'lib/syncify/association/polymorphic_association.rb', line 4 def from_class @from_class end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/syncify/association/polymorphic_association.rb', line 4 def name @name end |
#to_classes ⇒ Object
Returns the value of attribute to_classes.
4 5 6 |
# File 'lib/syncify/association/polymorphic_association.rb', line 4 def to_classes @to_classes end |
#traversed ⇒ Object
Returns the value of attribute traversed.
4 5 6 |
# File 'lib/syncify/association/polymorphic_association.rb', line 4 def traversed @traversed end |
Class Method Details
.identify_to_classes(from_class, association_name) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/syncify/association/polymorphic_association.rb', line 6 def self.identify_to_classes(from_class, association_name) association = from_class.reflect_on_association(association_name) @cache ||= {} @cache[from_class] ||= {} @cache[from_class][association_name] ||= from_class. where("#{association.foreign_type} != ''"). distinct. pluck(association.foreign_type). uniq. compact. map(&:constantize) end |
Instance Method Details
#create_destination(association_name) ⇒ Object
45 46 47 48 |
# File 'lib/syncify/association/polymorphic_association.rb', line 45 def create_destination(association_name) destination[name] ||= {} destination[name][association_name] = {} end |
#eql?(other_association) ⇒ Boolean
54 55 56 57 58 59 |
# File 'lib/syncify/association/polymorphic_association.rb', line 54 def eql?(other_association) return false unless other_association.is_a? PolymorphicAssociation self.from_class == other_association.from_class && self.to_classes == other_association.to_classes && self.name == other_association.name end |
#hash ⇒ Object
50 51 52 |
# File 'lib/syncify/association/polymorphic_association.rb', line 50 def hash "#{self.from_class.to_s}#{self.to_classes.map(&:to_s)}#{self.name}".hash end |
#inverse_of?(association) ⇒ Boolean
35 36 37 38 39 40 41 42 43 |
# File 'lib/syncify/association/polymorphic_association.rb', line 35 def inverse_of?(association) if association.polymorphic? association.to_classes.include?(from_class) && to_classes.include?(association.from_class) else from_class == association.to_class && to_classes.include?(association.from_class) end end |
#polymorphic? ⇒ Boolean
27 28 29 |
# File 'lib/syncify/association/polymorphic_association.rb', line 27 def polymorphic? true end |
#traversed? ⇒ Boolean
31 32 33 |
# File 'lib/syncify/association/polymorphic_association.rb', line 31 def traversed? traversed end |