From 0aa95918f662560318444388ac3a569060f5b9aa Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Sat, 21 Nov 2026 20:26:25 -0500 Subject: [PATCH] Fill {{customer}}, {{estimate}} and hooked macros in issue notes A journal patch fills macros when a note is saved or edited, so note templates can use them like issue templates do. Co-Authored-By: Claude Opus 5.5 --- README.md | 4 +-- lib/redmine_qbo.rb | 1 + lib/redmine_qbo/issue_macros.rb | 17 ++++++++++- lib/redmine_qbo/patches/journal_patch.rb | 32 +++++++++++++++++++++ test/unit/issue_macros_test.rb | 36 +++++++++++++++++++++++- 5 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 lib/redmine_qbo/patches/journal_patch.rb diff --git a/README.md b/README.md index 6be01b6..f021263 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ For production deployments, **use a tagged release**. * Automatically associates a **QuickBooks Invoice** with the Issue. -* **Macros** in an issue's subject or description are filled in when it's saved, e.g. an issue template's `Estimate {{estimate}} for {{customer}}`: `{{customer}}` is the customer's name and `{{estimate}}` the estimate number. A macro without a value yet (no estimate linked) stays as typed and fills on a later save. Companion plugins add more through the `:qbo_issue_macros` hook (redmine_qbo_vehicles: `{{vehicle}}`). +* **Macros** in an issue's subject, description or notes are filled in when it's saved, e.g. an issue template's `Estimate {{estimate}} for {{customer}}`: `{{customer}}` is the customer's name and `{{estimate}}` the estimate number. A macro without a value yet (no estimate linked) stays as typed and fills on a later save; in a note, which isn't saved again, it stays as typed unless the note is edited. Companion plugins add more through the `:qbo_issue_macros` hook (redmine_qbo_vehicles: `{{vehicle}}`). --- @@ -280,7 +280,7 @@ Hook|:qbo_billing_field_defaults, { customer:, issues: } | Return `{ field_id => Hook|:qbo_billing_field_options, { customer: } | Return `{ field_id => [{ value:, label: }, ...] }` to suggest in the editor's custom field boxes for a customer (redmine_qbo_vehicles: the customer's vehicles for the VIN box) View|:view_qbo_estimate_form_bottom, { form:, document: } | Add your plugin's script to the estimate/invoice editor. It can use `window.qboEditor` (`field(id)`, `fieldNamed(/name/)`, `setFieldOptions(id, options)`) and listen for `qbo:customer-changed` and `qbo:imported` on the form (redmine_qbo_vehicles fills the VIN and mileage) View|checkboxes with `data-ticked-field="field"`, links with `data-ticked-params` on the customer page | Ticking a box adds its value to every link with `data-ticked-params`, named by that format: `issue[%s]` for the New issue link, `%s` for a plain param (e.g. redmine_qbo_calendar's Add appointment link). One box per field (redmine_qbo's estimates: `estimate_id`, redmine_qbo_vehicles' vehicles: `vehicle_id`) -Hook|:qbo_issue_macros, { issue: } | Return `{ 'name' => text }` to fill `{{name}}` in an issue's subject and description when it's saved (redmine_qbo_vehicles: `{{vehicle}}`). Leave a name out, or give nil, while it has no value; the macro then waits for a later save +Hook|:qbo_issue_macros, { issue: } | Return `{ 'name' => text }` to fill `{{name}}` in an issue's subject, description and notes when they're saved (redmine_qbo_vehicles: `{{vehicle}}`). Leave a name out, or give nil, while it has no value; the macro then waits for a later save Hook|:qbo_billing_issue_carry_params | Return names of request params the Create issues from estimate page keeps through its form (redmine_qbo_calendar: `appointment_id`) Hook|:qbo_mail_settings | Return any of `shop_name`, `from`, `reply_to` and `delivery` (options for ActionMailer's `mail()`, e.g. an SMTP server) for customer emails such as refund summaries (redmine_qbo_reminders: its shop name and email settings). Blank ones fall back to Redmine's settings View Hook|:view_qbo_estimate_contextual, { estimate_id:, customer_id:, custom_fields:, doc_number: } | Adds links to an estimate page's action area (e.g. redmine_qbo_calendar's Add appointment); `custom_fields` is `{ field_id => value }` diff --git a/lib/redmine_qbo.rb b/lib/redmine_qbo.rb index fd385bc..91e4631 100644 --- a/lib/redmine_qbo.rb +++ b/lib/redmine_qbo.rb @@ -12,6 +12,7 @@ module RedmineQbo def self.setup # === Models === Issue.prepend RedmineQbo::Patches::IssuePatch + Journal.prepend RedmineQbo::Patches::JournalPatch User.prepend RedmineQbo::Patches::UserPatch # === Queries === diff --git a/lib/redmine_qbo/issue_macros.rb b/lib/redmine_qbo/issue_macros.rb index 54801d0..45dc732 100644 --- a/lib/redmine_qbo/issue_macros.rb +++ b/lib/redmine_qbo/issue_macros.rb @@ -16,6 +16,9 @@ module RedmineQbo # # A macro without a value yet (no estimate linked) stays as typed, so it fills on a later save. # Names nobody provides are left alone, which keeps Redmine's own wiki macros such as {{toc}} working. + # + # Issue notes are filled too (e.g. from a note template), when the note is saved or edited; see + # Patches::JournalPatch. A note isn't saved again later, so a macro without a value stays as typed there. module IssueMacros PATTERN = /\{\{\s*([a-z][a-z0-9_]*)\s*\}\}/i FIELDS = %i[subject description].freeze @@ -30,11 +33,23 @@ module RedmineQbo return if known.empty? texts.each do |field, text| - filled = text.gsub(PATTERN) { known[Regexp.last_match(1).downcase] || Regexp.last_match(0) } + filled = substitute(text, known) issue.send("#{field}=", filled) unless filled == text end end + # The text with this issue's macros filled in (e.g. a note); the text itself when there's nothing to fill + def fill_text(issue, text) + return text unless text.to_s.match?(PATTERN) + + known = values(issue) + known.empty? ? text : substitute(text, known) + end + + def substitute(text, known) + text.gsub(PATTERN) { known[Regexp.last_match(1).downcase] || Regexp.last_match(0) } + end + # { 'name' => text } for every macro with a value for this issue def values(issue) found = { diff --git a/lib/redmine_qbo/patches/journal_patch.rb b/lib/redmine_qbo/patches/journal_patch.rb new file mode 100644 index 0000000..cd8131b --- /dev/null +++ b/lib/redmine_qbo/patches/journal_patch.rb @@ -0,0 +1,32 @@ +#The MIT License (MIT) +# +#Copyright (c) 2016 - 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 RedmineQbo + module Patches + # Fills {{customer}}, {{estimate}} and hooked macros in an issue note (e.g. from a note template) + # when the note is saved, including when it's edited later + module JournalPatch + extend ActiveSupport::Concern + + prepended do + before_save :fill_qbo_macros + end + + private + + def fill_qbo_macros + return unless journalized.is_a?(Issue) && notes.to_s.match?(RedmineQbo::IssueMacros::PATTERN) + + filled = RedmineQbo::IssueMacros.fill_text(journalized, notes) + self.notes = filled unless filled == notes + end + end + end +end diff --git a/test/unit/issue_macros_test.rb b/test/unit/issue_macros_test.rb index 26d7fce..3fc6fa7 100644 --- a/test/unit/issue_macros_test.rb +++ b/test/unit/issue_macros_test.rb @@ -12,7 +12,7 @@ require File.expand_path('../test_helper', __dir__) # {{customer}}, {{estimate}} and hooked macros in issue subjects and descriptions (RedmineQbo::IssueMacros) class IssueMacrosTest < ActiveSupport::TestCase - fixtures :projects, :users, :issues, :trackers, :issue_statuses, :enumerations + fixtures :projects, :users, :issues, :trackers, :issue_statuses, :enumerations, :journals CUSTOMER_ID = 9311 ESTIMATE_ID = 9411 @@ -84,4 +84,38 @@ class IssueMacrosTest < ActiveSupport::TestCase @issue.valid? assert_equal 'Nothing to fill', @issue.description end + + def test_a_new_note_is_filled_when_the_issue_is_saved + @issue.estimate = @estimate + @issue.customer = @customer + @issue.save! + @issue.init_journal(User.find(1), 'Called {{customer}} about estimate {{estimate}}') + @issue.save! + assert_equal 'Called Jane Customer about estimate 1068', @issue.journals.order(:id).last.notes + end + + def test_a_note_uses_values_set_in_the_same_save + @issue.init_journal(User.find(1), 'Estimate {{estimate}}') + @issue.estimate = @estimate + @issue.save! + assert_equal 'Estimate 1068', @issue.journals.order(:id).last.notes + end + + def test_an_edited_note_is_filled + @issue.update_columns(customer_id: CUSTOMER_ID) + journal = Journal.create!(journalized: @issue, user: User.find(1), notes: 'Plain note') + journal.update!(notes: 'Spoke with {{customer}}') + assert_equal 'Spoke with Jane Customer', journal.reload.notes + end + + def test_a_note_macro_without_a_value_stays_as_typed + journal = Journal.create!(journalized: @issue, user: User.find(1), notes: 'Estimate {{estimate}} and {{toc}}') + assert_equal 'Estimate {{estimate}} and {{toc}}', journal.reload.notes + end + + def test_hooked_macros_fill_notes + MacroHook.value = '2018 Jeep Wrangler' + journal = Journal.create!(journalized: @issue, user: User.find(1), notes: 'Test drove the {{truck}}') + assert_equal 'Test drove the 2018 Jeep Wrangler', journal.reload.notes + end end