Module: Seedie::PolymorphicAssociationHelper

Included in:
Generators::InstallGenerator, Model::ModelSorter
Defined in:
lib/seedie/polymorphic_association_helper.rb

Instance Method Summary collapse

Instance Method Details

#find_polymorphic_types(_model, association_name) ⇒ Object

Returns the type of the polymorphic association We need only one polymorphic association while generating config this makes it easier to sort according to dependencies



8
9
10
11
# File 'lib/seedie/polymorphic_association_helper.rb', line 8

def find_polymorphic_types(_model, association_name)
  type = @models.find { |potential_model| has_association?(potential_model, association_name) }
  type&.name&.underscore
end

#has_association?(model, association_name) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/seedie/polymorphic_association_helper.rb', line 13

def has_association?(model, association_name)
  associations = select_associations(model)
  associations.any? { |association| association.options[:as] == association_name }
end

#select_associations(model) ⇒ Object



18
19
20
21
22
# File 'lib/seedie/polymorphic_association_helper.rb', line 18

def select_associations(model)
  model.reflect_on_all_associations.select do |reflection|
    %i[has_many has_one].include?(reflection.macro)
  end
end