| 1 | class CommentsController < ApplicationController |
| 2 | before_action :find_commentable |
| 3 | before_action :auth_user |
| 4 | |
| 5 | def new |
| 6 | @comment = Comment.new |
| 7 | end |
| 8 | |
| 9 | def create |
| 10 | @comment = @commentable.comments.new comment_params |
| 11 | |
| 12 | if @comment.save |
| 13 | flash[:success] = 'Your comment was successfully posted!' |
| 14 | else |
| 15 | flash[:danger] = "Your comment wasn't posted! Comment cannot be empty." |
| 16 | end |
| 17 | redirect_to("#{URI(request.referer).path}#discussion") |
| 18 | end |
| 19 | |
| 20 | private |
| 21 | |
| 22 | def comment_params |
| 23 | params.require(:comment).permit(:body, :user_id) |
| 24 | end |
| 25 | |
| 26 | def find_commentable |
| 27 | @commentable = Comment.find_by_id(params[:comment_id]) if params[:comment_id] |
| 28 | @commentable = Lyric.find_by_id(params[:lyric_id]) if params[:lyric_id] |
| 29 | @commentable = Chord.find_by_id(params[:chord_id]) if params[:chord_id] |
| 30 | @commentable = Artist.find_by_id(params[:artist_id]) if params[:artist_id] |
| 31 | @commentable = Album.find_by_id(params[:album_id]) if params[:album_id] |
| 32 | @commentable = Song.find_by_id(params[:song_id]) if params[:song_id] |
| 33 | @commentable = Page.find_by_id(params[:page_id]) if params[:page_id] |
| 34 | @commentable = AudioFile.find_by_id(params[:audio_file_id]) if params[:audio_file_id] |
| 35 | if params[:javanese_gending_notation_id] |
| 36 | @commentable = JavaneseGendingNotation.find_by_id(params[:javanese_gending_notation_id]) |
| 37 | end |
| 38 | @commentable = JavaneseGending.find_by_id(params[:javanese_gending_id]) if params[:javanese_gending_id] |
| 39 | @commentable = User.find_by_id(params[:user_id]) if params[:user_id] |
| 40 | end |
| 41 | |
| 42 | def auth_user |
| 43 | redirect_to login_path(redirect_to: "#{URI(request.referer).path}#discussion") unless user_signed_in? |
| 44 | end |
| 45 | end |