Class: Caboose::Category
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Caboose::Category
- Defined in:
- app/models/caboose/category.rb
Class Method Summary collapse
-
.root ⇒ Object
Class Methods.
- .sample(number) ⇒ Object
- .top_level ⇒ Object
Instance Method Summary collapse
- #active_products ⇒ Object
- #ancestry ⇒ Object
-
#generate_slug ⇒ Object
Instance Methods.
- #update_child_slugs ⇒ Object
Class Method Details
.root ⇒ Object
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_level ⇒ Object
53 54 55 |
# File 'app/models/caboose/category.rb', line 53 def self.top_level self.root.children end |
Instance Method Details
#active_products ⇒ Object
78 79 80 |
# File 'app/models/caboose/category.rb', line 78 def active_products self.products.where(:status => 'Active') end |
#ancestry ⇒ Object
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_slug ⇒ Object
Instance Methods
65 66 67 |
# File 'app/models/caboose/category.rb', line 65 def generate_slug self.name.gsub(' ', '-').downcase end |
#update_child_slugs ⇒ Object
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 |