Sha256: b26476ff1bac6d5998996b8d4254da8652450ba6372970d701d2a902cceaa561

Contents?: true

Size: 1.19 KB

Versions: 3

Compression:

Stored size: 1.19 KB

Contents

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

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

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

          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.for_store(store).complete : orders_by_email
        end

        def orders_by_email
          store.orders.where(email: email).complete
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spree_core-4.10.1 app/models/spree/promotion/rules/first_order.rb
spree_core-4.10.0 app/models/spree/promotion/rules/first_order.rb
spree_core-4.9.0 app/models/spree/promotion/rules/first_order.rb