Skip to content

Commit

Permalink
Merge pull request activeadmin#1480 from jpmckinney/const_defined
Browse files Browse the repository at this point in the history
Use const_defined in production to determine input_class
  • Loading branch information
gregbell committed Jul 7, 2012
2 parents 379bc72 + 2800376 commit ff62e86
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
4 changes: 4 additions & 0 deletions lib/active_admin/filters/forms.rb
Expand Up @@ -37,6 +37,10 @@ def default_input_type(method, options = {})
end

def custom_input_class_name(as)
"Filter#{as.to_s.camelize}Input"
end

def active_admin_input_class_name(as)
"ActiveAdmin::Inputs::Filter#{as.to_s.camelize}Input"
end

Expand Down
35 changes: 24 additions & 11 deletions lib/active_admin/form_builder.rb
Expand Up @@ -125,23 +125,36 @@ def active_admin_input_class_name(as)
"ActiveAdmin::Inputs::#{as.to_s.camelize}Input"
end

def input_class(as)
@input_classes_cache ||= {}
@input_classes_cache[as] ||= begin
# prevent exceptions in production environment for better performance
def input_class_with_const_defined(as)
input_class_name = custom_input_class_name(as)

if ::Object.const_defined?(input_class_name)
input_class_name.constantize
elsif ActiveAdmin::Inputs.const_defined?(input_class_name)
active_admin_input_class_name(as).constantize
elsif Formtastic::Inputs.const_defined?(input_class_name)
standard_input_class_name(as).constantize
else
raise Formtastic::UnknownInputError
end
end

# use auto-loading in development environment
def input_class_by_trying(as)
begin
begin
custom_input_class_name(as).constantize
rescue NameError
begin
custom_input_class_name(as).constantize
active_admin_input_class_name(as).constantize
rescue NameError
begin
active_admin_input_class_name(as).constantize
rescue NameError
standard_input_class_name(as).constantize
end
standard_input_class_name(as).constantize
end
rescue NameError
raise Formtastic::UnknownInputError
end
end
rescue NameError
raise Formtastic::UnknownInputError
end

private
Expand Down

0 comments on commit ff62e86

Please sign in to comment.