Sha256: 866fe666c4d4ee32024d53443a917e545a5194bea3a7017ef5014f3562c346d7

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

require "spree/preferences/persistable"

module SolidusFriendlyPromotions
  class PromotionRule < Spree::Base
    include Spree::Preferences::Persistable

    belongs_to :promotion

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

    validate :unique_per_promotion, on: :create

    def preload_relations
      []
    end

    def applicable?(_promotable)
      raise NotImplementedError, "applicable? should be implemented in a sub-class of SolidusFriendlyPromotions::Rule"
    end

    def eligible?(_promotable, _options = {})
      raise NotImplementedError, "eligible? should be implemented in a sub-class of SolidusFriendlyPromotions::Rule"
    end

    def level
      raise NotImplementedError, "level should be implemented in a sub-class of SolidusFriendlyPromotions::Rule"
    end

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

    def to_partial_path
      "solidus_friendly_promotions/admin/promotion_rules/rules/#{model_name.element}"
    end

    def updateable?
      preferences.any?
    end

    private

    def unique_per_promotion
      return unless self.class.exists?(promotion_id: promotion_id, type: self.class.name)

      errors[:base] << "Promotion already contains this rule type"
    end

    def eligibility_error_message(key, options = {})
      I18n.t(key, scope: [:solidus_friendly_promotions, :eligibility_errors, self.class.name.underscore], **options)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
solidus_friendly_promotions-1.0.0 app/models/solidus_friendly_promotions/promotion_rule.rb
solidus_friendly_promotions-1.0.0.rc.3 app/models/solidus_friendly_promotions/promotion_rule.rb
solidus_friendly_promotions-1.0.0.rc.2 app/models/solidus_friendly_promotions/promotion_rule.rb