Ask for tracker custom fields on the estimate issues preview

- New "Issue fields" section on the Create Issues from Estimate page
  lists the custom fields the selected trackers use; values are entered
  once and applied to every created issue whose tracker uses the field
- Required fields (the field's Required flag or a workflow rule for the
  tracker and the user's role) are starred; the list updates as lines
  are ticked or trackers change
- Values go through Issue#safe_attributes=, so fields that are hidden or
  read-only for the user, or unused by a tracker, are ignored
- Entered values are kept when creation fails validation

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
This commit is contained in:
Claude committed 2026-10-28 20:45:31 -04:00
1 parent 184c1e2464
commit b6cebd5b6b
6 files changed
+154 -4

No files matched your search

@@ -68,8 +68,9 @@ module RedmineQboCalendar
end
# rows: { line_id => { 'selected' => '1', 'subject' => ..., 'tracker_id' => ..., 'template' => 'global:3', 'estimated_hours' => ... } }
# custom_field_values: { custom_field_id => value }, applied to every issue whose tracker uses the field
# Returns the created issues, or [] with #errors filled in.
def create_issues(project:, rows:, author: User.current)
def create_issues(project:, rows:, custom_field_values: {}, author: User.current)
@errors = []
rows = (rows || {}).to_h.transform_keys(&:to_s)
@@ -82,7 +83,7 @@ module RedmineQboCalendar
allowed_trackers = Issue.allowed_target_trackers(project, author).to_a
built = selected.map do |proposal|
[proposal, build_issue(proposal, rows[proposal.line_id] || {}, project, author, allowed_trackers)]
[proposal, build_issue(proposal, rows[proposal.line_id] || {}, project, author, allowed_trackers, custom_field_values)]
end
Issue.transaction do
@@ -153,9 +154,15 @@ module RedmineQboCalendar
key.to_s
end
def build_issue(proposal, row, project, author, allowed_trackers)
def build_issue(proposal, row, project, author, allowed_trackers, custom_field_values)
issue = Issue.new(project: project, author: author)
issue.tracker = allowed_trackers.detect { |t| t.id == row['tracker_id'].to_i }
# Shared custom field values. Going through safe_attributes means fields the tracker doesn't
# use, or that this user can't edit (hidden / read-only by workflow), are ignored.
if custom_field_values.present? && issue.tracker
issue.send(:safe_attributes=, { 'custom_field_values' => custom_field_values.to_h }, author)
end
issue.subject = (row['subject'].presence || proposal.description).to_s.strip.truncate(255)
template = IssueTemplateSupport.find(row['template'], project)
@@ -0,0 +1,72 @@
#The MIT License (MIT)
#
#Copyright (c) 2026 rick barrette
#
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
#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 RedmineQboCalendar
# The issue custom fields shown on the "Create Issues from Estimate" preview page.
#
# For every tracker the user may pick, a sample new issue is built in the target project to find
# which custom fields the user can edit, and which are required (either the field's own
# "Required" flag or a workflow rule for that tracker / role). Values are entered once on the page
# and applied to every created issue whose tracker uses that field.
class IssueFieldSet
# File uploads can't be handled by this shared form
UNSUPPORTED_FORMATS = %w[attachment].freeze
Field = Struct.new(:custom_field, :custom_value, :tracker_ids, :required_tracker_ids, keyword_init: true) do
def id
custom_field.id
end
def required_for_any?
required_tracker_ids.any?
end
end
attr_reader :fields
# values: the submitted { custom_field_id => value } hash, used to refill the form after errors
def initialize(project:, trackers:, user: User.current, values: {})
@fields = []
by_id = {}
Array(trackers).each do |tracker|
issue = Issue.new(project: project, author: user)
issue.tracker = tracker
issue.editable_custom_field_values(user).each do |custom_value|
custom_field = custom_value.custom_field
next if UNSUPPORTED_FORMATS.include?(custom_field.field_format)
field = by_id[custom_field.id] ||= Field.new(
custom_field: custom_field,
custom_value: custom_value,
tracker_ids: [],
required_tracker_ids: []
)
field.tracker_ids << tracker.id
if custom_field.is_required? || issue.required_attribute?(custom_field.id.to_s, user)
field.required_tracker_ids << tracker.id
end
end
end
@fields = by_id.values.sort_by { |f| [f.required_for_any? ? 0 : 1, f.custom_field.position.to_i] }
values = (values || {}).to_h.transform_keys(&:to_s)
@fields.each do |field|
field.custom_value.value = values[field.id.to_s] if values.key?(field.id.to_s)
end
end
def any?
@fields.any?
end
end
end