Refactor options order in form_builder methods

This commit is contained in:
jonatack 2013-12-08 21:47:46 +01:00
parent 322e60ebc2
commit 180ec8f7e3
2 changed files with 25 additions and 21 deletions

View File

@ -20,13 +20,16 @@ module Ransack
super(value, options)
end
def attribute_select(action = 'search', options = {}, html_options = {})
def attribute_select(options = nil, html_options = nil, action = nil)
options = options || {}
html_options = html_options || {}
action = action || 'search'
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(bases, action)
collection = attribute_collection_for_bases(action, bases)
template_grouped_collection_select(collection, options, html_options)
else
collection = collection_for_base(action, bases.first)
@ -41,7 +44,7 @@ module Ransack
end
def sort_select(options = {}, html_options = {})
attribute_select('sort', options, html_options) +
attribute_select(options, html_options, 'sort') +
sort_direction_select(options, html_options)
end
@ -168,25 +171,10 @@ module Ransack
end
end
def attribute_collection_for_bases(bases, action)
def attribute_collection_for_bases(action, bases)
bases.map { |base| get_attribute_element(action, base) }.compact
end
def attribute_collection_for_base(attributes, base = nil)
attributes.map do |c|
[attr_from_base_and_column(base, c),
Translate.attribute(
attr_from_base_and_column(base, c), :context => object.context
)
]
end
end
def collection_for_base(action, base)
attribute_collection_for_base(
object.context.send("#{action}able_attributes", base), base)
end
def get_attribute_element(action, base)
begin
[Translate.association(base, :context => object.context),
@ -196,6 +184,22 @@ module Ransack
end
end
def attribute_collection_for_base(attributes, base = nil)
attributes.map do |c|
[attr_from_base_and_column(base, c),
Translate.attribute(
attr_from_base_and_column(base, c),
:context => object.context
)
]
end
end
def collection_for_base(action, base)
attribute_collection_for_base(
object.context.send("#{action}able_attributes", base), base)
end
def attr_from_base_and_column(base, column)
[base, column].reject { |v| v.blank? }.join('_')
end

View File

@ -93,7 +93,7 @@ module Ransack
it 'returns ransackable attributes for associations with :associations' do
attributes = Person.ransackable_attributes + Article.
ransackable_attributes.map { |a| "articles_#{a}" }
html = @f.attribute_select('search', :associations => ['articles'])
html = @f.attribute_select(:associations => ['articles'])
html.split(/\n/).should have(attributes.size).lines
attributes.each do |attribute|
html.should match /<option value="#{attribute}">/
@ -101,7 +101,7 @@ module Ransack
end
it 'returns option groups for base and associations with :associations' do
html = @f.attribute_select('search', :associations => ['articles'])
html = @f.attribute_select(:associations => ['articles'])
[Person, Article].each do |model|
html.should match /<optgroup label="#{model}">/
end