Work in progress with form.input.

This commit is contained in:
Carlos Antonio da Silva 2009-11-19 19:26:16 -02:00
parent 27f050b949
commit 4509ec9202
5 changed files with 226 additions and 60 deletions

View File

@ -1,4 +1,36 @@
module SimpleForm
class FormBuilder < ActionView::Helpers::FormBuilder
def input(attribute, options={})
input_type = (options.delete(:as) || default_input_type(attribute)).to_sym
html_options = options.delete(:html) || {}
html_options[:class] = "#{html_options[:class]} #{input_type}".strip
case input_type
when :boolean then check_box(attribute, html_options)
when :radio then
['yes', 'no'].inject('') do |result, value|
result << radio_button(attribute, value, html_options)
end
when :text then text_area(attribute, html_options)
when :datetime, :timestamp then
datetime_select(attribute, options, html_options)
when :date then
date_select(attribute, options, html_options)
when :time then
time_select(attribute, options, html_options)
else text_field(attribute, html_options)
end
end
private
def default_input_type(attribute)
input_type = @object.try(:column_for_attribute, attribute)
case input_type
when nil then :string
when :timestamp then :datetime
else input_type
end
end
end
end

143
test/form_builder_test.rb Normal file
View File

@ -0,0 +1,143 @@
require 'test_helper'
require 'ostruct'
class User < OpenStruct
def id
1
end
def new_record?
false
end
def column_for_attribute(attribute)
case attribute.to_sym
when :name, :status then :string
when :description then :text
when :age then :integer
when :credit_limit then :decimal
when :active then :boolean
when :born_at then :date
when :delivery_time then :time
when :created_at then :datetime
when :updated_at then :timestamp
end
end
end
class FormHelperTest < ActionView::TestCase
tests SimpleForm::FormBuilder
def user_path(*args)
'/users'
end
def setup
@user = User.new(
:name => 'New in Simple Form!',
:description => 'Hello!',
:created_at => Time.now
)
end
test 'input should generate a default text field' do
simple_form_for @user do |f|
concat f.input :name
concat f.input :description
end
assert_select 'form input[name=\'user[name]\'][id=user_name][value=New in Simple Form!]'
assert_select 'form textarea[name=\'user[description]\'][id=user_description]', 'Hello!'
end
test 'input should generate a default class for each input' do
simple_form_for @user do |f|
concat f.input :name
end
assert_select 'form input#user_name.string'
end
test 'input should allow passing options to text field' do
simple_form_for @user do |f|
concat f.input :name, :html => { :class => 'my_input', :id => 'my_input' }
end
assert_select 'form input#my_input.my_input.string'
end
test 'input should generate a text area by default for text attributes' do
simple_form_for @user do |f|
concat f.input :description
end
assert_select 'form textarea.text#user_description'
end
test 'input should generate a text field by default for integer attributes' do
simple_form_for @user do |f|
concat f.input :age
end
assert_select "form input.integer#user_age"
end
test 'input should generate a text field by default for decimal attributes' do
simple_form_for @user do |f|
concat f.input :credit_limit
end
assert_select "form input.decimal#user_credit_limit"
end
test 'input should generate a checkbox by default for boolean attributes' do
simple_form_for @user do |f|
concat f.input :active
end
assert_select "form input[type=checkbox].boolean#user_active"
end
test 'input should generate a datetime select by default for datetime or timestamp attributes' do
simple_form_for @user do |f|
concat f.input :created_at
concat f.input :updated_at
end
1.upto(5) do |i|
assert_select "form select.datetime#user_created_at_#{i}i"
assert_select "form select.datetime#user_updated_at_#{i}i"
end
end
test 'input should generate a date select by default for date attributes' do
simple_form_for @user do |f|
concat f.input :born_at
end
assert_select "form select.date#user_born_at_1i"
assert_select "form select.date#user_born_at_2i"
assert_select "form select.date#user_born_at_3i"
assert_no_tag "select", :attributes => { :id => "user_born_at_4i" }
end
test 'input should generate a time select by default for time attributes' do
simple_form_for @user do |f|
concat f.input :delivery_time
end
assert_select "form input[type=hidden]#user_delivery_time_1i"
assert_select "form input[type=hidden]#user_delivery_time_2i"
assert_select "form input[type=hidden]#user_delivery_time_3i"
assert_select "form select.time#user_delivery_time_4i"
assert_select "form select.time#user_delivery_time_5i"
end
test 'input should allow overwriting default type' do
simple_form_for @user do |f|
concat f.input :name, :as => :text
concat f.input :born_at, :as => :string
end
assert_select 'form textarea.text#user_name'
assert_select 'form input.string#user_born_at'
end
test 'it should allow boolean fields as radio buttons' do
simple_form_for @user do |f|
concat f.input :active, :as => :radio
end
assert_select 'form input[type=radio][value=yes].radio#user_active_yes'
assert_select 'form input[type=radio][value=no].radio#user_active_no'
end
end

17
test/form_helper_test.rb Normal file
View File

@ -0,0 +1,17 @@
require 'test_helper'
class FormHelperTest < ActionView::TestCase
tests SimpleForm::FormHelper
test 'yields an instance of FormBuilder' do
simple_form_for :user do |f|
assert f.instance_of?(SimpleForm::FormBuilder)
end
end
test 'pass options to simple form' do
simple_form_for :user, :url => '/account', :html => { :id => 'my_form' } do |f| end
assert_select 'form#my_form'
assert_select 'form[action=/account]'
end
end

View File

@ -1,48 +0,0 @@
require 'test_helper'
class MockController
def url_for(*args)
"http://example.com"
end
end
class MockResponse
def initialize(test_case)
@test_case = test_case
end
def content_type
'text/html'
end
def body
@test_case.send :output_buffer
end
end
class SimpleFormTest < ActionView::TestCase
tests SimpleForm::FormHelper
def protect_against_forgery?
false
end
def setup
@controller = MockController.new
@response = MockResponse.new(self)
end
test 'yields an instance of FormBuilder' do
simple_form_for :product do |f|
assert f.instance_of?(SimpleForm::FormBuilder)
end
end
test 'pass options to simple form' do
simple_form_for :product, :url => '/products', :html => { :id => 'my_form' } do |f| end
assert_select 'form#my_form'
assert_select 'form[action=/products]'
end
end

View File

@ -1,4 +1,3 @@
#ENV["RAILS_ENV"] = "test"
require 'rubygems'
require 'test/unit'
@ -7,18 +6,41 @@ require 'action_view/test_case'
require File.join(File.dirname(__FILE__), '..', 'lib', 'simple_form')
#Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
class MockController
#ActionController::Base.logger = nil
#ActionController::Routing::Routes.reload rescue nil
#ActionController::Base.session_store = nil
def url_for(*args)
"http://example.com"
end
end
#FIXTURE_LOAD_PATH = File.join(File.dirname(__FILE__), 'fixtures')
#ActionView::Base.cache_template_loading = true
#ActionController::Base.view_paths = FIXTURE_LOAD_PATH
class MockResponse
def initialize(test_case)
@test_case = test_case
end
#class ActiveSupport::TestCase
# self.use_transactional_fixtures = true
# self.use_instantiated_fixtures = false
#end
def content_type
'text/html'
end
def body
@test_case.send :output_buffer
end
end
class ActionView::TestCase
setup :set_controller
setup :set_response
def set_controller
@controller = MockController.new
end
def set_response
@response = MockResponse.new(self)
end
def protect_against_forgery?
false
end
end