Module: Plate::BloggingHelper

Included in:
View
Defined in:
lib/plate/helpers/blogging_helper.rb

Overview

Various view helpers related to blogging. Tags, categories, archives and the like.

Instance Method Summary collapse

Instance Method Details

#category_archives(category) ⇒ Object

Returns a customized hash of post archives for the given category



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/plate/helpers/blogging_helper.rb', line 7

def category_archives(category)
  result = {}

  posts.archives.keys.each do |year|
    posts.archives[year].keys.each do |month|
      posts.archives[year][month].keys.each do |day|
        posts.archives[year][month][day].each do |post|
          if post.category == category
            result[year] ||= {}
            result[year][month] ||= {}
            result[year][month][day] ||= []
            result[year][month][day] << post
          end
        end
      end
    end
  end

  result
end

#html_escape(text) ⇒ Object Also known as: xml_escape

Escape markup for use in XML or HTML



29
30
31
# File 'lib/plate/helpers/blogging_helper.rb', line 29

def html_escape(text)
  ::CGI.escapeHTML(text)
end

#month_name(year, month) ⇒ Object



34
35
36
# File 'lib/plate/helpers/blogging_helper.rb', line 34

def month_name(year, month)
  Date.new(year.to_i, month.to_i, 1).strftime('%B %Y')
end

#next_postObject

Grab the next blog post.

Returns nil if this is the last post.



41
42
43
44
# File 'lib/plate/helpers/blogging_helper.rb', line 41

def next_post
  return nil if post_index < 0 or post_index >= post_count
  @next_post ||= self.posts[post_index + 1]
end

#post_countObject

The total number blog posts



55
56
57
# File 'lib/plate/helpers/blogging_helper.rb', line 55

def post_count
  @post_count ||= self.posts.length
end

#post_indexObject

Returns a number of where this post is in all posts



60
61
62
# File 'lib/plate/helpers/blogging_helper.rb', line 60

def post_index
  @post_index ||= self.posts.index(self.post)
end

#posts_for_category(category) ⇒ Object

Find all posts for the given category



65
66
67
# File 'lib/plate/helpers/blogging_helper.rb', line 65

def posts_for_category(category)
  self.posts.select { |p| p.category == category }
end

#posts_for_month(year, month, category = nil) ⇒ Object

Find all posts for the given year, month and optional category.



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/plate/helpers/blogging_helper.rb', line 70

def posts_for_month(year, month, category = nil)
  result = []

  if months = self.posts.archives[year.to_s]
    if month = months[month.to_s]
      month.each_value do |day|
        result << day.select { |post| category == nil or post.category == category }
      end
    end
  end

  result.flatten.sort { |x,y| x.name <=> y.name }
end

#posts_for_year(year, category = nil) ⇒ Object

Find all posts for the given year and optional category.



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/plate/helpers/blogging_helper.rb', line 85

def posts_for_year(year, category = nil)
  result = []

  if months = self.posts.archives[year.to_s]
    months.each_value do |month|
      month.each_value do |day|
        result << day.select { |post| category == nil or post.category == category }
      end
    end
  end

  result.flatten
end

#previous_postObject

Grab the post previous to this one.

Returns nil if this is the first post.



49
50
51
52
# File 'lib/plate/helpers/blogging_helper.rb', line 49

def previous_post
  return nil if post_index < 1
  @previous_post ||= self.posts[post_index - 1]
end

#years_for_category(category) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/plate/helpers/blogging_helper.rb', line 99

def years_for_category(category)
  result = []

  self.posts.archives.keys.each do |year|
    if posts_for_year(year, category).size > 0
      result << year
    end
  end

  result.sort
end