Class: Thoth::PostController
- Inherits:
-
Controller
- Object
- Ramaze::Controller
- Controller
- Thoth::PostController
- Defined in:
- lib/thoth/controller/post.rb
Instance Method Summary collapse
- #atom(name = nil) ⇒ Object
- #delete(id = nil) ⇒ Object
- #edit(id = nil) ⇒ Object
- #index(name = nil) ⇒ Object
- #list(page = 1) ⇒ Object
- #new ⇒ Object
Methods inherited from Controller
Instance Method Details
#atom(name = nil) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 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 |
# File 'lib/thoth/controller/post.rb', line 122 def atom(name = nil) error_404 unless name && post = Post.get(name) # Permanently redirect id-based URLs to name-based URLs to reduce search # result dupes and improve pagerank. raw_redirect(post.atom_url, :status => 301) if name =~ /^\d+$/ response['Content-Type'] = 'application/atom+xml' comments = post.comments.reverse_order.limit(20) updated = comments.count > 0 ? comments.first.created_at.xmlschema : post.created_at.xmlschema x = Builder::XmlMarkup.new(:indent => 2) x.instruct! x.feed(:xmlns => 'http://www.w3.org/2005/Atom') { x.id post.url x.title "Comments on \"#{post.title}\" - #{Config.site['name']}" x.updated updated x.link :href => post.url x.link :href => post.atom_url, :rel => 'self' comments.all do |comment| x.entry { x.id comment.url x.title 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. { x.name comment. if comment. && !comment..empty? x.uri comment. end } } end } throw(:respond, x.target!) end |
#delete(id = nil) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/thoth/controller/post.rb', line 168 def delete(id = nil) require_auth error_404 unless id && @post = Post[id] if request.post? error_403 unless form_token_valid? if request[:confirm] == 'yes' @post.destroy Ramaze::Cache.action.clear flash[:success] = 'Blog post deleted.' redirect(MainController.r()) else redirect(@post.url) end end @title = "Delete Post: #{@post.title}" @show_post_edit = true end |
#edit(id = nil) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/thoth/controller/post.rb', line 190 def edit(id = nil) require_auth unless @post = Post[id] flash[:error] = 'Invalid post id.' redirect(rs(:new)) end if request.post? error_403 unless form_token_valid? if request[:name] && !request[:name].empty? @post.name = request[:name] end @post.title = request[:title] @post.body = request[:body] @post. = request[:tags] @post.allow_comments = !!request[:allow_comments] @post.is_draft = @post.is_draft ? request[:action] != 'Publish' : request[:action] == 'Unpublish & Save as Draft' @post.created_at = Time.now if @post.is_draft if @post.valid? && (@post.is_draft || request[:action] == 'Publish') begin Thoth.db.transaction do raise unless @post.save && @post. = request[:tags] end rescue => e @post_error = "There was an error saving your post: #{e}" else if @post.is_draft flash[:success] = 'Draft saved.' redirect(rs(:edit, @post.id)) else Ramaze::Cache.action.clear flash[:success] = 'Blog post published.' redirect(rs(@post.name)) end end end end @title = "Edit blog post - #{@post.title}" @form_action = rs(:edit, id) @show_post_edit = true end |
#index(name = nil) ⇒ 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 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 91 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 119 120 |
# File 'lib/thoth/controller/post.rb', line 37 def index(name = nil) error_404 unless name && @post = Post.get(name) # Permanently redirect id-based URLs to name-based URLs to reduce search # result dupes and improve pagerank. raw_redirect(@post.url, :status => 301) if name =~ /^\d+$/ cache_key = "comments_#{@post.id}_#{auth_key_valid?.to_s}" if request.post? && @post.allow_comments && Config.site['enable_comments'] # Dump the request if the robot traps were triggered. error_404 unless request['captcha'].empty? && request['comment'].empty? # Create a new comment. comment = Comment.new do |c| c.post_id = @post.id c. = request[:author] c. = request[:author_email] c. = request[:author_url] c.title = '' c.body = request[:body] c.ip = request.ip end # Set cookies. expire = Time.now + 5184000 # two months from now response.(:thoth_author, :expires => expire, :path => '/', :value => comment.) response.(:thoth_author_email, :expires => expire, :path => '/', :value => comment.) response.(:thoth_author_url, :expires => expire, :path => '/', :value => comment.) if comment.valid? && request[:action] == 'Post Comment' begin raise unless comment.save rescue => e @comment_error = 'There was an error posting your comment. ' << 'Please try again later.' else flash[:success] = 'Comment posted.' cache_value.delete(cache_key) redirect(rs(@post.name).to_s + "#comment-#{comment.id}") end end @author = comment. @author_email = comment. @author_url = comment. @preview = comment elsif @post.allow_comments && Config.site['enable_comments'] @author = (:thoth_author, '') @author_email = (:thoth_author_email, '') @author_url = (:thoth_author_url, '') end @title = @post.title if Config.site['enable_comments'] @comment_action = r(:/, @post.name).to_s + '#post-comment' # Since repeated calls to render_view are very expensive, we pre-render # the comments here and cache them to speed up future pageviews. unless @comments_rendered = cache_value[cache_key] @comments_rendered = '' @post.comments.all.each do |comment| @comments_rendered << CommentController.render_view(:comment, :comment => comment) end cache_value.store(cache_key, @comments_rendered, :ttl => 300) end @feeds = [{ :href => @post.atom_url, :title => 'Comments on this post', :type => 'application/atom+xml' }] end @show_post_edit = true end |
#list(page = 1) ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/thoth/controller/post.rb', line 240 def list(page = 1) require_auth page = page.to_i @columns = [:id, :title, :created_at, :updated_at] @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) @posts = Post.filter(:is_draft => false).paginate(page, 20).order( @order == :desc ? Sequel.desc(@sort) : @sort) if page == 1 @drafts = Post.filter(:is_draft => true).order( @order == :desc ? Sequel.desc(@sort) : @sort) end @title = "Blog Posts (page #{page} of #{[@posts.page_count, 1].max})" @pager = pager(@posts, rs(:list, '__page__', :sort => @sort, :order => @order)) end |
#new ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/thoth/controller/post.rb', line 263 def new require_auth @title = "New blog post - Untitled" @form_action = rs(:new) if request.post? error_403 unless form_token_valid? @post = Post.new do |p| if request[:name] && !request[:name].empty? p.name = request[:name] end p.title = request[:title] p.body = request[:body] p. = request[:tags] p.allow_comments = !!request[:allow_comments] p.is_draft = request[:action] == 'Save & Preview' end if @post.valid? begin Thoth.db.transaction do raise unless @post.save && @post. = request[:tags] end rescue => e @post.is_draft = true @post_error = "There was an error saving your post: #{e}" else if @post.is_draft flash[:success] = 'Draft saved.' redirect(rs(:edit, @post.id)) else Ramaze::Cache.action.clear flash[:success] = 'Blog post published.' redirect(rs(@post.name)) end end else @post.is_draft = true end @title = "New blog post - #{@post.title}" end end |