forked from railsbridge/bridge_troll
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ajaxify the potential organizer selector
Finishes railsbridge#369
- Loading branch information
1 parent
dc83452
commit 3c35281
Showing
8 changed files
with
87 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class UserSearcher | ||
def initialize(relation, query) | ||
@relation = relation | ||
@query = query | ||
end | ||
|
||
def as_json(options = {}) | ||
search_field = @relation.connection.concat('lower(first_name)', "' '", 'lower(last_name)') | ||
@relation | ||
.select(:id, :first_name, :last_name) | ||
.where("#{search_field} like ?", "%#{@query.downcase}%") | ||
.map { |u| {id: u.id, text: u.full_name} } | ||
end | ||
|
||
private | ||
|
||
def db_concat(*fields) | ||
if false | ||
"CONCAT(#{fields.join(', ')})" | ||
else | ||
fields.join(' || ') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# https://gist.github.com/eric1234/5622690 | ||
module ActiveRecord | ||
module ConnectionAdapters | ||
class AbstractAdapter | ||
def concat(*args) | ||
args * " || " | ||
end | ||
end | ||
|
||
class PostgreSQLAdapter < AbstractAdapter | ||
def concat(*args) | ||
"CONCAT(#{args * ', '})" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require 'rails_helper' | ||
|
||
describe UserSearcher do | ||
let!(:user1) { create(:user, first_name: 'Abrahat', last_name: 'Zachson') } | ||
let!(:user2) { create(:user, first_name: 'Rochalle', last_name: 'Gorfdoor') } | ||
|
||
it 'returns the users matching a search query' do | ||
searcher = UserSearcher.new(User, 'hat') | ||
expect(searcher.as_json).to match_array([{id: user1.id, text: user1.full_name}]) | ||
end | ||
end |