Class: BlogCategory

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps, Mongoid::Tree
Defined in:
app/models/blog_category.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.by_name(value) ⇒ Array

Returns the blog categories matching the specified name.

Parameters:

  • the (String)

    name to search for

Returns:

  • (Array)

    the matching blog categories



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.

Parameters:

  • the (String)

    slug to search for

Returns:

  • (Array)

    the matching blog categories



48
49
50
# File 'app/models/blog_category.rb', line 48

def self.by_slug value
  self.find :first, :conditions => {:slug => /^#{value}$/i}
end

.childrenArray

Overrides children to sort by name.

Returns:

  • (Array)

    this blog category’s children sorted by name



23
24
25
# File 'app/models/blog_category.rb', line 23

def self.children
  super.asc :name
end

.rootsArray

Overrides roots to sort by name.

Returns:

  • (Array)

    this blog category’s roots sorted by name



30
31
32
# File 'app/models/blog_category.rb', line 30

def self.roots
  super.asc :name
end

.roots_with_postsArray

Returns the root blog categories that have posts.

Returns:

  • (Array)

    blog categories with 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_postsArray

Returns all posts belonging to this blog category and its children.

Returns:

  • (Array)

    all posts for this blog category



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_postsArray

Returns child blog categories that have posts.

Returns:

  • (Array)

    child blog categories with 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.

Returns:

  • (Boolean)

    true if any posts exist



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.

Parameters:

  • the (String)

    parent category’s name



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.

Parameters:

  • parent (String)

    ID



71
72
73
# File 'app/models/blog_category.rb', line 71

def parent_id= value
  self[:parent_id] = value == 'nil' ? nil : value
end

#pathString

Returns the path for this blog category.

Returns:

  • (String)

    this blog category’s path



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.

Returns:

  • (Boolean)

    true if subcategory



111
112
113
# File 'app/models/blog_category.rb', line 111

def subcategory?
  ! self.root?
end

#urlObject

Deprecated.

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