Sha256: 7eab20416625a46d5f59e98d3c279b49e915eb00f54dfaa658ff2c585a1de40b

Contents?: true

Size: 1.48 KB

Versions: 62

Compression:

Stored size: 1.48 KB

Contents

module Workarea
  module Pricing
    class Discount
      # This class determines whether an item can qualify for a
      # {Pricing::Discount::ProductAttribute} discount.
      #
      class ProductAttribute::ItemQualifier
        attr_reader :discount, :item
        delegate :attribute_name, :attribute_value, to: :discount

        def initialize(discount, item)
          @discount = discount
          @item = item
        end

        # Whether the item qualifies, as defined by an optionaize comparision
        # of keys and value or any element of the array if the item has an
        # array.
        #
        # @return [Boolean]
        #
        def qualifies?
          !!details.detect do |key, value|
            next unless key.to_s.optionize == attribute_name.optionize

            if value.respond_to?(:any?)
              value.any? { |v| v.to_s.optionize == attribute_value.optionize }
            else
              value.to_s.optionize == attribute_value.optionize
            end
          end
        end

        private

        # Ugliness ensues to deal with not having the product model here, but
        # wanting to respect Mongoid localization.
        #
        def details
          return {} unless item.product_attributes.present?
          all_details = item.product_attributes['details']

          if all_details.key?(I18n.locale.to_s)
            all_details[I18n.locale.to_s]
          else
            all_details
          end
        end
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.4.13 app/models/workarea/pricing/discount/product_attribute/item_qualifier.rb
workarea-core-3.4.12 app/models/workarea/pricing/discount/product_attribute/item_qualifier.rb