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.5.27 app/models/workarea/pricing/discount/product_attribute/item_qualifier.rb
workarea-core-3.5.26 app/models/workarea/pricing/discount/product_attribute/item_qualifier.rb
workarea-core-3.4.45 app/models/workarea/pricing/discount/product_attribute/item_qualifier.rb
workarea-core-3.5.25 app/models/workarea/pricing/discount/product_attribute/item_qualifier.rb
workarea-core-3.5.23 app/models/workarea/pricing/discount/product_attribute/item_qualifier.rb
workarea-core-3.4.44 app/models/workarea/pricing/discount/product_attribute/item_qualifier.rb
workarea-core-3.5.22 app/models/workarea/pricing/discount/product_attribute/item_qualifier.rb
workarea-core-3.4.43 app/models/workarea/pricing/discount/product_attribute/item_qualifier.rb
workarea-core-3.5.21 app/models/workarea/pricing/discount/product_attribute/item_qualifier.rb
workarea-core-3.4.42 app/models/workarea/pricing/discount/product_attribute/item_qualifier.rb
workarea-core-3.5.20 app/models/workarea/pricing/discount/product_attribute/item_qualifier.rb
workarea-core-3.4.41 app/models/workarea/pricing/discount/product_attribute/item_qualifier.rb
workarea-core-3.5.19 app/models/workarea/pricing/discount/product_attribute/item_qualifier.rb
workarea-core-3.4.40 app/models/workarea/pricing/discount/product_attribute/item_qualifier.rb
workarea-core-3.5.18 app/models/workarea/pricing/discount/product_attribute/item_qualifier.rb
workarea-core-3.4.39 app/models/workarea/pricing/discount/product_attribute/item_qualifier.rb
workarea-core-3.5.17 app/models/workarea/pricing/discount/product_attribute/item_qualifier.rb
workarea-core-3.4.38 app/models/workarea/pricing/discount/product_attribute/item_qualifier.rb
workarea-core-3.5.16 app/models/workarea/pricing/discount/product_attribute/item_qualifier.rb
workarea-core-3.4.37 app/models/workarea/pricing/discount/product_attribute/item_qualifier.rb