Merge pull request #562 from hoshitocat/fixed/let-attributes-use-and-or

Fix handling attribute-name with `_and_` `_or_`
This commit is contained in:
Jon Atack 2015-07-12 13:06:49 +02:00
commit 3a3f8a373f
2 changed files with 8 additions and 9 deletions

View File

@ -9,7 +9,7 @@ module Ransack
class << self
def extract(context, key, values)
attributes, predicate = extract_attributes_and_predicate(key)
attributes, predicate = extract_attributes_and_predicate(key, context)
if attributes.size > 0 && predicate
combinator = key.match(/_(or|and)_/) ? $1 : nil
condition = self.new(context)
@ -31,14 +31,18 @@ module Ransack
private
def extract_attributes_and_predicate(key)
def extract_attributes_and_predicate(key, context = nil)
str = key.dup
name = Predicate.detect_and_strip_from_string!(str)
predicate = Predicate.named(name)
unless predicate || Ransack.options[:ignore_unknown_conditions]
raise ArgumentError, "No valid predicate for #{key}"
end
attributes = str.split(/_and_|_or_/)
if context.present? && context.attribute_method?(str)
attributes = [str]
else
attributes = str.split(/_and_|_or_/)
end
[attributes, predicate]
end
end

View File

@ -231,14 +231,9 @@ module Ransack
end
it "should function correctly when an attribute name has 'and' in it" do
# FIXME: this test does not pass!
p = Person.create!(:terms_and_conditions => true)
s = Person.ransack(:terms_and_conditions_eq => true)
# search is not detecting the attribute
puts "
FIXME: Search not detecting the `terms_and_conditions` attribute in
base_spec.rb, line 178: #{s.result.to_sql}"
# expect(s.result.to_a).to eq [p]
expect(s.result.to_a).to eq [p]
end
it 'allows sort by "only_sort" field' do