mirror of
https://github.com/rickbarrette/subtask_field_copier.git
synced 2026-11-17 12: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
|
||||
@@ -7,40 +7,29 @@
|
||||
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
#
|
||||
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
module SubtaskFieldCopier
|
||||
module Patches
|
||||
module IssuesControllerPatch
|
||||
|
||||
private
|
||||
|
||||
# Redmine builds the new issue here for the new-issue page, its form refreshes (e.g. changing the
|
||||
# tracker) and create. After Redmine builds it, fill a subtask's untouched fields from its parent.
|
||||
# Fields present in the request are left alone, so this never overrides what the user entered;
|
||||
# on a form refresh it fills fields that just appeared (e.g. from the newly chosen tracker).
|
||||
def build_new_issue_from_params
|
||||
# 1. Run the original Redmine method to initialize @issue
|
||||
super
|
||||
return if performed?
|
||||
return unless @issue&.new_record? && @issue.parent_issue_id.present?
|
||||
|
||||
# 2. Only intervene for new subtasks explicitly created via the UI (GET request).
|
||||
# This prevents overwriting user-submitted data if form validation fails on a POST request.
|
||||
if request.get? && @issue.new_record? && @issue.parent_issue_id.present?
|
||||
parent_issue = Issue.find_by_id(@issue.parent_issue_id)
|
||||
# Only a parent the user is allowed to see, so its values can't leak into the form
|
||||
parent = Issue.visible.find_by(id: @issue.parent_issue_id)
|
||||
return unless parent
|
||||
|
||||
if parent_issue
|
||||
# --- COPY CUSTOM FIELDS ---
|
||||
# Extract all custom field values from the parent and assign them to the new issue
|
||||
cf_values = {}
|
||||
parent_issue.custom_field_values.each do |cfv|
|
||||
cf_values[cfv.custom_field_id] = cfv.value
|
||||
end
|
||||
@issue.custom_field_values = cf_values
|
||||
|
||||
# --- COPY REGISTERED FIELDS ---
|
||||
# Iterate over any fields registered by other plugins
|
||||
SubtaskFieldCopier.registered_fields.each do |field|
|
||||
setter = "#{field}="
|
||||
|
||||
# Use metaprogramming to check if the setter/getter exists, then apply
|
||||
if @issue.respond_to?(setter) && parent_issue.respond_to?(field)
|
||||
@issue.send(setter, parent_issue.send(field))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
submitted = params[:issue].respond_to?(:to_unsafe_h) ? params[:issue].to_unsafe_h : {}
|
||||
SubtaskFieldCopier.copy_from_parent(@issue, parent, User.current, submitted)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user