added starts, ends

This commit is contained in:
Zhomart Mukhamejanov 2014-08-02 20:37:22 -07:00
parent 80c71f8b29
commit aff2151b10
3 changed files with 24 additions and 10 deletions

View File

@ -52,7 +52,7 @@ module Ransack
end
def matches other
{ name => /#{Regexp.escape(other)}/i }.to_inquiry
{ name => /#{other}/i }.to_inquiry
end
def matches_any others
@ -64,7 +64,7 @@ module Ransack
end
def does_not_match other
{ "$not" => { name => /#{Regexp.escape(other)}/i } }.to_inquiry
{ "$not" => { name => /#{other}/i } }.to_inquiry
end
def does_not_match_any others

View File

@ -3,32 +3,32 @@ module Ransack
DERIVED_PREDICATES = [
['cont', {
:arel_predicate => 'matches',
:formatter => proc { |v| "#{escape_wildcards(v)}" }
:formatter => proc { |v| "#{escape_regex(v)}" }
}
],
['not_cont', {
:arel_predicate => 'does_not_match',
:formatter => proc { |v| "#{escape_wildcards(v)}" }
:formatter => proc { |v| "#{escape_regex(v)}" }
}
],
['start', {
:arel_predicate => 'matches',
:formatter => proc { |v| "#{escape_wildcards(v)}%" }
:formatter => proc { |v| "\\A#{escape_regex(v)}" }
}
],
['not_start', {
:arel_predicate => 'does_not_match',
:formatter => proc { |v| "#{escape_wildcards(v)}%" }
:formatter => proc { |v| "\\A#{escape_regex(v)}" }
}
],
['end', {
:arel_predicate => 'matches',
:formatter => proc { |v| "%#{escape_wildcards(v)}" }
:formatter => proc { |v| "#{escape_regex(v)}\\Z" }
}
],
['not_end', {
:arel_predicate => 'does_not_match',
:formatter => proc { |v| "%#{escape_wildcards(v)}" }
:formatter => proc { |v| "#{escape_regex(v)}\\Z" }
}
],
['true', {
@ -81,8 +81,8 @@ module Ransack
module_function
# does nothing
def escape_wildcards(unescaped)
unescaped
def escape_regex(unescaped)
Regexp.escape(unescaped)
end
end
end

View File

@ -137,5 +137,19 @@ module Ransack
expect(@s.result.selector).to eq({ "created_at" => { '$lte' => time } })
end
end
describe 'starts_with' do
it 'generates an starts_with' do
@s.name_start = 'ric'
expect(@s.result.selector).to eq({ "name" => /\Aric/i })
end
end
describe 'ends_with' do
it 'generates an ends_with' do
@s.name_end = 'ric'
expect(@s.result.selector).to eq({ "name" => /ric\Z/i })
end
end
end
end