lib/zen/package/comments/lib/comments/controller/comments.rb in zen-0.3 vs lib/zen/package/comments/lib/comments/controller/comments.rb in zen-0.4
- old
+ new
@@ -39,19 +39,19 @@
#
# In order to manage existing comments you'll have to navigate to
# ``/admin/comments``. This page will show an overview of all existing
# comments (or a message if no comments were found).
#
- # ![Comments](../../_static/comments/comments.png)
+ # ![Comments](../../images/comments/comments.png)
#
# Comments can be edited by clicking on their name. Deleting comments can be
# done by checking the checkboxes in each row followed by clicking the
# "Delete selected comments" button.
#
# ## Editing Comments
#
- # ![Edit Comment](../../_static/comments/edit_comment.png)
+ # ![Edit Comment](../../images/comments/edit_comment.png)
#
# When editing a comment you can specify/update the following fields:
#
# * **Name**: the name of the author. This field can only be changed if the
# comment was posted by somebody that wasn't logged in.
@@ -75,51 +75,22 @@
# * show_comment
# * edit_comment
# * new_comment
# * delete_comment
#
- # ## Events
- #
- # All events called in this controller receive an instance of
- # {Comments::Model::Comment}. However, just like all other controllers the
- # ``delete_comment`` receives an instance of this model that has already
- # been destroyed.
- #
- # An example of using one of these events is to notify a user when his
- # comment has been marked as spam:
- #
- # require 'mail'
- #
- # Zen::Event.call(:after_edit_comment) do |comment|
- # email = comment.user.email
- # spam = Comments::Model::CommentStatus[:name => 'spam']
- #
- # if comment.comment_status_id == spam.id
- # Mail.deliver do
- # from 'example@domain.tld'
- # to email
- # subject 'Your comment has been marked as spam'
- # body "Dear #{comment.user.name}, your comment has been " \
- # "marked as spam"
- # end
- # end
- # end
- #
# @since 0.1
# @map /admin/comments
- # @event before_edit_comment
- # @event after_edit_comment
- # @event beore_delete_comment
- # @event after_delete_comment
#
class Comments < Zen::Controller::AdminController
map '/admin/comments'
helper :comment
title 'comments.titles.%s'
csrf_protection :save, :delete
+ autosave Model::Comment, Model::Comment::COLUMNS, :edit_comment
+
##
# Shows an overview of all existing comments and allows the user to edit
# or remove these comments.
#
# @since 0.1
@@ -154,71 +125,51 @@
set_breadcrumbs(
Comments.a(lang('comments.titles.index'), :index),
@page_title
)
- @comment = flash[:form_data] || validate_comment(id)
+ @comment = validate_comment(id)
+ @comment.set(flash[:form_data]) if flash[:form_data]
render_view(:form)
end
##
# Saves the changes made to an existing comment.
#
# @since 0.1
# @permission edit_comment
- # @event before_edit_comment
- # @event after_edit_comment
#
def save
authorize_user!(:edit_comment)
- # Copy the POST data so we can work with it without messing things up
- post = request.subset(
- :id,
- :user_id,
- :name,
- :website,
- :email,
- :comment,
- :comment_status_id,
- :section_entry_id
- )
+ post = post_fields(*Model::Comment::COLUMNS)
+ comment = validate_comment(request.params['id'])
- comment = validate_comment(post['id'])
-
- post.delete('id')
-
begin
- post.each { |k, v| comment.send("#{k}=", v) }
- Zen::Event.call(:before_edit_comment, comment)
-
+ comment.set(post)
comment.save
rescue => e
- Ramaze::Log.error(e.inspect)
+ Ramaze::Log.error(e)
message(:error, lang('comments.errors.save'))
flash[:form_errors] = comment.errors
- flash[:form_data] = comment
+ flash[:form_data] = post
redirect_referrer
end
- Zen::Event.call(:after_edit_comment, comment)
-
message(:success, lang('comments.success.save'))
redirect(Comments.r(:edit, comment.id))
end
##
# Deletes a number of comments. The IDs of these comments should be
# specified in the POSt array "comment_ids".
#
# @since 0.1
# @permission delete_comment
- # @event before_delete_comment
- # @event after_delete_comment
#
def delete
authorize_user!(:delete_comment)
# Obviously we'll require some IDs
@@ -231,21 +182,18 @@
# Delete each section
request.params['comment_ids'].each do |id|
comment = ::Comments::Model::Comment[id]
next if comment.nil?
- Zen::Event.call(:before_delete_comment, comment)
begin
comment.destroy
rescue => e
- Ramaze::Log.error(e.inspect)
+ Ramaze::Log.error(e)
message(:error, lang('comments.errors.delete') % id)
redirect_referrer
end
-
- Zen::Event.call(:after_delete_comment, comment)
end
message(:success, lang('comments.success.delete'))
redirect_referrer
end