Record added, changed and removed line items in the issue's history

Line item changes go into the journal of the issue update that made them, shown like
Redmine's own changes (e.g. "Line item Brake pads: 2 × $45.00 added"). They're only
visible to users who may view line items, in the history, emails, PDF, API and webhooks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-03 21:38:02 -05:00
1 parent a34c534bc1
commit 063da99904
6 files changed
+132

No files matched your search

+17
View File
@@ -17,6 +17,23 @@ class LineItem < ApplicationRecord
validates :unit_price, numericality: { greater_than_or_equal_to: 0 }
before_save :total
# The fields whose changes are recorded in the issue's history
JOURNALED_ATTRIBUTES = %w[description quantity unit_price item_id].freeze
# A one-line summary for the issue's history, e.g. "Brake pads: 2 × $45.00".
# With saved: true it describes the line as it is in the database, before any unsaved changes.
def journal_summary(saved: false)
desc, qty, price = %w[description quantity unit_price].map { |a| saved ? attribute_in_database(a) : self[a] }
qty = ActiveSupport::NumberHelper.number_to_rounded(qty.to_d, precision: 4, strip_insignificant_zeros: true)
price = ActiveSupport::NumberHelper.number_to_currency(price.to_d)
"#{desc.to_s.squish}: #{qty} × #{price}"
end
# True when a field shown in the issue's history has an unsaved change
def journaled_changes?
(changed & JOURNALED_ATTRIBUTES).any?
end
private
def total
+1
View File
@@ -13,6 +13,7 @@
en:
label_description: "Description"
label_line_item: "Line item"
label_line_items: "Line Items"
label_price: "Unit Price"
label_qty: "Quantity"
+47
View File
@@ -28,6 +28,53 @@ module LineItems
end
accepts_nested_attributes_for :line_items, allow_destroy: true, reject_if: proc { |attrs| attrs['description'].blank? }
# Record added, changed and removed line items in the issue's history (the journal Redmine is
# already writing for this update). Changes are collected before saving, while the old values are
# still known, and written after the line items are saved, so new lines have their ids.
# The after_update must be declared after accepts_nested_attributes_for: it then runs after the
# line items are saved, and before Redmine's after_save that saves the journal.
before_save :collect_line_item_changes
after_update :journalize_line_item_changes
def collect_line_item_changes
@line_item_changes = nil
return unless current_journal
# Only lines touched in this request are loaded into the association, so this doesn't query
@line_item_changes = association(:line_items).target.filter_map do |line|
if line.new_record?
[:added, line, nil] unless line.marked_for_destruction?
elsif line.marked_for_destruction?
[:removed, line, line.journal_summary(saved: true)]
elsif line.journaled_changes?
[:changed, line, line.journal_summary(saved: true)]
end
end
end
def journalize_line_item_changes
changes = @line_item_changes
@line_item_changes = nil
return if changes.blank? || current_journal.nil?
changes.each do |action, line, old_summary|
case action
when :added
add_line_item_detail(line, nil, line.journal_summary) if line.persisted?
when :removed
add_line_item_detail(line, old_summary, nil) if line.destroyed?
when :changed
new_summary = line.journal_summary
add_line_item_detail(line, old_summary, new_summary) unless new_summary == old_summary
end
end
end
def add_line_item_detail(line, old_value, value)
current_journal.details << JournalDetail.new(property: 'line_item', prop_key: line.id.to_s,
old_value: old_value, value: value)
end
# Returns line items for immediate children
def children_line_items
LineItem.where(issue_id: self.children.pluck(:id))
@@ -0,0 +1,40 @@
#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 LineItems
module Patches
# Shows line item changes in an issue's history, emails, PDF and Atom feed. Redmine's show_detail
# only knows its own kinds of change and returns nothing for others.
module IssuesHelperPatch
def show_detail(detail, no_html = false, options = {})
return super unless detail.property == 'line_item'
label = l(:label_line_item)
value = detail.value
old_value = detail.old_value
unless no_html
label = content_tag('strong', label)
value = content_tag('i', value) if value.present?
old_value = content_tag('i', old_value) if old_value.present?
old_value = content_tag('del', old_value) if old_value.present? && value.blank?
end
if detail.value.present? && detail.old_value.present?
l(:text_journal_changed, label: label, old: old_value, new: value).html_safe
elsif detail.value.present?
l(:text_journal_added, label: label, value: value).html_safe
else
l(:text_journal_deleted, label: label, old: old_value).html_safe
end
end
end
end
end
+25
View File
@@ -0,0 +1,25 @@
#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 LineItems
module Patches
# Line item changes in an issue's history are only shown to users who may view line items.
# Redmine uses visible_details for the history, emails (an update with nothing visible isn't sent),
# the API, PDFs and webhooks.
module JournalPatch
def visible_details(user = User.current)
details = super
return details if user.allowed_to?(:view_line_items, nil, global: true)
details.reject { |detail| detail.property == 'line_item' }
end
end
end
end
+2
View File
@@ -14,6 +14,8 @@ module RedmineQboLineItems
unless Issue.ancestors.include?(LineItems::Patches::IssuePatch)
# Patches
Issue.prepend LineItems::Patches::IssuePatch
Journal.prepend LineItems::Patches::JournalPatch
IssuesHelper.prepend LineItems::Patches::IssuesHelperPatch
# Hooks
LineItems::Hooks::ViewHookListener