Skip to content

Commit

Permalink
Fix/copy project with required custom fields (#6083)
Browse files Browse the repository at this point in the history
* remove no longer required before_action

* fill custom_field_values on project copy

The option, whether to copy custom_values is replaced by always filling in the values from the copied from project

* copy wp custom fields on wp copy

* refactor projects copy controller

[ci skip]
  • Loading branch information
ulferts authored and oliverguenther committed Dec 14, 2017
1 parent fb018bb commit e8930af
Show file tree
Hide file tree
Showing 10 changed files with 351 additions and 198 deletions.
44 changes: 26 additions & 18 deletions app/controllers/copy_projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,28 @@ class CopyProjectsController < ApplicationController
before_action :disable_api
before_action :find_project
before_action :authorize
before_action :prepare_for_copy_project

def copy
@copy_project = project_copy

if @copy_project.valid?
target_project_params = @copy_project.attributes.compact
enqueue_copy_job

copy_project_job = CopyProjectJob.new(user_id: User.current.id,
source_project_id: @project.id,
target_project_params: target_project_params,
associations_to_copy: params[:only],
send_mails: params[:notifications] == '1')

Delayed::Job.enqueue copy_project_job
flash[:notice] = I18n.t('copy_project.started',
source_project_name: @project.name,
target_project_name: permitted_params.project[:name])
redirect_to origin
else
from = (['admin', 'settings'].include?(params[:coming_from]) ? params[:coming_from] : 'settings')
render action: "copy_from_#{from}"
render action: copy_action
end
end

def copy_project
from = (['admin', 'settings'].include?(params[:coming_from]) ? params[:coming_from] : 'settings')
@copy_project = Project.copy_attributes(@project)
if @copy_project
@copy_project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
render action: "copy_from_#{from}"

render action: copy_action
else
redirect_to :back
end
Expand All @@ -73,6 +64,12 @@ def copy_project

private

def copy_action
from = (%w(admin settings).include?(params[:coming_from]) ? params[:coming_from] : 'settings')

"copy_from_#{from}"
end

def project_copy
copy_project = Project.new
copy_project.attributes = permitted_params.project
Expand All @@ -89,10 +86,21 @@ def origin
params[:coming_from] == 'admin' ? projects_path : settings_project_path(@project.id)
end

def prepare_for_copy_project
@issue_custom_fields = WorkPackageCustomField.order("#{CustomField.table_name}.position")
@types = ::Type.all
@root_projects = Project.where("parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}")
.order('name')
def enqueue_copy_job
copy_project_job = CopyProjectJob.new(user_id: User.current.id,
source_project_id: @project.id,
target_project_params: target_project_params,
associations_to_copy: params[:only],
send_mails: params[:notifications] == '1')

Delayed::Job.enqueue copy_project_job
end

def target_project_params
@copy_project
.attributes
.compact
.with_indifferent_access
.merge(custom_field_values: @copy_project.custom_value_attributes)
end
end
6 changes: 4 additions & 2 deletions app/models/project/copy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ def copy_attributes(project)
self.enabled_module_names = project.enabled_module_names
self.types = project.types
self.work_package_custom_fields = project.work_package_custom_fields
self.custom_field_values = project.custom_value_attributes
end
return self

self
rescue ActiveRecord::RecordNotFound
return nil
nil
end

def copy_associations(from_model, options = {})
Expand Down
3 changes: 2 additions & 1 deletion app/services/work_packages/copy_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ def copied_attributes(wp, override)
.attributes
.slice(*writable_work_package_attributes(wp))
.merge('author_id' => user.id,
'parent_id' => wp.parent_id)
'parent_id' => wp.parent_id,
'custom_field_values' => wp.custom_value_attributes)
.merge(override)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ See doc/COPYRIGHT.rdoc for more details.
<fieldset class="form--fieldset">
<legend class="form--fieldset-legend"><%= l(:button_copy) %></legend>

<%= render partial: "copy_projects/copy_settings/block_checkbox",
locals: { name: "custom_values", checked: true, label: l('copy_project.project_custom_fields'),
count: project.custom_values.count } %>
<%= render partial: "copy_projects/copy_settings/block_checkbox",
locals: { name: "queries", checked: true, label: l(:label_query_plural),
count: project.queries.count } %>
Expand Down
165 changes: 0 additions & 165 deletions features/projects/copy_project.feature

This file was deleted.

2 changes: 1 addition & 1 deletion lib/copy_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def copy_associations(from_model, options = {})
end
self
end
end
end
end

# copies everything (associations and attributes) based on
Expand Down
6 changes: 6 additions & 0 deletions lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ def custom_field_values
end
end

def custom_value_attributes
custom_field_values
.map { |cv| [cv.custom_field_id, cv.value] }
.to_h
end

def visible_custom_field_values
custom_field_values.select(&:visible?)
end
Expand Down
Loading

0 comments on commit e8930af

Please sign in to comment.