From 2074ec65b921ffc5fd3ecbf81da2b300fa3376a3 Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Mon, 2 Nov 2026 18:23:07 -0500 Subject: [PATCH] Fill the vehicle into issue subjects () An issue subject containing the placeholder (e.g. an issue template's "Frame Evaluation // ") gets the issue's vehicle when it's saved with one: on creation, or the first save after a vehicle is added (journaled like any subject change). The placeholder is a plugin setting, "Vehicle in subjects": by default (also for settings saved before it existed), blank turns it off. Co-Authored-By: Claude Opus 5.5 --- README.md | 1 + app/models/vehicle.rb | 12 ++++++++++++ app/views/settings/_vehicles_settings.html.erb | 5 +++++ config/locales/en.yml | 2 ++ init.rb | 3 ++- lib/vehicles/patches/issue_patch.rb | 10 ++++++++++ 6 files changed, 32 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0dab9d0..413606a 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ The goal of this project is to enable vehicle tracking for customer vehicles wit * **Estimates (redmine_qbo 2026.11.2+):** the VIN box suggests the customer's vehicles, an invoice made from an estimate without a VIN gets its work orders' vehicle, and the Create issues from estimate page has a **Vehicle** for all the issues: preselected with the appointment's vehicle, or else the customer's vehicle whose VIN is on the estimate; when none matches it says so, so the team can pick one before creating the issues. * **Vehicles page:** every vehicle in a Redmine list (vehicle, VIN, customer, last serviced date and mileage, last update), sortable by column and paged with Redmine's pagination and per-page choices. Search narrows it to the vehicles matching every word (e.g. "2018 jeep wrangler"; a single match opens it). A customer's vehicles (`/customers/:id/vehicles`) are the same list for that customer, with New Vehicle. * **Customer page:** ticking a vehicle adds it to the New issue link. +* **Vehicle in subjects:** an issue subject containing `` (e.g. from an issue template: `Frame Evaluation // `) gets the vehicle filled in when the issue is saved with one. The placeholder text is a plugin setting; blank turns it off. * **Appointments (redmine_qbo_calendar):** an appointment added from an estimate gets the customer's vehicle with the estimate's VIN. On an appointment without a vehicle, **Add Vehicle** creates one for the customer, sets it on the appointment and returns to it (for users who may edit appointments). * **For other plugins:** creating a vehicle calls the `:controller_vehicles_new_after_save` hook (context: `params`, `vehicle`), and the new vehicle form returns to `back_url` when given, so a plugin can link to `new_vehicle_path(customer_id:, back_url:, ...)` and act on the vehicle it gets back. diff --git a/app/models/vehicle.rb b/app/models/vehicle.rb index 96e2ff9..332c851 100644 --- a/app/models/vehicle.rb +++ b/app/models/vehicle.rb @@ -35,6 +35,18 @@ class Vehicle < ApplicationRecord issues.includes(:invoices).flat_map(&:invoices).uniq.compact end + # Text in an issue's subject replaced by its vehicle (e.g. a template's "Frame Evaluation // "), or nil + # when turned off in the plugin settings. Settings saved before this option existed get the default. + DEFAULT_SUBJECT_PLACEHOLDER = '' + + def self.subject_placeholder + settings = Setting.plugin_redmine_qbo_vehicles || {} + key = ['subject_placeholder', :subject_placeholder].detect { |k| settings.key?(k) } + return DEFAULT_SUBJECT_PLACEHOLDER unless key + + settings[key].to_s.strip.presence + end + # The issue custom field holding mileage: the one chosen in the plugin settings, else one named "Mileage" def self.mileage_custom_field settings = Setting.plugin_redmine_qbo_vehicles || {} diff --git a/app/views/settings/_vehicles_settings.html.erb b/app/views/settings/_vehicles_settings.html.erb index 5883e17..adafe8d 100644 --- a/app/views/settings/_vehicles_settings.html.erb +++ b/app/views/settings/_vehicles_settings.html.erb @@ -6,4 +6,9 @@ include_blank: true, id: 'settings_mileage_custom_field_id' %> <%= l(:text_mileage_custom_field_info) %>

+

+ + <%= text_field_tag 'settings[subject_placeholder]', Vehicle.subject_placeholder, id: 'settings_subject_placeholder', size: 20 %> + <%= l(:text_subject_vehicle_placeholder_info) %> +

diff --git a/config/locales/en.yml b/config/locales/en.yml index 90451c9..3b0c9bd 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -66,6 +66,8 @@ en: label_mileage_custom_field: "Mileage field" notice_vin_decode_timed_out: "VIN decode timed out. Please refresh or try again." text_mileage_custom_field_info: "Issue custom field used for \"Last serviced\" mileage. Leave blank to use a field named Mileage." + label_subject_vehicle_placeholder: "Vehicle in subjects" + text_subject_vehicle_placeholder_info: "Text in an issue's subject (e.g. from a template: Frame Evaluation // ) replaced by its vehicle when one is assigned. Leave blank to turn off." text_vin_already_on_file: "A vehicle with this VIN is already on file:" text_click_to_copy: "Click to copy" text_copied: "Copied!" diff --git a/init.rb b/init.rb index b068252..d109b77 100644 --- a/init.rb +++ b/init.rb @@ -27,7 +27,8 @@ Redmine::Plugin.register :redmine_qbo_vehicles do end # Which issue custom field holds the mileage shown as "Last serviced" (blank: a field named "Mileage") - settings default: { 'mileage_custom_field_id' => '' }, partial: 'settings/vehicles_settings' + # Text in issue subjects replaced by the issue's vehicle (blank: off) + settings default: { 'mileage_custom_field_id' => '', 'subject_placeholder' => '' }, partial: 'settings/vehicles_settings' # Add safe attributes for core models Issue.safe_attributes :vehicle_id diff --git a/lib/vehicles/patches/issue_patch.rb b/lib/vehicles/patches/issue_patch.rb index da90dd8..343f49e 100644 --- a/lib/vehicles/patches/issue_patch.rb +++ b/lib/vehicles/patches/issue_patch.rb @@ -22,10 +22,20 @@ module Vehicles # The vehicle must belong to the issue's customer. Only checked when either changes, so older # issues with a mismatch don't block unrelated edits. validate :vehicle_matches_customer, if: -> { will_save_change_to_vehicle_id? || will_save_change_to_customer_id? } + + # A subject with the placeholder (e.g. from a template) gets the vehicle once one is assigned + before_validation :fill_subject_vehicle end private + def fill_subject_vehicle + placeholder = Vehicle.subject_placeholder + return if placeholder.nil? || vehicle.nil? || !subject.to_s.include?(placeholder) + + self.subject = subject.gsub(placeholder, vehicle.to_s) + end + def vehicle_matches_customer return if vehicle_id.blank?