Sha256: 26d6748d8a716290df742ca521440f8971230e8acf8b7b956cfbc116c3198284

Contents?: true

Size: 1.74 KB

Versions: 4

Compression:

Stored size: 1.74 KB

Contents

class Dorsale::CommentsController < ::Dorsale::ApplicationController
  before_action :set_objects, only: [
    :edit,
    :update,
    :destroy,
  ]

  layout false

  def create
    @comment ||= scope.new(comment_params_for_create)

    authorize @comment, :create?

    if @comment.save
      render_comment
    else
      render_nothing
    end
  end

  def edit
    authorize @comment, :update?
  end

  def update
    authorize @comment, :update?

    if @comment.update(comment_params_for_update)
      render_comment
    else
      render_form
    end
  end

  def destroy
    authorize @comment, :delete?

    @comment.destroy

    render_nothing
  end

  private

  def render_comment
    if request.xhr?
      render partial: "comment", locals: {comment: @comment}
    else
      redirect_to back_url
    end
  end

  def render_form
    if request.xhr?
      render partial: "form", locals: {comment: @comment}
    else
      redirect_to back_url
    end
  end

  def render_nothing
    if request.xhr?
      head :ok
    else
      redirect_to back_url
    end
  end

  def back_url
    [
      params[:form_url],
      request.referer,
      (main_app.root_path rescue "/"),
    ].select(&:present?).first
  end

  def model
    ::Dorsale::Comment
  end

  def set_objects
    @comment ||= scope.find(params[:id])
  end

  def permitted_params_for_comment
    if params[:action] == "create"
      [
        :commentable_id,
        :commentable_type,
        :text,
      ]
    else
      [
        :text,
      ]
    end
  end

  def comment_params
    params.fetch(:comment, {}).permit(permitted_params_for_comment)
  end

  def comment_params_for_create
    comment_params.merge(author: current_user)
  end

  def comment_params_for_update
    comment_params
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dorsale-3.5.0 app/controllers/dorsale/comments_controller.rb
dorsale-3.4.0 app/controllers/dorsale/comments_controller.rb
dorsale-3.3.0 app/controllers/dorsale/comments_controller.rb
dorsale-3.2.0 app/controllers/dorsale/comments_controller.rb