From 5289a5b381d4b042f1227f3538473388eb71bf28 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Mon, 2 Nov 2020 19:41:40 +0000 Subject: [PATCH] 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 --- lib/tasks/sample_data.rake | 26 +-- lib/tasks/sample_data/customer_factory.rb | 28 +-- lib/tasks/sample_data/enterprise_factory.rb | 160 +++++++------- lib/tasks/sample_data/fee_factory.rb | 46 ++-- lib/tasks/sample_data/group_factory.rb | 64 +++--- lib/tasks/sample_data/inventory_factory.rb | 58 ++--- lib/tasks/sample_data/order_cycle_factory.rb | 198 +++++++++--------- lib/tasks/sample_data/order_factory.rb | 132 ++++++------ .../sample_data/payment_method_factory.rb | 100 ++++----- lib/tasks/sample_data/permission_factory.rb | 50 ++--- lib/tasks/sample_data/product_factory.rb | 168 +++++++-------- .../sample_data/shipping_method_factory.rb | 100 ++++----- lib/tasks/sample_data/taxon_factory.rb | 44 ++-- lib/tasks/sample_data/user_factory.rb | 66 +++--- 14 files changed, 633 insertions(+), 607 deletions(-) diff --git a/lib/tasks/sample_data.rake b/lib/tasks/sample_data.rake index 1dcac757c9..40cc210f6d 100644 --- a/lib/tasks/sample_data.rake +++ b/lib/tasks/sample_data.rake @@ -27,31 +27,31 @@ namespace :ofn do task sample_data: :environment do raise "Please run `rake db:seed` first." unless seeded? - users = UserFactory.new.create_samples + users = SampleData::UserFactory.new.create_samples - enterprises = EnterpriseFactory.new.create_samples(users) + enterprises = SampleData::EnterpriseFactory.new.create_samples(users) - PermissionFactory.new.create_samples(enterprises) + SampleData::PermissionFactory.new.create_samples(enterprises) - FeeFactory.new.create_samples(enterprises) + SampleData::FeeFactory.new.create_samples(enterprises) - ShippingMethodFactory.new.create_samples(enterprises) + SampleData::ShippingMethodFactory.new.create_samples(enterprises) - PaymentMethodFactory.new.create_samples(enterprises) + SampleData::PaymentMethodFactory.new.create_samples(enterprises) - TaxonFactory.new.create_samples + SampleData::TaxonFactory.new.create_samples - products = ProductFactory.new.create_samples(enterprises) + products = SampleData::ProductFactory.new.create_samples(enterprises) - InventoryFactory.new.create_samples(products) + SampleData::InventoryFactory.new.create_samples(products) - OrderCycleFactory.new.create_samples + SampleData::OrderCycleFactory.new.create_samples - CustomerFactory.new.create_samples(users) + SampleData::CustomerFactory.new.create_samples(users) - GroupFactory.new.create_samples + SampleData::GroupFactory.new.create_samples - OrderFactory.new.create_samples + SampleData::OrderFactory.new.create_samples end def seeded? diff --git a/lib/tasks/sample_data/customer_factory.rb b/lib/tasks/sample_data/customer_factory.rb index c574f3150b..866f98f487 100644 --- a/lib/tasks/sample_data/customer_factory.rb +++ b/lib/tasks/sample_data/customer_factory.rb @@ -1,19 +1,21 @@ require "tasks/sample_data/logging" -class CustomerFactory - include Logging +module SampleData + class CustomerFactory + include Logging - def create_samples(users) - log "Creating customers" - jane = users["Jane Customer"] - maryse_shop = Enterprise.find_by(name: "Maryse's Private Shop") - return if Customer.where(user_id: jane, enterprise_id: maryse_shop).exists? + def create_samples(users) + log "Creating customers" + jane = users["Jane Customer"] + maryse_shop = Enterprise.find_by(name: "Maryse's Private Shop") + return if Customer.where(user_id: jane, enterprise_id: maryse_shop).exists? - log "- #{jane.email}" - Customer.create!( - email: jane.email, - user: jane, - enterprise: maryse_shop - ) + log "- #{jane.email}" + Customer.create!( + email: jane.email, + user: jane, + enterprise: maryse_shop + ) + end end end diff --git a/lib/tasks/sample_data/enterprise_factory.rb b/lib/tasks/sample_data/enterprise_factory.rb index 296d05cef8..5c884dfc9e 100644 --- a/lib/tasks/sample_data/enterprise_factory.rb +++ b/lib/tasks/sample_data/enterprise_factory.rb @@ -1,94 +1,96 @@ require "tasks/sample_data/addressing" require "tasks/sample_data/logging" -class EnterpriseFactory - include Logging - include Addressing +module SampleData + class EnterpriseFactory + include Logging + include Addressing - def create_samples(users) - log "Creating enterprises:" - enterprise_data(users).map do |data| - name = data[:name] - log "- #{name}" - data[:long_description] = data[:long_description].strip_heredoc.tr("\n", " ") - Enterprise.create_with(data).find_or_create_by!(name: name) + def create_samples(users) + log "Creating enterprises:" + enterprise_data(users).map do |data| + name = data[:name] + log "- #{name}" + data[:long_description] = data[:long_description].strip_heredoc.tr("\n", " ") + Enterprise.create_with(data).find_or_create_by!(name: name) + end end - end - private + private - # rubocop:disable Metrics/MethodLength - def enterprise_data(users) - [ - { - name: "Penny's Profile", - owner: users["Penny Profile"], - is_primary_producer: false, - sells: "none", - address: address("25 Myrtle Street, Bayswater, 3153"), - long_description: <