Check exact matches before splitting

* if the supplied name contains '_and_' or '_or_' we should check
  if it exists before splitting and checking the parts, this way we
  handle 'terms_and_conditions' as well as pairs of fields like
  'name_and_email'.
This commit is contained in:
Joe Yates 2014-10-17 14:02:16 +02:00
parent e3163bc247
commit d6e80f2e88
3 changed files with 25 additions and 1 deletions

View File

@ -127,6 +127,7 @@ module Ransack
def attribute_method?(name)
name = strip_predicate_and_index(name)
return true if @context.attribute_method?(name)
case name
when /^(g|c|m|groupings|conditions|combinator)=?$/
true

View File

@ -7,7 +7,29 @@ module Ransack
@g = 1
end
let(:context) { Context.for(Person) }
subject { described_class.new(context) }
describe '#attribute_method?' do
context 'for attributes of the context' do
it 'is true' do
expect(subject.attribute_method?('name')).to be_true
end
context "where the attribute contains '_and_'" do
it 'is true' do
expect(subject.attribute_method?('terms_and_conditions')).to be_true
end
end
end
context 'for unknown attributes' do
it 'is false' do
expect(subject.attribute_method?('not_an_attribute')).to be_false
end
end
end
end
end
end
end

View File

@ -135,6 +135,7 @@ module Schema
t.string :only_admin
t.integer :salary
t.boolean :awesome, default: false
t.boolean :terms_and_conditions, default: false
t.timestamps null: false
end