From d4684b8ae9bd9ef7cf4928a10ecaebecb9ee1d76 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 29 Oct 2026 19:09:47 -0400 Subject: [PATCH] Fix vehicle dropdown after a customer change; drop vehicles.js The issue form already reloads on the server when the customer changes (redmine_qbo's updateIssueFrom), re-rendering the vehicle list for the new customer. 0a3c734 broke that by always keeping the issue's current vehicle in the list, so after changing the customer the previous customer's vehicle stayed listed and selected. - The current vehicle is only kept for an existing issue whose customer hasn't been changed in the form (keeps sold vehicles' history); otherwise only the selected customer's vehicles are offered - Removes vehicles.js, which duplicated the form reload, its include and the JSON vehicle listing it used Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM --- app/controllers/vehicles_controller.rb | 8 +--- assets/javascripts/vehicles.js | 42 ------------------- .../hooks/issues_form_hook_listener.rb | 21 ++++++---- lib/vehicles/hooks/view_hook_listener.rb | 3 +- 4 files changed, 16 insertions(+), 58 deletions(-) delete mode 100644 assets/javascripts/vehicles.js diff --git a/app/controllers/vehicles_controller.rb b/app/controllers/vehicles_controller.rb index 3486265..84f22d6 100644 --- a/app/controllers/vehicles_controller.rb +++ b/app/controllers/vehicles_controller.rb @@ -21,17 +21,13 @@ class VehiclesController < ApplicationController params.require(:vehicle).permit(:year, :make, :model, :customer_id, :notes, :vin) end - # Vehicles for one customer (also as JSON for the issue form), or search results + # Vehicles for one customer, or search results def index if params[:customer_id].present? @customer = Customer.find_by(id: params[:customer_id]) return render_404 unless @customer - vehicles = @customer.vehicles.order(:year, :make, :model) - respond_to do |format| - format.html { @vehicles = vehicles.paginate(page: params[:page]) } - format.json { render json: vehicles.map { |v| { id: v.id, name: v.name } } } - end + @vehicles = @customer.vehicles.order(:year, :make, :model).paginate(page: params[:page]) return end diff --git a/assets/javascripts/vehicles.js b/assets/javascripts/vehicles.js deleted file mode 100644 index 70af502..0000000 --- a/assets/javascripts/vehicles.js +++ /dev/null @@ -1,42 +0,0 @@ -//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. - -// Refreshes the issue form's vehicle list when the customer changes, so only that customer's vehicles -// can be picked. redmine_qbo's customer autocomplete fires "change" on #customer_id through jQuery, -// so this listens with jQuery too. -(function ($) { - if (!$) return; - - $(document).on('change', '#customer_id', function () { - var select = document.getElementById('issue_vehicle_id'); - if (!select || !select.dataset.vehiclesUrl) return; - - var customerId = this.value; - var previous = select.value; - - function fill(vehicles) { - select.innerHTML = ''; - select.appendChild(new Option('', '')); - vehicles.forEach(function (v) { select.appendChild(new Option(v.name, v.id)); }); - if (vehicles.some(function (v) { return String(v.id) === previous; })) select.value = previous; - } - - if (!customerId) { fill([]); return; } - - // No ".json" in the URL: Redmine treats those as API requests and ignores the browser session - fetch(select.dataset.vehiclesUrl.replace('__CUSTOMER_ID__', encodeURIComponent(customerId)), { - headers: { 'Accept': 'application/json' }, - credentials: 'same-origin' - }) - .then(function (res) { return res.ok ? res.json() : null; }) - .then(function (vehicles) { if (vehicles) fill(vehicles); }) // on failure, leave the list alone - .catch(function (err) { console.warn('Could not load vehicles', err); }); - }); -})(window.jQuery); diff --git a/lib/vehicles/hooks/issues_form_hook_listener.rb b/lib/vehicles/hooks/issues_form_hook_listener.rb index 9913a43..4a2c9d0 100644 --- a/lib/vehicles/hooks/issues_form_hook_listener.rb +++ b/lib/vehicles/hooks/issues_form_hook_listener.rb @@ -17,12 +17,18 @@ module Vehicles def view_issues_form_details_bottom(context={}) - # Load the customer's vehicles for selection in the issue form. The vehicle already on the issue is - # always included: after a vehicle is sold it belongs to another customer, and leaving it out - # would blank it on the next save, dropping the issue from that vehicle's history. + # The selected customer's vehicles. Changing the customer reloads the issue form (redmine_qbo's + # updateIssueFrom), which re-renders this list for the new customer. + # + # An existing issue keeps its vehicle in the list while its customer is unchanged: if the + # vehicle was sold since, it belongs to another customer now, and leaving it out would blank it + # on the next save, dropping the issue from the vehicle's history. Once the customer is changed + # in the form, only the new customer's vehicles are offered. issue = context[:issue] - vehicles = (issue.customer&.vehicles.to_a || []) - vehicles |= [issue.vehicle] if issue.vehicle + vehicles = issue.customer&.vehicles.to_a || [] + if issue.vehicle && issue.persisted? && !issue.customer_id_changed? + vehicles |= [issue.vehicle] + end context[:controller].send(:render_to_string, { partial: 'issues/form_hook_vehicles', @@ -30,9 +36,8 @@ module Vehicles vehicle: context[:form].select( :vehicle_id, vehicles.map { |v| [v.name, v.id] }, - { selected: context[:issue].vehicle&.id, include_blank: true }, - # vehicles.js swaps in the customer id and reloads this list when the customer changes - { data: { vehicles_url: customer_vehicles_path('__CUSTOMER_ID__') } } + selected: issue.vehicle&.id, + include_blank: true ) } }) diff --git a/lib/vehicles/hooks/view_hook_listener.rb b/lib/vehicles/hooks/view_hook_listener.rb index d097b4b..c02eefc 100644 --- a/lib/vehicles/hooks/view_hook_listener.rb +++ b/lib/vehicles/hooks/view_hook_listener.rb @@ -15,8 +15,7 @@ module Vehicles def view_layouts_base_html_head(context = {}) safe_join([ stylesheet_link_tag('style', plugin: :redmine_qbo_vehicles, media: :all), - javascript_include_tag('copy', plugin: :redmine_qbo_vehicles), - javascript_include_tag('vehicles', plugin: :redmine_qbo_vehicles) + javascript_include_tag('copy', plugin: :redmine_qbo_vehicles) ]) end