Class: SpreeCmCommissioner::PricingRules::BuildTemplate

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree_cm_commissioner/pricing_rules/build_template.rb

Class Method Summary collapse

Class Method Details

.call(pricing_model:, template_key:) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/services/spree_cm_commissioner/pricing_rules/build_template.rb', line 4

def self.call(pricing_model:, template_key:)
  template = PRICING_RULE_GROUP_TEMPLATES[template_key.to_sym]
  return unless template

  group = pricing_model.pricing_rule_groups.build

  template[:rules]&.each do |rule_data|
    group.pricing_rules.where(type: rule_data[:type]).build(
      type: rule_data[:type],
      private_metadata: rule_data[:private_metadata] || {}
    )
  end

  if template[:action]
    calculator = template[:action][:calculator_type].constantize.new
    BuildParams.set_preferences(calculator, template[:action][:preferences])
    group.build_pricing_action(calculator: calculator)
  end

  group
end