From e06af7742037313d2659ce01414dc3b3a37724cd Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Tue, 6 Mar 2018 12:55:13 +0100 Subject: [PATCH 1/2] Move from hash rocket to symbol syntax --- lib/tasks/dev.rake | 152 ++++++++++++++++++++++----------------------- 1 file changed, 76 insertions(+), 76 deletions(-) diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 017bc0817d..aa82bb9d54 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -4,7 +4,7 @@ namespace :openfoodnetwork do namespace :dev do desc 'load sample data' - task :load_sample_data => :environment do + task load_sample_data: :environment do require_relative '../../spec/factories' require_relative '../../spec/support/spree/init' task_name = "openfoodnetwork:dev:load_sample_data" @@ -17,20 +17,20 @@ namespace :openfoodnetwork do # -- Shipping / payment information unless Spree::Zone.find_by_name 'Australia' puts "[#{task_name}] Seeding shipping / payment information" - zone = FactoryGirl.create(:zone, :name => 'Australia', :zone_members => []) + zone = FactoryGirl.create(:zone, name: 'Australia', zone_members: []) country = Spree::Country.find_by_name('Australia') - Spree::ZoneMember.create(:zone => zone, :zoneable => country) - FactoryGirl.create(:shipping_method, :zone => zone) + Spree::ZoneMember.create(zone: zone, zoneable: country) + FactoryGirl.create(:shipping_method, zone: zone) end # -- Taxonomies unless Spree::Taxonomy.find_by_name 'Products' puts "[#{task_name}] Seeding taxonomies" - taxonomy = Spree::Taxonomy.find_by_name('Products') || FactoryGirl.create(:taxonomy, :name => 'Products') + taxonomy = Spree::Taxonomy.find_by_name('Products') || FactoryGirl.create(:taxonomy, name: 'Products') taxonomy_root = taxonomy.root ['Vegetables', 'Fruit', 'Oils', 'Preserves and Sauces', 'Dairy', 'Meat and Fish'].each do |taxon_name| - FactoryGirl.create(:taxon, :name => taxon_name, :parent_id => taxonomy_root.id) + FactoryGirl.create(:taxon, name: taxon_name, parent_id: taxonomy_root.id) end end @@ -38,43 +38,43 @@ namespace :openfoodnetwork do unless Spree::Address.find_by_zipcode "3160" puts "[#{task_name}] Seeding addresses" - FactoryGirl.create(:address, :address1 => "25 Myrtle Street", :zipcode => "3153", :city => "Bayswater") - FactoryGirl.create(:address, :address1 => "6 Rollings Road", :zipcode => "3156", :city => "Upper Ferntree Gully") - FactoryGirl.create(:address, :address1 => "72 Lake Road", :zipcode => "3130", :city => "Blackburn") - FactoryGirl.create(:address, :address1 => "7 Verbena Street", :zipcode => "3195", :city => "Mordialloc") - FactoryGirl.create(:address, :address1 => "20 Galvin Street", :zipcode => "3018", :city => "Altona") - FactoryGirl.create(:address, :address1 => "59 Websters Road", :zipcode => "3106", :city => "Templestowe") - FactoryGirl.create(:address, :address1 => "17 Torresdale Drive", :zipcode => "3155", :city => "Boronia") - FactoryGirl.create(:address, :address1 => "21 Robina CRT", :zipcode => "3764", :city => "Kilmore") - FactoryGirl.create(:address, :address1 => "25 Kendall Street", :zipcode => "3134", :city => "Ringwood") - FactoryGirl.create(:address, :address1 => "2 Mines Road", :zipcode => "3135", :city => "Ringwood East") - FactoryGirl.create(:address, :address1 => "183 Millers Road", :zipcode => "3025", :city => "Altona North") - FactoryGirl.create(:address, :address1 => "310 Pascoe Vale Road", :zipcode => "3040", :city => "Essendon") - FactoryGirl.create(:address, :address1 => "6 Martin Street", :zipcode => "3160", :city => "Belgrave") + FactoryGirl.create(:address, address1: "25 Myrtle Street", zipcode: "3153", city: "Bayswater") + FactoryGirl.create(:address, address1: "6 Rollings Road", zipcode: "3156", city: "Upper Ferntree Gully") + FactoryGirl.create(:address, address1: "72 Lake Road", zipcode: "3130", city: "Blackburn") + FactoryGirl.create(:address, address1: "7 Verbena Street", zipcode: "3195", city: "Mordialloc") + FactoryGirl.create(:address, address1: "20 Galvin Street", zipcode: "3018", city: "Altona") + FactoryGirl.create(:address, address1: "59 Websters Road", zipcode: "3106", city: "Templestowe") + FactoryGirl.create(:address, address1: "17 Torresdale Drive", zipcode: "3155", city: "Boronia") + FactoryGirl.create(:address, address1: "21 Robina CRT", zipcode: "3764", city: "Kilmore") + FactoryGirl.create(:address, address1: "25 Kendall Street", zipcode: "3134", city: "Ringwood") + FactoryGirl.create(:address, address1: "2 Mines Road", zipcode: "3135", city: "Ringwood East") + FactoryGirl.create(:address, address1: "183 Millers Road", zipcode: "3025", city: "Altona North") + FactoryGirl.create(:address, address1: "310 Pascoe Vale Road", zipcode: "3040", city: "Essendon") + FactoryGirl.create(:address, address1: "6 Martin Street", zipcode: "3160", city: "Belgrave") end # -- Enterprises unless Enterprise.count > 1 puts "[#{task_name}] Seeding enterprises" - 3.times { FactoryGirl.create(:supplier_enterprise, :address => Spree::Address.find_by_zipcode("3160")) } + 3.times { FactoryGirl.create(:supplier_enterprise, address: Spree::Address.find_by_zipcode("3160")) } - FactoryGirl.create(:distributor_enterprise, :name => "Green Grass", :address => Spree::Address.find_by_zipcode("3153")) - FactoryGirl.create(:distributor_enterprise, :name => "AusFarmers United", :address => Spree::Address.find_by_zipcode("3156")) - FactoryGirl.create(:distributor_enterprise, :name => "Blackburn FreeGrossers", :address => Spree::Address.find_by_zipcode("3130")) - FactoryGirl.create(:distributor_enterprise, :name => "MegaFoods", :address => Spree::Address.find_by_zipcode("3195")) - FactoryGirl.create(:distributor_enterprise, :name => "Eco Butchers", :address => Spree::Address.find_by_zipcode("3018")) - FactoryGirl.create(:distributor_enterprise, :name => "Western Wines", :address => Spree::Address.find_by_zipcode("3106")) - FactoryGirl.create(:distributor_enterprise, :name => "QuickFresh", :address => Spree::Address.find_by_zipcode("3155")) - FactoryGirl.create(:distributor_enterprise, :name => "Fooderers", :address => Spree::Address.find_by_zipcode("3764")) - FactoryGirl.create(:distributor_enterprise, :name => "Food Local", :address => Spree::Address.find_by_zipcode("3134")) - FactoryGirl.create(:distributor_enterprise, :name => "Green Food Trading Corporation", :address => Spree::Address.find_by_zipcode("3135")) - FactoryGirl.create(:distributor_enterprise, :name => "Better Food", :address => Spree::Address.find_by_zipcode("3025")) - FactoryGirl.create(:distributor_enterprise, :name => "Gippsland Poultry", :address => Spree::Address.find_by_zipcode("3040")) + FactoryGirl.create(:distributor_enterprise, name: "Green Grass", address: Spree::Address.find_by_zipcode("3153")) + FactoryGirl.create(:distributor_enterprise, name: "AusFarmers United", address: Spree::Address.find_by_zipcode("3156")) + FactoryGirl.create(:distributor_enterprise, name: "Blackburn FreeGrossers", address: Spree::Address.find_by_zipcode("3130")) + FactoryGirl.create(:distributor_enterprise, name: "MegaFoods", address: Spree::Address.find_by_zipcode("3195")) + FactoryGirl.create(:distributor_enterprise, name: "Eco Butchers", address: Spree::Address.find_by_zipcode("3018")) + FactoryGirl.create(:distributor_enterprise, name: "Western Wines", address: Spree::Address.find_by_zipcode("3106")) + FactoryGirl.create(:distributor_enterprise, name: "QuickFresh", address: Spree::Address.find_by_zipcode("3155")) + FactoryGirl.create(:distributor_enterprise, name: "Fooderers", address: Spree::Address.find_by_zipcode("3764")) + FactoryGirl.create(:distributor_enterprise, name: "Food Local", address: Spree::Address.find_by_zipcode("3134")) + FactoryGirl.create(:distributor_enterprise, name: "Green Food Trading Corporation", address: Spree::Address.find_by_zipcode("3135")) + FactoryGirl.create(:distributor_enterprise, name: "Better Food", address: Spree::Address.find_by_zipcode("3025")) + FactoryGirl.create(:distributor_enterprise, name: "Gippsland Poultry", address: Spree::Address.find_by_zipcode("3040")) end # -- Enterprise users - unless Spree::User.count > 1 + unless Spree::User.count > 1 puts "[#{task_name}] Seeding enterprise users" pw = "spree123" @@ -100,7 +100,7 @@ namespace :openfoodnetwork do # -- Enterprise Payment Methods unless Spree::PaymentMethod.count > 1 Enterprise.is_distributor.each do |distributor| - FactoryGirl.create(:payment_method, distributors: [distributor], name: "Cheque (#{distributor.name})", :environment => 'development') + FactoryGirl.create(:payment_method, distributors: [distributor], name: "Cheque (#{distributor.name})", environment: 'development') end end @@ -109,68 +109,68 @@ namespace :openfoodnetwork do puts "[#{task_name}] Seeding products" prod1 = FactoryGirl.create(:product, - :name => 'Garlic', :price => 20.00, - :supplier => Enterprise.is_primary_producer[0], - :taxons => [Spree::Taxon.find_by_name('Vegetables')]) + name: 'Garlic', price: 20.00, + supplier: Enterprise.is_primary_producer[0], + taxons: [Spree::Taxon.find_by_name('Vegetables')]) - ProductDistribution.create(:product => prod1, - :distributor => Enterprise.is_distributor[0], - :enterprise_fee => Enterprise.is_distributor[0].enterprise_fees.first) + ProductDistribution.create(product: prod1, + distributor: Enterprise.is_distributor[0], + enterprise_fee: Enterprise.is_distributor[0].enterprise_fees.first) prod2 = FactoryGirl.create(:product, - :name => 'Fuji Apple', :price => 5.00, - :supplier => Enterprise.is_primary_producer[1], - :taxons => [Spree::Taxon.find_by_name('Fruit')]) + name: 'Fuji Apple', price: 5.00, + supplier: Enterprise.is_primary_producer[1], + taxons: [Spree::Taxon.find_by_name('Fruit')]) - ProductDistribution.create(:product => prod2, - :distributor => Enterprise.is_distributor[1], - :enterprise_fee => Enterprise.is_distributor[1].enterprise_fees.first) + ProductDistribution.create(product: prod2, + distributor: Enterprise.is_distributor[1], + enterprise_fee: Enterprise.is_distributor[1].enterprise_fees.first) prod3 = FactoryGirl.create(:product, - :name => 'Beef - 5kg Trays', :price => 50.00, - :supplier => Enterprise.is_primary_producer[2], - :taxons => [Spree::Taxon.find_by_name('Meat and Fish')]) + name: 'Beef - 5kg Trays', price: 50.00, + supplier: Enterprise.is_primary_producer[2], + taxons: [Spree::Taxon.find_by_name('Meat and Fish')]) - ProductDistribution.create(:product => prod3, - :distributor => Enterprise.is_distributor[2], - :enterprise_fee => Enterprise.is_distributor[2].enterprise_fees.first) + ProductDistribution.create(product: prod3, + distributor: Enterprise.is_distributor[2], + enterprise_fee: Enterprise.is_distributor[2].enterprise_fees.first) prod4 = FactoryGirl.create(:product, - :name => 'Carrots', :price => 3.00, - :supplier => Enterprise.is_primary_producer[2], - :taxons => [Spree::Taxon.find_by_name('Meat and Fish')]) + name: 'Carrots', price: 3.00, + supplier: Enterprise.is_primary_producer[2], + taxons: [Spree::Taxon.find_by_name('Meat and Fish')]) - ProductDistribution.create(:product => prod4, - :distributor => Enterprise.is_distributor[2], - :enterprise_fee => Enterprise.is_distributor[2].enterprise_fees.first) + ProductDistribution.create(product: prod4, + distributor: Enterprise.is_distributor[2], + enterprise_fee: Enterprise.is_distributor[2].enterprise_fees.first) prod5 = FactoryGirl.create(:product, - :name => 'Potatoes', :price => 2.00, - :supplier => Enterprise.is_primary_producer[2], - :taxons => [Spree::Taxon.find_by_name('Meat and Fish')]) + name: 'Potatoes', price: 2.00, + supplier: Enterprise.is_primary_producer[2], + taxons: [Spree::Taxon.find_by_name('Meat and Fish')]) - ProductDistribution.create(:product => prod5, - :distributor => Enterprise.is_distributor[2], - :enterprise_fee => Enterprise.is_distributor[2].enterprise_fees.first) + ProductDistribution.create(product: prod5, + distributor: Enterprise.is_distributor[2], + enterprise_fee: Enterprise.is_distributor[2].enterprise_fees.first) prod6 = FactoryGirl.create(:product, - :name => 'Tomatoes', :price => 2.00, - :supplier => Enterprise.is_primary_producer[2], - :taxons => [Spree::Taxon.find_by_name('Meat and Fish')]) + name: 'Tomatoes', price: 2.00, + supplier: Enterprise.is_primary_producer[2], + taxons: [Spree::Taxon.find_by_name('Meat and Fish')]) - ProductDistribution.create(:product => prod6, - :distributor => Enterprise.is_distributor[2], - :enterprise_fee => Enterprise.is_distributor[2].enterprise_fees.first) + ProductDistribution.create(product: prod6, + distributor: Enterprise.is_distributor[2], + enterprise_fee: Enterprise.is_distributor[2].enterprise_fees.first) prod7 = FactoryGirl.create(:product, - :name => 'Potatoes', :price => 2.00, - :supplier => Enterprise.is_primary_producer[2], - :taxons => [Spree::Taxon.find_by_name('Meat and Fish')]) + name: 'Potatoes', price: 2.00, + supplier: Enterprise.is_primary_producer[2], + taxons: [Spree::Taxon.find_by_name('Meat and Fish')]) - ProductDistribution.create(:product => prod7, - :distributor => Enterprise.is_distributor[2], - :enterprise_fee => Enterprise.is_distributor[2].enterprise_fees.first) + ProductDistribution.create(product: prod7, + distributor: Enterprise.is_distributor[2], + enterprise_fee: Enterprise.is_distributor[2].enterprise_fees.first) end From 10d6a2950371334e14dd70a519eb8220be0166b8 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Tue, 6 Mar 2018 13:07:17 +0100 Subject: [PATCH 2/2] Move service class to its own file --- app/services/create_order_cycle.rb | 65 ++++++++++++++++++++++++++++++ lib/tasks/dev.rake | 65 ------------------------------ 2 files changed, 65 insertions(+), 65 deletions(-) create mode 100644 app/services/create_order_cycle.rb diff --git a/app/services/create_order_cycle.rb b/app/services/create_order_cycle.rb new file mode 100644 index 0000000000..a7c631991e --- /dev/null +++ b/app/services/create_order_cycle.rb @@ -0,0 +1,65 @@ +# Creates an order cycle for the provided enterprise and selecting all the +# variants specified for both incoming and outgoing exchanges +class CreateOrderCycle + # Constructor + # + # @param enterprise [Enterprise] + # @param variants [Array] + def initialize(enterprise, variants) + @enterprise = enterprise + @variants = variants + end + + # Creates the order cycle + def call + incoming_exchange.order_cycle = order_cycle + incoming_exchange.variants << variants + + outgoing_exchange.order_cycle = order_cycle + outgoing_exchange.variants << variants + + order_cycle.exchanges << incoming_exchange + order_cycle.exchanges << outgoing_exchange + + order_cycle.save! + end + + private + + attr_reader :enterprise, :variants + + # Builds an order cycle for the next month, starting now + # + # @return [OrderCycle] + def order_cycle + @order_cycle ||= OrderCycle.new( + coordinator_id: enterprise.id, + name: 'Monthly order cycle', + orders_open_at: Time.zone.now, + orders_close_at: Time.zone.now + 1.month + ) + end + + # Builds an exchange with the enterprise both as sender and receiver + # + # @return [Exchange] + def incoming_exchange + @incoming_exchange ||= Exchange.new( + sender_id: enterprise.id, + receiver_id: enterprise.id, + incoming: true + ) + end + + # Builds an exchange with the enterprise both as sender and receiver + # + # @return [Exchange] + def outgoing_exchange + @outgoing_exchange ||= Exchange.new( + sender_id: enterprise.id, + receiver_id: enterprise.id, + pickup_time: '8 am', + incoming: false + ) + end +end diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index aa82bb9d54..dda29a8f6b 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -195,70 +195,5 @@ namespace :openfoodnetwork do EnterpriseRole.create!(user: Spree::User.first, enterprise: enterprise2) end - # Creates an order cycle for the provided enterprise and selecting all the - # variants specified for both incoming and outgoing exchanges - class CreateOrderCycle - # Constructor - # - # @param enterprise [Enterprise] - # @param variants [Array] - def initialize(enterprise, variants) - @enterprise = enterprise - @variants = variants - end - - # Creates the order cycle - def call - incoming_exchange.order_cycle = order_cycle - incoming_exchange.variants << variants - - outgoing_exchange.order_cycle = order_cycle - outgoing_exchange.variants << variants - - order_cycle.exchanges << incoming_exchange - order_cycle.exchanges << outgoing_exchange - - order_cycle.save! - end - - private - - attr_reader :enterprise, :variants - - # Builds an order cycle for the next month, starting now - # - # @return [OrderCycle] - def order_cycle - @order_cycle ||= OrderCycle.new( - coordinator_id: enterprise.id, - name: 'Monthly order cycle', - orders_open_at: Time.zone.now, - orders_close_at: Time.zone.now + 1.month - ) - end - - # Builds an exchange with the enterprise both as sender and receiver - # - # @return [Exchange] - def incoming_exchange - @incoming_exchange ||= Exchange.new( - sender_id: enterprise.id, - receiver_id: enterprise.id, - incoming: true - ) - end - - # Builds an exchange with the enterprise both as sender and receiver - # - # @return [Exchange] - def outgoing_exchange - @outgoing_exchange ||= Exchange.new( - sender_id: enterprise.id, - receiver_id: enterprise.id, - pickup_time: '8 am', - incoming: false - ) - end - end end end