app/controllers/commontator/comments_controller.rb in commontator-5.1.0 vs app/controllers/commontator/comments_controller.rb in commontator-6.0.0.pre.1

- old
+ new

@@ -1,159 +1,168 @@ -module Commontator - class CommentsController < Commontator::ApplicationController - before_action :set_thread, only: [:new, :create] - before_action :set_comment_and_thread, except: [:new, :create] +class Commontator::CommentsController < Commontator::ApplicationController + before_action :set_thread, :commontator_set_thread_variables, only: [ :new, :create ] + before_action :set_comment_and_thread, except: [ :new, :create ] - # GET /threads/1/comments/new - def new - @comment = Comment.new - @comment.thread = @thread - @comment.creator = @user - security_transgression_unless @comment.can_be_created_by?(@user) + # GET /threads/1/comments/new + def new + @comment = Commontator::Comment.new(thread: @commontator_thread, creator: @commontator_user) + parent_id = params.dig(:comment, :parent_id) + unless parent_id.blank? + parent = Commontator::Comment.find parent_id + @comment.parent = parent + @comment.body = "<blockquote><span class=\"author\">#{ + Commontator.commontator_name(parent.creator) + }</span>\n#{ + parent.body + }\n</blockquote>\n" if @commontator_thread.config.comment_reply_style == :q + end + security_transgression_unless @comment.can_be_created_by?(@commontator_user) - @per_page = params[:per_page] || @thread.config.comments_per_page - - respond_to do |format| - format.html { redirect_to @thread } - format.js - end - + respond_to do |format| + format.html { redirect_to commontable_url } + format.js end + end - # POST /threads/1/comments - def create - @comment = Comment.new - @comment.thread = @thread - @comment.creator = @user - @comment.body = params[:comment].nil? ? nil : params[:comment][:body] - security_transgression_unless @comment.can_be_created_by?(@user) - subscribe_mentioned if Commontator.mentions_enabled + # POST /threads/1/comments + def create + @comment = Commontator::Comment.new( + thread: @commontator_thread, creator: @commontator_user, body: params.dig(:comment, :body) + ) + parent_id = params.dig(:comment, :parent_id) + @comment.parent = Commontator::Comment.find(parent_id) unless parent_id.blank? + security_transgression_unless @comment.can_be_created_by?(@commontator_user) - respond_to do |format| - if !params[:cancel].nil? - format.html { redirect_to @thread } - format.js { render :cancel } - elsif @comment.save - sub = @thread.config.thread_subscription.to_sym - @thread.subscribe(@user) if sub == :a || sub == :b - Subscription.comment_created(@comment) + respond_to do |format| + if params[:cancel].blank? + if @comment.save + sub = @commontator_thread.config.thread_subscription.to_sym + @commontator_thread.subscribe(@commontator_user) if sub == :a || sub == :b + subscribe_mentioned if @commontator_thread.config.mentions_enabled + Commontator::Subscription.comment_created(@comment) - @per_page = params[:per_page] || @thread.config.comments_per_page - - format.html { redirect_to @thread } format.js else - format.html { redirect_to @thread } format.js { render :new } end + else + format.js { render :cancel } end + + format.html { redirect_to commontable_url } end + end - # GET /comments/1/edit - def edit - security_transgression_unless @comment.can_be_edited_by?(@user) + # GET /comments/1/edit + def edit + @comment.editor = @commontator_user + security_transgression_unless @comment.can_be_edited_by?(@commontator_user) - respond_to do |format| - format.html { redirect_to @thread } - format.js - end + respond_to do |format| + format.html { redirect_to commontable_url } + format.js end + end - # PUT /comments/1 - def update - security_transgression_unless @comment.can_be_edited_by?(@user) - @comment.body = params[:comment].nil? ? nil : params[:comment][:body] - @comment.editor = @user - subscribe_mentioned if Commontator.mentions_enabled + # PUT /comments/1 + def update + @comment.editor = @commontator_user + @comment.body = params.dig(:comment, :body) + security_transgression_unless @comment.can_be_edited_by?(@commontator_user) - respond_to do |format| - if !params[:cancel].nil? - format.html { redirect_to @thread } - format.js { render :cancel } - elsif @comment.save - format.html { redirect_to @thread } + respond_to do |format| + if params[:cancel].blank? + if @comment.save + subscribe_mentioned if @commontator_thread.config.mentions_enabled + format.js else - format.html { redirect_to @thread } format.js { render :edit } end + else + @comment.restore_attributes + + format.js { render :cancel } end + + format.html { redirect_to commontable_url } end + end - # PUT /comments/1/delete - def delete - security_transgression_unless @comment.can_be_deleted_by?(@user) + # PUT /comments/1/delete + def delete + security_transgression_unless @comment.can_be_deleted_by?(@commontator_user) - @comment.errors.add(:base, t('commontator.comment.errors.already_deleted')) \ - unless @comment.delete_by(@user) + @comment.errors.add(:base, t('commontator.comment.errors.already_deleted')) \ + unless @comment.delete_by(@commontator_user) - respond_to do |format| - format.html { redirect_to @thread } - format.js { render :delete } - end + respond_to do |format| + format.html { redirect_to commontable_url } + format.js { render :delete } end + end - # PUT /comments/1/undelete - def undelete - security_transgression_unless @comment.can_be_deleted_by?(@user) + # PUT /comments/1/undelete + def undelete + security_transgression_unless @comment.can_be_deleted_by?(@commontator_user) - @comment.errors.add(:base, t('commontator.comment.errors.not_deleted')) \ - unless @comment.undelete_by(@user) + @comment.errors.add(:base, t('commontator.comment.errors.not_deleted')) \ + unless @comment.undelete_by(@commontator_user) - respond_to do |format| - format.html { redirect_to @thread } - format.js { render :delete } - end + respond_to do |format| + format.html { redirect_to commontable_url } + format.js { render :delete } end + end - # PUT /comments/1/upvote - def upvote - security_transgression_unless @comment.can_be_voted_on_by?(@user) + # PUT /comments/1/upvote + def upvote + security_transgression_unless @comment.can_be_voted_on_by?(@commontator_user) - @comment.upvote_from @user + @comment.upvote_from @commontator_user - respond_to do |format| - format.html { redirect_to @thread } - format.js { render :vote } - end + respond_to do |format| + format.html { redirect_to commontable_url } + format.js { render :vote } end + end - # PUT /comments/1/downvote - def downvote - security_transgression_unless @comment.can_be_voted_on_by?(@user) &&\ - @comment.thread.config.comment_voting.to_sym == :ld + # PUT /comments/1/downvote + def downvote + security_transgression_unless @comment.can_be_voted_on_by?(@commontator_user) &&\ + @comment.thread.config.comment_voting.to_sym == :ld - @comment.downvote_from @user + @comment.downvote_from @commontator_user - respond_to do |format| - format.html { redirect_to @thread } - format.js { render :vote } - end + respond_to do |format| + format.html { redirect_to commontable_url } + format.js { render :vote } end + end - # PUT /comments/1/unvote - def unvote - security_transgression_unless @comment.can_be_voted_on_by?(@user) + # PUT /comments/1/unvote + def unvote + security_transgression_unless @comment.can_be_voted_on_by?(@commontator_user) - @comment.unvote voter: @user + @comment.unvote voter: @commontator_user - respond_to do |format| - format.html { redirect_to @thread } - format.js { render :vote } - end + respond_to do |format| + format.html { redirect_to commontable_url } + format.js { render :vote } end + end - protected + protected - def set_comment_and_thread - @comment = Comment.find(params[:id]) - @thread = @comment.thread - commontator_set_new_comment(@thread, @user) - end + def set_comment_and_thread + @comment = Commontator::Comment.find(params[:id]) + @commontator_thread = @comment.thread + commontator_set_new_comment + end - def subscribe_mentioned - Commontator.commontator_mentions(@user, @thread, '').where(id: params[:mentioned_ids]).each do |user| - @thread.subscribe(user) - end + def subscribe_mentioned + Commontator.commontator_mentions(@commontator_user, @commontator_thread, '') + .where(id: params[:mentioned_ids]).each do |user| + @commontator_thread.subscribe(user) end end end