Vehicles page lists every vehicle, sortable, searched like the customers page

- The Vehicles page shows all vehicles without a search: a Redmine list with vehicle, VIN (click to
  copy), customer, last serviced date and mileage, and last update, sortable by column and paged by
  Redmine with its per-page choices
- Search narrows it to the vehicles matching every word (a single match opens it); sorting still applies
  and Clear goes back to everyone
- A customer's vehicles (/customers/:id/vehicles) are the same list for that customer, under a
  Customers » customer » Vehicles trail, with New Vehicle; its search stays within the customer
- Last serviced for a page of vehicles in one query (Vehicle.last_service_infos); ties on the issue's
  date go to the newest issue, here and on the vehicle page
- The customer page's vehicle list says "No data to display" when empty, not the search wording

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-01 17:56:12 -05:00
1 parent 4db8b812dc
commit 063e5f12cf
7 files changed
+110 -19

No files matched your search

+1
View File
@@ -22,6 +22,7 @@ The goal of this project is to enable vehicle tracking for customer vehicles wit
* **QuickBooks VIN and Mileage fields (redmine_qbo 2026.11.2+):** redmine_qbo shows every QuickBooks text custom field on its estimate/invoice editor; this plugin fills the one named like **VIN** and, if there is one, the one named like **Mileage** or **Odometer**. VINs are shown in capitals.
* **Invoices (redmine_qbo 2026.11.2+):** importing a work order's time or estimate fills the invoice's VIN box with its vehicle's VIN and the mileage box with the highest value of the work orders' **Mileage** issue custom field (Redmine's own custom field named "Mileage"), and an invoice whose work orders are for more than one vehicle isn't saved (one vehicle per invoice).
* **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.
* **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.
+19 -8
View File
@@ -10,6 +10,9 @@
class VehiclesController < ApplicationController
helper :sort
include SortHelper
# Checks the vehicle permissions declared in init.rb (also requires login)
before_action :authorize_global
before_action :find_vehicle, only: [:show, :status, :edit, :update, :destroy]
@@ -21,20 +24,28 @@ class VehiclesController < ApplicationController
params.require(:vehicle).permit(:year, :make, :model, :customer_id, :notes, :vin)
end
# Vehicles for one customer, or search results
# Every vehicle (or one customer's), sortable and paged like Redmine's lists; a search narrows it to the
# vehicles matching every word (a single match opens that vehicle)
def index
if params[:customer_id].present?
@customer = Customer.find_by(id: params[:customer_id])
return render_404 unless @customer
paginate_vehicles(@customer.vehicles.order(:year, :make, :model, :id))
return
end
if params[:search].present?
paginate_vehicles(Vehicle.search_all(params[:search]).order(:id))
redirect_to @vehicles.first if @vehicle_count == 1
end
sort_init 'vehicle', 'asc'
sort_update 'vehicle' => %w[year make model].map { |c| "#{Vehicle.table_name}.#{c}" },
'vin' => "#{Vehicle.table_name}.vin",
'customer' => "#{Customer.table_name}.name",
'updated' => "#{Vehicle.table_name}.updated_at"
@search = params[:search].to_s.strip
scope = @customer ? @customer.vehicles : Vehicle.all
# Searched before joining customers, whose name column would make Vehicle.search's ambiguous
scope = scope.search_all(@search) if @search.present?
paginate_vehicles(scope.left_joins(:customer).preload(:customer).reorder(sort_clause).order(:id))
return redirect_to(@vehicles.first) if @search.present? && @vehicle_count == 1
@last_services = Vehicle.last_service_infos(@vehicles.map(&:id))
end
# Form for a new vehicle, pre-filled with the customer when coming from a customer or appointment
+16 -2
View File
@@ -51,7 +51,7 @@ class Vehicle < ApplicationRecord
.joins(:custom_values)
.where(custom_values: { custom_field_id: field.id })
.where.not(custom_values: { value: ['', nil] })
.order(created_on: :desc)
.order(created_on: :desc, id: :desc)
.first
end
@@ -65,13 +65,27 @@ class Vehicle < ApplicationRecord
.joins(:custom_values)
.where(custom_values: { custom_field_id: field.id })
.where.not(custom_values: { value: ['', nil] })
.order(created_on: :desc)
.order(created_on: :desc, id: :desc)
.pick(:created_on, 'custom_values.value')
end
@last_service_info = row && { date: row[0]&.to_date, mileage: row[1].presence }
end
# last_service_info for many vehicles in one query (a list's Last serviced column):
# { vehicle id => { date:, mileage: } }, leaving out vehicles without a mileage recorded
def self.last_service_infos(ids)
field = mileage_custom_field
return {} unless field && ids.any?
Issue.joins(:custom_values)
.where(vehicle_id: ids, custom_values: { custom_field_id: field.id })
.where.not(custom_values: { value: ['', nil] })
.order(created_on: :desc, id: :desc)
.pluck(:vehicle_id, :created_on, 'custom_values.value')
.each_with_object({}) { |(id, on, value), infos| infos[id] ||= { date: on&.to_date, mileage: value.presence } }
end
def last_service_date
last_service_info&.dig(:date)
end
+1 -1
View File
@@ -48,5 +48,5 @@
<%=t(:label_matching)%> <%= local_assigns[:count] || vehicles.size %> <%=t(:field_vehicles) %>
<% else %>
<p><%=t(:label_no_vehicles)%> <%= params[:search] %>.</p>
<p class="nodata"><%= l(:label_no_data) %></p>
<% end %>
-4
View File
@@ -1,4 +0,0 @@
<%= form_tag(vehicles_path, method: "get", id: "vehicles-search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: t(:label_search_vehicles), autocomplete: "off" %>
<%= submit_tag t(:label_search) %>
<% end %>
+71 -3
View File
@@ -1,4 +1,72 @@
<h2><%=t(:label_customer_vehicles)%> <span style="float:right"> <%= render partial: 'vehicles/search' %> </span> </h2>
<br/>
<% html_title(*[@customer&.to_s, l(:field_vehicles)].compact) %>
<%= render partial: 'vehicles/list', locals: {vehicles: @vehicles, pages: @vehicle_pages, count: @vehicle_count, show_customer: true, show_checkbox: false} %>
<div class="contextual">
<% if @customer && User.current.allowed_to?(:add_vehicles, nil, global: true) %>
<%= link_to sprite_icon('add', l(:button_new_vehicle)), new_customer_vehicle_path(@customer), class: 'icon icon-add' %>
<% end %>
</div>
<h2>
<% if @customer %>
<%= link_to l(:label_customers), customers_path %> &#187; <%= link_to @customer.to_s, customer_path(@customer) %> &#187;
<% end %>
<%= l(:field_vehicles) %>
</h2>
<%= form_tag(@customer ? customer_vehicles_path(@customer) : vehicles_path, method: :get, id: 'vehicles-search-form') do %>
<p class="vehicles-search">
<%= text_field_tag :search, @search, size: 40, placeholder: t(:label_search_vehicles), autocomplete: 'off' %>
<%= submit_tag l(:label_search), name: nil %>
<% if @search.present? %>
<%= link_to sprite_icon('reload', l(:button_clear)), @customer ? customer_vehicles_path(@customer) : vehicles_path,
class: 'icon icon-reload' %>
<% end %>
</p>
<% end %>
<% if @vehicles.empty? %>
<p class="nodata"><%= @search.present? ? l(:label_no_vehicles, search: @search) : l(:label_no_data) %></p>
<% else %>
<div class="autoscroll">
<table class="list vehicles odd-even">
<thead>
<tr>
<%= sort_header_tag('vehicle', caption: l(:field_vehicle)) %>
<%= sort_header_tag('vin', caption: l(:field_vin)) %>
<% unless @customer %>
<%= sort_header_tag('customer', caption: l(:field_customer)) %>
<% end %>
<th><%= l(:label_last_serviced) %></th>
<%= sort_header_tag('updated', caption: l(:field_updated_on), default_order: 'desc') %>
</tr>
</thead>
<tbody>
<% @vehicles.each do |vehicle| %>
<% service = @last_services[vehicle.id] %>
<tr class="<%= cycle('odd', 'even') %>">
<td class="name"><%= link_to vehicle.to_s, vehicle_path(vehicle) %></td>
<td class="vin"><%= render partial: 'vehicles/vin', locals: { vin: vehicle.vin } %></td>
<% unless @customer %>
<td class="customer"><%= link_to vehicle.customer.to_s, customer_path(vehicle.customer) if vehicle.customer %></td>
<% end %>
<td class="last-service">
<% if service %>
<%= format_date(service[:date]) %>
<% if service[:mileage] %>&middot; <%= number_with_delimiter(service[:mileage]) %> <%= l(:label_miles) %><% end %>
<% end %>
</td>
<td class="updated_on"><%= format_time(vehicle.updated_at) if vehicle.updated_at %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<span class="pagination"><%= pagination_links_full @vehicle_pages, @vehicle_count %></span>
<% end %>
<style>
table.vehicles td.name, table.vehicles td.customer { text-align: left; }
table.vehicles td.vin, table.vehicles td.last-service, table.vehicles td.updated_on { white-space: nowrap; }
table.vehicles td.vin .vehicle-vin { display: inline-block; }
.vehicles-search input { vertical-align: middle; }
</style>
+2 -1
View File
@@ -30,7 +30,8 @@ en:
label_miles: "miles"
label_model: "Model"
label_new_vehicle: "New Customer Vehicle"
label_no_vehicles: "There are no vehicles containing the term(s)"
label_no_vehicles: "No vehicles match \"%{search}\"."
label_last_serviced: "Last serviced"
label_search_vehicles: "Search Vehicles"
label_unknown: "Unknown"
label_vehicle: "Vehicle"