Class: Caboose::Category

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/caboose/category.rb

Constant Summary collapse

STATUS_ACTIVE =
'Active'
STATUS_INACTIVE =
'Inactive'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.options(site_id) ⇒ Object



94
95
96
97
98
99
100
101
# File 'app/models/caboose/category.rb', line 94

def self.options(site_id)            
  cat = Category.where("site_id = ? and parent_id is null", site_id).first      
  if cat.nil?
    cat = Category.create(:site_id => site_id, :name => 'All Products', :url => '/')
  end
  arr = self.options_helper(cat, '')
  return arr
end

.options_helper(cat, prefix) ⇒ Object



103
104
105
106
107
108
109
110
# File 'app/models/caboose/category.rb', line 103

def self.options_helper(cat, prefix)
  return [] if cat.nil?
  arr = [{ :value => cat.id, :text => "#{prefix}#{cat.name}" }]      
  cat.children.each do |c|
    arr = arr + self.options_helper(c, "#{prefix} - ")        
  end
  return arr
end

.rootObject

Class Methods



54
55
56
# File 'app/models/caboose/category.rb', line 54

def self.root
  self.find_by_url('/products')
end

.sample(number) ⇒ Object



62
63
64
# File 'app/models/caboose/category.rb', line 62

def self.sample(number)
  Caboose::Category.top_level.collect { |category| category if category.active_products.any? }.compact.sample(number)
end

.top_levelObject



58
59
60
# File 'app/models/caboose/category.rb', line 58

def self.top_level
  self.root.children
end

Instance Method Details

#active_productsObject



83
84
85
# File 'app/models/caboose/category.rb', line 83

def active_products
  self.products.where(:status => 'Active')      
end

#ancestryObject



87
88
89
90
91
92
# File 'app/models/caboose/category.rb', line 87

def ancestry
  return [self] if self.parent.nil?
  ancestors = self.parent.ancestry
  ancestors << self
  return ancestors 
end

#generate_slugObject

Instance Methods



70
71
72
# File 'app/models/caboose/category.rb', line 70

def generate_slug
  self.name.gsub(' ', '-').downcase
end

#update_child_slugsObject



74
75
76
77
78
79
80
81
# File 'app/models/caboose/category.rb', line 74

def update_child_slugs
  return if self.children.nil? or self.children.empty?
  
  self.children.each do |child|
    child.update_attribute(:url, "#{self.url}/#{child.slug}")
    child.update_child_slugs
  end
end