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

114 lines
2.9 KiB
Ruby
Raw Normal View History

2015-04-30 13:06:18 -04:00
class Groups::MilestonesController < Groups::ApplicationController
2017-04-25 10:25:20 -04:00
include MilestoneActions
2016-03-20 16:03:53 -04:00
before_action :group_projects
2018-08-06 15:02:52 -04:00
before_action :milestone, only: [:edit, :show, :update, :merge_requests, :participants, :labels, :destroy]
before_action :authorize_admin_milestones!, only: [:edit, :new, :create, :update, :destroy]
def index
respond_to do |format|
format.html do
2017-07-07 11:08:49 -04:00
@milestone_states = GlobalMilestone.states_count(group_projects, group)
@milestones = Kaminari.paginate_array(milestones).page(params[:page])
end
2017-06-22 19:31:25 -04:00
format.json do
render json: milestones.map { |m| m.for_display.slice(:id, :title, :name) }
2017-06-22 19:31:25 -04:00
end
end
end
2014-06-25 10:19:03 -04:00
def new
@milestone = Milestone.new
end
def create
2017-07-07 11:08:49 -04:00
@milestone = Milestones::CreateService.new(group, current_user, milestone_params).execute
2017-07-07 11:08:49 -04:00
if @milestone.persisted?
redirect_to milestone_path
else
2017-07-07 11:08:49 -04:00
render "new"
end
end
def show
end
2017-07-07 11:08:49 -04:00
def edit
render_404 if @milestone.legacy_group_milestone?
2014-06-26 07:32:35 -04:00
end
2017-07-07 11:08:49 -04:00
def update
# Keep this compatible with legacy group milestones where we have to update
# all projects milestones states at once.
if @milestone.legacy_group_milestone?
2017-07-07 11:08:49 -04:00
update_params = milestone_params.select { |key| key == "state_event" }
milestones = @milestone.milestones
else
update_params = milestone_params
milestones = [@milestone]
end
2016-04-01 14:50:17 -04:00
2017-07-07 11:08:49 -04:00
milestones.each do |milestone|
Milestones::UpdateService.new(milestone.parent, current_user, update_params).execute(milestone)
2016-04-04 16:04:35 -04:00
end
2017-07-07 11:08:49 -04:00
redirect_to milestone_path
end
2018-08-06 15:02:52 -04:00
def destroy
return render_404 if @milestone.legacy_group_milestone?
Milestones::DestroyService.new(group, current_user).execute(@milestone)
respond_to do |format|
format.html { redirect_to group_milestones_path(group), status: :see_other }
format.js { head :ok }
end
end
2017-07-07 11:08:49 -04:00
private
2016-03-20 16:03:53 -04:00
def authorize_admin_milestones!
2018-08-06 15:02:52 -04:00
return render_404 unless can?(current_user, :admin_milestone, 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
2017-07-07 11:08:49 -04:00
def milestone_path
if @milestone.legacy_group_milestone?
2017-07-07 11:08:49 -04:00
group_milestone_path(group, @milestone.safe_title, title: @milestone.title)
else
group_milestone_path(group, @milestone.iid)
end
end
def milestones
2017-07-07 11:08:49 -04:00
milestones = MilestonesFinder.new(search_params).execute
2017-11-06 12:51:24 -05:00
@sort = params[:sort] || 'due_date_asc'
MilestoneArray.sort(milestones + legacy_milestones, @sort)
end
def legacy_milestones
GroupMilestone.build_collection(group, group_projects, params)
end
def milestone
2017-07-07 11:08:49 -04:00
@milestone =
if params[:title]
GroupMilestone.build(group, group_projects, params[:title])
else
group.milestones.find_by_iid(params[:id])
end
render_404 unless @milestone
end
def search_params
params.permit(:state).merge(group_ids: group.id)
end
end