Class: Spree::ProductScope

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
Scopes::Dynamic
Defined in:
app/models/spree/product_scope.rb,
app/models/spree/product_scope/scopes.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Scopes::Dynamic

price_scopes_for

Class Method Details

.all_scopesObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/spree/product_scope/scopes.rb', line 10

def self.all_scopes
  {
    # 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],
    },
  }
end

.arguments_for_scope_name(name) ⇒ Object



41
42
43
44
45
# File 'app/models/spree/product_scope/scopes.rb', line 41

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

Instance Method Details

#apply_on(another_scope) ⇒ Object

Applies product scope on Spree::Product model or another named scope



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/spree/product_scope.rb', line 26

def apply_on(another_scope)
  array = Array.wrap(self.arguments)
  if Product.respond_to?(self.name.intern)
    relation2 = if (array.blank? || array.size < 2)
                  if Product.method(self.name.intern).arity == 0
                    Product.send(self.name.intern)
                  else
                    Product.send(self.name.intern, array.try(:first))
                  end
                else
                    Product.send(self.name.intern, *array)
                end
  else
    relation2 = Product.metasearch({ self.name.intern => array.join("") }).relation
  end
  unless another_scope.class == ActiveRecord::Relation
    another_scope = another_scope.send(:relation)
  end
  another_scope.merge(relation2)
end

#check_validity_of_scopeObject

checks validity of the named scope (if its safe and can be applied on Spree::Product)



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/spree/product_scope.rb', line 48

def check_validity_of_scope
  errors.add(:name, 'is not a valid scope name') unless Product.respond_to?(self.name.intern)
  apply_on(Product).limit(0) != nil
rescue Exception => e
  unless Rails.env.production?

    puts "name: #{self.name}"
    puts "arguments: #{self.arguments.inspect}"
    puts e.message
    puts e.backtrace
  end
  errors.add(:arguments, 'are incorrect')
end

#is_ordering?Boolean

test ordering scope by looking for name pattern or missed arguments

Returns:

  • (Boolean)


63
64
65
# File 'app/models/spree/product_scope.rb', line 63

def is_ordering?
  name =~ /^(ascend_by|descend_by)/ || arguments.blank?
end

#productsObject

Get all products with this scope



19
20
21
22
23
# File 'app/models/spree/product_scope.rb', line 19

def products
  if Product.respond_to?(name)
    Product.send(name, *arguments)
  end
end

#to_sObject



73
74
75
# File 'app/models/spree/product_scope.rb', line 73

def to_s
  to_sentence
end

#to_sentenceObject



67
68
69
70
71
# File 'app/models/spree/product_scope.rb', line 67

def to_sentence
  result = I18n.t(:sentence, :scope => [:product_scopes, :scopes, self.name], :default => '')
  result = I18n.t(:name, :scope => [:product_scopes, :scopes, self.name]) if result.blank?
  result % [*self.arguments]
end