issues_controller.rb 2.48 KB
Newer Older
gitlabhq's avatar
gitlabhq committed
1 2
class IssuesController < ApplicationController
  before_filter :authenticate_user!
Nihad Abbasov's avatar
Nihad Abbasov committed
3
  before_filter :project
gitlabhq's avatar
gitlabhq committed
4
  before_filter :issue, :only => [:edit, :update, :destroy, :show]
gitlabhq's avatar
gitlabhq committed
5
  layout "project"
gitlabhq's avatar
gitlabhq committed
6 7 8 9

  # Authorize
  before_filter :add_project_abilities
  before_filter :authorize_read_issue!
Nihad Abbasov's avatar
Nihad Abbasov committed
10
  before_filter :authorize_write_issue!, :only => [:new, :create, :close, :edit, :update, :sort]
gitlabhq's avatar
gitlabhq committed
11 12 13 14 15

  respond_to :js

  def index
    @issues = case params[:f].to_i
gitlabhq's avatar
gitlabhq committed
16
              when 1 then @project.issues
gitlabhq's avatar
gitlabhq committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
              when 2 then @project.issues.closed
              when 3 then @project.issues.opened.assigned(current_user)
              else @project.issues.opened
              end

    respond_to do |format|
      format.html # index.html.erb
      format.js
    end
  end

  def new
    @issue = @project.issues.new
    respond_with(@issue)
  end

  def edit
    respond_with(@issue)
  end

  def show
gitlabhq's avatar
gitlabhq committed
38
    @notes = @issue.notes.order("created_at DESC").limit(20)
gitlabhq's avatar
gitlabhq committed
39
    @note = @project.notes.new(:noteable => @issue)
gitlabhq's avatar
gitlabhq committed
40 41 42

    respond_to do |format| 
      format.html
43
      format.js { respond_with_notes }
gitlabhq's avatar
gitlabhq committed
44
    end
gitlabhq's avatar
gitlabhq committed
45 46 47 48 49
  end

  def create
    @issue = @project.issues.new(params[:issue])
    @issue.author = current_user
50
    if @issue.save && @issue.assignee != current_user
gitlabhq's avatar
gitlabhq committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
      Notify.new_issue_email(@issue).deliver
    end

    respond_with(@issue)
  end

  def update
    @issue.update_attributes(params[:issue])

    respond_to do |format|
      format.js
      format.html { redirect_to [@project, @issue]}
    end
  end

  def destroy
gitlabhq's avatar
gitlabhq committed
67 68
    return access_denied! unless can?(current_user, :admin_issue, @issue)

gitlabhq's avatar
gitlabhq committed
69 70 71
    @issue.destroy

    respond_to do |format|
Nihad Abbasov's avatar
Nihad Abbasov committed
72
      format.js { render :nothing => true }
gitlabhq's avatar
gitlabhq committed
73 74
    end
  end
VSizov's avatar
VSizov committed
75 76

  def sort
gitlabhq's avatar
gitlabhq committed
77
    @issues = @project.issues.where(:id => params['issue'])
VSizov's avatar
VSizov committed
78 79 80 81 82 83 84
    @issues.each do |issue|
      issue.position = params['issue'].index(issue.id.to_s) + 1
      issue.save
    end

    render :nothing => true
  end
gitlabhq's avatar
gitlabhq committed
85

Adam Leonard's avatar
Adam Leonard committed
86
  def search
87 88 89 90 91 92 93 94 95 96 97
    terms = params['terms']

    @project  = Project.find(params['project'])
    @issues   = case params[:status].to_i
                  when 1 then @project.issues
                  when 2 then @project.issues.closed
                  when 3 then @project.issues.opened.assigned(current_user)
                  else @project.issues.opened
                end

    @issues = @issues.where("title LIKE ? OR content LIKE ?", "%#{terms}%", "%#{terms}%") unless terms.blank?
Adam Leonard's avatar
Adam Leonard committed
98 99 100 101

    render :partial => 'issues'
  end

Nihad Abbasov's avatar
Nihad Abbasov committed
102
  protected
gitlabhq's avatar
gitlabhq committed
103 104 105 106

  def issue
    @issue ||= @project.issues.find(params[:id])
  end
gitlabhq's avatar
gitlabhq committed
107
end