Permalink
Browse files

implements partial match functionality

  • Loading branch information...
1 parent 5badef6 commit 3fa3b402ddb2c26d368c7d3670b8cca3c2cccfba Lecky Lao committed May 3, 2012
Showing with 52 additions and 1 deletion.
  1. +12 −1 lib/texticle.rb
  2. +40 −0 spec/texticle_spec.rb
View
@@ -61,11 +61,22 @@ def parse_query_hash(query, table_name = quoted_table_name)
column = connection.quote_column_name(column_or_table)
search_term = connection.quote normalize(Helper.normalize(search_term))
@similarities << "ts_rank(to_tsvector(#{language}, #{table_name}.#{column}::text), to_tsquery(#{language}, #{search_term}::text))"
- @conditions << "to_tsvector(#{language}, #{table_name}.#{column}::text) @@ to_tsquery(#{language}, #{search_term}::text)"
+ condition = "to_tsvector(#{language}, #{table_name}.#{column}::text) @@ to_tsquery(#{language}, #{search_term}::text)"
+ partial_match_condition = " OR #{table_name}.#{column} #{partial_match_case_sensitive ? 'LIKE' : 'ILIKE'} #{search_term}"
+ condition = condition + partial_match_condition if partial_match && column !~ /id/
+ @conditions << condition
end
end
end
+ def partial_match
+ false
+ end
+
+ def partial_match_case_sensitive
+ false
+ end
+
def normalize(query)
query
end
View
@@ -228,4 +228,44 @@ def Game.searchable_language
assert_not_empty Game.search_by_title("harry")
end
end
+
+ context "when setting partial match" do
+ setup do
+ Game.create :system => "PS3", :title => "Harry Potter & the Deathly Hallows"
+ assert_empty Game.search_by_title("%rry%")
+ def Game.partial_match
+ true
+ end
+ end
+
+ teardown do
+ def Game.partial_match
+ false
+ end
+ Game.delete_all
+ end
+
+ should "search with word partial match" do
+ assert_not_empty Game.search_by_title("%rry%")
+ end
+
+ context "and setting patial match case sensitive" do
+ setup do
+ def Game.partial_match_case_sensitive
+ true
+ end
+ end
+
+ teardown do
+ def Game.partial_match_case_sensitive
+ true
+ end
+ end
+
+ should "search with case sensitive" do
+ assert_empty Game.search_by_title("%rry p%")
+ assert_not_empty Game.search_by_title("%rry P%")
+ end
+ end
+ end
end

0 comments on commit 3fa3b40

Please sign in to comment.