From 8b30302f3b7eabe967b16e78f898759bce37ff0c Mon Sep 17 00:00:00 2001 From: hoshitocat Date: Sun, 12 Jul 2015 14:58:38 +0900 Subject: [PATCH 1/2] Fix handle attribute-names-including test failure --- lib/ransack/nodes/condition.rb | 10 +++++++--- spec/ransack/adapters/active_record/base_spec.rb | 7 +------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/ransack/nodes/condition.rb b/lib/ransack/nodes/condition.rb index 385874135..bb15d0712 100644 --- a/lib/ransack/nodes/condition.rb +++ b/lib/ransack/nodes/condition.rb @@ -9,7 +9,7 @@ class Condition < Node 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 @@ def extract(context, key, values) 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 diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index 475870021..304f79143 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -228,14 +228,9 @@ def self.sane_adapter? 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 From 9927d9944e37f73fad1014ee79aef0104fbcac84 Mon Sep 17 00:00:00 2001 From: hoshitocat Date: Sun, 12 Jul 2015 16:39:14 +0900 Subject: [PATCH 2/2] Add surrounding space in default value assignment --- lib/ransack/nodes/condition.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ransack/nodes/condition.rb b/lib/ransack/nodes/condition.rb index bb15d0712..c47d3bdbe 100644 --- a/lib/ransack/nodes/condition.rb +++ b/lib/ransack/nodes/condition.rb @@ -31,7 +31,7 @@ def extract(context, key, values) private - def extract_attributes_and_predicate(key, context=nil) + def extract_attributes_and_predicate(key, context = nil) str = key.dup name = Predicate.detect_and_strip_from_string!(str) predicate = Predicate.named(name)