BigW Consortium Gitlab

milestones_controller.rb 2.86 KB
Newer Older
1
class Projects::MilestonesController < Projects::ApplicationController
2
  before_filter :module_enabled
3
  before_filter :milestone, only: [:edit, :update, :destroy, :show, :sort_issues, :sort_merge_requests]
4 5 6 7 8

  # Allow read any milestone
  before_filter :authorize_read_milestone!

  # Allow admin milestone
9
  before_filter :authorize_admin_milestone!, except: [:index, :show]
10 11 12 13

  respond_to :html

  def index
14
    @milestones = case params[:f]
15
                  when 'all'; @project.milestones.order("state, due_date DESC")
16
                  when 'closed'; @project.milestones.closed.order("due_date DESC")
17
                  else @project.milestones.active.order("due_date ASC")
18 19
                  end

20
    @milestones = @milestones.includes(:project)
21 22 23 24 25 26 27 28 29 30 31 32 33
    @milestones = @milestones.page(params[:page]).per(20)
  end

  def new
    @milestone = @project.milestones.new
    respond_with(@milestone)
  end

  def edit
    respond_with(@milestone)
  end

  def show
34
    @issues = @milestone.issues
35
    @users = @milestone.participants.uniq
36
    @merge_requests = @milestone.merge_requests
37 38 39
  end

  def create
40
    @milestone = Milestones::CreateService.new(project, current_user, milestone_params).execute
41 42 43 44 45 46 47 48 49

    if @milestone.save
      redirect_to project_milestone_path(@project, @milestone)
    else
      render "new"
    end
  end

  def update
50
    @milestone = Milestones::UpdateService.new(project, current_user, milestone_params).execute(milestone)
51 52 53

    respond_to do |format|
      format.js
54
      format.html do
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        if @milestone.valid?
          redirect_to [@project, @milestone]
        else
          render :edit
        end
      end
    end
  end

  def destroy
    return access_denied! unless can?(current_user, :admin_milestone, @milestone)

    @milestone.destroy

    respond_to do |format|
      format.html { redirect_to project_milestones_path }
71
      format.js { render nothing: true }
72 73 74
    end
  end

75 76 77 78 79 80 81 82 83 84
  def sort_issues
    @issues = @milestone.issues.where(id: params['sortable_issue'])
    @issues.each do |issue|
      issue.position = params['sortable_issue'].index(issue.id.to_s) + 1
      issue.save
    end

    render json: { saved: true }
  end

85 86 87 88 89 90 91 92 93 94
  def sort_merge_requests
    @merge_requests = @milestone.merge_requests.where(id: params['sortable_merge_request'])
    @merge_requests.each do |merge_request|
      merge_request.position = params['sortable_merge_request'].index(merge_request.id.to_s) + 1
      merge_request.save
    end

    render json: { saved: true }
  end

95 96 97
  protected

  def milestone
skv committed
98
    @milestone ||= @project.milestones.find_by!(iid: params[:id])
99 100 101 102 103 104 105 106 107
  end

  def authorize_admin_milestone!
    return render_404 unless can?(current_user, :admin_milestone, @project)
  end

  def module_enabled
    return render_404 unless @project.issues_enabled
  end
108 109 110 111

  def milestone_params
    params.require(:milestone).permit(:title, :description, :due_date, :state_event)
  end
112
end