Sha256: c36109beca1d2c237e101722f4c1ff839f730be7b3c6595e9cbe0436f14338f9

Contents?: true

Size: 1004 Bytes

Versions: 34

Compression:

Stored size: 1004 Bytes

Contents

# frozen_string_literal: true

module Spree
  class Promotion < Spree::Base
    module Rules
      class FirstOrder < PromotionRule
        attr_reader :user, :email

        def applicable?(promotable)
          promotable.is_a?(Spree::Order)
        end

        def eligible?(order, options = {})
          @user = order.try(:user) || options[:user]
          @email = order.email

          if user || email
            if !completed_orders.blank? && completed_orders.first != order
              eligibility_errors.add(:base, eligibility_error_message(:not_first_order))
            end
          else
            eligibility_errors.add(:base, eligibility_error_message(:no_user_or_email_specified))
          end

          eligibility_errors.empty?
        end

        private

        def completed_orders
          user ? user.orders.complete : orders_by_email
        end

        def orders_by_email
          Spree::Order.where(email: email).complete
        end
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 2 rubygems

Version Path
solidus_core-2.8.6 app/models/spree/promotion/rules/first_order.rb
solidus_core-2.6.6 app/models/spree/promotion/rules/first_order.rb
solidus_core-2.7.4 app/models/spree/promotion/rules/first_order.rb
solidus_core-2.8.5 app/models/spree/promotion/rules/first_order.rb
solidus_core-2.7.3 app/models/spree/promotion/rules/first_order.rb
solidus_core-2.6.5 app/models/spree/promotion/rules/first_order.rb
solidus_core-2.8.4 app/models/spree/promotion/rules/first_order.rb
solidus_core-2.8.3 app/models/spree/promotion/rules/first_order.rb
solidus_core-2.7.2 app/models/spree/promotion/rules/first_order.rb
solidus_core-2.6.4 app/models/spree/promotion/rules/first_order.rb
solidus_core-2.8.2 app/models/spree/promotion/rules/first_order.rb
solidus_core-2.8.1 app/models/spree/promotion/rules/first_order.rb
solidus_core-2.6.3 app/models/spree/promotion/rules/first_order.rb
solidus_core-2.8.0 app/models/spree/promotion/rules/first_order.rb
solidus_core-2.7.1 app/models/spree/promotion/rules/first_order.rb
solidus_core-2.6.2 app/models/spree/promotion/rules/first_order.rb
solidus_core_devise_token_auth-2.8.0.alpha.14 app/models/spree/promotion/rules/first_order.rb
solidus_core_devise_token_auth-2.8.0.alpha.13 app/models/spree/promotion/rules/first_order.rb
solidus_core_devise_token_auth-2.8.0.alpha.12 app/models/spree/promotion/rules/first_order.rb
solidus_core_devise_token_auth-2.8.0.alpha.11 app/models/spree/promotion/rules/first_order.rb