From d2e6be1677b124b3eef5b3ebe0fd4a0d31d8a2bf Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Sat, 5 Dec 2015 20:23:12 -0500 Subject: [PATCH] introduce predicate {does_not_}matches_regexp --- lib/arel/predications.rb | 8 ++++++++ test/visitors/test_postgres.rb | 19 +++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/arel/predications.rb b/lib/arel/predications.rb index ed083f5402..1d2b0de235 100644 --- a/lib/arel/predications.rb +++ b/lib/arel/predications.rb @@ -122,6 +122,10 @@ Passing a range to `#not_in` is deprecated. Call `#not_between`, instead. Nodes::Matches.new self, quoted_node(other), escape, case_sensitive end + def matches_regexp other, case_sensitive = true + Nodes::Regexp.new self, quoted_node(other), case_sensitive + end + def matches_any others, escape = nil, case_sensitive = false grouping_any :matches, others, escape, case_sensitive end @@ -134,6 +138,10 @@ Passing a range to `#not_in` is deprecated. Call `#not_between`, instead. Nodes::DoesNotMatch.new self, quoted_node(other), escape, case_sensitive end + def does_not_match_regexp other, case_sensitive = true + Nodes::NotRegexp.new self, quoted_node(other), case_sensitive + end + def does_not_match_any others, escape = nil grouping_any :does_not_match, others, escape end diff --git a/test/visitors/test_postgres.rb b/test/visitors/test_postgres.rb index 4209712e8e..5901c56968 100644 --- a/test/visitors/test_postgres.rb +++ b/test/visitors/test_postgres.rb @@ -114,21 +114,25 @@ module Arel describe "Nodes::Regexp" do it "should know how to visit" do - node = Arel::Nodes::Regexp.new(@table[:name], Nodes.build_quoted('foo.*')) + node = @table[:name].matches_regexp('foo.*') + node.must_be_kind_of Nodes::Regexp + node.case_sensitive.must_equal(true) compile(node).must_be_like %{ "users"."name" ~ 'foo.*' } end it "can handle case insensitive" do - node = Arel::Nodes::Regexp.new(@table[:name], Nodes.build_quoted('foo.*'), false) + node = @table[:name].matches_regexp('foo.*', false) + node.must_be_kind_of Nodes::Regexp + node.case_sensitive.must_equal(false) compile(node).must_be_like %{ "users"."name" ~* 'foo.*' } end it 'can handle subqueries' do - subquery = @table.project(:id).where(Arel::Nodes::Regexp.new(@table[:name], Nodes.build_quoted('foo.*'))) + subquery = @table.project(:id).where(@table[:name].matches_regexp('foo.*')) node = @attr.in subquery compile(node).must_be_like %{ "users"."id" IN (SELECT id FROM "users" WHERE "users"."name" ~ 'foo.*') @@ -138,21 +142,24 @@ module Arel describe "Nodes::NotRegexp" do it "should know how to visit" do - node = Arel::Nodes::NotRegexp.new(@table[:name], Nodes.build_quoted('foo.*')) + node = @table[:name].does_not_match_regexp('foo.*') + node.must_be_kind_of Nodes::NotRegexp + node.case_sensitive.must_equal(true) compile(node).must_be_like %{ "users"."name" !~ 'foo.*' } end it "can handle case insensitive" do - node = Arel::Nodes::NotRegexp.new(@table[:name], Nodes.build_quoted('foo.*'), false) + node = @table[:name].does_not_match_regexp('foo.*', false) + node.case_sensitive.must_equal(false) compile(node).must_be_like %{ "users"."name" !~* 'foo.*' } end it 'can handle subqueries' do - subquery = @table.project(:id).where(Arel::Nodes::NotRegexp.new(@table[:name], Nodes.build_quoted('foo.*'))) + subquery = @table.project(:id).where(@table[:name].does_not_match_regexp('foo.*')) node = @attr.in subquery compile(node).must_be_like %{ "users"."id" IN (SELECT id FROM "users" WHERE "users"."name" !~ 'foo.*')