Class: BlogCategory

Inherits:
Object
  • Object
show all
Includes:
LuckySneaks::StringExtensions, 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



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.

Parameters:

  • the (String)

    slug to search for

Returns:

  • (Array)

    the matching blog categories



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

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



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

def self.children
  super.asc :name
end

.rootsArray

Overrides roots to sort by name.

Returns:

  • (Array)

    this blog category’s roots sorted by name



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

def self.roots
  super.asc :name
end

.roots_with_postsArray

Returns the root blog categories that have posts.

Returns:

  • (Array)

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

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

Returns:

  • (Array)

    all posts for this blog category



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_postsArray

Returns child blog categories that have posts.

Returns:

  • (Array)

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

Returns:

  • (Boolean)

    true if any posts exist



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.

Parameters:

  • the (String)

    parent category’s name



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.

Parameters:

  • parent (String)

    ID



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

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



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.

Returns:

  • (Boolean)

    true if subcategory



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

def subcategory?
  ! self.root?
end

#urlObject

Deprecated.

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