Initial support to simple_fields_for and simple_remote_form_for

This commit is contained in:
Carlos Antonio da Silva 2009-12-10 08:44:34 -02:00
parent 02f716717a
commit aabeacf40b
6 changed files with 67 additions and 13 deletions

View File

@ -1,6 +1,5 @@
== General
* Support simple_fields_for and simple_remote_form_for
* Allow I18n lookup for labels and hints based on the current action
* Sample CSS
* Test forms with non objects
@ -21,4 +20,4 @@
* Add automatic :required check
* Add unobstrusive javascript validations support
* Add mask support (same as hint, but with JS)
* Add mask support (same as hint, but with JS)

View File

@ -12,8 +12,14 @@ module SimpleForm
label("#{attribute}_#{value}", text, :class => "radio")
end
end
def simple_fields_for(*args, &block)
options = args.extract_options!
options[:builder] = SimpleForm::FormBuilder
fields_for(*(args << options), &block)
end
end
end
end
ActionView::Helpers::FormBuilder.send :include, SimpleForm::ActionViewExtensions::Builder
ActionView::Helpers::FormBuilder.send :include, SimpleForm::ActionViewExtensions::Builder

View File

@ -2,12 +2,26 @@ module SimpleForm
module ActionViewExtensions
module FormHelper
def simple_form_for(*args, &block)
options = args.extract_options!
options[:builder] = SimpleForm::FormBuilder
form_for(*(args << options), &block)
build_simple_form(:form_for, *args, &block)
end
def simple_fields_for(*args, &block)
build_simple_form(:fields_for, *args, &block)
end
def simple_remote_form_for(*args, &block)
build_simple_form(:remote_form_for, *args, &block)
end
private
def build_simple_form(form_method, *args, &block)
options = args.extract_options!
options[:builder] = SimpleForm::FormBuilder
send(form_method, *(args << options), &block)
end
end
end
end
ActionView::Base.send :include, SimpleForm::ActionViewExtensions::FormHelper
ActionView::Base.send :include, SimpleForm::ActionViewExtensions::FormHelper

View File

@ -1,7 +1,7 @@
require 'test_helper'
class BuilderTest < ActionView::TestCase
test "collection radio accepts a collection and generate inputs from value method" do
test 'collection radio accepts a collection and generate inputs from value method' do
form_for @user do |f|
concat f.collection_radio :active, [true, false], :to_s, :to_s
end
@ -13,7 +13,7 @@ class BuilderTest < ActionView::TestCase
assert_select 'form label[for=user_active_false]', 'false'
end
test "collection radio accepts a collection and generate inputs from label method" do
test 'collection radio accepts a collection and generate inputs from label method' do
form_for @user do |f|
concat f.collection_radio :active, [true, false], :to_s, :to_s
end
@ -22,12 +22,20 @@ class BuilderTest < ActionView::TestCase
assert_select 'form label[for=user_active_false]', 'false'
end
test "collection radio accepts html options as input" do
test 'collection radio accepts html options as input' do
form_for @user do |f|
concat f.collection_radio :active, [[1, true], [0, false]], :last, :first, :class => "radio"
concat f.collection_radio :active, [[1, true], [0, false]], :last, :first, :class => 'radio'
end
assert_select 'form input[type=radio][value=true].radio#user_active_true'
assert_select 'form input[type=radio][value=false].radio#user_active_false'
end
test 'simple fields for is available and yields an instance of FormBuilder' do
form_for @user do |f|
f.simple_fields_for :posts do |posts_form|
assert posts_form.instance_of?(SimpleForm::FormBuilder)
end
end
end
end

View File

@ -2,7 +2,7 @@ require 'test_helper'
class FormHelperTest < ActionView::TestCase
test 'yields an instance of FormBuilder' do
test 'simple form for yields an instance of FormBuilder' do
simple_form_for :user do |f|
assert f.instance_of?(SimpleForm::FormBuilder)
end
@ -13,4 +13,23 @@ class FormHelperTest < ActionView::TestCase
assert_select 'form#my_form'
assert_select 'form[action=/account]'
end
test 'fields for yields an instance of FormBuilder' do
simple_fields_for :user do |f|
assert f.instance_of?(SimpleForm::FormBuilder)
end
end
test 'remote form for yields an instance of FormBuilder' do
simple_remote_form_for :user do |f|
assert f.instance_of?(SimpleForm::FormBuilder)
end
assert_select "form[onsubmit]"
end
test 'pass options to simple remote form' do
simple_remote_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

@ -136,5 +136,13 @@ class FormBuilderTest < ActionView::TestCase
assert_select 'form p input#user_name.string'
end
end
test 'nested simple fields should yields an instance of FormBuilder' do
simple_form_for :user do |f|
f.simple_fields_for :posts do |posts_form|
assert posts_form.instance_of?(SimpleForm::FormBuilder)
end
end
end
end