Class: Caboose::Category
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Caboose::Category
- Defined in:
- app/models/caboose/category.rb
Constant Summary collapse
- STATUS_ACTIVE =
'Active'
- STATUS_INACTIVE =
'Inactive'
Class Method Summary collapse
- .options(site_id) ⇒ Object
- .options_helper(cat, prefix) ⇒ Object
-
.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
.options(site_id) ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'app/models/caboose/category.rb', line 94 def self.(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.(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.(cat, prefix) return [] if cat.nil? arr = [{ :value => cat.id, :text => "#{prefix}#{cat.name}" }] cat.children.each do |c| arr = arr + self.(c, "#{prefix} - ") end return arr end |
.root ⇒ Object
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_level ⇒ Object
58 59 60 |
# File 'app/models/caboose/category.rb', line 58 def self.top_level self.root.children end |
Instance Method Details
#active_products ⇒ Object
83 84 85 |
# File 'app/models/caboose/category.rb', line 83 def active_products self.products.where(:status => 'Active') end |
#ancestry ⇒ Object
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_slug ⇒ Object
Instance Methods
70 71 72 |
# File 'app/models/caboose/category.rb', line 70 def generate_slug self.name.gsub(' ', '-').downcase end |
#update_child_slugs ⇒ Object
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 |