1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

introduce predicate {does_not_}matches_regexp

This commit is contained in:
Keenan Brock 2015-12-05 20:23:12 -05:00
parent 4e7b4693b8
commit d2e6be1677
2 changed files with 21 additions and 6 deletions

View file

@ -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

View file

@ -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.*')