Last active
August 29, 2015 14:17
-
-
Save rubyrider/211c74d8851b7e9275a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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