Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support matches and lower predicate methods #235

Merged
merged 4 commits into from
May 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/mobility/arel/nodes/pg_ops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ module Nodes
def eq(other)
Equality.new self, quoted_node(other)
end

def lower
super self
end
end)
end

Expand All @@ -37,7 +41,16 @@ def quoted_node(other)
end
end

class Jsonb < JsonbDashArrow; end
class Jsonb < JsonbDashArrow
def matches *args
JsonDashDoubleArrow.new(left, right).matches(*args)
end

def lower
JsonDashDoubleArrow.new(left, right).lower
end
end

class Hstore < HstoreDashArrow; end
class Json < JsonDashDoubleArrow; end
end
Expand Down
24 changes: 20 additions & 4 deletions spec/support/shared_examples/querying_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -402,15 +402,31 @@ def query(*args, &block); model_class.i18n(*args, &block); end
end
end

# TODO: support LIKE with JSONB/CONTAINER backends
describe "LIKE/ILIKE (matches)" do
it "includes partial string matches" do
skip "Not yet supported by #{backend_name}" if [:jsonb, :container].include?(backend_name)
foobar = model_class.create(a1 => "foobar")
barfoo = model_class.create(a1 => "barfoo")
foobar = model_class.create(a1 => "foObar")
barfoo = model_class.create(a1 => "barfOo")
expect(query { __send__(a1).matches("foo%") }).to match_array([i[0], *i[5..6], foobar])
expect(query { __send__(a1).matches("%foo") }).to match_array([i[0], *i[5..6], barfoo])
end

if ENV['DB'] == 'postgres' && ::ActiveRecord::VERSION::STRING >= '5.0'
it "works with case_sensitive option" do
foobar = model_class.create(a1 => "foObar")
barfoo = model_class.create(a1 => "barfOo")
expect(query { __send__(a1).matches("foo%", nil, true) }).not_to include(foobar)
expect(query { __send__(a1).matches("foO%", nil, true) }).to include(foobar)
expect(query { __send__(a1).matches("%foo", nil, true) }).not_to include(barfoo)
expect(query { __send__(a1).matches("%fOo", nil, true) }).to include(barfoo)
end
end
end

describe "LOWER" do
it "matches lowercase string values" do
foobar = model_class.create(a1 => "foObar")
expect(query { __send__(a1).lower.eq("foobar") }).to match_array([foobar])
end
end
end

Expand Down