Sha256: dd330551addaf38b34e31dea032fc4a15b00d14a01b11b380b44f93391c2d0ca

Contents?: true

Size: 1.28 KB

Versions: 58

Compression:

Stored size: 1.28 KB

Contents

# Base class for all promotion rules
module Spree
  class PromotionRule < Spree::Base
    belongs_to :promotion, class_name: 'Spree::Promotion', inverse_of: :promotion_rules

    scope :of_type, ->(t) { where(type: t) }

    validate :unique_per_promotion, on: :create

    def self.for(promotable)
      all.select { |rule| rule.applicable?(promotable) }
    end

    def applicable?(promotable)
      raise 'applicable? should be implemented in a sub-class of Spree::PromotionRule'
    end

    def eligible?(promotable, options = {})
      raise 'eligible? should be implemented in a sub-class of Spree::PromotionRule'
    end

    # This states if a promotion can be applied to the specified line item
    # It is true by default, but can be overridden by promotion rules to provide conditions
    def actionable?(line_item)
      true
    end

    def eligibility_errors
      @eligibility_errors ||= ActiveModel::Errors.new(self)
    end

    private
    def unique_per_promotion
      if Spree::PromotionRule.exists?(promotion_id: promotion_id, type: self.class.name)
        errors[:base] << "Promotion already contains this rule type"
      end
    end

    def eligibility_error_message(key, options = {})
      Spree.t(key, Hash[scope: [:eligibility_errors, :messages]].merge(options))
    end
  end
end

Version data entries

58 entries across 58 versions & 1 rubygems

Version Path
spree_core-3.3.0.rc4 app/models/spree/promotion_rule.rb
spree_core-3.3.0.rc3 app/models/spree/promotion_rule.rb
spree_core-3.1.7 app/models/spree/promotion_rule.rb
spree_core-3.2.3 app/models/spree/promotion_rule.rb
spree_core-3.2.2 app/models/spree/promotion_rule.rb
spree_core-3.3.0.rc2 app/models/spree/promotion_rule.rb
spree_core-3.1.6 app/models/spree/promotion_rule.rb
spree_core-3.3.0.rc1 app/models/spree/promotion_rule.rb
spree_core-3.1.5 app/models/spree/promotion_rule.rb
spree_core-3.2.1 app/models/spree/promotion_rule.rb
spree_core-3.2.0 app/models/spree/promotion_rule.rb
spree_core-3.1.4 app/models/spree/promotion_rule.rb
spree_core-3.2.0.rc3 app/models/spree/promotion_rule.rb
spree_core-3.2.0.rc2 app/models/spree/promotion_rule.rb
spree_core-3.2.0.rc1 app/models/spree/promotion_rule.rb
spree_core-3.1.3 app/models/spree/promotion_rule.rb
spree_core-3.1.2 app/models/spree/promotion_rule.rb
spree_core-3.0.10 app/models/spree/promotion_rule.rb
spree_core-3.1.1 app/models/spree/promotion_rule.rb
spree_core-3.1.0 app/models/spree/promotion_rule.rb