Class: BlogCategory
- Inherits:
-
Object
- Object
- BlogCategory
- Includes:
- LuckySneaks::StringExtensions, Mongoid::Document, Mongoid::Timestamps, Mongoid::Tree
- Defined in:
- app/models/blog_category.rb
Class Method Summary collapse
-
.by_name(value) ⇒ Array
Returns the blog categories matching the specified name.
-
.by_slug(value) ⇒ Array
Returns the blog categories matching the specified slug.
-
.children ⇒ Array
Overrides children to sort by name.
-
.roots ⇒ Array
Overrides roots to sort by name.
-
.roots_with_posts ⇒ Array
Returns the root blog categories that have posts.
Instance Method Summary collapse
-
#all_posts ⇒ Array
Returns all posts belonging to this blog category and its children.
-
#children_with_posts ⇒ Array
Returns child blog categories that have posts.
-
#has_posts? ⇒ Boolean
Returns true if this blog category has any posts.
-
#parent_category=(name) ⇒ Object
Sets the specified category as this blog category’s parent.
-
#parent_id=(value) ⇒ Object
Override the parent-ID setter to accept nil as a string.
-
#path ⇒ String
Returns the path for this blog category.
-
#subcategory? ⇒ Boolean
Returns true if this blog category has a parent.
-
#url ⇒ Object
deprecated
Deprecated.
Please use #path instead
Class Method Details
.by_name(value) ⇒ Array
Returns the blog categories matching the specified name.
41 42 43 |
# File 'app/models/blog_category.rb', line 41 def self.by_name value self.find :first, :conditions => {:name => value} end |
.by_slug(value) ⇒ Array
Returns the blog categories matching the specified slug.
49 50 51 |
# File 'app/models/blog_category.rb', line 49 def self.by_slug value self.find :first, :conditions => {:slug => /^#{value}$/i} end |
.children ⇒ Array
Overrides children to sort by name.
24 25 26 |
# File 'app/models/blog_category.rb', line 24 def self.children super.asc :name end |
.roots ⇒ Array
Overrides roots to sort by name.
31 32 33 |
# File 'app/models/blog_category.rb', line 31 def self.roots super.asc :name end |
.roots_with_posts ⇒ Array
Returns the root blog categories that have posts.
56 57 58 |
# File 'app/models/blog_category.rb', line 56 def self.roots_with_posts self.roots.asc(:name).select{ |c| c.has_posts? } end |
Instance Method Details
#all_posts ⇒ Array
Returns all posts belonging to this blog category and its children.
98 99 100 |
# File 'app/models/blog_category.rb', line 98 def all_posts (self.posts + self.children.map{ |c| c.posts }).uniq.flatten end |
#children_with_posts ⇒ Array
Returns child blog categories that have posts.
65 66 67 |
# File 'app/models/blog_category.rb', line 65 def children_with_posts self.children.asc(:name).select{ |c| c.has_posts? } end |
#has_posts? ⇒ Boolean
Returns true if this blog category has any posts.
81 82 83 |
# File 'app/models/blog_category.rb', line 81 def has_posts? ! self.all_posts.blank? end |
#parent_category=(name) ⇒ Object
Sets the specified category as this blog category’s parent.
88 89 90 91 92 93 |
# File 'app/models/blog_category.rb', line 88 def parent_category=(name) unless name.blank? self.parent = BlogCategory.find_or_create_by :name => name self.save end end |
#parent_id=(value) ⇒ Object
Override the parent-ID setter to accept nil as a string.
72 73 74 |
# File 'app/models/blog_category.rb', line 72 def parent_id= value self[:parent_id] = value == 'nil' ? nil : value end |
#path ⇒ String
Returns the path for this blog category.
105 106 107 |
# File 'app/models/blog_category.rb', line 105 def path "#{Blog.first.path}/topics/#{self.slug}" end |
#subcategory? ⇒ Boolean
Returns true if this blog category has a parent.
112 113 114 |
# File 'app/models/blog_category.rb', line 112 def subcategory? ! self.root? end |
#url ⇒ Object
Please use #path instead
117 118 119 120 |
# File 'app/models/blog_category.rb', line 117 def url warn "[DEPRECATION] `url` is deprecated. Please use `path` instead." self.path end |