Class: Seedie::Associations::HasMany

Inherits:
BaseAssociation show all
Defined in:
lib/seedie/associations/has_many.rb

Constant Summary

Constants inherited from BaseAssociation

BaseAssociation::DEFAULT_COUNT, BaseAssociation::INDEX

Instance Attribute Summary

Attributes inherited from BaseAssociation

#association_config, #model, #record, #reporters

Instance Method Summary collapse

Methods inherited from BaseAssociation

#generate_association, #initialize

Methods included from Reporters::Reportable

#add_observers, #report

Constructor Details

This class inherits a constructor from Seedie::Associations::BaseAssociation

Instance Method Details

#generate_associationsObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/seedie/associations/has_many.rb', line 6

def generate_associations
  return if association_config["has_many"].nil?

  report(:has_many_start)
  association_config["has_many"].each do |association_name, association_config|
    association_class = association_name.to_s.classify.constantize
    count = get_association_count(association_config)
    config = only_count_given?(association_config) ? {} : association_config

    report(:associated_records, name: association_name, count: count, parent_name: model.to_s)
    count.times do |index|
      field_values_set = FieldValuesSet.new(association_class, config, index).generate_field_values_with_associations
      record_creator = Model::Creator.new(record.send(association_name), reporters)

      record_creator.create!(field_values_set)
    end
  end
end