class Thoth::TagController

Public Instance Methods

atom(name = nil) click to toggle source
# 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) click to toggle source
# 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