Sha256: 60c8b3c2f41e367cd3039e95407b135d9c19ae9e0dfaa8bd7a8af56aee7b7469

Contents?: true

Size: 1.19 KB

Versions: 6

Compression:

Stored size: 1.19 KB

Contents

module Spree
  class Promotion
    module Actions
      class CreateLineItems < PromotionAction
        has_many :promotion_action_line_items, :foreign_key => :promotion_action_id

        attr_accessor :line_items_string

        def perform(options = {})
          return unless order = options[:order]
          promotion_action_line_items.each do |item|
            current_quantity = order.quantity_of(item.variant)
            if current_quantity < item.quantity
              order.add_variant(item.variant, item.quantity - current_quantity)
              order.update!
            end
          end
        end

        def line_items_string
          promotion_action_line_items.map { |li| "#{li.variant_id}x#{li.quantity}" }.join(',')
        end

        def line_items_string=(value)
          promotion_action_line_items.destroy_all
          value.to_s.split(',').each do |str|
            variant_id, quantity = str.split('x')
            if variant_id && quantity && variant = Variant.find_by_id(variant_id)
              promotion_action_line_items.create({:variant => variant, :quantity => quantity.to_i}, :without_protection => true)
            end
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
spree_promo-1.2.4 app/models/spree/promotion/actions/create_line_items.rb
spree_promo-1.2.3 app/models/spree/promotion/actions/create_line_items.rb
spree_promo-1.2.2 app/models/spree/promotion/actions/create_line_items.rb
spree_promo-1.2.0 app/models/spree/promotion/actions/create_line_items.rb
spree_promo-1.2.0.rc2 app/models/spree/promotion/actions/create_line_items.rb
spree_promo-1.2.0.rc1 app/models/spree/promotion/actions/create_line_items.rb