Merge pull request #317 from pdf/default_select_options

Allow specifying default attribute and predicate via options[:default]
This commit is contained in:
Jon Atack 2013-12-10 02:28:41 -08:00
commit 935af8a446
3 changed files with 12 additions and 10 deletions

View File

@ -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
end

View File

@ -5,7 +5,7 @@ module Ransack
i18n_alias :a => :attribute, :p => :predicate, :m => :combinator,
:v => :value
attr_reader :predicate
attr_accessor :predicate
class << self
def extract(context, key, values)
@ -161,11 +161,6 @@ module Ransack
end
alias :p= :predicate_name=
def predicate=(predicate)
@predicate = predicate
predicate
end
def predicate_name
predicate.name if predicate
end
@ -234,4 +229,4 @@ module Ransack
end
end
end
end

View File

@ -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
@ -190,4 +189,4 @@ module Ransack
end
end
end
end
end