Sha256: 1fadca7cfbcb6068d4c095e24c0dbe637982e859a905ad73b949ccd3ac88ef0e

Contents?: true

Size: 1.5 KB

Versions: 37

Compression:

Stored size: 1.5 KB

Contents

# A rule to limit a promotion based on products in the order.
# Can require all or any of the products to be present.
# Valid products either come from assigned product group or are assingned directly to the rule.
module Spree
  class Promotion
    module Rules
      class Product < PromotionRule
        has_and_belongs_to_many :products, :class_name => '::Spree::Product', :join_table => 'spree_products_promotion_rules', :foreign_key => 'promotion_rule_id'
        validate :only_one_promotion_per_product

        MATCH_POLICIES = %w(any all)
        preference :match_policy, :string, :default => MATCH_POLICIES.first

        # scope/association that is used to test eligibility
        def eligible_products
          products
        end

        def eligible?(order, options = {})
          return true if eligible_products.empty?
          if preferred_match_policy == 'all'
            eligible_products.all? {|p| order.products.include?(p) }
          else
            order.products.any? {|p| eligible_products.include?(p) }
          end
        end

        def product_ids_string
          product_ids.join(',')
        end

        def product_ids_string=(s)
          self.product_ids = s.to_s.split(',').map(&:strip)
        end

        private

          def only_one_promotion_per_product
            if Spree::Promotion::Rules::Product.all.map(&:products).flatten.uniq!
              errors[:base] << "You can't create two promotions for the same product"
            end
          end
      end
    end
  end
end

Version data entries

37 entries across 37 versions & 3 rubygems

Version Path
spree_core-2.0.13 app/models/spree/promotion/rules/product.rb
spree_core-2.0.12 app/models/spree/promotion/rules/product.rb
spree_core-2.0.11 app/models/spree/promotion/rules/product.rb
spree_core-2.0.10 app/models/spree/promotion/rules/product.rb
spree_core-2.0.9 app/models/spree/promotion/rules/product.rb
spree_promo-1.3.5 app/models/spree/promotion/rules/product.rb
spree_core-2.0.8 app/models/spree/promotion/rules/product.rb
spree_core-2.0.7 app/models/spree/promotion/rules/product.rb
spree_core-2.0.6 app/models/spree/promotion/rules/product.rb
spree_promo-1.3.4 app/models/spree/promotion/rules/product.rb
spree_core-2.0.5 app/models/spree/promotion/rules/product.rb
spree_core-2.0.4 app/models/spree/promotion/rules/product.rb
spree_promo-1.2.5 app/models/spree/promotion/rules/product.rb
spree_promo-1.3.3 app/models/spree/promotion/rules/product.rb
spree_core-2.0.3 app/models/spree/promotion/rules/product.rb
spree_core-2.0.2 app/models/spree/promotion/rules/product.rb
spree_core-2.0.1 app/models/spree/promotion/rules/product.rb
spree_core-2.0.0 app/models/spree/promotion/rules/product.rb
spree_core-2.0.0.rc1 app/models/spree/promotion/rules/product.rb
spree_promo-1.1.6 app/models/spree/promotion/rules/product.rb