Module: Scopes::Product

Included in:
Product
Defined in:
lib/scopes/product.rb

Constant Summary collapse

SCOPES =

TODO: change this to array pairs so we preserve order?

{
  # Scopes for selecting products based on taxon
  :taxon => {
    :taxons_name_eq => [:taxon_name],
    :in_taxons => [:taxon_names],
  },
  # product selection based on name, or search
  :search => {
    :in_name => [:words],
    :in_name_or_keywords => [:words],
    :in_name_or_description => [:words],
    :with_ids => [:ids]
  },
  # Scopes for selecting products based on option types and properties
  :values => {
    :with => [:value],
    :with_property => [:property],
    :with_property_value => [:property, :value],
    :with_option => [:option],
    :with_option_value => [:option, :value],
  },
  # product selection based upon master price
  :price => {
    :price_between => [:low, :high],
    :master_price_lte => [:amount],
    :master_price_gte => [:amount],
  },
}
ORDERING =
[
  :ascend_by_updated_at,
  :descend_by_updated_at,
  :ascend_by_name,
  :descend_by_name,
  :ascend_by_master_price,
  :descend_by_master_price,
  :descend_by_popularity,
]
ATTRIBUTE_HELPER_METHODS =
{
  :with_ids => :product_picker_field
}

Class Method Summary collapse

Class Method Details

.arguments_for_scope_name(name) ⇒ Object



241
242
243
244
245
# File 'lib/scopes/product.rb', line 241

def self.arguments_for_scope_name(name)
  if group = Scopes::Product::SCOPES.detect{|k,v| v[name.to_sym]}
    group[1][name.to_sym]
  end
end

.get_taxons(*ids_or_records_or_names) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/scopes/product.rb', line 247

def self.get_taxons(*ids_or_records_or_names)
  ids_or_records_or_names.flatten.map { |t|
    case t
    when Integer then Taxon.find_by_id(t)
    when ActiveRecord::Base then t
    when String
      Taxon.find_by_name(t) ||
      Taxon.find(:first, :conditions => [
        "taxons.permalink LIKE ? OR taxons.permalink = ?", "%/#{t}/", "#{t}/"
      ])
    end
  }.compact.uniq
end

.prepare_taxon_conditions(taxons) ⇒ Object

specifically avoid having an order for taxon search (conflicts with main order)



262
263
264
265
# File 'lib/scopes/product.rb', line 262

def self.prepare_taxon_conditions(taxons)
  ids = taxons.map{|taxon| taxon.self_and_descendants.map(&:id)}.flatten.uniq
  { :joins => :taxons, :conditions => ["taxons.id IN (?)", ids] }
end

.prepare_words(words) ⇒ Object

Produce an array of keywords for use in scopes. Always return array with at least an empty string to avoid SQL errors



235
236
237
238
239
# File 'lib/scopes/product.rb', line 235

def self.prepare_words(words)
  return [''] if words.blank?
  a = words.split(/[,\s]/).map(&:strip)
  a.any? ? a : ['']
end