adds self.search to Debate
This commit is contained in:
@@ -128,6 +128,10 @@ class Debate < ActiveRecord::Base
|
|||||||
self.hot_score = (age_in_units**3 + weighted_score * 1000).round
|
self.hot_score = (age_in_units**3 + weighted_score * 1000).round
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.search(terms)
|
||||||
|
terms.present? ? where("title ILIKE ? OR description ILIKE ?", "%#{terms}%", "%#{terms}%") : none
|
||||||
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def sanitize_description
|
def sanitize_description
|
||||||
|
|||||||
@@ -239,6 +239,33 @@ describe Debate do
|
|||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "self.search" do
|
||||||
|
it "find debates by title" do
|
||||||
|
debate1 = create(:debate, title: "Karpov vs Kasparov")
|
||||||
|
create(:debate, title: "Bird vs Magic")
|
||||||
|
search = Debate.search("Kasparov")
|
||||||
|
expect(search.size).to eq(1)
|
||||||
|
expect(search.first).to eq(debate1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "find debates by description" do
|
||||||
|
debate1 = create(:debate, description: "...chess masters...")
|
||||||
|
create(:debate, description: "...basket masters...")
|
||||||
|
search = Debate.search("chess")
|
||||||
|
expect(search.size).to eq(1)
|
||||||
|
expect(search.first).to eq(debate1)
|
||||||
|
end
|
||||||
|
|
||||||
end
|
it "find debates by title and description" do
|
||||||
|
create(:debate, title: "Karpov vs Kasparov", description: "...played like Gauss...")
|
||||||
|
create(:debate, title: "Euler vs Gauss", description: "...math masters...")
|
||||||
|
search = Debate.search("Gauss")
|
||||||
|
expect(search.size).to eq(2)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns no results if no search term provided" do
|
||||||
|
expect(Debate.search(" ").size).to eq(0)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
@@ -249,7 +249,7 @@ describe User do
|
|||||||
expect(search.first).to eq(user1)
|
expect(search.first).to eq(user1)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns no results if no email provided" do
|
it "returns no results if no search term provided" do
|
||||||
expect(User.search(" ").size).to eq(0)
|
expect(User.search(" ").size).to eq(0)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user