From e00da6167ec9fbafbeca836926aa3feb1e9bf2bc Mon Sep 17 00:00:00 2001 From: Carlos Palhares Date: Fri, 10 May 2024 00:51:46 -0300 Subject: [PATCH] Allow user searching on multiple adapters --- audiences/app/models/audiences/users_search.rb | 9 ++++++++- audiences/spec/models/audiences/user_search_spec.rb | 10 ++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/audiences/app/models/audiences/users_search.rb b/audiences/app/models/audiences/users_search.rb index edce4c82..07f67ab9 100644 --- a/audiences/app/models/audiences/users_search.rb +++ b/audiences/app/models/audiences/users_search.rb @@ -27,7 +27,14 @@ def users private def result - @result ||= @scope.where("data LIKE ?", "%#{@query}%") + @result ||= @scope.where("#{data_attribute_query} LIKE ?", "%#{@query}%") + end + + def data_attribute_query + case @scope.connection.adapter_name + when "PostgreSQL" then "CAST(data AS TEXT)" + else "CAST(data AS CHAR)" + end end end end diff --git a/audiences/spec/models/audiences/user_search_spec.rb b/audiences/spec/models/audiences/user_search_spec.rb index 3f68895f..369dac80 100644 --- a/audiences/spec/models/audiences/user_search_spec.rb +++ b/audiences/spec/models/audiences/user_search_spec.rb @@ -4,8 +4,14 @@ RSpec.describe Audiences::UsersSearch do it "searches through any serialized data attribute" do - john_doe = Audiences::ExternalUser.create(user_id: 123, data: { name: "John Doe" }) - frank_doe = Audiences::ExternalUser.create(user_id: 321, data: { name: "Frank Doe", territory: "Philadelphia" }) + john_doe = Audiences::ExternalUser.create( + user_id: 123, + data: { displayName: "John Doe" } + ) + frank_doe = Audiences::ExternalUser.create( + user_id: 321, + data: { displayName: "Frank Doe", territory: "Philadelphia" } + ) john_search = Audiences::UsersSearch.new(query: "John") phila_search = Audiences::UsersSearch.new(query: "Phila")