diff --git a/lib/ransack/configuration.rb b/lib/ransack/configuration.rb index 198c745..e0e0c04 100644 --- a/lib/ransack/configuration.rb +++ b/lib/ransack/configuration.rb @@ -19,6 +19,7 @@ module Ransack opts[:name] = name compounds = opts.delete(:compounds) compounds = true if compounds.nil? + compounds = false if opts[:wants_array] opts[:arel_predicate] = opts[:arel_predicate].to_s self.predicates[name] = Predicate.new(opts) diff --git a/lib/ransack/predicate.rb b/lib/ransack/predicate.rb index 9b04bc0..a54e59b 100644 --- a/lib/ransack/predicate.rb +++ b/lib/ransack/predicate.rb @@ -41,7 +41,7 @@ module Ransack @formatter = opts[:formatter] @validator = opts[:validator] || lambda { |v| v.respond_to?(:empty?) ? !v.empty? : !v.nil? } @compound = opts[:compound] - @wants_array = @compound || ['in', 'not_in'].include?(@arel_predicate) + @wants_array = opts[:wants_array] == true || @compound || ['in', 'not_in'].include?(@arel_predicate) end def eql?(other) diff --git a/spec/ransack/configuration_spec.rb b/spec/ransack/configuration_spec.rb index bd6d23a..9c1598c 100644 --- a/spec/ransack/configuration_spec.rb +++ b/spec/ransack/configuration_spec.rb @@ -45,5 +45,15 @@ module Ransack # restore original state so we don't break other tests Ransack.options = before end + + it 'adds predicates that take arrays, overriding compounds' do + Ransack.configure do |config| + config.add_predicate :test_array_predicate, :wants_array => true, :compounds => true + end + + Ransack.predicates['test_array_predicate'].wants_array.should eq true + Ransack.predicates.should_not have_key 'test_array_predicate_any' + Ransack.predicates.should_not have_key 'test_array_predicate_all' + end end end \ No newline at end of file diff --git a/spec/ransack/search_spec.rb b/spec/ransack/search_spec.rb index 8f6b150..1e41a60 100644 --- a/spec/ransack/search_spec.rb +++ b/spec/ransack/search_spec.rb @@ -90,6 +90,20 @@ module Ransack conditions.should have(2).items conditions.map {|c| c.class}.should eq [Nodes::Condition, Nodes::Condition] end + + it 'creates Conditions for custom predicates that take arrays' do + Ransack.configure do |config| + config.add_predicate 'ary_pred', + :wants_array => true + end + + search = Search.new(Person, :name_ary_pred => ['Ernie', 'Bert']) + condition = search.base[:name_ary_pred] + condition.should be_a Nodes::Condition + condition.predicate.name.should eq 'ary_pred' + condition.attributes.first.name.should eq 'name' + condition.value.should eq ['Ernie', 'Bert'] + end end describe '#result' do