Class: Thoth::TagController

Inherits:
Controller
  • Object
show all
Defined in:
lib/thoth/controller/tag.rb

Instance Method Summary collapse

Methods inherited from Controller

action_missing

Instance Method Details

#atom(name = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/thoth/controller/tag.rb', line 62

def atom(name = nil)
  error_404 unless name && tag = Tag[:name => name.strip.downcase]

  response['Content-Type'] = 'application/atom+xml'

  posts   = tag.posts.limit(10)
  updated = posts.count > 0 ? posts.first.created_at.xmlschema :
      Time.at(0).xmlschema

  x = Builder::XmlMarkup.new(:indent => 2)
  x.instruct!

  x.feed(:xmlns => 'http://www.w3.org/2005/Atom') {
    x.id       tag.url
    x.title    "Posts tagged with \"#{tag.name}\" - #{Config.site['name']}"
    x.updated  updated
    x.link     :href => tag.url
    x.link     :href => tag.atom_url, :rel => 'self'

    x.author {
      x.name  Config.admin['name']
      x.email Config.admin['email']
      x.uri   Config.site['url']
    }

    posts.all do |post|
      x.entry {
        x.id        post.url
        x.title     post.title
        x.published post.created_at.xmlschema
        x.updated   post.updated_at.xmlschema
        x.link      :href => post.url, :rel => 'alternate'
        x.content   post.body_rendered, :type => 'html'

        post.tags.each do |tag|
          x.category :term => tag.name, :label => tag.name,
              :scheme => tag.url
        end
      }
    end
  }

  throw(:respond, x.target!)
end

#index(name = nil, page = 1) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/thoth/controller/tag.rb', line 37

def index(name = nil, page = 1)
  error_404 unless name && @tag = Tag[:name => name.strip.downcase]

  page = page.to_i
  page = 1 unless page >= 1

  @posts = @tag.posts.paginate(page, 10)

  if page > @posts.page_count && @posts.page_count > 0
    page   = @posts.page_count
    @posts = @tag.posts.paginate(page, 10)
  end

  @title = "Posts tagged with \"#{@tag.name}\" (page #{page} of " <<
      "#{@posts.page_count > 0 ? @posts.page_count : 1})"

  @pager = pager(@posts, rs(:/, name, '__page__'))

  @feeds = [{
    :href  => @tag.atom_url,
    :title => 'Posts with this tag',
    :type  => 'application/atom+xml'
  }]
end