Skip to content

Commit

Permalink
Allow user searching on multiple adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
xjunior committed May 10, 2024
1 parent 97c8473 commit 971622a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 8 additions & 1 deletion audiences/app/models/audiences/users_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 8 additions & 2 deletions audiences/spec/models/audiences/user_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 971622a

Please sign in to comment.