Permalink
Browse files

Normalizer#add_normalization accepts an Arel node

  • Loading branch information...
1 parent d55e58a commit 64317d6a0e6bc13799f2e77971106d3c750e667c @nertzy nertzy committed Apr 2, 2013
Showing with 52 additions and 20 deletions.
  1. +19 −11 lib/pg_search/normalizer.rb
  2. +33 −9 spec/lib/pg_search/normalizer_spec.rb
@@ -5,18 +5,26 @@ def initialize(config)
end
def add_normalization(sql_expression)
- if config.ignore.include?(:accents)
- if config.postgresql_version < 90000
- raise PgSearch::NotSupportedForPostgresqlVersion.new(<<-MESSAGE.gsub /^\s*/, '')
- Sorry, {:ignoring => :accents} only works in PostgreSQL 9.0 and above.
- #{config.inspect}
- MESSAGE
- else
- "#{PgSearch.unaccent_function}(#{sql_expression})"
- end
- else
- sql_expression
+ return sql_expression unless config.ignore.include?(:accents)
+
+ if config.postgresql_version < 90000
+ raise PgSearch::NotSupportedForPostgresqlVersion.new(<<-MESSAGE.gsub /^\s*/, '')
+ Sorry, {:ignoring => :accents} only works in PostgreSQL 9.0 and above.
+ #{config.inspect}
+ MESSAGE
end
+
+ sql_node = case sql_expression
+ when Arel::Nodes::Node
+ sql_expression
+ else
+ Arel.sql(sql_expression)
+ end
+
+ Arel::Nodes::NamedFunction.new(
+ PgSearch.unaccent_function,
+ [sql_node]
+ ).to_sql
end
private
@@ -4,21 +4,45 @@
describe "#add_normalization" do
context "for PostgreSQL 9.0 and above" do
context "when config[:ignore] includes :accents" do
- it "wraps the expression in unaccent()" do
- config = stub("config", :ignore => [:accents], :postgresql_version => 90000)
+ context "when passed an Arel node" do
+ it "wraps the expression in unaccent()" do
+ config = stub("config", :ignore => [:accents], :postgresql_version => 90000)
+ node = Arel::Nodes::NamedFunction.new("foo", ["bar"])
- normalizer = PgSearch::Normalizer.new(config)
- normalizer.add_normalization("foo").should == "unaccent(foo)"
- end
+ normalizer = PgSearch::Normalizer.new(config)
+ normalizer.add_normalization(node).should == "unaccent(foo('bar'))"
+ end
+
+ context "when a custom unaccent function is specified" do
+ it "wraps the expression in that function" do
+ PgSearch.stub(:unaccent_function).and_return("my_unaccent")
+ node = Arel::Nodes::NamedFunction.new("foo", ["bar"])
- context "when a custom unaccent function is specified" do
- it "wraps the expression in that function" do
- PgSearch.stub(:unaccent_function).and_return("my_unaccent")
+ config = stub("config", :ignore => [:accents], :postgresql_version => 90000)
+ normalizer = PgSearch::Normalizer.new(config)
+ normalizer.add_normalization(node).should == "my_unaccent(foo('bar'))"
+ end
+ end
+ end
+
+ context "when passed a String" do
+ it "wraps the expression in unaccent()" do
config = stub("config", :ignore => [:accents], :postgresql_version => 90000)
normalizer = PgSearch::Normalizer.new(config)
- normalizer.add_normalization("foo").should == "my_unaccent(foo)"
+ normalizer.add_normalization("foo").should == "unaccent(foo)"
+ end
+
+ context "when a custom unaccent function is specified" do
+ it "wraps the expression in that function" do
+ PgSearch.stub(:unaccent_function).and_return("my_unaccent")
+
+ config = stub("config", :ignore => [:accents], :postgresql_version => 90000)
+
+ normalizer = PgSearch::Normalizer.new(config)
+ normalizer.add_normalization("foo").should == "my_unaccent(foo)"
+ end
end
end
end

0 comments on commit 64317d6

Please sign in to comment.