lib/ransack/helpers/form_builder.rb in ransack-1.0.0 vs lib/ransack/helpers/form_builder.rb in ransack-1.1.0

- old
+ new

@@ -21,20 +21,15 @@ raise ArgumentError, "attribute_select must be called inside a search FormBuilder!" unless object.respond_to?(:context) options[:include_blank] = true unless options.has_key?(:include_blank) bases = [''] + association_array(options[:associations]) if bases.size > 1 @template.grouped_collection_select( - @object_name, :name, attribute_collection_for_bases(bases), :last, :first, :first, :last, + @object_name, :name, searchable_attribute_collection_for_bases(bases), :last, :first, :first, :last, objectify_options(options), @default_options.merge(html_options) ) else - collection = object.context.searchable_attributes(bases.first).map do |c| - [ - attr_from_base_and_column(bases.first, c), - Translate.attribute(attr_from_base_and_column(bases.first, c), :context => object.context) - ] - end + collection = searchable_attribute_collection_for_base(bases.first) @template.collection_select( @object_name, :name, collection, :first, :last, objectify_options(options), @default_options.merge(html_options) ) end @@ -44,23 +39,18 @@ raise ArgumentError, "sort_select must be called inside a search FormBuilder!" unless object.respond_to?(:context) options[:include_blank] = true unless options.has_key?(:include_blank) bases = [''] + association_array(options[:associations]) if bases.size > 1 @template.grouped_collection_select( - @object_name, :name, attribute_collection_for_bases(bases), :last, :first, :first, :last, + @object_name, :name, sortable_attribute_collection_for_bases(bases), :last, :first, :first, :last, objectify_options(options), @default_options.merge(html_options) ) + @template.collection_select( @object_name, :dir, [['asc', object.translate('asc')], ['desc', object.translate('desc')]], :first, :last, objectify_options(options), @default_options.merge(html_options) ) else - collection = object.context.searchable_attributes(bases.first).map do |c| - [ - attr_from_base_and_column(bases.first, c), - Translate.attribute(attr_from_base_and_column(bases.first, c), :context => object.context) - ] - end + collection = sortable_attribute_collection_for_base(bases.first) @template.collection_select( @object_name, :name, collection, :first, :last, objectify_options(options), @default_options.merge(html_options) ) + @template.collection_select( @object_name, :dir, [['asc', object.translate('asc')], ['desc', object.translate('desc')]], :first, :last, @@ -169,26 +159,50 @@ def attr_from_base_and_column(base, column) [base, column].reject {|v| v.blank?}.join('_') end - def attribute_collection_for_bases(bases) + 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 sortable_attribute_collection_for_base(base=nil) + attribute_collection_for_base(object.context.sortable_attributes(base), base) + end + + def searchable_attribute_collection_for_base(base=nil) + attribute_collection_for_base(object.context.searchable_attributes(base), base) + end + + def sortable_attribute_collection_for_bases(bases) bases.map do |base| begin [ Translate.association(base, :context => object.context), - object.context.searchable_attributes(base).map do |c| - [ - attr_from_base_and_column(base, c), - Translate.attribute(attr_from_base_and_column(base, c), :context => object.context) - ] - end + sortable_attribute_collection_for_base(base) ] rescue UntraversableAssociationError => e nil end end.compact end + def searchable_attribute_collection_for_bases(bases) + bases.map do |base| + begin + [ + Translate.association(base, :context => object.context), + searchable_attribute_collection_for_base(base) + ] + rescue UntraversableAssociationError => e + nil + end + end.compact + end end end end \ No newline at end of file