mirror of
https://github.com/rickbarrette/subtask_field_copier.git
synced 2026-11-17 20:10:36 -05:00
Only copy visible values into untouched subtask fields
- Parent is looked up with Issue.visible, so a new-subtask form can't be used to read another issue's custom field values - Only custom fields the user can see on the parent and edit on the subtask are copied (per-role field visibility) - A parent value is copied only when the parent has one and the subtask's field is untouched (empty or still at its default); blank parent values no longer wipe defaults, and values the user typed or a link passed in are never overwritten - Also fills fields that appear when the form refreshes (e.g. after changing the tracker), leaving submitted fields alone - Registered plugin fields (customer, vehicle, ...) follow the same rules - build_new_issue_from_params stays private Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
This commit is contained in:
2 files changed
+71
-28
No files matched your search
@@ -10,6 +10,7 @@
|
||||
|
||||
module SubtaskFieldCopier
|
||||
|
||||
# Issue attributes other plugins ask to have copied to subtasks (e.g. :customer, :vehicle, :estimate)
|
||||
mattr_accessor :registered_fields
|
||||
self.registered_fields = []
|
||||
|
||||
@@ -18,4 +19,57 @@ module SubtaskFieldCopier
|
||||
IssuesController.prepend SubtaskFieldCopier::Patches::IssuesControllerPatch
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Fills a new subtask's untouched fields from its parent.
|
||||
#
|
||||
# A field is copied only when:
|
||||
# - the user can see it on the parent and edit it on the subtask (per-role field visibility), and
|
||||
# - the parent has a value, and
|
||||
# - the subtask's field wasn't submitted with the request (so values the user typed or a link
|
||||
# passed in are never overwritten), and
|
||||
# - the subtask's field is still untouched: empty, or still at that field's default value.
|
||||
#
|
||||
# submitted: the issue params of the request (a Hash, possibly empty).
|
||||
def self.copy_from_parent(issue, parent, user, submitted = {})
|
||||
submitted = (submitted || {}).to_h.stringify_keys
|
||||
submitted_cf_ids = (submitted['custom_field_values'] || {}).to_h.keys.map(&:to_s)
|
||||
|
||||
parent_values = parent.visible_custom_field_values(user).index_by(&:custom_field_id)
|
||||
copied = {}
|
||||
|
||||
issue.editable_custom_field_values(user).each do |child_value|
|
||||
field = child_value.custom_field
|
||||
next if submitted_cf_ids.include?(field.id.to_s)
|
||||
|
||||
parent_value = parent_values[field.id]&.value
|
||||
next if blank_value?(parent_value)
|
||||
next unless untouched?(child_value)
|
||||
|
||||
copied[field.id.to_s] = parent_value
|
||||
end
|
||||
issue.custom_field_values = copied if copied.any?
|
||||
|
||||
registered_fields.each do |field|
|
||||
field = field.to_s
|
||||
setter = "#{field}="
|
||||
next unless issue.respond_to?(setter) && parent.respond_to?(field)
|
||||
next if submitted.key?(field) || submitted.key?("#{field}_id")
|
||||
|
||||
value = parent.public_send(field)
|
||||
next if blank_value?(value)
|
||||
next unless blank_value?(issue.public_send(field))
|
||||
|
||||
issue.public_send(setter, value)
|
||||
end
|
||||
end
|
||||
|
||||
def self.untouched?(custom_value)
|
||||
current = custom_value.value
|
||||
default = custom_value.custom_field.default_value
|
||||
blank_value?(current) || Array(current).map(&:to_s).sort == Array(default).map(&:to_s).sort
|
||||
end
|
||||
|
||||
def self.blank_value?(value)
|
||||
value.respond_to?(:empty?) ? Array(value).reject { |v| v.to_s.strip.empty? }.empty? : value.nil?
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user