From 5b401947c99ad17bc5a20a02857c7da75a757dab Mon Sep 17 00:00:00 2001 From: Peter Fern Date: Tue, 10 Dec 2013 20:57:26 +1100 Subject: [PATCH] Allow specifying default attribute and predicate via options[:default] Affects `predicate_select` and `attribute_select` --- lib/ransack/helpers/form_builder.rb | 10 +++++++++- lib/ransack/nodes/condition.rb | 9 ++------- lib/ransack/nodes/grouping.rb | 3 +-- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/ransack/helpers/form_builder.rb b/lib/ransack/helpers/form_builder.rb index 79dd213..b944713 100644 --- a/lib/ransack/helpers/form_builder.rb +++ b/lib/ransack/helpers/form_builder.rb @@ -24,15 +24,20 @@ module Ransack options = options || {} html_options = html_options || {} action = action || 'search' + default = options.delete(:default) raise ArgumentError, formbuilder_error_message( "#{action}_select") unless object.respond_to?(:context) options[:include_blank] = true unless options.has_key?(:include_blank) bases = [''] + association_array(options[:associations]) if bases.size > 1 collection = attribute_collection_for_bases(action, bases) + object.name ||= default if object.respond_to?(:name=) && default && + collection.flatten(2).map{|a| a.is_a?(Array) ? a.first : nil}.compact.include?(default.to_s) template_grouped_collection_select(collection, options, html_options) else collection = collection_for_base(action, bases.first) + object.name ||= default if object.respond_to?(:name=) && default && + collection.map{|a| a.is_a?(Array) ? a.first : nil}.compact.include?(default.to_s) template_collection_select(:name, collection, options, html_options) end end @@ -96,6 +101,7 @@ module Ransack def predicate_select(options = {}, html_options = {}) options[:compounds] = true if options[:compounds].nil? + default = options.delete(:default) || 'eq' keys = options[:compounds] ? Predicate.names : Predicate.names.reject { |k| k.match(/_(any|all)$/) } if only = options[:only] @@ -107,6 +113,8 @@ module Ransack end end collection = keys.map { |k| [k, Translate.predicate(k)] } + object.predicate ||= Predicate.named(default) if object.respond_to?(:predicate=) && + default && keys.include?(default.to_s) template_collection_select(:p, collection, options, html_options) end @@ -211,4 +219,4 @@ module Ransack end end -end \ No newline at end of file +end diff --git a/lib/ransack/nodes/condition.rb b/lib/ransack/nodes/condition.rb index 07feaf2..1f11429 100644 --- a/lib/ransack/nodes/condition.rb +++ b/lib/ransack/nodes/condition.rb @@ -4,7 +4,7 @@ module Ransack i18n_word :attribute, :predicate, :combinator, :value i18n_alias :a => :attribute, :p => :predicate, :m => :combinator, :v => :value - attr_reader :predicate + attr_accessor :predicate class << self def extract(context, key, values) @@ -152,11 +152,6 @@ module Ransack end alias :p= :predicate_name= - def predicate=(predicate) - @predicate = predicate - predicate - end - def predicate_name predicate.name if predicate end @@ -216,4 +211,4 @@ module Ransack end end -end \ No newline at end of file +end diff --git a/lib/ransack/nodes/grouping.rb b/lib/ransack/nodes/grouping.rb index 08a0d6a..a7d81b0 100644 --- a/lib/ransack/nodes/grouping.rb +++ b/lib/ransack/nodes/grouping.rb @@ -84,7 +84,6 @@ module Ransack attrs = opts[:attributes] || 1 vals = opts[:values] || 1 condition = Condition.new(@context) - condition.predicate = Predicate.named('eq') attrs.times { condition.build_attribute } vals.times { condition.build_value } condition @@ -196,4 +195,4 @@ module Ransack end end -end \ No newline at end of file +end