gitlab-org--gitlab-foss/app/controllers/groups/milestones_controller.rb

77 lines
2 KiB
Ruby
Raw Normal View History

2015-04-30 13:06:18 -04:00
class Groups::MilestonesController < Groups::ApplicationController
include GlobalMilestones
2016-03-20 16:03:53 -04:00
before_action :group_projects
before_action :milestone, only: [:show, :update]
2016-03-20 16:03:53 -04:00
before_action :authorize_admin_milestones!, only: [:new, :create, :update]
def index
respond_to do |format|
format.html do
@milestones = Kaminari.paginate_array(milestones).page(params[:page])
end
end
end
2014-06-25 10:19:03 -04:00
def new
@milestone = Milestone.new
end
def create
project_ids = params[:milestone][:project_ids].reject(&:blank?)
title = milestone_params[:title]
2016-04-05 10:51:19 -04:00
if create_milestones(project_ids)
2016-04-01 14:50:17 -04:00
redirect_to milestone_path(title)
else
2016-04-04 16:04:35 -04:00
render_new_with_error(project_ids.empty?)
end
end
def show
end
2014-06-26 07:32:35 -04:00
def update
@milestone.milestones.each do |milestone|
Milestones::UpdateService.new(milestone.project, current_user, milestone_params).execute(milestone)
2014-06-26 07:32:35 -04:00
end
redirect_back_or_default(default: milestone_path(@milestone.title))
2014-06-26 07:32:35 -04:00
end
2014-06-26 05:58:40 -04:00
private
2016-04-05 10:51:19 -04:00
def create_milestones(project_ids)
2016-04-04 16:04:35 -04:00
return false unless project_ids.present?
2016-04-01 14:50:17 -04:00
2016-04-04 16:04:35 -04:00
ActiveRecord::Base.transaction do
@projects.where(id: project_ids).each do |project|
Milestones::CreateService.new(project, current_user, milestone_params).execute
end
2016-04-04 16:04:35 -04:00
end
true
2016-04-05 10:51:19 -04:00
rescue ActiveRecord::ActiveRecordError => e
2016-04-04 16:04:35 -04:00
flash.now[:alert] = "An error occurred while creating the milestone: #{e.message}"
false
end
2016-04-04 16:04:35 -04:00
def render_new_with_error(empty_project_ids)
@milestone = Milestone.new(milestone_params)
@milestone.errors.add(:base, "Please select at least one project.") if empty_project_ids
render :new
end
2016-03-20 16:03:53 -04:00
def authorize_admin_milestones!
return render_404 unless can?(current_user, :admin_milestones, group)
end
def milestone_params
2016-11-15 12:48:30 -05:00
params.require(:milestone).permit(:title, :description, :start_date, :due_date, :state_event)
2014-06-26 05:58:40 -04:00
end
def milestone_path(title)
group_milestone_path(@group, title.to_slug.to_s, title: title)
end
end