class CommentsController < ApplicationController
  before_filter :authenticate_user!
  # lol embedded documents
  before_filter :find_book_and_chapter_and_note
  
  def create
    @comment = @note.comments.build(params[:comment].merge!(:user => current_user))
    if @comment.save
      change_note_state!
      @comment.send_notifications!
      flash[:notice] = "Comment has been created."
      redirect_to [@book, @chapter, @note]
    else
      @comments = @note.comments
      flash[:error] = "Comment could not be created."
      render "notes/show"
    end
  end
  
  private

  def change_note_state!
    if current_user.author?
      case params[:commit]
      when "Accept"
        @comment.note.accept!
      when "Reject"
        @comment.note.reject!
      when "Reopen"
        @comment.note.reopen!
      end
    end
  end
  
  def find_book_and_chapter_and_note
    @book = Book.where(permalink: params[:book_id]).first
    @chapter = @book.chapters.where(:position => params[:chapter_id]).first
    @note = @chapter.notes.where(:number => params[:note_id]).first
  end
  
end
class CommentsController < ApplicationController
  before_filter :authenticate_user!
  # lol embedded documents
  before_filter :find_book_and_chapter_and_note

  def create
    @comments = @note.comments
    @comment = @note.comments.build(params[:comment].merge!(:user => current_user))
    if params[:comment][:text].blank?
      attempt_state_update
    else
      create_new_comment
    end
  end

  private

  def attempt_state_update
    if current_user.author?
      change_note_state!
      flash[:notice] = "Note state updated."
      render "notes/show"
    else
      @comment = @note.comments.build
      flash[:error] = "Comment could not be created."
      render "notes/show"
    end
  end

  def create_new_comment
    if @comment.save
      @comment.send_notifications!
      flash[:notice] = "Comment has been created."
      redirect_to [@book, @chapter, @note]
    else
      flash[:error] = "Comment could not be created."
      render "notes/show"
    end
  end

  def change_note_state!
    case params[:commit]
    when "Accept"
      @note.accept!
    when "Reject"
      @note.reject!
    when "Reopen"
      @note.reopen!
    end
  end
  
  def find_book_and_chapter_and_note
    @book = Book.where(permalink: params[:book_id]).first
    @chapter = @book.chapters.where(:position => params[:chapter_id]).first
    @note = @chapter.notes.where(:number => params[:note_id]).first
  end
  
end

Allow note state to be changed without being forced to leave a comment
Allow note state to be changed without being forced to leave a comment
