Class: Caboose::Category

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.rootObject

Class Methods



49
50
51
# File 'app/models/caboose/category.rb', line 49

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

.sample(number) ⇒ Object



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

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

.top_levelObject



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

def self.top_level
  self.root.children
end

Instance Method Details

#active_productsObject



78
79
80
# File 'app/models/caboose/category.rb', line 78

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

#ancestryObject



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

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

#generate_slugObject

Instance Methods



65
66
67
# File 'app/models/caboose/category.rb', line 65

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

#update_child_slugsObject



69
70
71
72
73
74
75
76
# File 'app/models/caboose/category.rb', line 69

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