BigW Consortium Gitlab

milestones_controller.rb 2.5 KB
Newer Older
1
class Groups::MilestonesController < Groups::ApplicationController
2 3
  include MilestoneActions

4
  before_action :group_projects
Felipe Artur committed
5 6
  before_action :milestone, only: [:edit, :show, :update, :merge_requests, :participants, :labels]
  before_action :authorize_admin_milestones!, only: [:edit, :new, :create, :update]
7

8
  def index
9 10
    respond_to do |format|
      format.html do
Felipe Artur committed
11
        @milestone_states = GlobalMilestone.states_count(group_projects, group)
12 13
        @milestones = Kaminari.paginate_array(milestones).page(params[:page])
      end
14 15 16
      format.json do
        render json: milestones.map { |m| m.for_display.slice(:title, :name) }
      end
17
    end
18
  end
19

20
  def new
21
    @milestone = Milestone.new
22 23 24
  end

  def create
Felipe Artur committed
25
    @milestone = Milestones::CreateService.new(group, current_user, milestone_params).execute
26

Felipe Artur committed
27 28
    if @milestone.persisted?
      redirect_to milestone_path
29
    else
Felipe Artur committed
30
      render "new"
31 32 33
    end
  end

34 35 36
  def show
  end

Felipe Artur committed
37
  def edit
38
    render_404 if @milestone.legacy_group_milestone?
39 40
  end

Felipe Artur committed
41 42 43
  def update
    # Keep this compatible with legacy group milestones where we have to update
    # all projects milestones states at once.
44
    if @milestone.legacy_group_milestone?
Felipe Artur committed
45 46 47 48 49 50
      update_params = milestone_params.select { |key| key == "state_event" }
      milestones = @milestone.milestones
    else
      update_params = milestone_params
      milestones = [@milestone]
    end
Felipe Artur committed
51

Felipe Artur committed
52 53
    milestones.each do |milestone|
      Milestones::UpdateService.new(milestone.parent, current_user, update_params).execute(milestone)
Felipe Artur committed
54 55
    end

Felipe Artur committed
56
    redirect_to milestone_path
57 58
  end

Felipe Artur committed
59
  private
60

61
  def authorize_admin_milestones!
62
    return render_404 unless can?(current_user, :admin_milestones, group)
63 64
  end

65
  def milestone_params
66
    params.require(:milestone).permit(:title, :description, :start_date, :due_date, :state_event)
67
  end
68

Felipe Artur committed
69
  def milestone_path
70
    if @milestone.legacy_group_milestone?
Felipe Artur committed
71 72 73 74
      group_milestone_path(group, @milestone.safe_title, title: @milestone.title)
    else
      group_milestone_path(group, @milestone.iid)
    end
75
  end
76 77

  def milestones
Felipe Artur committed
78 79 80 81 82 83
    search_params = params.merge(group_ids: group.id)

    milestones = MilestonesFinder.new(search_params).execute
    legacy_milestones = GroupMilestone.build_collection(group, group_projects, params)

    milestones + legacy_milestones
84 85 86
  end

  def milestone
Felipe Artur committed
87 88 89 90 91 92 93
    @milestone =
      if params[:title]
        GroupMilestone.build(group, group_projects, params[:title])
      else
        group.milestones.find_by_iid(params[:id])
      end

94 95
    render_404 unless @milestone
  end
96
end