Class: Spree::Promotion::Actions::FreeShipping
- Inherits:
-
Spree::PromotionAction
- Object
- ActiveRecord::Base
- Base
- Spree::PromotionAction
- Spree::Promotion::Actions::FreeShipping
- Defined in:
- app/models/spree/promotion/actions/free_shipping.rb
Instance Method Summary collapse
- #compute_amount(shipment) ⇒ Object
- #label ⇒ Object
- #perform(payload = {}) ⇒ Object
-
#remove_from(order) ⇒ void
Removes any adjustments generated by this action from the order’s shipments.
Methods inherited from Spree::PromotionAction
Methods included from Spree::ParanoiaDeprecations
#paranoia_delete, #paranoia_destroy
Methods inherited from Base
display_includes, #initialize_preference_defaults, page, preference
Methods included from Spree::Preferences::Preferable
#admin_form_preference_names, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference
Instance Method Details
#compute_amount(shipment) ⇒ Object
31 32 33 |
# File 'app/models/spree/promotion/actions/free_shipping.rb', line 31 def compute_amount(shipment) shipment.cost * -1 end |
#label ⇒ Object
27 28 29 |
# File 'app/models/spree/promotion/actions/free_shipping.rb', line 27 def label "#{I18n.t('spree.promotion')} (#{promotion.name})" end |
#perform(payload = {}) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'app/models/spree/promotion/actions/free_shipping.rb', line 7 def perform(payload = {}) order = payload[:order] promotion_code = payload[:promotion_code] results = order.shipments.map do |shipment| return false if promotion_credit_exists?(shipment) shipment.adjustments.create!( order: shipment.order, amount: compute_amount(shipment), source: self, promotion_code: promotion_code, label: label ) true end # Did we actually end up applying any adjustments? # If so, then this action should be classed as 'successful' results.any? { |r| r == true } end |
#remove_from(order) ⇒ void
This method returns an undefined value.
Removes any adjustments generated by this action from the order’s
shipments.
39 40 41 42 43 44 45 46 47 |
# File 'app/models/spree/promotion/actions/free_shipping.rb', line 39 def remove_from(order) order.shipments.each do |shipment| shipment.adjustments.each do |adjustment| if adjustment.source == self shipment.adjustments.destroy(adjustment) end end end end |