notes_controller.rb 1.08 KB
Newer Older
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
class NotesController < ApplicationController
  before_filter :project 

  # Authorize
  before_filter :add_project_abilities
  before_filter :authorize_write_note!, :only => [:create] 

  respond_to :js

  def create
    @note = @project.notes.new(params[:note])
    @note.author = current_user

    if @note.save
      notify if params[:notify] == '1'
    end


    respond_to do |format|
      format.html {redirect_to :back}
      format.js  
    end
  end

  def destroy
    @note = @project.notes.find(params[:id])
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
27 28 29

    return access_denied! unless can?(current_user, :admin_note, @note)

Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    @note.destroy

    respond_to do |format|
      format.js { render :nothing => true }  
    end
  end

  protected 

  def notify
    @project.users.reject { |u| u.id == current_user.id } .each do |u|
      case @note.noteable_type
      when "Commit" then
        Notify.note_commit_email(u, @note).deliver
      when "Issue" then
        Notify.note_issue_email(u, @note).deliver
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
46 47
      when "Snippet"
        true
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
48 49 50 51 52 53
      else
        Notify.note_wall_email(u, @note).deliver
      end
    end
  end
end