Skip to content

Commit

Permalink
Fix input type detection for CarrierWave
Browse files Browse the repository at this point in the history
See the added comment for why it needs to come first.
  • Loading branch information
betelgeuse committed Jul 27, 2011
1 parent 65e8bc2 commit 88a3114
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
16 changes: 9 additions & 7 deletions lib/formtastic/helpers/input_helper.rb
Expand Up @@ -267,6 +267,9 @@ def input(method, options = {})

protected

# First try if we can detect special things like :file. With CarrierWave the method does have
# an underlying column so we don't want :string to get selected.
#
# For methods that have a database column, take a best guess as to what the input method
# should be. In most cases, it will just return the column type (eg :string), but for special
# cases it will simplify (like the case of :integer, :float & :decimal to :number), or do
Expand All @@ -275,6 +278,12 @@ def input(method, options = {})
# If there is no column for the method (eg "virtual columns" with an attr_accessor), the
# default is a :string, a similar behaviour to Rails' scaffolding.
def default_input_type(method, options = {}) #:nodoc:
if @object
return :select if reflection_for(method)

return :file if is_file?(method, options)
end

if column = column_for(method)
# Special cases where the column type doesn't map to an input method.
case column.type
Expand All @@ -300,12 +309,6 @@ def default_input_type(method, options = {}) #:nodoc:
# Try 3: Assume the input name will be the same as the column type (e.g. string_input).
return column.type
else
if @object
return :select if reflection_for(method)

return :file if is_file?(method, options)
end

return :select if options.key?(:collection)
return :password if method.to_s =~ /password/
return :string
Expand Down Expand Up @@ -363,4 +366,3 @@ def standard_input_class_name(as)
end
end
end

1 change: 0 additions & 1 deletion spec/helpers/input_helper_spec.rb
Expand Up @@ -412,7 +412,6 @@ def length_should_be_required(options)
describe 'defaulting to file column' do
Formtastic::FormBuilder.file_methods.each do |method|
it "should default to :file for attributes that respond to ##{method}" do
@new_post.stub!(:column_for_attribute).and_return(nil)
column = mock('column')

Formtastic::FormBuilder.file_methods.each do |test|
Expand Down

0 comments on commit 88a3114

Please sign in to comment.