-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reference to #211
- Loading branch information
Showing
8 changed files
with
107 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module SearchHelper | ||
TOKEN_SEP = ":" | ||
|
||
def self.process_search(query = "") | ||
parts = {} | ||
|
||
query.strip.split.each do |t| | ||
subtokens = t.split(TOKEN_SEP) | ||
|
||
if subtokens.length == 1 | ||
parts[:name] = /#{Regexp.escape(subtokens[0].strip)}/i | ||
elsif subtokens.length == 2 | ||
field = subtokens[0].strip | ||
value = subtokens[1].strip | ||
|
||
# Try to find the type | ||
klass = Character.fields.select { |k,v| v.options && v.options[:as] && v.options[:as].to_s == field } | ||
|
||
# Convert to Integer if we should or otherwise turn into regex | ||
if klass.length >= 1 && klass.first[1].options[:type] == Integer | ||
value = value.to_i | ||
elsif field == "gender" | ||
value = /\A#{Regexp.escape(value)}\Z/i | ||
else | ||
value = /#{Regexp.escape(value)}/i | ||
end | ||
|
||
# Set it | ||
parts[field.to_sym] = value | ||
end | ||
end | ||
|
||
parts | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require_relative '../spec_helper' | ||
|
||
describe 'SearchHelper', :unit do | ||
it "still works without a query" do | ||
result = SearchHelper.process_search() | ||
|
||
assert_equal({}, result) | ||
end | ||
|
||
it "filters by name for a simple query" do | ||
result = SearchHelper.process_search("foo") | ||
|
||
assert_equal({:name => /foo/i}, result) | ||
end | ||
|
||
it "can handle various fields in the query" do | ||
result = SearchHelper.process_search("level:275") | ||
assert_equal({:level => 275 }, result) | ||
|
||
result = SearchHelper.process_search("level: 275") | ||
assert_equal({:name => /275/i }, result) | ||
|
||
result = SearchHelper.process_search(" level: 275 ") | ||
assert_equal({:name => /275/i }, result) | ||
|
||
result = SearchHelper.process_search("level:12") | ||
assert_equal({:level => 12 }, result) | ||
|
||
result = SearchHelper.process_search("level:12 rank:2") | ||
assert_equal({:level => 12, :rank => 2}, result) | ||
|
||
result = SearchHelper.process_search("foo:bar bazz:buzz") | ||
assert_equal({:foo => /bar/i, :bazz => /buzz/i}, result) | ||
end | ||
|
||
it "doesn't use unbonded regexp for gender" do | ||
result = SearchHelper.process_search("gender:male") | ||
assert_equal({:gender => /\Amale\Z/i }, result) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
%span Search defaults to filtering by character name and supports partial matching. | ||
%br | ||
%br | ||
%span You may also search using a Google-like syntax following the form <code>field:value</code>, e.g., <code>level:275</code>. | ||
%br | ||
%br | ||
%span The full list of special fields you can use this syntax with are: | ||
|
||
%ul{:style => "margin-bottom: 0.25em;"} | ||
%li name | ||
%li server | ||
%li race | ||
%li gender | ||
%li level | ||
%li rank | ||
%li allegiance_name |
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