app/controllers/muck/comments_controller.rb in muck-comments-0.1.6 vs app/controllers/muck/comments_controller.rb in muck-comments-0.1.7
- old
+ new
@@ -1,35 +1,52 @@
class Muck::CommentsController < ApplicationController
unloadable
- before_filter :get_parent, :only => [:create]
+ before_filter :setup_parent, :only => [:new, :create]
+ before_filter :has_permission?, :only => [:new, :create]
before_filter :get_comment, :only => [:destroy]
-
+
+ def index
+ @parent = get_parent
+ @comment = Comment.find(params[:id]) rescue nil
+ @comments = @parent.comments.by_newest if !@parent.blank?
+ @comments ||= @comment.children if !@comment.blank?
+
+ respond_to do |format|
+ format.html { render :template => 'comments/index', :layout => 'popup' }
+ format.pjs { render :template => 'comments/index', :layout => false }
+ end
+ end
+
+ def new
+ title = @parent.title rescue ''
+ title ||= @parent.name rescue ''
+ if title.blank?
+ @page_title = t('muck.comments.new_comment_no_title')
+ else
+ @page_title = t('muck.comments.new_comment_with_title', :title => title)
+ end
+ respond_to do |format|
+ format.html { render :template => 'comments/new', :layout => 'popup' }
+ format.pjs { render :template => 'comments/new', :layout => false }
+ end
+ end
+
def create
@comment = @parent.comments.build(params[:comment])
@comment.user = current_user
@comment.save!
- respond_to do |format|
- format.html do
- redirect_back_or_default(@parent)
- end
- format.json { render :json => { :success => true, :comment => @comment, :parent_id => @parent.id, :html => get_parent_comment_html(@parent) } }
- end
+ message = t('muck.comments.create_success')
+ handle_after_create(true, message)
rescue ActiveRecord::RecordInvalid => ex
if @comment
- @errors = @comment.errors.full_messages.to_sentence
+ errors = @comment.errors.full_messages.to_sentence
else
- @errors = ex
+ errors = ex
end
- message = t('muck.comments.create_error', :errors => @errors)
- respond_to do |format|
- format.html do
- flash[:error] = message
- redirect_back_or_default(@parent)
- end
- format.json { render :json => { :success => false, :message => message, :errors => @errors } }
- end
+ message = t('muck.comments.create_error', :errors => errors)
+ handle_after_create(false, message)
end
def destroy
@comment.destroy
respond_to do |format|
@@ -46,22 +63,34 @@
end
end
protected
- def get_parent
- if !params[:parent_type] || !params[:parent_id]
- raise t('muck.comments.missing_parent_id_error')
- return
+ def handle_after_create(success, message = '')
+ if success
+ respond_to do |format|
+ format.html { do_after_create_action(success, message) }
+ format.json { render :json => { :success => true, :comment => @comment, :message => message, :parent_id => @parent.id, :html => get_parent_comment_html(@parent, @comment) } }
+ end
+ else
+ respond_to do |format|
+ format.html { do_after_create_action }
+ format.json { render :json => { :success => false, :message => message, :errors => @errors } }
+ end
end
- @klass = params[:parent_type].to_s.constantize
- @parent = @klass.find(params[:parent_id])
- unless has_permission_to_comment(current_user, @parent)
- permission_denied
+ end
+
+ def do_after_create_action(success, message)
+ flash[:error] = message if !success
+ if "true" == params[:render_new]
+ flash[:notice] = message if success
+ render :template => 'comments/new'
+ else
+ redirect_back_or_default(@parent)
end
end
-
+
def get_comment
@comment = Comment.find(params[:id])
unless @comment.can_edit?(current_user)
respond_to do |format|
format.html do
@@ -71,20 +100,21 @@
format.js { render(:update){|page| page.alert I18n.t('muck.comments.cant_delete_comment')}}
end
end
end
- def get_parent_comment_html(parent)
- old_format = @template.template_format
- @template.template_format = :html
- comment = render_to_string(:partial => "#{parent.class.to_s.tableize}/comment", :locals => {:comment_owner => parent})
- @template.template_format = old_format
- comment
+ def get_parent_comment_html(parent, comment)
+ render_as_html do
+ render_to_string(:partial => "#{parent.class.to_s.tableize}/comment", :object => comment, :locals => {:comment_owner => parent})
+ end
rescue ActionView::MissingTemplate
- I18n.t('muck.comments.missing_comment_template_error', :partial => "#{parent.class.to_s.tableize}/comment")
+ render_as_html do
+ render_to_string(:partial => "comments/comment", :object => comment, :locals => {:comment_owner => parent})
+ end
+ #I18n.t('muck.comments.missing_comment_template_error', :partial => "#{parent.class.to_s.tableize}/comment")
end
- def has_permission_to_comment(user, parent)
- parent.can_comment?(user)
+ def has_permission?
+ @parent.can_comment?(current_user)
end
end