Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubyrider/211c74d8851b7e9275a8 to your computer and use it in GitHub Desktop.
Save rubyrider/211c74d8851b7e9275a8 to your computer and use it in GitHub Desktop.
module ActiveAdmin
module Filters
# This form builder defines methods to build filter forms such
# as the one found in the sidebar of the index page of a standard resource.
class FormBuilder < ::ActiveAdmin::FormBuilder
include ::ActiveAdmin::Filters::FormtasticAddons
self.input_namespaces = [::Object, ::ActiveAdmin::Inputs::Filters, ::ActiveAdmin::Inputs, ::Formtastic::Inputs]
# TODO: remove input class finders after formtastic 4 (where it will be default)
self.input_class_finder = ::Formtastic::InputClassFinder
def filter(method, options = {})
if method.present? && options[:as] ||= default_input_type(method)
if __klass = self.object.klass
__reflections = __klass.reflections.keys
if __reflections.include?(method)
__assoc_type = __klass.reflect_on_association(method).try(:macro)
if __assoc_type && __assoc_type == :belongs_to
if method.to_s.classify.safe_constantize
_collection_class = method.to_s.classify.safe_constantize
options[:collection] ||= _collection_class.send(:all).map {|c| [c.id, c.display_name]}
end
method = __klass.reflections[method].foreign_key
end
end
end
template.concat input(method, options)
end
end
protected
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment