From 6d42a0d5b2d45c0349ddea988cc87c475f90b815 Mon Sep 17 00:00:00 2001 From: Matt De Leon Date: Wed, 26 Dec 2012 00:46:51 -0500 Subject: [PATCH 1/5] Defaults passed to simple_form_for should propagate to input_field --- lib/simple_form/form_builder.rb | 2 + test/form_builder/general_test.rb | 68 +++++++++++++++++++------------ 2 files changed, 44 insertions(+), 26 deletions(-) diff --git a/lib/simple_form/form_builder.rb b/lib/simple_form/form_builder.rb index e5444791..d696acb2 100644 --- a/lib/simple_form/form_builder.rb +++ b/lib/simple_form/form_builder.rb @@ -134,6 +134,8 @@ module SimpleForm def input_field(attribute_name, options={}) options = options.dup options[:input_html] = options.except(:as, :collection, :label_method, :value_method) + options = @defaults.deep_dup.deep_merge(options) if @defaults + SimpleForm::Wrappers::Root.new([:input], :wrapper => false).render find_input(attribute_name, options) end diff --git a/test/form_builder/general_test.rb b/test/form_builder/general_test.rb index 5ac35ec0..0b2eea72 100644 --- a/test/form_builder/general_test.rb +++ b/test/form_builder/general_test.rb @@ -306,49 +306,65 @@ class FormBuilderTest < ActionView::TestCase end # DEFAULT OPTIONS - test 'builder should receive a default argument and pass it to the inputs' do - with_concat_form_for @user, :defaults => { :input_html => { :class => 'default_class' } } do |f| - f.input :name + [:input, :input_field].each do |method| + test "builder should receive a default argument and pass it to the inputs when calling '#{method}'" do + with_concat_form_for @user, :defaults => { :input_html => { :class => 'default_class' } } do |f| + f.send(method, :name) + end + assert_select 'input.default_class' + end + + test "builder should receive a default argument and pass it to the inputs without changing the defaults when calling '#{method}'" do + with_concat_form_for @user, :defaults => { :input_html => { :class => 'default_class', :id => 'default_id' } } do |f| + concat(f.send(method, :name)) + concat(f.send(method, :credit_limit)) + end + + assert_select "input.string.default_class[name='user[name]']" + assert_no_select "input.string[name='user[credit_limit]']" + end + + test "builder should receive a default argument and pass it to the inputs and nested form when calling '#{method}'" do + @user.company = Company.new(1, 'Empresa') + + with_concat_form_for @user, :defaults => { :input_html => { :class => 'default_class' } } do |f| + concat(f.send(method, :name)) + concat(f.simple_fields_for(:company) do |company_form| + concat(company_form.send(method, :name)) + end) + end + + assert_select "input.string.default_class[name='user[name]']" + assert_select "input.string.default_class[name='user[company_attributes][name]']" end - assert_select 'input.default_class' end - test 'builder should receive a default argument and pass it to the inputs, respecting the specific options' do + test "builder should receive a default argument and pass it to the inputs when calling 'input', respecting the specific options" do with_concat_form_for @user, :defaults => { :input_html => { :class => 'default_class' } } do |f| f.input :name, :input_html => { :id => 'specific_id' } end assert_select 'input.default_class#specific_id' end - test 'builder should receive a default argument and pass it to the inputs, overwriting the defaults with specific options' do + test "builder should receive a default argument and pass it to the inputs when calling 'input_field', respecting the specific options" do + with_concat_form_for @user, :defaults => { :input_html => { :class => 'default_class' } } do |f| + f.input_field :name, :id => 'specific_id' + end + assert_select 'input.default_class#specific_id' + end + + test "builder should receive a default argument and pass it to the inputs when calling 'input', overwriting the defaults with specific options" do with_concat_form_for @user, :defaults => { :input_html => { :class => 'default_class', :id => 'default_id' } } do |f| f.input :name, :input_html => { :id => 'specific_id' } end assert_select 'input.default_class#specific_id' end - test 'builder should receive a default argument and pass it to the inputs without changing the defaults' do + test "builder should receive a default argument and pass it to the inputs when calling 'input_field', overwriting the defaults with specific options" do with_concat_form_for @user, :defaults => { :input_html => { :class => 'default_class', :id => 'default_id' } } do |f| - concat(f.input :name) - concat(f.input :credit_limit) + f.input_field :name, :id => 'specific_id' end - - assert_select "input.string.default_class[name='user[name]']" - assert_no_select "input.string[name='user[credit_limit]']" - end - - test 'builder should receive a default argument and pass it to the inputs and nested form' do - @user.company = Company.new(1, 'Empresa') - - with_concat_form_for @user, :defaults => { :input_html => { :class => 'default_class' } } do |f| - concat(f.input :name) - concat(f.simple_fields_for(:company) do |company_form| - concat(company_form.input :name) - end) - end - - assert_select "input.string.default_class[name='user[name]']" - assert_select "input.string.default_class[name='user[company_attributes][name]']" + assert_select 'input.default_class#specific_id' end # WITHOUT OBJECT From fe1c91761ef8c719ff52e58b5119b361eb0117f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Wed, 26 Dec 2012 15:04:37 -0300 Subject: [PATCH 2/5] Use the 3-2-stable branch in the tests since mocha doesn't work well with ruby 1.8 and rails 3.2.9 --- Gemfile | 6 +++--- Gemfile.lock | 44 +++++++++++++++++++++++++------------------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/Gemfile b/Gemfile index 7196e4c1..c5d3283c 100644 --- a/Gemfile +++ b/Gemfile @@ -3,9 +3,9 @@ source :rubygems gemspec gem 'country_select' -gem 'railties', '~> 3.2.0' -gem 'activemodel', '~> 3.2.0' -gem 'actionpack', '~> 3.2.0' +gem 'railties', '~> 3.2.0', :github => 'rails/rails', :branch => '3-2-stable' +gem 'activemodel', '~> 3.2.0', :github => 'rails/rails', :branch => '3-2-stable' +gem 'actionpack', '~> 3.2.0', :github => 'rails/rails', :branch => '3-2-stable' gem 'rake' gem 'rdoc' gem 'mocha', :require => false diff --git a/Gemfile.lock b/Gemfile.lock index d61b89c2..98ea4ef8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,12 +1,7 @@ -PATH - remote: . - specs: - simple_form (2.1.0.dev) - actionpack (~> 3.0) - activemodel (~> 3.0) - -GEM - remote: http://rubygems.org/ +GIT + remote: git://github.com/rails/rails.git + revision: 8c938dddd22f16e2e15471a648aaa96be380c562 + branch: 3-2-stable specs: actionpack (3.2.9) activemodel (= 3.2.9) @@ -24,6 +19,24 @@ GEM activesupport (3.2.9) i18n (~> 0.6) multi_json (~> 1.0) + railties (3.2.9) + actionpack (= 3.2.9) + activesupport (= 3.2.9) + rack-ssl (~> 1.3.2) + rake (>= 0.8.7) + rdoc (~> 3.4) + thor (>= 0.14.6, < 2.0) + +PATH + remote: . + specs: + simple_form (2.1.0.dev) + actionpack (~> 3.0) + activemodel (~> 3.0) + +GEM + remote: http://rubygems.org/ + specs: builder (3.0.4) country_select (1.0.1) erubis (2.7.0) @@ -42,13 +55,6 @@ GEM rack rack-test (0.6.2) rack (>= 1.0) - railties (3.2.9) - actionpack (= 3.2.9) - activesupport (= 3.2.9) - rack-ssl (~> 1.3.2) - rake (>= 0.8.7) - rdoc (~> 3.4) - thor (>= 0.14.6, < 2.0) rake (10.0.1) rdoc (3.12) json (~> 1.4) @@ -65,11 +71,11 @@ PLATFORMS ruby DEPENDENCIES - actionpack (~> 3.2.0) - activemodel (~> 3.2.0) + actionpack (~> 3.2.0)! + activemodel (~> 3.2.0)! country_select mocha - railties (~> 3.2.0) + railties (~> 3.2.0)! rake rdoc simple_form! From cf9f0b5fbde531f96767998633164a595cb65887 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Wed, 26 Dec 2012 15:06:58 -0300 Subject: [PATCH 3/5] Add CHANGELOG entry for #713 [ci skip] --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 148f3585..749089be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ * Generates additional wrapper class based on object + attribute name. ([@lucasmazza](https://github/lucasmazza)) Closes [#576](https://github.com/plataformatec/simple_form/issues/576). + * Allow `input_field` to work with `:defaults` options. + ([@smidwap](https://github.com/smidwap)) ### bug fix * Do not lookup for hints if it was explicitly given false. From 22c80ffec95d75fb4500a12b57fbb2ad00164b31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Wed, 26 Dec 2012 15:13:29 -0300 Subject: [PATCH 4/5] Use a version of mocha working with every rails version --- Gemfile | 8 +++---- Gemfile.lock | 52 ++++++++++++++++++++------------------------- test/test_helper.rb | 2 +- 3 files changed, 28 insertions(+), 34 deletions(-) diff --git a/Gemfile b/Gemfile index c5d3283c..f9321782 100644 --- a/Gemfile +++ b/Gemfile @@ -3,10 +3,10 @@ source :rubygems gemspec gem 'country_select' -gem 'railties', '~> 3.2.0', :github => 'rails/rails', :branch => '3-2-stable' -gem 'activemodel', '~> 3.2.0', :github => 'rails/rails', :branch => '3-2-stable' -gem 'actionpack', '~> 3.2.0', :github => 'rails/rails', :branch => '3-2-stable' +gem 'railties', '~> 3.2.0' +gem 'activemodel', '~> 3.2.0' +gem 'actionpack', '~> 3.2.0' gem 'rake' gem 'rdoc' -gem 'mocha', :require => false +gem 'mocha', '~> 0.12.0', :require => false gem 'tzinfo' diff --git a/Gemfile.lock b/Gemfile.lock index 98ea4ef8..4f051d62 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,12 @@ -GIT - remote: git://github.com/rails/rails.git - revision: 8c938dddd22f16e2e15471a648aaa96be380c562 - branch: 3-2-stable +PATH + remote: . + specs: + simple_form (2.1.0.dev) + actionpack (~> 3.0) + activemodel (~> 3.0) + +GEM + remote: http://rubygems.org/ specs: actionpack (3.2.9) activemodel (= 3.2.9) @@ -19,24 +24,6 @@ GIT activesupport (3.2.9) i18n (~> 0.6) multi_json (~> 1.0) - railties (3.2.9) - actionpack (= 3.2.9) - activesupport (= 3.2.9) - rack-ssl (~> 1.3.2) - rake (>= 0.8.7) - rdoc (~> 3.4) - thor (>= 0.14.6, < 2.0) - -PATH - remote: . - specs: - simple_form (2.1.0.dev) - actionpack (~> 3.0) - activemodel (~> 3.0) - -GEM - remote: http://rubygems.org/ - specs: builder (3.0.4) country_select (1.0.1) erubis (2.7.0) @@ -45,9 +32,9 @@ GEM journey (1.0.4) json (1.7.5) metaclass (0.0.1) - mocha (0.13.0) + mocha (0.12.7) metaclass (~> 0.0.1) - multi_json (1.3.7) + multi_json (1.5.0) rack (1.4.1) rack-cache (1.2) rack (>= 0.4) @@ -55,10 +42,17 @@ GEM rack rack-test (0.6.2) rack (>= 1.0) + railties (3.2.9) + actionpack (= 3.2.9) + activesupport (= 3.2.9) + rack-ssl (~> 1.3.2) + rake (>= 0.8.7) + rdoc (~> 3.4) + thor (>= 0.14.6, < 2.0) rake (10.0.1) rdoc (3.12) json (~> 1.4) - sprockets (2.2.1) + sprockets (2.2.2) hike (~> 1.2) multi_json (~> 1.0) rack (~> 1.0) @@ -71,11 +65,11 @@ PLATFORMS ruby DEPENDENCIES - actionpack (~> 3.2.0)! - activemodel (~> 3.2.0)! + actionpack (~> 3.2.0) + activemodel (~> 3.2.0) country_select - mocha - railties (~> 3.2.0)! + mocha (~> 0.12.0) + railties (~> 3.2.0) rake rdoc simple_form! diff --git a/test/test_helper.rb b/test/test_helper.rb index f42d8b2c..1fefdbdb 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -2,7 +2,7 @@ require 'rubygems' require 'bundler/setup' require 'test/unit' -require 'mocha/setup' +require 'mocha' require 'active_model' require 'action_controller' From e8bb7b391b93056bda605be12b995e5798f0474d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Wed, 26 Dec 2012 15:16:15 -0300 Subject: [PATCH 5/5] Forgot to push the Gemfiles --- gemfiles/Gemfile-rails.3.0.x | 2 +- gemfiles/Gemfile-rails.3.1.x | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gemfiles/Gemfile-rails.3.0.x b/gemfiles/Gemfile-rails.3.0.x index 59dacb50..61edb1e2 100644 --- a/gemfiles/Gemfile-rails.3.0.x +++ b/gemfiles/Gemfile-rails.3.0.x @@ -8,5 +8,5 @@ gem 'activemodel', '~> 3.0.0' gem 'actionpack', '~> 3.0.0' gem 'rake' gem 'rdoc' -gem 'mocha', :require => false +gem 'mocha', '~> 0.12.0', :require => false gem 'tzinfo' diff --git a/gemfiles/Gemfile-rails.3.1.x b/gemfiles/Gemfile-rails.3.1.x index a3fcfba0..43e84d4f 100644 --- a/gemfiles/Gemfile-rails.3.1.x +++ b/gemfiles/Gemfile-rails.3.1.x @@ -8,5 +8,5 @@ gem 'activemodel', '~> 3.1.0' gem 'actionpack', '~> 3.1.0' gem 'rake' gem 'rdoc' -gem 'mocha', :require => false +gem 'mocha', '~> 0.12.0', :require => false gem 'tzinfo'