Sha256: e43f6b3c702d9516ec51059c2f4aa8a492099476aaa8d17f86b7a0f9bf7bf52b

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

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

        delegate :eligible?, :to => :promotion

        attr_accessor :line_items_string

        def perform(options = {})
          return unless order = options[:order]
          return unless eligible?(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

1 entries across 1 versions & 1 rubygems

Version Path
spree_promo-1.2.5 app/models/spree/promotion/actions/create_line_items.rb