Class: Syncify::Association::PolymorphicAssociation

Inherits:
Object
  • Object
show all
Defined in:
lib/syncify/association/polymorphic_association.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#destinationObject

Returns the value of attribute destination.



4
5
6
# File 'lib/syncify/association/polymorphic_association.rb', line 4

def destination
  @destination
end

#from_classObject

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

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/syncify/association/polymorphic_association.rb', line 4

def name
  @name
end

#to_classesObject

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

#traversedObject

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

Returns:

  • (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

#hashObject



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

Returns:

  • (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

Returns:

  • (Boolean)


27
28
29
# File 'lib/syncify/association/polymorphic_association.rb', line 27

def polymorphic?
  true
end

#traversed?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/syncify/association/polymorphic_association.rb', line 31

def traversed?
  traversed
end