BigW Consortium Gitlab

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

  # Authorize
  before_filter :add_project_abilities
10 11

  # Allow read any issue
gitlabhq committed
12
  before_filter :authorize_read_issue!
13 14 15 16 17 18 19 20 21

  # Allow write(create) issue
  before_filter :authorize_write_issue!, :only => [:new, :create]

  # Allow modify issue
  before_filter :authorize_modify_issue!, :only => [:close, :edit, :update, :sort]

  # Allow destroy issue
  before_filter :authorize_admin_issue!, :only => [:destroy]
gitlabhq committed
22

23
  respond_to :js, :html
gitlabhq committed
24 25 26

  def index
    @issues = case params[:f].to_i
gitlabhq committed
27
              when 1 then @project.issues
gitlabhq committed
28 29 30
              when 2 then @project.issues.closed
              when 3 then @project.issues.opened.assigned(current_user)
              else @project.issues.opened
31
              end.page(params[:page]).per(20)
gitlabhq committed
32

33
    @issues = @issues.includes(:author, :project).order("critical, updated_at")
gitlabhq committed
34

gitlabhq committed
35 36 37
    respond_to do |format|
      format.html # index.html.erb
      format.js
38
      format.atom { render :layout => false }
gitlabhq committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52
    end
  end

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

  def edit
    respond_with(@issue)
  end

  def show
    @note = @project.notes.new(:noteable => @issue)
gitlabhq committed
53

54 55 56 57 58 59 60
    @commits = if @issue.branch_name && @project.repo.heads.map(&:name).include?(@issue.branch_name)
                 @project.repo.commits_between("master", @issue.branch_name)
               else 
                 []
               end


61
    respond_to do |format|
gitlabhq committed
62
      format.html
63
      format.js
gitlabhq committed
64
    end
gitlabhq committed
65 66 67 68 69
  end

  def create
    @issue = @project.issues.new(params[:issue])
    @issue.author = current_user
Valery Sizov committed
70
    @issue.save
gitlabhq committed
71

72 73 74 75
    respond_to do |format|
      format.html { redirect_to project_issue_path(@project, @issue) }
      format.js
    end
gitlabhq committed
76 77 78
  end

  def update
79
    @issue.update_attributes(params[:issue].merge(:author_id_of_changes => current_user.id))
gitlabhq committed
80 81 82

    respond_to do |format|
      format.js
83 84 85 86 87 88 89
      format.html do 
        if @issue.valid?
          redirect_to [@project, @issue]
        else
          render :edit
        end
      end
gitlabhq committed
90 91 92 93
    end
  end

  def destroy
gitlabhq committed
94 95
    return access_denied! unless can?(current_user, :admin_issue, @issue)

gitlabhq committed
96 97 98
    @issue.destroy

    respond_to do |format|
99
      format.html { redirect_to project_issues_path }
Nihad Abbasov committed
100
      format.js { render :nothing => true }
gitlabhq committed
101 102
    end
  end
VSizov committed
103 104

  def sort
gitlabhq committed
105
    @issues = @project.issues.where(:id => params['issue'])
VSizov committed
106 107 108 109 110 111 112
    @issues.each do |issue|
      issue.position = params['issue'].index(issue.id.to_s) + 1
      issue.save
    end

    render :nothing => true
  end
gitlabhq committed
113

114
  def search
115 116 117 118 119 120 121 122
    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
Dmitriy Zaporozhets committed
123
                end.page(params[:page]).per(100)
124

Nihad Abbasov committed
125
    @issues = @issues.where("title LIKE ?", "%#{terms}%") unless terms.blank?
126 127 128 129

    render :partial => 'issues'
  end

Nihad Abbasov committed
130
  protected
gitlabhq committed
131 132 133 134

  def issue
    @issue ||= @project.issues.find(params[:id])
  end
135 136

  def authorize_modify_issue!
137
    return render_404 unless can?(current_user, :modify_issue, @issue)
138 139 140
  end

  def authorize_admin_issue!
141
    return render_404 unless can?(current_user, :admin_issue, @issue)
142
  end
143 144 145 146

  def module_enabled
    return render_404 unless @project.issues_enabled
  end
gitlabhq committed
147
end