adds search method to Organization
This commit is contained in:
@@ -28,4 +28,8 @@ class Organization < ActiveRecord::Base
|
||||
(verified_at.blank? || verified_at < rejected_at)
|
||||
end
|
||||
|
||||
def self.search(text)
|
||||
text.present? ? joins(:user).where("users.email = ? OR users.phone_number = ? OR organizations.name ILIKE ?", text, text, "%#{text}%") : none
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -43,4 +43,31 @@ describe Organization do
|
||||
expect(subject.rejected?).to be false
|
||||
end
|
||||
end
|
||||
|
||||
describe "self.search" do
|
||||
before(:all) {@organization = create(:organization, name: "Watershed", user: create(:user, phone_number: "333"))}
|
||||
|
||||
it "returns no results if search term is empty" do
|
||||
expect(Organization.search(" ").size).to eq(0)
|
||||
end
|
||||
|
||||
it "finds fuzzily by name" do
|
||||
expect(Organization.search("Greenpeace").size).to eq 0
|
||||
search = Organization.search("Tershe")
|
||||
expect(search.size).to eq 1
|
||||
expect(search.first).to eq @organization
|
||||
end
|
||||
|
||||
scenario "finds by users email" do
|
||||
search = Organization.search(@organization.user.email)
|
||||
expect(search.size).to eq 1
|
||||
expect(search.first).to eq @organization
|
||||
end
|
||||
|
||||
scenario "finds by users phone number" do
|
||||
search = Organization.search(@organization.user.phone_number)
|
||||
expect(search.size).to eq 1
|
||||
expect(search.first).to eq @organization
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user