Compare commits

...

6 Commits

3 changed files with 51 additions and 47 deletions

View File

@@ -9,6 +9,9 @@
#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 Customer < ActiveRecord::Base
include Redmine::Acts::Searchable
include Redmine::Acts::Event
has_many :issues
has_many :invoices
@@ -17,6 +20,16 @@ class Customer < ActiveRecord::Base
validates_presence_of :id, :name
self.primary_key = :id
acts_as_searchable columns: %w[name phone_number mobile_phone_number ],
scope: ->(_context) { left_joins(:project) },
date_column: :updated_at
acts_as_event :title => Proc.new {|o| "#{o}"},
:url => Proc.new {|o| { :controller => 'customers', :action => 'show', :id => o.id} },
:type => :to_s,
:description => Proc.new {|o| "#{I18n.t :label_primary_phone}: #{o.phone_number} #{I18n.t:label_mobile_phone}: #{o.mobile_phone_number}"},
:datetime => Proc.new {|o| o.updated_at || o.created_at}
# Convenience Method
# returns the customer's email
@@ -35,27 +48,6 @@ class Customer < ActiveRecord::Base
pull unless @details
@details.email_address = s
end
# Redmine Search Event Interface
def event_type
:customer
end
def event_title
to_s
end
def event_description
"#{I18n.t :label_primary_phone}: #{primary_phone} #{I18n.t:label_mobile_phone}: #{mobile_phone}"
end
def event_url
Rails.application.routes.url_helpers.customer_path(self)
end
def event_datetime
updated_at || created_at
end
# Convenience Method
# returns the customer's primary phone
@@ -79,7 +71,8 @@ class Customer < ActiveRecord::Base
update_phone_number
end
# Customers are not bound by a project.
# Customers are not bound by a project
# but we need to implement this method for the Redmine::Acts::Searchable interface
def project
nil
end
@@ -187,33 +180,28 @@ class Customer < ActiveRecord::Base
end
end
end
# Searchs the database for a customer by name or phone number with out special chars
def self.search(search)
search = sanitize_sql_like(search)
customers = where("name LIKE ? OR phone_number LIKE ? OR mobile_phone_number LIKE ?", "%#{search}%", "%#{search}%", "%#{search}%")
return customers.order(:name)
end
def self.searchable_columns
['name', 'phone_number', 'mobile_phone_number']
end
def self.searchable_options
{
columns: searchable_columns,
scope: self.all
}
end
def self.search_results_from_ids(ids, *args)
return [] if ids.blank?
ids = ids.map(&:to_i)
records = where(id: ids).index_by(&:id)
ids.map { |id| records[id] }.compact
return all if search.blank?
# 1. Clean the input: Remove existing stars and special Boolean operators
# to prevent "red**" or syntax errors from hyphens/plus signs.
clean_search = search.gsub(/[*+\-><()~]/, '')
# 2. Add a single trailing wildcard for partial matching
ft_query = "#{clean_search}*"
# 3. Use the exact column list from your migration
# Using a hybrid approach to ensure "Jonh" still finds "John"
where(
"MATCH(name, phone_number, mobile_phone_number) AGAINST(? IN BOOLEAN MODE) OR
SOUNDEX(SUBSTRING_INDEX(name, ' ', 1)) = SOUNDEX(?) OR
name LIKE ?",
ft_query, clean_search, "%#{sanitize_sql_like(clean_search)}%"
).order(Arel.sql("MATCH(name, phone_number, mobile_phone_number) AGAINST(#{connection.quote(clean_search)}) DESC"))
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?

View File

@@ -0,0 +1,16 @@
#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 AddFullTextIndexToCustomers < ActiveRecord::Migration[7.0]
def change
# This creates a combined index for name and phone fields
add_index :customers, [:name, :phone_number, :mobile_phone_number], type: :fulltext, name: 'ft_search_idx'
end
end

View File

@@ -14,7 +14,7 @@ Redmine::Plugin.register :redmine_qbo do
name 'Redmine QBO plugin'
author 'Rick Barrette'
description 'A pluging for Redmine to connect with QuickBooks Online to create Time Activity Entries for billable hours logged when an Issue is closed'
version '2026.2.12'
version '2026.2.14'
url 'https://github.com/rickbarrette/redmine_qbo'
author_url 'https://barrettefabrication.com'
settings default: {empty: true}, partial: 'qbo/settings'