Class: BlogCategory
- Inherits:
-
Object
- Object
- BlogCategory
- Includes:
- 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.
40 41 42 |
# File 'app/models/blog_category.rb', line 40 def self.by_name value self.find :first, :conditions => {:name => value} end |
.by_slug(value) ⇒ Array
Returns the blog categories matching the specified slug.
48 49 50 |
# File 'app/models/blog_category.rb', line 48 def self.by_slug value self.find :first, :conditions => {:slug => /^#{value}$/i} end |
.children ⇒ Array
Overrides children to sort by name.
23 24 25 |
# File 'app/models/blog_category.rb', line 23 def self.children super.asc :name end |
.roots ⇒ Array
Overrides roots to sort by name.
30 31 32 |
# File 'app/models/blog_category.rb', line 30 def self.roots super.asc :name end |
.roots_with_posts ⇒ Array
Returns the root blog categories that have posts.
55 56 57 |
# File 'app/models/blog_category.rb', line 55 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.
97 98 99 |
# File 'app/models/blog_category.rb', line 97 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.
64 65 66 |
# File 'app/models/blog_category.rb', line 64 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.
80 81 82 |
# File 'app/models/blog_category.rb', line 80 def has_posts? ! self.all_posts.blank? end |
#parent_category=(name) ⇒ Object
Sets the specified category as this blog category’s parent.
87 88 89 90 91 92 |
# File 'app/models/blog_category.rb', line 87 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.
71 72 73 |
# File 'app/models/blog_category.rb', line 71 def parent_id= value self[:parent_id] = value == 'nil' ? nil : value end |
#path ⇒ String
Returns the path for this blog category.
104 105 106 |
# File 'app/models/blog_category.rb', line 104 def path "#{Blog.first.path}/topics/#{self.slug}" end |
#subcategory? ⇒ Boolean
Returns true if this blog category has a parent.
111 112 113 |
# File 'app/models/blog_category.rb', line 111 def subcategory? ! self.root? end |
#url ⇒ Object
Please use #path instead
116 117 118 119 |
# File 'app/models/blog_category.rb', line 116 def url warn "[DEPRECATION] `url` is deprecated. Please use `path` instead." self.path end |