diff --git a/lib/ransack/helpers/form_builder.rb b/lib/ransack/helpers/form_builder.rb
index 3351281..18aa79f 100644
--- a/lib/ransack/helpers/form_builder.rb
+++ b/lib/ransack/helpers/form_builder.rb
@@ -3,42 +3,37 @@ require 'action_view'
module Ransack
module Helpers
class FormBuilder < ::ActionView::Helpers::FormBuilder
+
def label(method, *args, &block)
options = args.extract_options!
text = args.first
i18n = options[:i18n] || {}
- text ||= object.translate(method, i18n.reverse_merge(include_associations: true)
+ text ||= object.translate(
+ method, i18n.reverse_merge(include_associations: true)
) if object.respond_to? :translate
super(method, text, options, &block)
end
- def submit(value=nil, options={})
+ def submit(value = nil, options = {})
value, options = nil, value if value.is_a?(Hash)
value ||= Translate.word(:search).titleize
super(value, options)
end
- def attribute_select(options = {}, html_options = {})
- raise ArgumentError,
- "attribute_select must be called inside a search FormBuilder!" unless object.
- respond_to?(:context)
+ def attribute_select(action = 'search', options = {}, html_options = {})
+ raise ArgumentError, formbuilder_error_message(action) 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),
+ attribute_collection_for_bases(bases, action),
: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 = collection_for_base(action, bases.first)
@template.collection_select(
@object_name, :name,
collection,
@@ -49,45 +44,23 @@ module Ransack
end
def sort_select(options = {}, html_options = {})
+ attribute_select('sort', options, html_options) +
+ sort_direction_select(options, html_options)
+ end
+
+ def sort_direction_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])
- if bases.size > 1
- @template.grouped_collection_select(
- @object_name, :name,
- 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)
+ "sort_direction must be called inside a search FormBuilder!" unless
+ object.respond_to?(:context)
+ @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
- @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,
- objectify_options(options), @default_options.merge(html_options)
- )
- end
end
def sort_fields(*args, &block)
@@ -138,8 +111,8 @@ module Ransack
def predicate_select(options = {}, html_options = {})
options[:compounds] = true if options[:compounds].nil?
- keys = options[:compounds] ? Predicate.names : Predicate.names.
- reject { |k| k.match(/_(any|all)$/) }
+ keys = options[:compounds] ? Predicate.names :
+ Predicate.names.reject { |k| k.match(/_(any|all)$/) }
if only = options[:only]
if only.respond_to? :call
keys = keys.select { |k| only.call(k) }
@@ -148,7 +121,6 @@ module Ransack
keys = keys.select { |k| only.include? k.sub(/_(any|all)$/, '') }
end
end
-
@template.collection_select(
@object_name, :p,
keys.map { |k| [k, Translate.predicate(k)] },
@@ -191,31 +163,53 @@ module Ransack
end
else
[obj]
- end).
- compact.flat_map { |v| [prefix, v].compact.join('_') }
+ end)
+ .compact
+ .flat_map { |v| [prefix, v].compact.join('_') }
end
def attr_from_base_and_column(base, column)
- [base, column].reject {|v| v.blank?}.join('_')
+ [base, column]
+ .reject { |v| v.blank? }
+ .join('_')
end
- def attribute_collection_for_bases(bases)
- bases.map do |base|
- begin
+ def collection_for_base(action, base)
+ attribute_collection_for_base(
+ object.context.send("#{action}able_attributes", base), base
+ )
+ end
+
+ def attribute_collection_for_base(attributes, base = nil)
+ attributes.map do |c|
[
- 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
+ attr_from_base_and_column(base, c),
+ Translate.attribute(
+ attr_from_base_and_column(base, c), context: object.context
+ )
]
- rescue UntraversableAssociationError => e
- nil
+ end
+ end
+
+ def attribute_collection_for_bases(bases, action)
+ (
+ bases.map do |base|
+ begin
+ [
+ Translate.association(base, context: object.context),
+ collection_for_base(action, base)
+ ]
+ rescue UntraversableAssociationError => e
+ nil
+ end
end
- end.compact
+ )
+ .compact
+ end
+
+ def formbuilder_error_message(action)
+ "#{action == 'search' ? 'attribute' : action
+ }_select must be called inside a search FormBuilder!"
end
end
diff --git a/spec/ransack/helpers/form_builder_spec.rb b/spec/ransack/helpers/form_builder_spec.rb
index 34f33ab..c4a10ff 100644
--- a/spec/ransack/helpers/form_builder_spec.rb
+++ b/spec/ransack/helpers/form_builder_spec.rb
@@ -89,7 +89,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 associations: ['articles']
+ html = @f.attribute_select('search', associations: ['articles'])
html.split(/\n/).should have(attributes.size).lines
attributes.each do |attribute|
html.should match /