milestones_controller.rb 1.3 KB
Newer Older
Douwe Maan's avatar
Douwe Maan committed
1
class Groups::MilestonesController < Groups::ApplicationController
2
  include GlobalMilestones
3

4 5 6 7
  before_action :projects
  before_action :milestones, only: [:index]
  before_action :milestone, only: [:show, :update]
  before_action :authorize_group_milestone!, only: [:create, :update]
8

9
  def index
10
  end
11

12
  def new
13
    @milestone = Milestone.new
14 15 16 17 18 19 20 21 22 23
  end

  def create
    project_ids = params[:milestone][:project_ids]
    title = milestone_params[:title]

    @group.projects.where(id: project_ids).each do |project|
      Milestones::CreateService.new(project, current_user, milestone_params).execute
    end

24
    redirect_to milestone_path(title)
25 26
  end

27 28 29
  def show
  end

30
  def update
31 32
    @milestone.milestones.each do |milestone|
      Milestones::UpdateService.new(milestone.project, current_user, milestone_params).execute(milestone)
33 34
    end

35
    redirect_back_or_default(default: milestone_path(@milestone.title))
36 37
  end

Marin Jankovski's avatar
Marin Jankovski committed
38 39
  private

40
  def authorize_group_milestone!
41
    return render_404 unless can?(current_user, :admin_milestones, group)
42 43
  end

44 45
  def milestone_params
    params.require(:milestone).permit(:title, :description, :due_date, :state_event)
Marin Jankovski's avatar
Marin Jankovski committed
46
  end
47

48
  def milestone_path(title)
49
    group_milestone_path(@group, title.to_slug.to_s, title: title)
50
  end
51

52 53
  def projects
    @projects ||= @group.projects
54
  end
55
end