diff --git a/lib/redmine_qbo/patches/issue_patch.rb b/lib/redmine_qbo/patches/issue_patch.rb index 1c08656..92c41e0 100644 --- a/lib/redmine_qbo/patches/issue_patch.rb +++ b/lib/redmine_qbo/patches/issue_patch.rb @@ -25,7 +25,7 @@ module RedmineQbo # Same as typing in the class base.class_eval do - belongs_to :customer, primary_key: :id + belongs_to :customer, class_name: 'Customer', foreign_key: :customer_id, optional: true belongs_to :customer_token, primary_key: :id belongs_to :estimate, primary_key: :id has_and_belongs_to_many :invoices diff --git a/lib/redmine_qbo/patches/query_patch.rb b/lib/redmine_qbo/patches/query_patch.rb index 29209a9..8c657af 100644 --- a/lib/redmine_qbo/patches/query_patch.rb +++ b/lib/redmine_qbo/patches/query_patch.rb @@ -13,6 +13,15 @@ require_dependency 'issue_query' module RedmineQbo module Patches module QueryPatch + + def base_scope + scope = super + if filters['customer_name'].present? + scope = scope.left_outer_joins(:customer) + end + scope + end + # Add qbo options to the aviable columns def available_columns @@ -26,10 +35,27 @@ module RedmineQbo # Add customers to filters def initialize_available_filters - add_available_filter "customer_id", type: :list, name: l(:field_customer), :values => lambda {Customer.pluck(:name, :id).map {|name, id| [name, id.to_s]}} + #add_available_filter "customer_id", type: :list, name: l(:field_customer), :values => lambda {Customer.pluck(:name, :id).map {|name, id| [name, id.to_s]}} + add_available_filter( 'customer_name', type: :text, name: l(:field_customer)) super end + def sql_for_customer_name_field(field, operator, value) + pattern = "%#{value.first}%" + + sql = case operator + when '~' + "#{Customer.table_name}.name LIKE ?" + when '!~' + "#{Customer.table_name}.name NOT LIKE ?" + else + return nil + end + + Issue.joins(:customer).sanitize_sql_for_conditions([sql, pattern]) + end + + end # Add module to Issue