Commit 1ba9a91c authored by Felipe's avatar Felipe

Fix problem when creating milestones in groups without projects

parent 59466a47
......@@ -18,14 +18,14 @@ class Groups::MilestonesController < Groups::ApplicationController
end
def create
project_ids = params[:milestone][:project_ids]
project_ids = params[:milestone][:project_ids].reject(&:blank?)
title = milestone_params[:title]
@projects.where(id: project_ids).each do |project|
Milestones::CreateService.new(project, current_user, milestone_params).execute
if project_ids.present?
create_milestones(project_ids, title)
else
render_new_with_error("Select a project(s).")
end
redirect_to milestone_path(title)
end
def show
......@@ -41,6 +41,24 @@ class Groups::MilestonesController < Groups::ApplicationController
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!
return render_404 unless can?(current_user, :admin_milestones, group)
end
......
......@@ -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(Milestone.where(title: title).count).to eq(2)
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
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