From d6138843f8e5cb7963a76ba3814385912b01e8fe Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Sat, 21 Feb 2026 09:22:04 -0500 Subject: [PATCH 1/4] Started to extend redmine's search --- app/models/vehicle.rb | 64 +++++++++++++++++++++++++++++++++ db/migrate/003_add_timestamp.rb | 17 +++++++++ init.rb | 4 +++ 3 files changed, 85 insertions(+) create mode 100644 db/migrate/003_add_timestamp.rb diff --git a/app/models/vehicle.rb b/app/models/vehicle.rb index de57649..1d2da6c 100644 --- a/app/models/vehicle.rb +++ b/app/models/vehicle.rb @@ -15,7 +15,71 @@ class Vehicle < ActiveRecord::Base validates_presence_of :customer validates :vin, uniqueness: true before_save :decode_vin + + def self.searchable_columns + ['vin', 'make', 'model', 'year'] + end + + def self.searchable_options + { + columns: searchable_columns, + scope: self.all + } + end + + def self.search_result_ranks_and_ids(tokens, user, project = nil, options = {}) + return {} if tokens.blank? + + scope = self.all + + tokens.each do |token| + q = "%#{sanitize_sql_like(token)}%" + scope = where("vin LIKE ? OR make LIKE ? OR model LIKE ? OR year LIKE ?", "%#{q}%", "%#{q}%", "%#{q}%", "%#{q}%") + end + + ids = scope.distinct.limit(options[:limit] || 100).pluck(:id) + + # Assign simple uniform ranking + ids.each_with_object({}) { |id, h| h[id] = id } + end + + def self.search_results_from_ids(ids, *args) + return [] if ids.blank? + + Rails.logger.warn "IDS: #{ids.inspect}" + + ids = ids.map(&:to_i) + records = where(id: ids).index_by(&:id) + ids.map { |id| records[id] }.compact + end + # ---- Redmine Search Event Interface ---- + def event_type + 'vehicle' + end + + def event_title + to_s + end + + def event_description + "#{vin} #{year} #{make} #{model}" + end + + def event_url + Rails.application.routes.url_helpers.vehicle_path(self) + end + + def event_datetime + updated_at || created_at + end + + def project + # Vehicles are not project scoped in your schema. + # Return nil so global search works cleanly. + nil + end + # returns a human readable string def to_s if year.nil? or make.nil? or model.nil? diff --git a/db/migrate/003_add_timestamp.rb b/db/migrate/003_add_timestamp.rb new file mode 100644 index 0000000..552e6fa --- /dev/null +++ b/db/migrate/003_add_timestamp.rb @@ -0,0 +1,17 @@ +#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. + +class AddTimestamp < ActiveRecord::Migration[7.0] + + def change + add_timestamps(:vehicles, null: true) + end + +end diff --git a/init.rb b/init.rb index 8764578..369f6e6 100644 --- a/init.rb +++ b/init.rb @@ -35,6 +35,10 @@ Redmine::Plugin.register :redmine_qbo_vehicles do # Register top menu items menu :top_menu, :vehicles, { controller: :vehicles, action: :index }, caption: :field_vehicles, if: Proc.new { User.current.logged? } + Redmine::Search.map do |search| + search.register :vehicles + end + end # Dynamically load all Hooks & Patches recursively From 2bd13a819b8da1a7ca6b28d41058a3617cc37a14 Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Sat, 21 Feb 2026 14:00:34 -0500 Subject: [PATCH 2/4] Removed unneeded BR --- app/views/vehicles/_list.html.erb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/views/vehicles/_list.html.erb b/app/views/vehicles/_list.html.erb index cd8fd25..d30144e 100644 --- a/app/views/vehicles/_list.html.erb +++ b/app/views/vehicles/_list.html.erb @@ -19,7 +19,6 @@
<%= vehicle.vin.scan(/.{1,9}/)[0] if vehicle.vin %><%=vehicle.vin.scan(/.{1,9}/)[1] if vehicle.vin%>
<% if show_customer %> -
<%= vehicle.customer %> <% end %>
From b2c1bebea344f06a9bc730f9e8cc1eca5295dbdf Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Sat, 21 Feb 2026 14:07:03 -0500 Subject: [PATCH 3/4] show_checkbox: true --- app/views/customers/_show_hook.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/customers/_show_hook.html.erb b/app/views/customers/_show_hook.html.erb index 46457eb..57a55eb 100644 --- a/app/views/customers/_show_hook.html.erb +++ b/app/views/customers/_show_hook.html.erb @@ -1,5 +1,5 @@

<%=t(:field_vehicles)%>:

-<%= render partial: 'vehicles/list', locals: { vehicles: customer.vehicles.paginate(page: params[:page]), show_customer: false, show_checkbox: false } %> +<%= render partial: 'vehicles/list', locals: { vehicles: customer.vehicles.paginate(page: params[:page]), show_customer: false, show_checkbox: true } %>
<%= button_to t(:button_new_vehicle), new_customer_vehicle_path(customer), method: :get %>
\ No newline at end of file From 4b8b3c349586e962c80eb0ae21cc76b1318b6861 Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Sat, 21 Feb 2026 19:06:23 -0500 Subject: [PATCH 4/4] Update search form ID for consistency --- app/views/vehicles/_search.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/vehicles/_search.html.erb b/app/views/vehicles/_search.html.erb index 960b353..1688c0b 100644 --- a/app/views/vehicles/_search.html.erb +++ b/app/views/vehicles/_search.html.erb @@ -1,4 +1,4 @@ -<%= form_tag(vehicles_path, method: "get", id: "search-form") do %> +<%= 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 %>