lib/formtastic/helpers/input_helper.rb in formtastic-3.1.5 vs lib/formtastic/helpers/input_helper.rb in formtastic-4.0.0.rc1

- old
+ new

@@ -34,13 +34,10 @@ # # @see #input # @see Formtastic::Helpers::InputsHelper#inputs # @see Formtastic::Helpers::FormHelper#semantic_form_for module InputHelper - INPUT_CLASS_DEPRECATION = 'configure Formtastic::FormBuilder.input_class_finder instead (upgrade guide on wiki: http://bit.ly/1F9QtKc )'.freeze - private_constant(:INPUT_CLASS_DEPRECATION) - include Formtastic::Helpers::Reflection include Formtastic::Helpers::Enum include Formtastic::Helpers::FileColumnDetection # Returns a chunk of HTML markup for a given `method` on the form object, wrapped in @@ -235,11 +232,11 @@ def input(method, options = {}) method = method.to_sym options = options.dup # Allow options to be shared without being tainted by Formtastic options[:as] ||= default_input_type(method, options) - klass = input_class(options[:as]) + klass = namespaced_input_class(options[:as]) klass.new(self, template, @object, @object_name, method, options).to_html end protected @@ -284,10 +281,14 @@ return :datetime_select when :time return :time_select when :date return :date_select + when :hstore, :json, :jsonb + return :text + when :citext, :inet + return :string end # Try look for hints in options hash. Quite common senario: Enum keys stored as string in the database. return :select if column.type == :string && options.key?(:collection) # Try 3: Assume the input name will be the same as the column type (e.g. string_input). @@ -298,16 +299,24 @@ return :string end end # Get a column object for a specified attribute method - if possible. + # @return [ActiveModel::Type::Value, #type] in case of rails 5 attributes api + # @return [ActiveRecord::ConnectionAdapters::Column] in case of rails 4 def column_for(method) # @private - if @object.respond_to?(:column_for_attribute) - # Remove deprecation wrapper & review after Rails 5.0 ships - ActiveSupport::Deprecation.silence do - @object.column_for_attribute(method) - end + case + when @object.class.respond_to?(:type_for_attribute) + @object.class.type_for_attribute(method.to_s) + when @object.class.respond_to?(:column_for_attribute) + @object.class.column_for_attribute(method) + when @object.respond_to?(:column_for_attribute) + # Remove deprecation wrapper & review after Rails 5.0 ships + ActiveSupport::Deprecation.silence do + @object.column_for_attribute(method) + end + else nil end end # Takes the `:as` option and attempts to return the corresponding input # class. In the case of `:as => :awesome` it will first attempt to find a @@ -333,75 +342,8 @@ @input_class_finder ||= input_class_finder.new(self) @input_class_finder.find(as) rescue Formtastic::InputClassFinder::NotFoundError raise Formtastic::UnknownInputError, "Unable to find input #{$!.message}" end - - # @api private - # @deprecated Use {#namespaced_input_class} instead. - def input_class(as) - return namespaced_input_class(as) if input_class_finder - - input_class_deprecation_warning(__method__) - - @input_classes_cache ||= {} - @input_classes_cache[as] ||= begin - config = Rails.application.config - use_const_defined = config.respond_to?(:eager_load) ? config.eager_load : config.cache_classes - use_const_defined ? input_class_with_const_defined(as) : input_class_by_trying(as) - end - end - - # @api private - # @deprecated Use {InputClassFinder#find} instead. - # 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, "Unable to find input class #{input_class_name}" - end - end - - # @api private - # @deprecated Use {InputClassFinder#find} instead. - # 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, "Unable to find input class for #{as}" - end - - # @api private - # @deprecated Use {InputClassFinder#class_name} instead. - # :as => :string # => StringInput - def custom_input_class_name(as) - input_class_deprecation_warning(__method__) - "#{as.to_s.camelize}Input" - end - - # @api private - # @deprecated Use {InputClassFinder#class_name} instead. - # :as => :string # => {Formtastic::Inputs::StringInput} - def standard_input_class_name(as) - input_class_deprecation_warning(__method__) - "Formtastic::Inputs::#{as.to_s.camelize}Input" - end - - private - - def input_class_deprecation_warning(method) - @input_class_deprecation_warned ||= - Formtastic.deprecation.deprecation_warning(method, INPUT_CLASS_DEPRECATION, caller(2)) - end - end end end