Class: Thoth::CommentController

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

Instance Method Summary collapse

Methods inherited from Controller

action_missing

Instance Method Details

#atomObject



52
53
54
55
56
57
58
59
60
61
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
# File 'lib/thoth/controller/comment.rb', line 52

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

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

  x.feed(:xmlns => 'http://www.w3.org/2005/Atom') {
    comments_url = Config.site['url'].chomp('/') + rs().to_s

    x.id       comments_url
    x.title    "#{Config.site['name']} Recent Comments"
    x.subtitle Config.site['desc']
    x.updated  Time.now.xmlschema # TODO: use modification time of the last post
    x.link     :href => comments_url
    x.link     :href => Config.site['url'].chomp('/') + rs(:atom).to_s,
                   :rel => 'self'

    Comment.recent.all.each do |comment|
      x.entry {
        x.id        comment.url
        x.title     comment.title.empty? ? comment.summary : comment.title
        x.published comment.created_at.xmlschema
        x.updated   comment.updated_at.xmlschema
        x.link      :href => comment.url, :rel => 'alternate'
        x.content   comment.body_rendered, :type => 'html'

        x.author {
          x.name comment.author

          if comment.author_url && !comment.author_url.empty?
            x.uri comment.author_url
          end
        }
      }
    end
  }

  throw(:respond, x.target!)
end

#delete(id = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/thoth/controller/comment.rb', line 92

def delete(id = nil)
  require_auth

  error_404 unless id && @comment = Comment[id]

  if request.post?
    error_403 unless form_token_valid?

    comment_url = @comment.url

    if request[:confirm] == 'yes'
      @comment.deleted = true

      if @comment.save(:changed => true, :validate => false)
        Ramaze::Cache.action.clear
        Ramaze::Cache.cache_helper_value.clear
        flash[:success] = 'Comment deleted.'
      else
        flash[:error] = 'Error deleting comment.'
      end
    end

    redirect(comment_url)
  end

  @title = "Delete Comment: #{@comment.title}"
end

#indexObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/thoth/controller/comment.rb', line 41

def index
  now = Time.now.strftime('%Y%j')

  comments = Comment.recent.partition do |comment|
    comment.created_at('%Y%j') == now
  end

  @title = 'Recent Comments'
  @today, @ancient = comments
end

#list(page = 1) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/thoth/controller/comment.rb', line 120

def list(page = 1)
  require_auth

  page = page.to_i

  @columns  = [:id, :title, :author, :created_at, :deleted]
  @order    = (request[:order] || :desc).to_sym
  @sort     = (request[:sort]  || :created_at).to_sym
  @sort     = :created_at unless @columns.include?(@sort)
  @sort_url = rs(:list, page)

  @comments = Comment.order(@order == :desc ? Sequel.desc(@sort) : @sort).paginate(page, 20)
  @title    = "Comments (page #{page} of #{[@comments.page_count, 1].max})"
  @pager    = pager(@comments, rs(:list, '__page__', :sort => @sort, :order => @order))
end

#rssObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/thoth/controller/comment.rb', line 136

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

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

  x.rss(:version     => '2.0',
        'xmlns:atom' => 'http://www.w3.org/2005/Atom',
        'xmlns:dc'   => 'http://purl.org/dc/elements/1.1/') {
    x.channel {
      x.title          "#{Config.site['name']} Recent Comments"
      x.link           Config.site['url']
      x.description    Config.site['desc']
      x.managingEditor "#{Config.admin['email']} (#{Config.admin['name']})"
      x.webMaster      "#{Config.admin['email']} (#{Config.admin['name']})"
      x.docs           'http://backend.userland.com/rss/'
      x.ttl            30
      x.atom           :link, :rel => 'self',
                           :type => 'application/rss+xml',
                           :href => Config.site['url'].chomp('/') + rs(:rss).to_s

      Comment.recent.all.each do |comment|
        x.item {
          x.title       comment.title.empty? ? comment.summary : comment.title
          x.link        comment.url
          x.dc          :creator, comment.author
          x.guid        comment.url, :isPermaLink => 'true'
          x.pubDate     comment.created_at.rfc2822
          x.description comment.body_rendered
        }
      end
    }
  }

  throw(:respond, x.target!)
end