mirror of
https://github.com/rickbarrette/redmine_qbo_vehicles.git
synced 2026-11-17 18:40:36 -05:00
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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
This commit is contained in:
4 files changed
+16
-58
No files matched your search
@@ -21,17 +21,13 @@ class VehiclesController < ApplicationController
|
|||||||
params.require(:vehicle).permit(:year, :make, :model, :customer_id, :notes, :vin)
|
params.require(:vehicle).permit(:year, :make, :model, :customer_id, :notes, :vin)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Vehicles for one customer (also as JSON for the issue form), or search results
|
# Vehicles for one customer, or search results
|
||||||
def index
|
def index
|
||||||
if params[:customer_id].present?
|
if params[:customer_id].present?
|
||||||
@customer = Customer.find_by(id: params[:customer_id])
|
@customer = Customer.find_by(id: params[:customer_id])
|
||||||
return render_404 unless @customer
|
return render_404 unless @customer
|
||||||
|
|
||||||
vehicles = @customer.vehicles.order(:year, :make, :model)
|
@vehicles = @customer.vehicles.order(:year, :make, :model).paginate(page: params[:page])
|
||||||
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
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
|
||||||
@@ -17,12 +17,18 @@ module Vehicles
|
|||||||
|
|
||||||
def view_issues_form_details_bottom(context={})
|
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
|
# The selected customer's vehicles. Changing the customer reloads the issue form (redmine_qbo's
|
||||||
# always included: after a vehicle is sold it belongs to another customer, and leaving it out
|
# updateIssueFrom), which re-renders this list for the new customer.
|
||||||
# would blank it on the next save, dropping the issue from that vehicle's history.
|
#
|
||||||
|
# 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]
|
issue = context[:issue]
|
||||||
vehicles = (issue.customer&.vehicles.to_a || [])
|
vehicles = issue.customer&.vehicles.to_a || []
|
||||||
vehicles |= [issue.vehicle] if issue.vehicle
|
if issue.vehicle && issue.persisted? && !issue.customer_id_changed?
|
||||||
|
vehicles |= [issue.vehicle]
|
||||||
|
end
|
||||||
|
|
||||||
context[:controller].send(:render_to_string, {
|
context[:controller].send(:render_to_string, {
|
||||||
partial: 'issues/form_hook_vehicles',
|
partial: 'issues/form_hook_vehicles',
|
||||||
@@ -30,9 +36,8 @@ module Vehicles
|
|||||||
vehicle: context[:form].select(
|
vehicle: context[:form].select(
|
||||||
:vehicle_id,
|
:vehicle_id,
|
||||||
vehicles.map { |v| [v.name, v.id] },
|
vehicles.map { |v| [v.name, v.id] },
|
||||||
{ selected: context[:issue].vehicle&.id, include_blank: true },
|
selected: issue.vehicle&.id,
|
||||||
# vehicles.js swaps in the customer id and reloads this list when the customer changes
|
include_blank: true
|
||||||
{ data: { vehicles_url: customer_vehicles_path('__CUSTOMER_ID__') } }
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -15,8 +15,7 @@ module Vehicles
|
|||||||
def view_layouts_base_html_head(context = {})
|
def view_layouts_base_html_head(context = {})
|
||||||
safe_join([
|
safe_join([
|
||||||
stylesheet_link_tag('style', plugin: :redmine_qbo_vehicles, media: :all),
|
stylesheet_link_tag('style', plugin: :redmine_qbo_vehicles, media: :all),
|
||||||
javascript_include_tag('copy', plugin: :redmine_qbo_vehicles),
|
javascript_include_tag('copy', plugin: :redmine_qbo_vehicles)
|
||||||
javascript_include_tag('vehicles', plugin: :redmine_qbo_vehicles)
|
|
||||||
])
|
])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user