milestones_controller.rb 1.63 KB
Newer Older
1
class Groups::MilestonesController < Groups::ApplicationController
2
  before_action :authorize_group_milestone!, only: :update
3

4
  def index
Douwe Maan's avatar
Douwe Maan committed
5 6 7 8
    project_milestones = case params[:state]
                         when 'all'; state
                         when 'closed'; state('closed')
                         else state('active')
9
                         end
10
    @group_milestones = Milestones::GroupService.new(project_milestones).execute
11
    @group_milestones = Kaminari.paginate_array(@group_milestones).page(params[:page]).per(PER_PAGE)
12
  end
13

14
  def show
15
    project_milestones = Milestone.where(project_id: group.projects).order("due_date ASC")
16
    @group_milestone = Milestones::GroupService.new(project_milestones).milestone(title)
17 18
  end

19
  def update
20
    project_milestones = Milestone.where(project_id: group.projects).order("due_date ASC")
21
    @group_milestones = Milestones::GroupService.new(project_milestones).milestone(title)
22

23
    @group_milestones.milestones.each do |milestone|
24
      Milestones::UpdateService.new(milestone.project, current_user, params[:milestone]).execute(milestone)
25 26 27 28 29 30 31 32 33 34
    end

    respond_to do |format|
      format.js
      format.html do
        redirect_to group_milestones_path(group)
      end
    end
  end

Marin Jankovski's avatar
Marin Jankovski committed
35 36
  private

37 38 39 40
  def group
    @group ||= Group.find_by(path: params[:group_id])
  end

41
  def title
42
    params[:title]
43 44
  end

Douwe Maan's avatar
Douwe Maan committed
45
  def state(state = nil)
46 47 48
    conditions = { project_id: group.projects }
    conditions.reverse_merge!(state: state) if state
    Milestone.where(conditions).order("title ASC")
Marin Jankovski's avatar
Marin Jankovski committed
49
  end
50 51

  def authorize_group_milestone!
52
    return render_404 unless can?(current_user, :admin_group, group)
53
  end
54
end