Permalink
Browse files

Merge pull request #783 from SaschaKonietzke/prevent-input-class-exce…

…ptions

Performance Improvement #1: Don't raise exceptions (#744)
  • Loading branch information...
2 parents 65ccfbd + d408b9e commit 9a3e287c91fd5a24dd5025e91c360da5b1d4bced @justinfrench committed Feb 6, 2012
Showing with 26 additions and 10 deletions.
  1. +26 −10 lib/formtastic/helpers/input_helper.rb
@@ -330,20 +330,36 @@ def column_for(method) #:nodoc:
#
# @example When a top-level class is found
# input_class(:string) #=> StringInput
- # input_class(:awesome) #=> AwesomeInput
+ # input_class(:awesome) #=> AwesomeInput
def input_class(as)
@input_classes_cache ||= {}
@input_classes_cache[as] ||= begin
- begin
- begin
- custom_input_class_name(as).constantize
- rescue NameError
- standard_input_class_name(as).constantize
- end
- rescue NameError
- raise Formtastic::UnknownInputError
- end
+ Rails.application.config.cache_classes ? input_class_with_const_defined(as) : input_class_by_trying(as)
+ end
+ end
+
+ # 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 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
+ custom_input_class_name(as).constantize
+ rescue NameError
+ standard_input_class_name(as).constantize
end
+ rescue NameError
+ raise Formtastic::UnknownInputError
end
# :as => :string # => StringInput

0 comments on commit 9a3e287

Please sign in to comment.