Class: Spree::Catalogs::SetOrderMinimums
- Inherits:
-
Object
- Object
- Spree::Catalogs::SetOrderMinimums
- Includes:
- ServiceModule::Base
- Defined in:
- app/services/spree/catalogs/set_order_minimums.rb
Overview
Replaces a catalog's order minimums with the given set.
A currency absent from the payload has its minimum lifted, so the whole set arrives in one request — the agreement editor stages every term behind the catalog's Save, and applying half of them would leave the agreement in a state the merchant never asked for.
Instance Method Summary collapse
-
#call(catalog:, order_minimums:) ⇒ Spree::ServiceModule::Result
Value is the catalog.
Methods included from ServiceModule::Base
Instance Method Details
#call(catalog:, order_minimums:) ⇒ Spree::ServiceModule::Result
Returns value is the catalog.
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 |
# File 'app/services/spree/catalogs/set_order_minimums.rb', line 15 def call(catalog:, order_minimums:) rows = Array(order_minimums).reject { |row| row[:currency].blank? } invalid = nil ApplicationRecord.transaction do keep = rows.map { |row| row[:currency].to_s.upcase } catalog.order_minimums.where.not(currency: keep).destroy_all if keep.any? catalog.order_minimums.destroy_all if keep.empty? rows.each do |row| minimum = catalog.order_minimums.find_or_initialize_by(currency: row[:currency].to_s.upcase) minimum.amount = row[:amount] next if minimum.save # A plain service's `failure` returns rather than raising, so a # bad row has to take the transaction down itself — otherwise the # rest of the set applies and the caller is told it all worked. invalid = minimum raise ActiveRecord::Rollback end end return failure(invalid) if invalid success(catalog.reload) end |