Sha256: 3018182ebceafe4b45a68cc875317108e5575765e0f9339ce9cfca7f3c32739b

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.1.6 app/models/spree/promotion/actions/create_line_items.rb
spree_promo-1.1.5 app/models/spree/promotion/actions/create_line_items.rb
spree_promo-1.1.4 app/models/spree/promotion/actions/create_line_items.rb
spree_promo-1.1.3 app/models/spree/promotion/actions/create_line_items.rb
spree_promo-1.1.2 app/models/spree/promotion/actions/create_line_items.rb
spree_promo-1.1.2.rc1 app/models/spree/promotion/actions/create_line_items.rb