Sha256: 26ca72625d58392b6475ac5683b4b0aba0a4c5d49db5712c49eb27d56ae4212a

Contents?: true

Size: 1.32 KB

Versions: 28

Compression:

Stored size: 1.32 KB

Contents

class Spree::PromotionBuilder
  include ActiveModel::Model

  attr_reader :promotion
  attr_accessor :base_code, :number_of_codes, :user

  validates :number_of_codes,
    numericality: { only_integer: true, greater_than: 0 },
    allow_nil: true

  validate :promotion_validity

  class_attribute :code_builder_class
  self.code_builder_class = ::Spree::PromotionCode::CodeBuilder

  # @param promotion_attrs [Hash] The desired attributes for the newly promotion
  # @param attributes [Hash] The desired attributes for this builder
  # @param user [Spree::User] The user who triggered this promotion build
  def initialize(attributes={}, promotion_attributes={})
    @promotion = Spree::Promotion.new(promotion_attributes)
    super(attributes)
  end

  def perform
    if can_build_codes?
      @promotion.codes = code_builder.build_promotion_codes
    end

    return false unless valid?

    @promotion.save
  end

  def number_of_codes= value
    @number_of_codes = value.presence.try(:to_i)
  end

  private

  def promotion_validity
    if !@promotion.valid?
      @promotion.errors.each do |attribute, error|
        errors[attribute].push error
      end
    end
  end

  def can_build_codes?
    @base_code && @number_of_codes
  end

  def code_builder
    self.class.code_builder_class.new(@promotion, @base_code, @number_of_codes)
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
solidus_core-1.2.3 app/models/spree/promotion_builder.rb
solidus_core-1.1.4 app/models/spree/promotion_builder.rb
solidus_core-1.0.7 app/models/spree/promotion_builder.rb
solidus_core-1.0.6 app/models/spree/promotion_builder.rb
solidus_core-1.2.2 app/models/spree/promotion_builder.rb
solidus_core-1.2.1 app/models/spree/promotion_builder.rb
solidus_core-1.1.3 app/models/spree/promotion_builder.rb
solidus_core-1.0.5 app/models/spree/promotion_builder.rb
solidus_core-1.2.0 app/models/spree/promotion_builder.rb
solidus_core-1.2.0.rc2 app/models/spree/promotion_builder.rb
solidus_core-1.0.4 app/models/spree/promotion_builder.rb
solidus_core-1.1.2 app/models/spree/promotion_builder.rb
solidus_core-1.2.0.rc1 app/models/spree/promotion_builder.rb
solidus_core-1.2.0.beta1 app/models/spree/promotion_builder.rb
solidus_core-1.0.3 app/models/spree/promotion_builder.rb
solidus_core-1.1.1 app/models/spree/promotion_builder.rb
solidus_core-1.1.0 app/models/spree/promotion_builder.rb
solidus_core-1.1.0.pre2 app/models/spree/promotion_builder.rb
solidus_core-1.1.0.pre1 app/models/spree/promotion_builder.rb
solidus_core-1.1.0.beta1 app/models/spree/promotion_builder.rb