Sha256: 8a5ca359d198efd56d6f976df33c57d894d37b167dd671baff68f3045f35a079

Contents?: true

Size: 1.82 KB

Versions: 30

Compression:

Stored size: 1.82 KB

Contents

require 'product_group_search/core_field_strategy'
require 'product_group_search/custom_field_strategy'
require 'product_group_search/base_field'
require 'product_group_search/text_field'
require 'product_group_search/number_field'
require 'product_group_search/date_field'

module ProductGroupSearch
  extend ActiveSupport::Concern

  included do

    # query_column is the value of name column in product_group_condition for core fields.
    # For custom fields this value will be overridden
    attr_reader :query_column

    # field_handler could be TextField, NumberField or any other field handler.
    attr_reader :field_handler

    # index is used by custom_field_strategy
    attr_accessor :index

    delegate  :where, :valid_value_data_type?, :valid_operator?, to: :field_handler

    validate  :validate_value_data_type
    validate  :validate_operator

    after_initialize  :prepare_instance
  end

  def name=(val)
    val = val.try(:to_s)
    if super(val)
      prepare_instance
    end
  end

  def summary
    I18n.t(self.operator.to_sym, { field: localized_name, value: self.value })
  end

  private

  def validate_operator
    self.errors.add(:operator, :invalid) unless valid_operator?(operator)
  end

  def validate_value_data_type
    self.errors.add(:value, :invalid) unless valid_value_data_type?
  end

  def prepare_instance
    set_strategy
    set_field_handler
  end

  def set_strategy
    extend(custom_field? ? CustomFieldStrategy : CoreFieldStrategy)
  end

  def set_field_handler
    klass = ProductGroupSearch.const_get("#{field_type.classify}Field")
    @field_handler = klass.new(self)
  end

  def query_field
    target_table.send(:[], query_column)
  end

  def custom_field?
    # for a custom field the name column contains the primary key of the custom_fields table
    name.try(:match, /\A\d+?\Z/).present?
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
nimbleshop_core-0.0.23 lib/nimbleshop/product_group_search.rb
nimbleshop_core-0.0.21 lib/nimbleshop/product_group_search.rb
nimbleshop_core-0.0.20 lib/nimbleshop/product_group_search.rb
nimbleshop_core-0.0.19 lib/nimbleshop/product_group_search.rb
nimbleshop_core-0.0.17 lib/nimbleshop/product_group_search.rb
nimbleshop_core-0.0.16 lib/nimbleshop/product_group_search.rb
nimbleshop_core-0.0.15 lib/nimbleshop/product_group_search.rb
nimbleshop_core-0.0.14 lib/nimbleshop/product_group_search.rb
nimbleshop_core-0.0.14.rc2 lib/nimbleshop/product_group_search.rb
nimbleshop_core-0.0.14.rc1 lib/nimbleshop/product_group_search.rb
nimbleshop_core-0.0.13 lib/nimbleshop/product_group_search.rb
nimbleshop_core-0.0.12 lib/nimbleshop/product_group_search.rb
nimbleshop_core-0.0.11 lib/nimbleshop/product_group_search.rb
nimbleshop_core-0.0.10 lib/nimbleshop/product_group_search.rb
nimbleshop_core-0.0.9 lib/nimbleshop/product_group_search.rb
nimbleshop_core-0.0.8 lib/nimbleshop/product_group_search.rb
nimbleshop_core-0.0.7 lib/nimbleshop/product_group_search.rb
nimbleshop_core-0.0.5 lib/nimbleshop/product_group_search.rb
nimbleshop_core-0.0.4.beta1 lib/nimbleshop/product_group_search.rb
nimbleshop_core-0.0.4 lib/nimbleshop/product_group_search.rb