mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-11-21 10:50:37 -05:00
Fill {{customer}}, {{estimate}} and hooked macros in issues
Issue templates can use {{name}} macros in the subject and description;
they're filled when the issue is saved, once they have a value. Other
plugins add macros through the new :qbo_issue_macros hook.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8ce702cb44
commit
eb8bfc724b
4 files changed
+198
-54
No files matched your search
@@ -0,0 +1,52 @@
|
||||
#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 RedmineQbo
|
||||
# Fills {{name}} macros in an issue's subject and description (e.g. from an issue template:
|
||||
# "Estimate {{estimate}} for {{customer}}") when the issue is saved. Built in: {{customer}} and
|
||||
# {{estimate}}. Other plugins add their own through the :qbo_issue_macros hook (e.g.
|
||||
# redmine_qbo_vehicles' {{vehicle}}).
|
||||
#
|
||||
# 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.
|
||||
module IssueMacros
|
||||
PATTERN = /\{\{\s*([a-z][a-z0-9_]*)\s*\}\}/i
|
||||
FIELDS = %i[subject description].freeze
|
||||
|
||||
module_function
|
||||
|
||||
def fill(issue)
|
||||
texts = FIELDS.to_h { |field| [field, issue.send(field).to_s] }
|
||||
return unless texts.values.any? { |text| text.match?(PATTERN) }
|
||||
|
||||
known = values(issue)
|
||||
return if known.empty?
|
||||
|
||||
texts.each do |field, text|
|
||||
filled = text.gsub(PATTERN) { known[Regexp.last_match(1).downcase] || Regexp.last_match(0) }
|
||||
issue.send("#{field}=", filled) unless filled == text
|
||||
end
|
||||
end
|
||||
|
||||
# { 'name' => text } for every macro with a value for this issue
|
||||
def values(issue)
|
||||
found = {
|
||||
'customer' => issue.customer&.name,
|
||||
'estimate' => issue.estimate&.to_s
|
||||
}
|
||||
Redmine::Hook.call_hook(:qbo_issue_macros, { issue: issue }).each do |result|
|
||||
next unless result.is_a?(Hash)
|
||||
|
||||
result.each { |name, value| found[name.to_s.downcase] = value }
|
||||
end
|
||||
found.transform_values { |value| value.to_s.strip.presence }.compact
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,55 +1,57 @@
|
||||
#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
|
||||
module IssuePatch
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
prepended do
|
||||
belongs_to :customer
|
||||
belongs_to :estimate
|
||||
has_and_belongs_to_many :invoices
|
||||
|
||||
before_save :titlize_subject
|
||||
after_commit :enqueue_billing, on: :update
|
||||
end
|
||||
|
||||
# Enqueue a background job to bill the time spent on this issue to QuickBooks if the issue is closed and assigned to an employee
|
||||
def enqueue_billing
|
||||
log "Checking if issue needs billing for ##{id}"
|
||||
return unless closed? && customer.present? && assigned_to&.employee_id.present?
|
||||
|
||||
log "Enqueuing billing for issue ##{id}"
|
||||
BillIssueTimeJob.perform_later(id)
|
||||
end
|
||||
|
||||
# Capitalize each word of the subject for consistent billing descriptions, without lowercasing
|
||||
# the rest of the word (keeps LED, JL, 4x4 as typed). Only runs when the subject changes.
|
||||
def titlize_subject
|
||||
return unless subject.present? && will_save_change_to_subject?
|
||||
|
||||
log "Titlizing subject for issue ##{id}"
|
||||
self.subject = RedmineQbo::TitleCase.call(subject)
|
||||
end
|
||||
|
||||
# Generate a shareable token linking this issue to the customer for QuickBooks
|
||||
def share_token
|
||||
CustomerToken.get_token(self)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def log(msg)
|
||||
Rails.logger.info "[IssuePatch] #{msg}"
|
||||
end
|
||||
end
|
||||
end
|
||||
#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
|
||||
module IssuePatch
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
prepended do
|
||||
belongs_to :customer
|
||||
belongs_to :estimate
|
||||
has_and_belongs_to_many :invoices
|
||||
|
||||
# {{customer}}, {{estimate}} and other plugins' macros (e.g. from an issue template)
|
||||
before_validation { RedmineQbo::IssueMacros.fill(self) }
|
||||
before_save :titlize_subject
|
||||
after_commit :enqueue_billing, on: :update
|
||||
end
|
||||
|
||||
# Enqueue a background job to bill the time spent on this issue to QuickBooks if the issue is closed and assigned to an employee
|
||||
def enqueue_billing
|
||||
log "Checking if issue needs billing for ##{id}"
|
||||
return unless closed? && customer.present? && assigned_to&.employee_id.present?
|
||||
|
||||
log "Enqueuing billing for issue ##{id}"
|
||||
BillIssueTimeJob.perform_later(id)
|
||||
end
|
||||
|
||||
# Capitalize each word of the subject for consistent billing descriptions, without lowercasing
|
||||
# the rest of the word (keeps LED, JL, 4x4 as typed). Only runs when the subject changes.
|
||||
def titlize_subject
|
||||
return unless subject.present? && will_save_change_to_subject?
|
||||
|
||||
log "Titlizing subject for issue ##{id}"
|
||||
self.subject = RedmineQbo::TitleCase.call(subject)
|
||||
end
|
||||
|
||||
# Generate a shareable token linking this issue to the customer for QuickBooks
|
||||
def share_token
|
||||
CustomerToken.get_token(self)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def log(msg)
|
||||
Rails.logger.info "[IssuePatch] #{msg}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user