mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-11-22 11:00:36 -05:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
6006bd98f0
commit
0aa95918f6
5 files changed
+86
-4
No files matched your search
@@ -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 ===
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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
|
||||
Reference in new issue
Block a user