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
This commit is contained in:
Luis Ramos
2020-11-02 19:41:40 +00:00
parent bea8982085
commit 5289a5b381
14 changed files with 633 additions and 607 deletions

View File

@@ -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?

View File

@@ -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

View File

@@ -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: <<DESC
This enterprise is a profile which means that it's not a producer and it
doesn't sell anything either.
# 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: <<DESC
This enterprise is a profile which means that it's not a producer and it
doesn't sell anything either.
DESC
},
{
name: "Fred's Farm",
owner: users["Fred Farmer"],
is_primary_producer: true,
sells: "none",
address: address("6 Rollings Road, Upper Ferntree Gully, 3156"),
long_description: <<DESC
This enterprise is a producer only. It has products, which are sold
through the online shops of other enterprises.
},
{
name: "Fred's Farm",
owner: users["Fred Farmer"],
is_primary_producer: true,
sells: "none",
address: address("6 Rollings Road, Upper Ferntree Gully, 3156"),
long_description: <<DESC
This enterprise is a producer only. It has products, which are sold
through the online shops of other enterprises.
DESC
},
{
name: "Freddy's Farm Shop",
owner: users["Freddy Shop Farmer"],
is_primary_producer: true,
sells: "own",
address: address("72 Lake Road, Blackburn, 3130"),
long_description: <<DESC
This enterprise is a producer which also sells directly to consumers.
It has its own online shop and sells through other enterprises.
},
{
name: "Freddy's Farm Shop",
owner: users["Freddy Shop Farmer"],
is_primary_producer: true,
sells: "own",
address: address("72 Lake Road, Blackburn, 3130"),
long_description: <<DESC
This enterprise is a producer which also sells directly to consumers.
It has its own online shop and sells through other enterprises.
DESC
},
{
name: "Fredo's Farm Hub",
owner: users["Fredo Hub Farmer"],
is_primary_producer: true,
sells: "any",
address: address("7 Verbena Street, Mordialloc, 3195"),
long_description: <<DESC
This enterprise is a producer selling its own and other produce to
consumers.
},
{
name: "Fredo's Farm Hub",
owner: users["Fredo Hub Farmer"],
is_primary_producer: true,
sells: "any",
address: address("7 Verbena Street, Mordialloc, 3195"),
long_description: <<DESC
This enterprise is a producer selling its own and other produce to
consumers.
DESC
},
{
name: "Mary's Online Shop",
owner: users["Mary Retailer"],
is_primary_producer: false,
sells: "any",
address: address("20 Galvin Street, Altona, 3018"),
long_description: <<DESC
This enterprise sells the products of producers, but doesn't have any
products of its own.
},
{
name: "Mary's Online Shop",
owner: users["Mary Retailer"],
is_primary_producer: false,
sells: "any",
address: address("20 Galvin Street, Altona, 3018"),
long_description: <<DESC
This enterprise sells the products of producers, but doesn't have any
products of its own.
DESC
},
{
name: "Maryse's Private Shop",
owner: users["Maryse Private"],
is_primary_producer: false,
sells: "any",
address: address("6 Martin Street, Belgrave, 3160"),
require_login: true,
long_description: <<DESC
This enterprise sells the products of producers in a private shop front.
Users have to be registered customers of this enterprise to access the
shop.
},
{
name: "Maryse's Private Shop",
owner: users["Maryse Private"],
is_primary_producer: false,
sells: "any",
address: address("6 Martin Street, Belgrave, 3160"),
require_login: true,
long_description: <<DESC
This enterprise sells the products of producers in a private shop front.
Users have to be registered customers of this enterprise to access the
shop.
DESC
}
]
}
]
end
# rubocop:enable Metrics/MethodLength
end
# rubocop:enable Metrics/MethodLength
end

View File

@@ -1,29 +1,31 @@
require "tasks/sample_data/logging"
class FeeFactory
include Logging
module SampleData
class FeeFactory
include Logging
def create_samples(enterprises)
log "Creating fees:"
enterprises.each do |enterprise|
next if enterprise.enterprise_fees.present?
def create_samples(enterprises)
log "Creating fees:"
enterprises.each do |enterprise|
next if enterprise.enterprise_fees.present?
log "- #{enterprise.name} charges markup"
calculator = Calculator::FlatPercentPerItem.new(preferred_flat_percent: 10)
create_fee(enterprise, calculator)
calculator.save!
log "- #{enterprise.name} charges markup"
calculator = Calculator::FlatPercentPerItem.new(preferred_flat_percent: 10)
create_fee(enterprise, calculator)
calculator.save!
end
end
private
def create_fee(enterprise, calculator)
fee = enterprise.enterprise_fees.new(
fee_type: "sales",
name: "markup",
inherits_tax_category: true,
)
fee.calculator = calculator
fee.save!
end
end
private
def create_fee(enterprise, calculator)
fee = enterprise.enterprise_fees.new(
fee_type: "sales",
name: "markup",
inherits_tax_category: true,
)
fee.calculator = calculator
fee.save!
end
end

View File

@@ -1,41 +1,43 @@
require "tasks/sample_data/addressing"
require "tasks/sample_data/logging"
class GroupFactory
include Logging
include Addressing
module SampleData
class GroupFactory
include Logging
include Addressing
def create_samples
log "Creating groups"
return if EnterpriseGroup.where(name: "Producer group").exists?
def create_samples
log "Creating groups"
return if EnterpriseGroup.where(name: "Producer group").exists?
create_group(
{
name: "Producer group",
owner: enterprises.first.owner,
on_front_page: true,
description: "The seed producers"
},
"6 Rollings Road, Upper Ferntree Gully, 3156"
)
end
create_group(
{
name: "Producer group",
owner: enterprises.first.owner,
on_front_page: true,
description: "The seed producers"
},
"6 Rollings Road, Upper Ferntree Gully, 3156"
)
end
private
private
def create_group(params, group_address)
group = EnterpriseGroup.new(params)
group.address = address(group_address)
group.enterprises = enterprises
group.save!
end
def create_group(params, group_address)
group = EnterpriseGroup.new(params)
group.address = address(group_address)
group.enterprises = enterprises
group.save!
end
def enterprises
@enterprises ||= Enterprise.where(
name: [
"Fred's Farm",
"Freddy's Farm Shop",
"Fredo's Farm Hub"
]
)
def enterprises
@enterprises ||= Enterprise.where(
name: [
"Fred's Farm",
"Freddy's Farm Shop",
"Fredo's Farm Hub"
]
)
end
end
end

View File

@@ -1,34 +1,36 @@
require "tasks/sample_data/logging"
class InventoryFactory
include Logging
module SampleData
class InventoryFactory
include Logging
def create_samples(products)
log "Creating inventories"
marys_shop = Enterprise.find_by(name: "Mary's Online Shop")
products.each do |product|
create_item(marys_shop, product)
def create_samples(products)
log "Creating inventories"
marys_shop = Enterprise.find_by(name: "Mary's Online Shop")
products.each do |product|
create_item(marys_shop, product)
end
end
private
def create_item(shop, product)
InventoryItem.create_with(
enterprise: shop,
variant: product.variants.first,
visible: true
).find_or_create_by!(variant_id: product.variants.first.id)
create_override(shop, product)
end
def create_override(shop, product)
VariantOverride.create_with(
variant: product.variants.first,
hub: shop,
price: 12,
on_demand: false,
count_on_hand: 5
).find_or_create_by!(variant_id: product.variants.first.id)
end
end
private
def create_item(shop, product)
InventoryItem.create_with(
enterprise: shop,
variant: product.variants.first,
visible: true
).find_or_create_by!(variant_id: product.variants.first.id)
create_override(shop, product)
end
def create_override(shop, product)
VariantOverride.create_with(
variant: product.variants.first,
hub: shop,
price: 12,
on_demand: false,
count_on_hand: 5
).find_or_create_by!(variant_id: product.variants.first.id)
end
end

View File

@@ -1,111 +1,113 @@
require "tasks/sample_data/logging"
class OrderCycleFactory
include Logging
# rubocop:disable Metrics/MethodLength
def create_samples
log "Creating order cycles"
create_order_cycle(
"Freddy's Farm Shop OC",
"Freddy's Farm Shop",
["Freddy's Farm Shop"],
["Freddy's Farm Shop"],
receival_instructions: "Dear self, don't forget the keys.",
pickup_time: "the weekend",
pickup_instructions: "Bring your own shopping bags or boxes."
)
module SampleData
class OrderCycleFactory
include Logging
# rubocop:disable Metrics/MethodLength
def create_samples
log "Creating order cycles"
create_order_cycle(
"Freddy's Farm Shop OC",
"Freddy's Farm Shop",
["Freddy's Farm Shop"],
["Freddy's Farm Shop"],
receival_instructions: "Dear self, don't forget the keys.",
pickup_time: "the weekend",
pickup_instructions: "Bring your own shopping bags or boxes."
)
create_order_cycle(
"Fredo's Farm Hub OC",
"Fredo's Farm Hub",
["Fred's Farm", "Fredo's Farm Hub"],
["Fredo's Farm Hub"],
receival_instructions: "Under the shed, please.",
pickup_time: "Wednesday 2pm",
pickup_instructions: "Boxes for packaging under the roof."
)
create_order_cycle(
"Fredo's Farm Hub OC",
"Fredo's Farm Hub",
["Fred's Farm", "Fredo's Farm Hub"],
["Fredo's Farm Hub"],
receival_instructions: "Under the shed, please.",
pickup_time: "Wednesday 2pm",
pickup_instructions: "Boxes for packaging under the roof."
)
create_order_cycle(
"Mary's Online Shop OC",
"Mary's Online Shop",
["Fred's Farm", "Freddy's Farm Shop", "Fredo's Farm Hub"],
["Mary's Online Shop"],
receival_instructions: "Please shut the gate.",
pickup_time: "midday"
)
create_order_cycle(
"Mary's Online Shop OC",
"Mary's Online Shop",
["Fred's Farm", "Freddy's Farm Shop", "Fredo's Farm Hub"],
["Mary's Online Shop"],
receival_instructions: "Please shut the gate.",
pickup_time: "midday"
)
create_order_cycle(
"Multi Shop OC",
"Mary's Online Shop",
["Fred's Farm", "Freddy's Farm Shop", "Fredo's Farm Hub"],
["Mary's Online Shop", "Maryse's Private Shop"],
receival_instructions: "Please shut the gate.",
pickup_time: "dusk"
)
end
# rubocop:enable Metrics/MethodLength
private
def create_order_cycle(name, coordinator_name, supplier_names, distributor_names, data)
coordinator = Enterprise.find_by(name: coordinator_name)
return if OrderCycle.active.where(name: name).exists?
log "- #{name}"
cycle = create_order_cycle_with_fee(name, coordinator)
create_exchanges(cycle, supplier_names, distributor_names, data)
end
def create_order_cycle_with_fee(name, coordinator)
cycle = OrderCycle.create!(
name: name,
orders_open_at: 1.day.ago,
orders_close_at: 1.month.from_now,
coordinator: coordinator
)
cycle.coordinator_fees << coordinator.enterprise_fees.first
cycle
end
def create_exchanges(cycle, supplier_names, distributor_names, data)
suppliers = Enterprise.where(name: supplier_names)
distributors = Enterprise.where(name: distributor_names)
incoming = incoming_exchanges(cycle, suppliers, data)
outgoing = outgoing_exchanges(cycle, distributors, data)
all_exchanges = incoming + outgoing
add_products(suppliers, all_exchanges)
end
def incoming_exchanges(cycle, suppliers, data)
suppliers.map do |supplier|
Exchange.create!(
order_cycle: cycle,
sender: supplier,
receiver: cycle.coordinator,
incoming: true,
receival_instructions: data[:receival_instructions]
create_order_cycle(
"Multi Shop OC",
"Mary's Online Shop",
["Fred's Farm", "Freddy's Farm Shop", "Fredo's Farm Hub"],
["Mary's Online Shop", "Maryse's Private Shop"],
receival_instructions: "Please shut the gate.",
pickup_time: "dusk"
)
end
end
# rubocop:enable Metrics/MethodLength
def outgoing_exchanges(cycle, distributors, data)
distributors.map do |distributor|
Exchange.create!(
order_cycle: cycle,
sender: cycle.coordinator,
receiver: distributor,
incoming: false,
pickup_time: data[:pickup_time],
pickup_instructions: data[:pickup_instructions]
)
private
def create_order_cycle(name, coordinator_name, supplier_names, distributor_names, data)
coordinator = Enterprise.find_by(name: coordinator_name)
return if OrderCycle.active.where(name: name).exists?
log "- #{name}"
cycle = create_order_cycle_with_fee(name, coordinator)
create_exchanges(cycle, supplier_names, distributor_names, data)
end
end
def add_products(suppliers, exchanges)
products = suppliers.flat_map(&:supplied_products)
products.each do |product|
exchanges.each { |exchange| exchange.variants << product.variants.first }
def create_order_cycle_with_fee(name, coordinator)
cycle = OrderCycle.create!(
name: name,
orders_open_at: 1.day.ago,
orders_close_at: 1.month.from_now,
coordinator: coordinator
)
cycle.coordinator_fees << coordinator.enterprise_fees.first
cycle
end
def create_exchanges(cycle, supplier_names, distributor_names, data)
suppliers = Enterprise.where(name: supplier_names)
distributors = Enterprise.where(name: distributor_names)
incoming = incoming_exchanges(cycle, suppliers, data)
outgoing = outgoing_exchanges(cycle, distributors, data)
all_exchanges = incoming + outgoing
add_products(suppliers, all_exchanges)
end
def incoming_exchanges(cycle, suppliers, data)
suppliers.map do |supplier|
Exchange.create!(
order_cycle: cycle,
sender: supplier,
receiver: cycle.coordinator,
incoming: true,
receival_instructions: data[:receival_instructions]
)
end
end
def outgoing_exchanges(cycle, distributors, data)
distributors.map do |distributor|
Exchange.create!(
order_cycle: cycle,
sender: cycle.coordinator,
receiver: distributor,
incoming: false,
pickup_time: data[:pickup_time],
pickup_instructions: data[:pickup_instructions]
)
end
end
def add_products(suppliers, exchanges)
products = suppliers.flat_map(&:supplied_products)
products.each do |product|
exchanges.each { |exchange| exchange.variants << product.variants.first }
end
end
end
end

View File

@@ -3,84 +3,86 @@
require "tasks/sample_data/logging"
require "tasks/sample_data/addressing"
class OrderFactory
include Logging
include Addressing
module SampleData
class OrderFactory
include Logging
include Addressing
def create_samples
log "Creating orders"
@order_cycle = OrderCycle.find_by(name: "Fredo's Farm Hub OC")
@distributor = Enterprise.find_by(name: "Fredo's Farm Hub")
@email = "new.customer@example.org"
def create_samples
log "Creating orders"
@order_cycle = OrderCycle.find_by(name: "Fredo's Farm Hub OC")
@distributor = Enterprise.find_by(name: "Fredo's Farm Hub")
@email = "new.customer@example.org"
log "- cart order"
create_cart_order
log "- cart order"
create_cart_order
log "- complete order - not paid"
create_complete_order
log "- complete order - not paid"
create_complete_order
log "- complete order - paid"
order = create_complete_order
order.payments.first.capture!
log "- complete order - paid"
order = create_complete_order
order.payments.first.capture!
log "- complete order - delivery"
order = create_complete_order
order.select_shipping_method(delivery_shipping_method_id)
order.save
log "- complete order - delivery"
order = create_complete_order
order.select_shipping_method(delivery_shipping_method_id)
order.save
log "- complete order - shipped"
order = create_complete_order
order.payments.first.capture!
order.save
order.shipment.reload.ship!
end
log "- complete order - shipped"
order = create_complete_order
order.payments.first.capture!
order.save
order.shipment.reload.ship!
end
private
private
def create_cart_order
order = create_order
order.save
order
end
def create_cart_order
order = create_order
order.save
order
end
def create_complete_order
order = create_cart_order
OrderWorkflow.new(order).complete
order
end
def create_complete_order
order = create_cart_order
OrderWorkflow.new(order).complete
order
end
def create_order
order = Spree::Order.create!(
email: @email,
order_cycle: @order_cycle,
distributor: @distributor,
bill_address: order_address,
ship_address: order_address
)
order.line_items.create(variant_id: first_variant.id, quantity: 5)
order.payments.create(payment_method_id: first_payment_method_id)
order
end
def create_order
order = Spree::Order.create!(
email: @email,
order_cycle: @order_cycle,
distributor: @distributor,
bill_address: order_address,
ship_address: order_address
)
order.line_items.create(variant_id: first_variant.id, quantity: 5)
order.payments.create(payment_method_id: first_payment_method_id)
order
end
def first_variant
# First variant on the first outgoing exchange of the OC
@order_cycle.exchanges.outgoing.first.variants.first
end
def first_variant
# First variant on the first outgoing exchange of the OC
@order_cycle.exchanges.outgoing.first.variants.first
end
def first_payment_method_id
# First payment method of the distributor
@distributor.payment_methods.first.id
end
def first_payment_method_id
# First payment method of the distributor
@distributor.payment_methods.first.id
end
def delivery_shipping_method_id
@distributor.shipping_methods.find_by(name: "Home delivery Fredo's Farm Hub").id
end
def delivery_shipping_method_id
@distributor.shipping_methods.find_by(name: "Home delivery Fredo's Farm Hub").id
end
def order_address
address = address("25 Myrtle Street, Bayswater, 3153")
address.firstname = "John"
address.lastname = "Mistery"
address.phone = "0987654321"
address
def order_address
address = address("25 Myrtle Street, Bayswater, 3153")
address.firstname = "John"
address.lastname = "Mistery"
address.phone = "0987654321"
address
end
end
end

View File

@@ -1,56 +1,58 @@
require "tasks/sample_data/addressing"
require "tasks/sample_data/logging"
class PaymentMethodFactory
include Logging
include Addressing
module SampleData
class PaymentMethodFactory
include Logging
include Addressing
def create_samples(enterprises)
log "Creating payment methods:"
distributors = enterprises.select(&:is_distributor)
distributors.each do |enterprise|
create_payment_methods(enterprise)
def create_samples(enterprises)
log "Creating payment methods:"
distributors = enterprises.select(&:is_distributor)
distributors.each do |enterprise|
create_payment_methods(enterprise)
end
end
private
def create_payment_methods(enterprise)
return if enterprise.payment_methods.present?
log "- #{enterprise.name}"
create_cash_method(enterprise)
create_card_method(enterprise)
end
def create_cash_method(enterprise)
create_payment_method(
Spree::PaymentMethod::Check,
enterprise,
"Cash on collection",
"Pay on collection!",
::Calculator::FlatRate.new
)
end
def create_card_method(enterprise)
create_payment_method(
Spree::Gateway::Bogus,
enterprise,
"Credit card (fake)",
"We charge 1%, but won't ask for your details. ;-)",
::Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 1)
)
end
def create_payment_method(provider_class, enterprise, name, description, calculator)
payment_method = provider_class.new(
name: name,
description: description,
environment: Rails.env,
distributor_ids: [enterprise.id]
)
calculator.calculable = payment_method
calculator.save!
end
end
private
def create_payment_methods(enterprise)
return if enterprise.payment_methods.present?
log "- #{enterprise.name}"
create_cash_method(enterprise)
create_card_method(enterprise)
end
def create_cash_method(enterprise)
create_payment_method(
Spree::PaymentMethod::Check,
enterprise,
"Cash on collection",
"Pay on collection!",
::Calculator::FlatRate.new
)
end
def create_card_method(enterprise)
create_payment_method(
Spree::Gateway::Bogus,
enterprise,
"Credit card (fake)",
"We charge 1%, but won't ask for your details. ;-)",
::Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 1)
)
end
def create_payment_method(provider_class, enterprise, name, description, calculator)
payment_method = provider_class.new(
name: name,
description: description,
environment: Rails.env,
distributor_ids: [enterprise.id]
)
calculator.calculable = payment_method
calculator.save!
end
end

View File

@@ -1,33 +1,35 @@
require "tasks/sample_data/logging"
class PermissionFactory
include Logging
module SampleData
class PermissionFactory
include Logging
def create_samples(enterprises)
all_permissions = [
:add_to_order_cycle,
:manage_products,
:edit_profile,
:create_variant_overrides
]
enterprises.each do |enterprise|
log "#{enterprise.name} permits everybody to do everything."
enterprise_permits_to(enterprise, enterprises, all_permissions)
def create_samples(enterprises)
all_permissions = [
:add_to_order_cycle,
:manage_products,
:edit_profile,
:create_variant_overrides
]
enterprises.each do |enterprise|
log "#{enterprise.name} permits everybody to do everything."
enterprise_permits_to(enterprise, enterprises, all_permissions)
end
end
end
private
private
def enterprise_permits_to(enterprise, receivers, permissions)
receivers.each do |receiver|
EnterpriseRelationship.where(
parent_id: enterprise,
child_id: receiver
).first_or_create!(
parent: enterprise,
child: receiver,
permissions_list: permissions
)
def enterprise_permits_to(enterprise, receivers, permissions)
receivers.each do |receiver|
EnterpriseRelationship.where(
parent_id: enterprise,
child_id: receiver
).first_or_create!(
parent: enterprise,
child: receiver,
permissions_list: permissions
)
end
end
end
end

View File

@@ -1,89 +1,91 @@
require "tasks/sample_data/logging"
class ProductFactory
include Logging
module SampleData
class ProductFactory
include Logging
def create_samples(enterprises)
log "Creating products:"
product_data(enterprises).map do |hash|
create_product(hash)
def create_samples(enterprises)
log "Creating products:"
product_data(enterprises).map do |hash|
create_product(hash)
end
end
private
def product_data(enterprises)
vegetables = Spree::Taxon.find_by(name: 'Vegetables')
fruit = Spree::Taxon.find_by(name: 'Fruit')
fungi = Spree::Taxon.find_by(name: 'Fungi')
producers = enterprises.select(&:is_primary_producer)
distributors = enterprises.select(&:is_distributor)
[
{
name: 'Garlic',
price: 20.00,
supplier: producers[0],
taxons: [vegetables],
distributor: distributors[0]
},
{
name: 'Fuji Apple',
price: 5.00,
supplier: producers[1],
taxons: [fruit],
distributor: distributors[0]
},
{
name: 'Mushrooms',
price: 50.00,
supplier: producers[1],
taxons: [fungi],
distributor: distributors[0]
},
{
name: 'Carrots',
price: 3.00,
supplier: producers[2],
taxons: [vegetables],
distributor: distributors[0]
},
{
name: 'Potatoes',
price: 2.00,
supplier: producers[2],
taxons: [vegetables],
distributor: distributors[0]
},
{
name: 'Tomatoes',
price: 2.00,
supplier: producers[2],
taxons: [vegetables],
distributor: distributors[0]
}
]
end
def create_product(hash)
log "- #{hash[:name]}"
params = hash.merge(
supplier_id: hash[:supplier].id,
primary_taxon_id: hash[:taxons].first.id,
variant_unit: "weight",
variant_unit_scale: 1,
unit_value: 1,
shipping_category: DefaultShippingCategory.find_or_create,
tax_category_id: find_or_create_tax_category.id
)
product = Spree::Product.create_with(params).find_or_create_by!(name: params[:name])
product.variants.first.update_attribute :on_demand, true
product
end
def find_or_create_tax_category
tax_category_name = "Tax Category"
tax_category = Spree::TaxCategory.find_by(name: tax_category_name)
tax_category ||= Spree::TaxCategory.create!(name: tax_category_name)
tax_category
end
end
private
def product_data(enterprises)
vegetables = Spree::Taxon.find_by(name: 'Vegetables')
fruit = Spree::Taxon.find_by(name: 'Fruit')
fungi = Spree::Taxon.find_by(name: 'Fungi')
producers = enterprises.select(&:is_primary_producer)
distributors = enterprises.select(&:is_distributor)
[
{
name: 'Garlic',
price: 20.00,
supplier: producers[0],
taxons: [vegetables],
distributor: distributors[0]
},
{
name: 'Fuji Apple',
price: 5.00,
supplier: producers[1],
taxons: [fruit],
distributor: distributors[0]
},
{
name: 'Mushrooms',
price: 50.00,
supplier: producers[1],
taxons: [fungi],
distributor: distributors[0]
},
{
name: 'Carrots',
price: 3.00,
supplier: producers[2],
taxons: [vegetables],
distributor: distributors[0]
},
{
name: 'Potatoes',
price: 2.00,
supplier: producers[2],
taxons: [vegetables],
distributor: distributors[0]
},
{
name: 'Tomatoes',
price: 2.00,
supplier: producers[2],
taxons: [vegetables],
distributor: distributors[0]
}
]
end
def create_product(hash)
log "- #{hash[:name]}"
params = hash.merge(
supplier_id: hash[:supplier].id,
primary_taxon_id: hash[:taxons].first.id,
variant_unit: "weight",
variant_unit_scale: 1,
unit_value: 1,
shipping_category: DefaultShippingCategory.find_or_create,
tax_category_id: find_or_create_tax_category.id
)
product = Spree::Product.create_with(params).find_or_create_by!(name: params[:name])
product.variants.first.update_attribute :on_demand, true
product
end
def find_or_create_tax_category
tax_category_name = "Tax Category"
tax_category = Spree::TaxCategory.find_by(name: tax_category_name)
tax_category ||= Spree::TaxCategory.create!(name: tax_category_name)
tax_category
end
end

View File

@@ -1,56 +1,58 @@
require "tasks/sample_data/addressing"
require "tasks/sample_data/logging"
class ShippingMethodFactory
include Logging
include Addressing
module SampleData
class ShippingMethodFactory
include Logging
include Addressing
def create_samples(enterprises)
log "Creating shipping methods:"
distributors = enterprises.select(&:is_distributor)
distributors.each do |enterprise|
create_shipping_methods(enterprise)
def create_samples(enterprises)
log "Creating shipping methods:"
distributors = enterprises.select(&:is_distributor)
distributors.each do |enterprise|
create_shipping_methods(enterprise)
end
end
private
def create_shipping_methods(enterprise)
return if enterprise.shipping_methods.present?
log "- #{enterprise.name}"
create_pickup(enterprise)
create_delivery(enterprise)
end
def create_pickup(enterprise)
create_shipping_method(
enterprise,
name: "Pickup #{enterprise.name}",
description: "pick-up at your awesome hub gathering place",
require_ship_address: false,
calculator_type: "Calculator::Weight"
)
end
def create_delivery(enterprise)
delivery = create_shipping_method(
enterprise,
name: "Home delivery #{enterprise.name}",
description: "yummy food delivered at your door",
require_ship_address: true,
calculator_type: "Calculator::FlatRate"
)
delivery.calculator.preferred_amount = 2
delivery.calculator.save!
end
def create_shipping_method(enterprise, params)
params[:distributor_ids] = [enterprise.id]
method = enterprise.shipping_methods.new(params)
method.zones << zone
method.shipping_categories << Spree::ShippingCategory.find_or_create_by(name: 'Default')
method.save!
method
end
end
private
def create_shipping_methods(enterprise)
return if enterprise.shipping_methods.present?
log "- #{enterprise.name}"
create_pickup(enterprise)
create_delivery(enterprise)
end
def create_pickup(enterprise)
create_shipping_method(
enterprise,
name: "Pickup #{enterprise.name}",
description: "pick-up at your awesome hub gathering place",
require_ship_address: false,
calculator_type: "Calculator::Weight"
)
end
def create_delivery(enterprise)
delivery = create_shipping_method(
enterprise,
name: "Home delivery #{enterprise.name}",
description: "yummy food delivered at your door",
require_ship_address: true,
calculator_type: "Calculator::FlatRate"
)
delivery.calculator.preferred_amount = 2
delivery.calculator.save!
end
def create_shipping_method(enterprise, params)
params[:distributor_ids] = [enterprise.id]
method = enterprise.shipping_methods.new(params)
method.zones << zone
method.shipping_categories << Spree::ShippingCategory.find_or_create_by(name: 'Default')
method.save!
method
end
end

View File

@@ -1,27 +1,29 @@
require "tasks/sample_data/logging"
class TaxonFactory
include 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)
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
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

View File

@@ -1,40 +1,42 @@
require "tasks/sample_data/logging"
class UserFactory
include Logging
module SampleData
class UserFactory
include Logging
def create_samples
log "Creating users:"
usernames.map { |name|
create_user(name)
}.to_h
end
def create_samples
log "Creating users:"
usernames.map { |name|
create_user(name)
}.to_h
end
private
private
def usernames
[
"Manel Super Admin",
"Penny Profile",
"Fred Farmer",
"Freddy Shop Farmer",
"Fredo Hub Farmer",
"Mary Retailer",
"Maryse Private",
"Jane Customer"
]
end
def usernames
[
"Manel Super Admin",
"Penny Profile",
"Fred Farmer",
"Freddy Shop Farmer",
"Fredo Hub Farmer",
"Mary Retailer",
"Maryse Private",
"Jane Customer"
]
end
def create_user(name)
email = "#{name.downcase.tr(' ', '.')}@example.org"
password = Spree::User.friendly_token
log "- #{email}"
user = Spree::User.create_with(
password: password,
password_confirmation: password,
confirmation_sent_at: Time.zone.now,
confirmed_at: Time.zone.now
).find_or_create_by!(email: email)
[name, user]
def create_user(name)
email = "#{name.downcase.tr(' ', '.')}@example.org"
password = Spree::User.friendly_token
log "- #{email}"
user = Spree::User.create_with(
password: password,
password_confirmation: password,
confirmation_sent_at: Time.zone.now,
confirmed_at: Time.zone.now
).find_or_create_by!(email: email)
[name, user]
end
end
end