Module: Postview::Helpers

Defined in:
lib/postview/helpers.rb

Overview

Copyright © 2009 Hallison Batista

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#all_archived_postsObject (readonly)

All archived posts.



25
26
27
# File 'lib/postview/helpers.rb', line 25

def all_archived_posts
  @all_archived_posts
end

#all_drafted_postsObject (readonly)

All archived posts.



28
29
30
# File 'lib/postview/helpers.rb', line 28

def all_drafted_posts
  @all_drafted_posts
end

#all_postsObject (readonly)

All posts



22
23
24
# File 'lib/postview/helpers.rb', line 22

def all_posts
  @all_posts
end

#all_tagsObject (readonly)

All tags used in posts and archived posts.



19
20
21
# File 'lib/postview/helpers.rb', line 19

def all_tags
  @all_tags
end

#archived_posts_foundObject (readonly)

All archived posts found by search.



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

def archived_posts_found
  @archived_posts_found
end

#current_postObject (readonly)

Current post.



13
14
15
# File 'lib/postview/helpers.rb', line 13

def current_post
  @current_post
end

#current_tagObject (readonly)

Current tag.



16
17
18
# File 'lib/postview/helpers.rb', line 16

def current_tag
  @current_tag
end

#error_messageObject (readonly)

HTTP status error message



37
38
39
# File 'lib/postview/helpers.rb', line 37

def error_message
  @error_message
end

#pageObject (readonly)

Current page.



10
11
12
# File 'lib/postview/helpers.rb', line 10

def page
  @page
end

#posts_foundObject (readonly)

All posts found by search.



31
32
33
# File 'lib/postview/helpers.rb', line 31

def posts_found
  @posts_found
end

#siteObject (readonly)

Site information.



7
8
9
# File 'lib/postview/helpers.rb', line 7

def site
  @site
end

Instance Method Details

Returns all tags related with a post tags.



67
68
69
70
71
# File 'lib/postview/helpers.rb', line 67

def all_related_tags
  (all_posts + all_archived_posts).reject do |post|
    (post.tags & current_post.tags).empty?
  end.map{ |post| post.tags }.flatten.uniq.sort
end

#count_posts_by_tag(name) ⇒ Object

Count posts by tag name.



74
75
76
77
78
79
80
# File 'lib/postview/helpers.rb', line 74

def count_posts_by_tag(name)
  @count_posts_by_tag ||= all_tags.inject({}) do |count, tag|
    count[tag] = (all_posts + all_archived_posts).count{ |post| post.tags.include? tag }
    count
  end
  @count_posts_by_tag[name]
end

#latest_posts(limit = 5) ⇒ Object

Returns latest posts.



40
41
42
# File 'lib/postview/helpers.rb', line 40

def latest_posts(limit = 5)
  @all_posts.limit(limit) || []
end

Returns all posts related to any post.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/postview/helpers.rb', line 45

def related_posts_in(folder = :posts)
  posts = case folder
            when :posts
              all_posts
            when :archive
              all_archived_posts
            when :drafts
              all_drafted_posts
            else
              return nil
          end
  posts.reject do |post|
    (post.tags & current_post.tags).empty? || post == current_post
  end
end

Returns all tags related with a post tags.



62
63
64
# File 'lib/postview/helpers.rb', line 62

def related_tags_in(folder = :posts)
  (related_posts_in(folder) << current_post).map{ |post| post.tags }.flatten.uniq.sort
end