Sha256: 5bbce699376550c7bd6028cbb75cfe328b653f1d496f48685405f145b46740f9

Contents?: true

Size: 1.03 KB

Versions: 8

Compression:

Stored size: 1.03 KB

Contents

require_dependency "storytime/application_controller"

module Storytime
  class CommentsController < ApplicationController
    before_action :authenticate_user!
    after_action :verify_authorized

    before_action :set_post, only: :create

    respond_to :json, only: :destroy

    def create
      @comment = Comment.new(comment_params)
      @comment.user = current_user
      @comment.post = @post

      authorize @comment
      opts = { notice: I18n.t('flash.comments.create.success') } if @comment.save
      redirect_to @post, opts
    end

    def destroy
      @comment = current_user.storytime_comments.find(params[:id])
      authorize @comment
      @comment.destroy
      respond_with @comment
    end

  private

    def set_post
      @post = Post.friendly.find(params["#{post_type_name}_id".to_sym])
    end

    def post_type_name
      @post_type_name = request.path.split("/")[1].singularize
    end

    def comment_params
      params.require(:comment).permit(*policy(@comment || Comment.new).permitted_attributes)
    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
storytime-2.1.6 app/controllers/storytime/comments_controller.rb
storytime-2.1.5 app/controllers/storytime/comments_controller.rb
storytime-2.1.4 app/controllers/storytime/comments_controller.rb
storytime-2.1.3 app/controllers/storytime/comments_controller.rb
storytime-2.1.2 app/controllers/storytime/comments_controller.rb
storytime-2.1.1 app/controllers/storytime/comments_controller.rb
storytime-2.1.0 app/controllers/storytime/comments_controller.rb
storytime-2.0.0 app/controllers/storytime/comments_controller.rb