Sha256: 7d3f25907466a49ca8a5bd04833fa4d18dd042a433b586a177b4d3e7dbca7ba9

Contents?: true

Size: 1.99 KB

Versions: 12

Compression:

Stored size: 1.99 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
      notify_attachable!(:create)
      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)
      notify_attachable!(:update)
      render_comment
    else
      render_form
    end
  end

  def destroy
    authorize @comment, :delete?

    @comment.destroy!
    notify_attachable!(:delete)

    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
    safe_params = [
      :title,
      :date,
      :text,
    ]

    if params[:action] == "create"
      safe_params << :commentable_id
      safe_params << :commentable_type
    end

    safe_params
  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

  def notify_attachable!(action)
    @comment.commentable.try(:receive_comment_notification, @comment, action)
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
dorsale-3.8.1 app/controllers/dorsale/comments_controller.rb
dorsale-3.8.0 app/controllers/dorsale/comments_controller.rb
dorsale-3.7.8 app/controllers/dorsale/comments_controller.rb
dorsale-3.7.7 app/controllers/dorsale/comments_controller.rb
dorsale-3.7.6 app/controllers/dorsale/comments_controller.rb
dorsale-3.7.5 app/controllers/dorsale/comments_controller.rb
dorsale-3.7.4 app/controllers/dorsale/comments_controller.rb
dorsale-3.7.2 app/controllers/dorsale/comments_controller.rb
dorsale-3.7.1 app/controllers/dorsale/comments_controller.rb
dorsale-3.7.0 app/controllers/dorsale/comments_controller.rb
dorsale-3.6.1 app/controllers/dorsale/comments_controller.rb
dorsale-3.6.0 app/controllers/dorsale/comments_controller.rb