diff --git a/lib/ransack/helpers/form_builder.rb b/lib/ransack/helpers/form_builder.rb index 5b7b3eb..702b8ea 100644 --- a/lib/ransack/helpers/form_builder.rb +++ b/lib/ransack/helpers/form_builder.rb @@ -34,8 +34,13 @@ module Ransack ) end end - + def sort_select(options = {}, html_options = {}) + sort_attribute_select(options, html_options) + + sort_direction_select(options, html_options) + end + + def sort_attribute_select(options = {}, html_options = {}) 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]) @@ -43,16 +48,27 @@ module Ransack @template.grouped_collection_select( @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 = 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( + ) + end + end + + def sort_direction_select(options = {}, html_options = {}) + raise ArgumentError, "sort_direction must be called inside a search FormBuilder!" unless object.respond_to?(:context) + bases = [''] + association_array(options[:associations]) + if bases.size > 1 + @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 = sortable_attribute_collection_for_base(bases.first) + @template.collection_select( @object_name, :dir, [['asc', object.translate('asc')], ['desc', object.translate('desc')]], :first, :last, objectify_options(options), @default_options.merge(html_options) )