Initial support to simple_fields_for and simple_remote_form_for
This commit is contained in:
parent
02f716717a
commit
aabeacf40b
|
@ -1,6 +1,5 @@
|
||||||
== General
|
== General
|
||||||
|
|
||||||
* Support simple_fields_for and simple_remote_form_for
|
|
||||||
* Allow I18n lookup for labels and hints based on the current action
|
* Allow I18n lookup for labels and hints based on the current action
|
||||||
* Sample CSS
|
* Sample CSS
|
||||||
* Test forms with non objects
|
* Test forms with non objects
|
||||||
|
@ -21,4 +20,4 @@
|
||||||
|
|
||||||
* Add automatic :required check
|
* Add automatic :required check
|
||||||
* Add unobstrusive javascript validations support
|
* Add unobstrusive javascript validations support
|
||||||
* Add mask support (same as hint, but with JS)
|
* Add mask support (same as hint, but with JS)
|
||||||
|
|
|
@ -12,8 +12,14 @@ module SimpleForm
|
||||||
label("#{attribute}_#{value}", text, :class => "radio")
|
label("#{attribute}_#{value}", text, :class => "radio")
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
ActionView::Helpers::FormBuilder.send :include, SimpleForm::ActionViewExtensions::Builder
|
ActionView::Helpers::FormBuilder.send :include, SimpleForm::ActionViewExtensions::Builder
|
||||||
|
|
|
@ -2,12 +2,26 @@ module SimpleForm
|
||||||
module ActionViewExtensions
|
module ActionViewExtensions
|
||||||
module FormHelper
|
module FormHelper
|
||||||
def simple_form_for(*args, &block)
|
def simple_form_for(*args, &block)
|
||||||
options = args.extract_options!
|
build_simple_form(:form_for, *args, &block)
|
||||||
options[:builder] = SimpleForm::FormBuilder
|
|
||||||
form_for(*(args << options), &block)
|
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
ActionView::Base.send :include, SimpleForm::ActionViewExtensions::FormHelper
|
ActionView::Base.send :include, SimpleForm::ActionViewExtensions::FormHelper
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
|
||||||
class BuilderTest < ActionView::TestCase
|
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|
|
form_for @user do |f|
|
||||||
concat f.collection_radio :active, [true, false], :to_s, :to_s
|
concat f.collection_radio :active, [true, false], :to_s, :to_s
|
||||||
end
|
end
|
||||||
|
@ -13,7 +13,7 @@ class BuilderTest < ActionView::TestCase
|
||||||
assert_select 'form label[for=user_active_false]', 'false'
|
assert_select 'form label[for=user_active_false]', 'false'
|
||||||
end
|
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|
|
form_for @user do |f|
|
||||||
concat f.collection_radio :active, [true, false], :to_s, :to_s
|
concat f.collection_radio :active, [true, false], :to_s, :to_s
|
||||||
end
|
end
|
||||||
|
@ -22,12 +22,20 @@ class BuilderTest < ActionView::TestCase
|
||||||
assert_select 'form label[for=user_active_false]', 'false'
|
assert_select 'form label[for=user_active_false]', 'false'
|
||||||
end
|
end
|
||||||
|
|
||||||
test "collection radio accepts html options as input" do
|
test 'collection radio accepts html options as input' do
|
||||||
form_for @user do |f|
|
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
|
end
|
||||||
|
|
||||||
assert_select 'form input[type=radio][value=true].radio#user_active_true'
|
assert_select 'form input[type=radio][value=true].radio#user_active_true'
|
||||||
assert_select 'form input[type=radio][value=false].radio#user_active_false'
|
assert_select 'form input[type=radio][value=false].radio#user_active_false'
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -2,7 +2,7 @@ require 'test_helper'
|
||||||
|
|
||||||
class FormHelperTest < ActionView::TestCase
|
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|
|
simple_form_for :user do |f|
|
||||||
assert f.instance_of?(SimpleForm::FormBuilder)
|
assert f.instance_of?(SimpleForm::FormBuilder)
|
||||||
end
|
end
|
||||||
|
@ -13,4 +13,23 @@ class FormHelperTest < ActionView::TestCase
|
||||||
assert_select 'form#my_form'
|
assert_select 'form#my_form'
|
||||||
assert_select 'form[action=/account]'
|
assert_select 'form[action=/account]'
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -136,5 +136,13 @@ class FormBuilderTest < ActionView::TestCase
|
||||||
assert_select 'form p input#user_name.string'
|
assert_select 'form p input#user_name.string'
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
Loading…
Reference in New Issue