Sha256: e7fc0821207cf9b70089be07349bd04ad3a70056c829d4a06f6034107389bf0c

Contents?: true

Size: 1020 Bytes

Versions: 26

Compression:

Stored size: 1020 Bytes

Contents

module Spree
  class Promotion
    module Rules
      class UserRole < PromotionRule
        preference :role_ids, :array, default: []

        MATCH_POLICIES = %w(any all)
        preference :match_policy, default: MATCH_POLICIES.first

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

        def eligible?(order, _options = {})
          return false unless order.user
          if all_match_policy?
            match_all_roles?(order)
          else
            match_any_roles?(order)
          end
        end

        private

        def all_match_policy?
          preferred_match_policy == 'all' && preferred_role_ids.present?
        end

        def user_roles(order)
          order.user.spree_roles.where(id: preferred_role_ids)
        end

        def match_all_roles?(order)
          user_roles(order).count == preferred_role_ids.count
        end

        def match_any_roles?(order)
          user_roles(order).exists?
        end
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
solidus_core-2.2.0 app/models/spree/promotion/rules/user_role.rb
solidus_core-2.2.0.rc1 app/models/spree/promotion/rules/user_role.rb
solidus_core-2.2.0.beta1 app/models/spree/promotion/rules/user_role.rb
solidus_core-2.1.0 app/models/spree/promotion/rules/user_role.rb
solidus_core-2.1.0.rc1 app/models/spree/promotion/rules/user_role.rb
solidus_core-2.1.0.beta1 app/models/spree/promotion/rules/user_role.rb