15 Commits

11 changed files with 135 additions and 65 deletions

View File

@@ -9,52 +9,26 @@
#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 Vehicle < ActiveRecord::Base
include Redmine::Acts::Searchable
include Redmine::Acts::Event
belongs_to :customer
has_many :issues
validates_presence_of :customer
validates :vin, uniqueness: true
before_save :decode_vin
# returns a human readable string
def to_s
if year.nil? or make.nil? or model.nil?
return "#{vin}"
else
split_vin = vin.scan(/.{1,9}/)
return "#{year} #{make} #{model} - #{split_vin[1]}"
end
end
# returns the raw JSON details from NHTSA
def details
get_details if @details.nil?
return @details
end
# Force Upper Case for make numbers
def make=(val)
# The to_s is in case you get nil/non-string
write_attribute(:make, val.to_s.titleize)
end
# Force Upper Case for model numbers
def model=(val)
# The to_s is in case you get nil/non-string
write_attribute(:model, val.to_s.titleize)
end
# Force Upper Case & strip VIN of all illegal chars (for barcode scanner)
def vin=(val)
val = val.to_s.upcase.gsub(/[^A-HJ-NPR-Za-hj-npr-z\d]+/,"")
write_attribute(:vin, val)
end
# search for a vin
def self.search(search)
where("vin LIKE ?", "%#{search}%")
end
acts_as_searchable columns: %w[vin make model year],
scope: ->(_context) { left_joins(:project) },
date_column: :updated_at
acts_as_event :title => Proc.new {|o| "#{o.to_s}"},
:url => Proc.new {|o| { :controller => 'vehicles', :action => 'show', :id => o.id} },
:type => :to_s,
:description => Proc.new {|o| "#{o.vin} - #{o.customer}"},
:datetime => Proc.new {|o| o.updated_at || o.created_at}
# decodes a vin and updates self
def decode_vin
get_details
@@ -72,15 +46,77 @@ class Vehicle < ActiveRecord::Base
self.name = to_s
end
# reurns all invoices for this vehicle
def invoices
self.issues.flat_map(&:invoices).uniq.compact
# returns the raw JSON details from NHTSA
def details
get_details if @details.nil?
return @details
end
# returns all estimates for this vehicle
def estimates
self.issues.flat_map(&:estimate).uniq.compact
end
# reurns all invoices for this vehicle
def invoices
self.issues.flat_map(&:invoices).uniq.compact
end
# Force Upper Case for make numbers
def make=(val)
# The to_s is in case you get nil/non-string
write_attribute(:make, val.to_s.titleize)
end
# Force Upper Case for model numbers
def model=(val)
# The to_s is in case you get nil/non-string
write_attribute(:model, val.to_s.titleize)
end
# needed for redmine's search and event system, but we don't want to tie vehicles to projects
def project
nil
end
# search for a vehicle by vin, make, model, or year
def self.search(query)
q = sanitize_sql_like(query)
where("vin LIKE ? OR make LIKE ? OR model LIKE ? OR year LIKE ?", "%#{q}%", "%#{q}%", "%#{q}%", "%#{q}%")
end
# Override the defult redmine seach method to rank results by id
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)
# rank by id
ids.each_with_object({}) { |id, h| h[id] = id }
end
# returns a human readable string
def to_s
if year.nil? or make.nil? or model.nil?
return "#{vin}"
else
split_vin = vin.scan(/.{1,9}/)
return "#{year} #{make} #{model} - #{split_vin[1]}"
end
end
# Force Upper Case & strip VIN of all illegal chars (for barcode scanner)
def vin=(val)
val = val.to_s.upcase.gsub(/[^A-HJ-NPR-Za-hj-npr-z\d]+/,"")
write_attribute(:vin, val)
end
private

View File

@@ -1,5 +1,5 @@
<h4><%=t(:field_vehicles)%>:</h4>
<%= render partial: 'vehicles/list', locals: { vehicles: customer.vehicles.paginate(page: params[:page]) } %>
<%= render partial: 'vehicles/list', locals: { vehicles: customer.vehicles.paginate(page: params[:page]), show_customer: false, show_checkbox: true } %>
<div style="float: right;">
<%= button_to t(:button_new_vehicle), new_customer_vehicle_path(customer), method: :get %>
</div>

View File

@@ -6,7 +6,7 @@
<div class="vehicle_vin attribute">
<div class="label"><%=t(:field_vin)%>:</div>
<div class="value" id="vin">
<a href="#" id="copyLink" onclick="handleCopy(event)"><%=split_vin[0] if split_vin%><b><%=split_vin[1] if split_vin%></b></a>
<div id="copyLink" onclick="handleCopy(event)"><%=split_vin[0] if split_vin%><b><%=split_vin[1] if split_vin%></b></div>
</div>
</div>

View File

@@ -18,8 +18,8 @@
<tr>
<th><%= t(:field_vin) %></th>
<td id="vin">
<a href="#" onclick="handleCopy(event)"><%= @vin[0] if @vin %><b><%=@vin[1] if @vin%></b></a>
<td>
<div onclick="handleCopy(event)"><%= @vin[0] if @vin %><b><%=@vin[1] if @vin%></b></div>
</td>
</tr>

View File

@@ -5,14 +5,22 @@
<div class="container">
<%= check_box_tag "vehicle_ids[]", vehicle.id, false, onchange: "updateLink()", data: { url: vehicle_path(vehicle).html_safe, text: vehicle.to_s }, class: "appointment checkbox" %>
<% if show_checkbox %>
<%= check_box_tag "vehicle_ids[]", vehicle.id, false, onchange: "updateLink()", data: { url: vehicle_path(vehicle).html_safe, text: vehicle.to_s }, class: "appointment checkbox" %>
<% else %>
<div class='checkbox'>
</div>
<% end %>
<div class='label-main'>
<%= link_to vehicle.to_s, vehicle_path(vehicle) %>
</div>
<div class="label-sub">
<%= vehicle.vin.scan(/.{1,9}/)[0] if vehicle.vin %><b><%=vehicle.vin.scan(/.{1,9}/)[1] if vehicle.vin%></b>
<div onclick="handleCopy(event)"><%= vehicle.vin.scan(/.{1,9}/)[0] if vehicle.vin %><b><%=vehicle.vin.scan(/.{1,9}/)[1] if vehicle.vin%></b></div>
<% if show_customer %>
<%= vehicle.customer %>
<% end %>
</div>
</div>

View File

@@ -1,4 +1,4 @@
<%= form_tag(vehicles_path, method: "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: t(:label_search_vin), autocomplete: "off" %>
<%= 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 %>

View File

@@ -1,4 +1,4 @@
<h2><%=t(:label_customer_vehicles)%> <span style="float:right"> <%= render partial: 'vehicles/search' %> </span> </h2>
<br/>
<%= render partial: 'vehicles/list', locals: {vehicles: @vehicles} %>
<%= render partial: 'vehicles/list', locals: {vehicles: @vehicles, show_customer: true, show_checkbox: false} %>

View File

@@ -1,22 +1,27 @@
async function handleCopy(event) {
console.log("Copy link clicked");
// 1. Prevent the link from actually navigating
event.preventDefault();
// 2. Grab the text from our span
const text = document.getElementById('vin').innerText;
let text;
let link;
// Grab the text from our clicked link
if(event.target.tagName.toLowerCase() === 'b'){
text = event.target.parentElement.innerText;
link = event.target.parentElement;
} else {
text = event.target.innerText;
link = event.target;
}
try {
// 3. Write to clipboard
// Write to clipboard
await navigator.clipboard.writeText(text);
// 4. Update the UI to show it worked
const link = event.target;
// Update the UI to show it worked
const originalText = link.innerText;
link.innerHTML = "<b>Copied!</b>";
link.style.color = "#4CAF50"; // Turn green
// 5. Reset after 2 seconds
// Reset after 2 seconds
setTimeout(() => {
// Check if the text is long enough to prevent errors
if (originalText.length >= 8) {

View File

@@ -28,7 +28,7 @@ en:
label_model: "Model"
label_new_vehicle: "New Customer Vehicle"
label_no_vehicles: "There are no vehicles containing the term(s)"
label_search_vin: "Search Vehicles by VIN"
label_search_vehicles: "Search Vehicles"
label_year: "Year"
no_customer: "Customer no longer exists"
notice_vehicle_created: "Vehicle was successfully created."

View File

@@ -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

View File

@@ -14,7 +14,7 @@ Redmine::Plugin.register :redmine_qbo_vehicles do
name 'Redmine QBO Vehicles plugin'
author 'Rick Barrette'
description 'This is a plugin for Redmine to intergrate with the redmine_qbo plugin to provide vehicle data tracking'
version '2026.2.3'
version '2026.2.6'
url 'https://github.com/rickbarrette/redmine_qbo_vehicles'
author_url 'https://barrettefabrication.com'
requires_redmine version_or_higher: '6.1.0'
@@ -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