Class: ProductScope

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

Overview

ProductScope is model for storing named scopes with their arguments, to be used with ProductGroups.

Each product Scope can be applied to Product (or product scope) with #apply_on method which returns new combined named scope

Instance Method Summary collapse

Methods included from Scopes::Dynamic

price_scopes_for

Instance Method Details

#apply_on(another_scope) ⇒ Object

Applies product scope on Product model or another named scope



25
26
27
# File 'app/models/product_scope.rb', line 25

def apply_on(another_scope)
  another_scope.send(self.name, *self.arguments)
end

#check_validity_of_scopeObject

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



37
38
39
40
41
42
# File 'app/models/product_scope.rb', line 37

def check_validity_of_scope
  errors.add(:name, "is not a valid scope name") unless Product.condition?(self.name)
  apply_on(Product).limit(0) != nil
rescue Exception
  errors.add(:arguments, "are incorrect")
end

#is_ordering?Boolean

test ordering scope by looking for name pattern or missed arguments

Returns:

  • (Boolean)


45
46
47
# File 'app/models/product_scope.rb', line 45

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

#productsObject

Get all products with this scope



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

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

#to_sObject



55
56
57
# File 'app/models/product_scope.rb', line 55

def to_s
  to_sentence
end

#to_sentenceObject



49
50
51
52
53
# File 'app/models/product_scope.rb', line 49

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