/
shipping_method_factory.rb
60 lines (51 loc) · 1.62 KB
/
shipping_method_factory.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# frozen_string_literal: true
require "tasks/sample_data/addressing"
require "tasks/sample_data/logging"
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)
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
end