mirror of
https://github.com/rickbarrette/redmine_qbo_vehicles.git
synced 2026-11-17 18:40:36 -05:00
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:
1 parent
4db8b812dc
commit
063e5f12cf
7 files changed
+110
-19
No files matched your search
@@ -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
@@ -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
|
||||
|
||||
@@ -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 %>
|
||||
@@ -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 %>
|
||||
@@ -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 %> » <%= link_to @customer.to_s, customer_path(@customer) %> »
|
||||
<% 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] %>· <%= 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>
|
||||
Reference in new issue
Block a user