Files
openfoodnetwork/lib/tasks/sample_data/taxon_factory.rb
Luis Ramos 5289a5b381 Add namespace to all sample data factories
These factories are not used in testing and this way we avoid collisions on the root namespace as it was happening already with OrderFactory
2020-11-10 22:03:36 +00:00

30 lines
704 B
Ruby

require "tasks/sample_data/logging"
module SampleData
class TaxonFactory
include Logging
def create_samples
log "Creating taxonomies:"
taxonomy = Spree::Taxonomy.find_or_create_by!(name: 'Products')
taxons = ['Vegetables', 'Fruit', 'Oils', 'Preserves and Sauces', 'Dairy', 'Fungi']
taxons.each do |taxon_name|
create_taxon(taxonomy, taxon_name)
end
end
private
def create_taxon(taxonomy, taxon_name)
return if Spree::Taxon.where(name: taxon_name).exists?
log "- #{taxon_name}"
Spree::Taxon.create!(
name: taxon_name,
parent_id: taxonomy.root.id,
taxonomy_id: taxonomy.id
)
end
end
end