Merge pull request #204 from jhdavids8/separate_search_sort_attributes
Separate searchable and sortable ransackable attributes
This commit is contained in:
commit
3250d339be
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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 }
|
||||
|
||||
|
|
Loading…
Reference in New Issue