Add support to file method.
This commit is contained in:
parent
f1dd67469a
commit
b3076912f3
|
@ -35,4 +35,8 @@ module SimpleForm
|
|||
# How the label text should be generated altogether with the required text.
|
||||
mattr_accessor :label_text
|
||||
@@label_text = lambda { |label, required| "#{required} #{label}" }
|
||||
|
||||
# Collection of methods to detect if a file type was given.
|
||||
mattr_accessor :file_methods
|
||||
@@file_methods = [ :file?, :public_filename ]
|
||||
end
|
||||
|
|
|
@ -17,6 +17,7 @@ module SimpleForm
|
|||
map_type :select, :to => :collection_select, :options => true, :collection => true
|
||||
map_type :radio, :to => :collection_radio, :collection => true
|
||||
map_type :string, :to => :text_field
|
||||
map_type :file, :to => :file_field
|
||||
|
||||
# Numeric types
|
||||
map_type :integer, :float, :decimal, :to => :text_field
|
||||
|
|
|
@ -265,12 +265,24 @@ module SimpleForm
|
|||
when :timestamp
|
||||
:datetime
|
||||
when :string, nil
|
||||
@attribute.to_s =~ /password/ ? :password : :string
|
||||
match = case @attribute.to_s
|
||||
when /password/ then :password
|
||||
when /time_zone/ then :time_zone
|
||||
when /country/ then :country
|
||||
end
|
||||
|
||||
match || input_type || file_method? || :string
|
||||
else
|
||||
input_type
|
||||
end
|
||||
end
|
||||
|
||||
# Checks if attribute is a file_method.
|
||||
def file_method? #:nodoc:
|
||||
file = @object.send(@attribute) if @object.respond_to?(@attribute)
|
||||
:file if file && SimpleForm.file_methods.any? { |m| file.respond_to?(m) }
|
||||
end
|
||||
|
||||
# Finds the database column for the given attribute
|
||||
def find_attribute_column #:nodoc:
|
||||
@object.column_for_attribute(@attribute) if @object.respond_to?(:column_for_attribute)
|
||||
|
|
|
@ -78,6 +78,11 @@ class InputTest < ActionView::TestCase
|
|||
assert_select 'input#user_name[type=hidden]'
|
||||
end
|
||||
|
||||
test 'input should generate a file field' do
|
||||
with_input_for @user, :name, :file
|
||||
assert_select 'input#user_name[type=file]'
|
||||
end
|
||||
|
||||
test 'input should generate a datetime select by default for datetime attributes' do
|
||||
with_input_for @user, :created_at, :datetime
|
||||
1.upto(5) do |i|
|
||||
|
|
|
@ -88,6 +88,15 @@ class FormBuilderTest < ActionView::TestCase
|
|||
assert_select 'form select#user_updated_at_1i.datetime'
|
||||
end
|
||||
|
||||
test 'builder should generate file for file columns' do
|
||||
@user.avatar = mock("file")
|
||||
@user.avatar.expects(:respond_to?).with(:file?).returns(false)
|
||||
@user.avatar.expects(:respond_to?).with(:public_filename).returns(true)
|
||||
|
||||
with_form_for @user, :avatar
|
||||
assert_select 'form input#user_avatar.file'
|
||||
end
|
||||
|
||||
test 'build should generate select if a collection is given' do
|
||||
with_form_for @user, :age, :collection => 1..60
|
||||
assert_select 'form select#user_age.select'
|
||||
|
|
Loading…
Reference in New Issue