diff --git a/lib/ransack/adapters/active_record/base.rb b/lib/ransack/adapters/active_record/base.rb index 5998892..51d814b 100644 --- a/lib/ransack/adapters/active_record/base.rb +++ b/lib/ransack/adapters/active_record/base.rb @@ -23,6 +23,11 @@ module Ransack column_names + _ransackers.keys end + def ransortable_attributes(auth_object = nil) + # Here so users can overwrite the attributes that show up in the sort_select + ransackable_attributes(auth_object) + end + def ransackable_associations(auth_object = nil) reflect_on_all_associations.map {|a| a.name.to_s} end diff --git a/lib/ransack/context.rb b/lib/ransack/context.rb index f46d7b7..5ec1ef6 100644 --- a/lib/ransack/context.rb +++ b/lib/ransack/context.rb @@ -131,6 +131,10 @@ module Ransack traverse(str).ransackable_attributes(auth_object) end + def sortable_attributes(str = '') + traverse(str).ransortable_attributes(auth_object) + end + def searchable_associations(str = '') traverse(str).ransackable_associations(auth_object) end diff --git a/lib/ransack/helpers/form_builder.rb b/lib/ransack/helpers/form_builder.rb index f91dc8b..5b7b3eb 100644 --- a/lib/ransack/helpers/form_builder.rb +++ b/lib/ransack/helpers/form_builder.rb @@ -23,16 +23,11 @@ module Ransack 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) @@ -46,19 +41,14 @@ module Ransack 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) @@ -171,17 +161,29 @@ module Ransack [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 @@ -189,6 +191,18 @@ module Ransack 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 diff --git a/spec/ransack/adapters/active_record/base_spec.rb b/spec/ransack/adapters/active_record/base_spec.rb index 88ddfd8..a827984 100644 --- a/spec/ransack/adapters/active_record/base_spec.rb +++ b/spec/ransack/adapters/active_record/base_spec.rb @@ -58,6 +58,14 @@ module Ransack it { should include 'doubled_name' } end + describe '#ransortable_attributes' do + subject { Person.ransortable_attributes } + + it { should include 'name' } + it { should include 'reversed_name' } + it { should include 'doubled_name' } + end + describe '#ransackable_associations' do subject { Person.ransackable_associations }