Commit a33095d4 authored by Felipe Artur's avatar Felipe Artur Committed by Yorick Peterse

Fix problem when creating milestones in groups without projects

parent 265f796b
...@@ -18,14 +18,14 @@ class Groups::MilestonesController < Groups::ApplicationController ...@@ -18,14 +18,14 @@ class Groups::MilestonesController < Groups::ApplicationController
end end
def create def create
project_ids = params[:milestone][:project_ids] project_ids = params[:milestone][:project_ids].reject(&:blank?)
title = milestone_params[:title] title = milestone_params[:title]
@projects.where(id: project_ids).each do |project| if project_ids.present?
Milestones::CreateService.new(project, current_user, milestone_params).execute create_milestones(project_ids, title)
else
render_new_with_error("Select a project(s).")
end end
redirect_to milestone_path(title)
end end
def show def show
...@@ -41,6 +41,24 @@ class Groups::MilestonesController < Groups::ApplicationController ...@@ -41,6 +41,24 @@ class Groups::MilestonesController < Groups::ApplicationController
private private
def create_milestones(project_ids, title)
begin
@projects.where(id: project_ids).each do |project|
ActiveRecord::Base.transaction { Milestones::CreateService.new(project, current_user, milestone_params).execute }
end
redirect_to milestone_path(title)
rescue => e
render_new_with_error("Error creating milestones: #{e.message}")
end
end
def render_new_with_error(error)
@milestone = Milestone.new(milestone_params)
flash[:alert] = error
render :new
end
def authorize_admin_milestones! def authorize_admin_milestones!
return render_404 unless can?(current_user, :admin_milestones, group) return render_404 unless can?(current_user, :admin_milestones, group)
end end
......
...@@ -23,5 +23,11 @@ describe Groups::MilestonesController do ...@@ -23,5 +23,11 @@ describe Groups::MilestonesController do
expect(response).to redirect_to(group_milestone_path(group, title.to_slug.to_s, title: title)) expect(response).to redirect_to(group_milestone_path(group, title.to_slug.to_s, title: title))
expect(Milestone.where(title: title).count).to eq(2) expect(Milestone.where(title: title).count).to eq(2)
end end
it "redirects to new when there are no project ids" do
post :create, group_id: group.id, milestone: { title: title, project_ids: [""] }
expect(response).to render_template :new
expect(flash[:alert]).to_not be_nil
end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment