Refactor form_builder
This commit is contained in:
parent
025e229bfe
commit
382537bc55
|
@ -31,13 +31,15 @@ module Ransack
|
||||||
bases = [''] + association_array(options[:associations])
|
bases = [''] + association_array(options[:associations])
|
||||||
if bases.size > 1
|
if bases.size > 1
|
||||||
collection = attribute_collection_for_bases(action, bases)
|
collection = attribute_collection_for_bases(action, bases)
|
||||||
object.name ||= default if object.respond_to?(:name=) && default &&
|
object.name ||= default if can_use_default?(
|
||||||
collection.flatten(2).map{|a| a.is_a?(Array) ? a.first : nil}.compact.include?(default.to_s)
|
default, :name, mapped_values(collection.flatten(2))
|
||||||
|
)
|
||||||
template_grouped_collection_select(collection, options, html_options)
|
template_grouped_collection_select(collection, options, html_options)
|
||||||
else
|
else
|
||||||
collection = collection_for_base(action, bases.first)
|
collection = collection_for_base(action, bases.first)
|
||||||
object.name ||= default if object.respond_to?(:name=) && default &&
|
object.name ||= default if can_use_default?(
|
||||||
collection.map{|a| a.is_a?(Array) ? a.first : nil}.compact.include?(default.to_s)
|
default, :name, mapped_values(collection)
|
||||||
|
)
|
||||||
template_collection_select(:name, collection, options, html_options)
|
template_collection_select(:name, collection, options, html_options)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -113,8 +115,9 @@ module Ransack
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
collection = keys.map { |k| [k, Translate.predicate(k)] }
|
collection = keys.map { |k| [k, Translate.predicate(k)] }
|
||||||
object.predicate ||= Predicate.named(default) if object.respond_to?(:predicate=) &&
|
object.predicate ||= Predicate.named(default) if can_use_default?(
|
||||||
default && keys.include?(default.to_s)
|
default, :predicate, keys
|
||||||
|
)
|
||||||
template_collection_select(:p, collection, options, html_options)
|
template_collection_select(:p, collection, options, html_options)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -138,6 +141,15 @@ module Ransack
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def can_use_default?(default, attribute, values)
|
||||||
|
object.respond_to?("#{attribute}=") && default &&
|
||||||
|
values.include?(default.to_s)
|
||||||
|
end
|
||||||
|
|
||||||
|
def mapped_values(values)
|
||||||
|
values.map { |v| v.is_a?(Array) ? v.first : nil }.compact
|
||||||
|
end
|
||||||
|
|
||||||
def sort_array
|
def sort_array
|
||||||
[['asc', object.translate('asc')], ['desc', object.translate('desc')]]
|
[['asc', object.translate('asc')], ['desc', object.translate('desc')]]
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue