From f32b5184a643a1b2300f1b52b4f9aae2b1b34784 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 7 Nov 2010 10:18:37 +0100 Subject: [PATCH] Bring tests back to life. --- Gemfile.lock | 10 ++++++++-- .../simple_form/templates/simple_form.rb | 4 ++-- lib/simple_form.rb | 2 +- lib/simple_form/error_notification.rb | 3 +-- lib/simple_form/form_builder.rb | 16 ++++++++-------- lib/simple_form/inputs/block_input.rb | 5 +++-- test/components/error_test.rb | 8 ++------ test/components/hint_test.rb | 8 ++------ test/components/label_test.rb | 9 ++------- test/inputs_test.rb | 2 +- test/test_helper.rb | 2 +- 11 files changed, 31 insertions(+), 38 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index baad67c5..68da2dfc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -31,9 +31,11 @@ GEM arel (1.0.1) activesupport (~> 3.0.0) builder (2.1.2) + columnize (0.3.2) erubis (2.6.6) abstract (>= 1.0.0) i18n (0.4.2) + linecache (0.43) mail (2.2.9) activesupport (>= 2.3.6) i18n (~> 0.4.1) @@ -62,7 +64,11 @@ GEM rake (>= 0.8.4) thor (~> 0.14.0) rake (0.8.7) - test-unit (2.1.1) + ruby-debug (0.10.4) + columnize (>= 0.1) + ruby-debug-base (~> 0.10.4.0) + ruby-debug-base (0.10.4) + linecache (>= 0.3) thor (0.14.4) treetop (1.4.8) polyglot (>= 0.3.1) @@ -74,4 +80,4 @@ PLATFORMS DEPENDENCIES mocha rails (= 3.0.0) - test-unit + ruby-debug diff --git a/lib/generators/simple_form/templates/simple_form.rb b/lib/generators/simple_form/templates/simple_form.rb index 27837f6f..1392f264 100644 --- a/lib/generators/simple_form/templates/simple_form.rb +++ b/lib/generators/simple_form/templates/simple_form.rb @@ -23,10 +23,10 @@ SimpleForm.setup do |config| # config.error_notification_tag = :p # CSS class to add for error notification helper. - # config.error_notification_class = '' + # config.error_notification_class = :error_notification # ID to add for error notification helper. - # config.error_notification_id = '' + # config.error_notification_id = nil # You can wrap all inputs in a pre-defined tag. # config.wrapper_tag = :div diff --git a/lib/simple_form.rb b/lib/simple_form.rb index 82c5eb16..36b978f9 100644 --- a/lib/simple_form.rb +++ b/lib/simple_form.rb @@ -37,7 +37,7 @@ module SimpleForm # CSS class to add for error notification helper. mattr_accessor :error_notification_class - @@error_notification_class = nil + @@error_notification_class = :error_notification # ID to add for error notification helper. mattr_accessor :error_notification_id diff --git a/lib/simple_form/error_notification.rb b/lib/simple_form/error_notification.rb index b7f40967..12a34cd4 100644 --- a/lib/simple_form/error_notification.rb +++ b/lib/simple_form/error_notification.rb @@ -26,8 +26,7 @@ module SimpleForm end def html_options - css_class = SimpleForm.error_notification_class || @options[:class] - @options[:class] = "error_notification #{css_class}".strip + @options[:class] = "#{SimpleForm.error_notification_class} #{@options[:class]}".strip @options[:id] = SimpleForm.error_notification_id if SimpleForm.error_notification_id @options end diff --git a/lib/simple_form/form_builder.rb b/lib/simple_form/form_builder.rb index 5fac8cb1..51d05dce 100644 --- a/lib/simple_form/form_builder.rb +++ b/lib/simple_form/form_builder.rb @@ -81,11 +81,11 @@ module SimpleForm def input(attribute_name, options={}, &block) column = find_attribute_column(attribute_name) input_type = default_input_type(attribute_name, column, options) + if block_given? - SimpleForm::Inputs::BlockInput.new(self, block).render + SimpleForm::Inputs::BlockInput.new(self, attribute_name, column, input_type, options, &block).render else - klass = self.class.mappings[input_type] || - self.class.const_get(:"#{input_type.to_s.camelize}Input") + klass = self.class.mappings[input_type] || self.class.const_get("#{input_type.to_s.camelize}Input") klass.new(self, attribute_name, column, input_type, options).render end end @@ -159,8 +159,8 @@ module SimpleForm options = args.extract_options! options[:class] = "button #{options[:class]}".strip args << options - if respond_to?(:"#{type}_button") - send(:"#{type}_button", *args, &block) + if respond_to?("#{type}_button") + send("#{type}_button", *args, &block) else send(type, *args, &block) end @@ -219,9 +219,9 @@ module SimpleForm def label(attribute_name, *args) return super if args.first.is_a?(String) options = args.extract_options! - options[:label] = options.delete(:label) - options[:label_html] = options - options[:required] = options.delete(:required) + options[:label] = options.delete(:label) + options[:label_html] = options + options[:required] = options.delete(:required) column = find_attribute_column(attribute_name) input_type = default_input_type(attribute_name, column, options) SimpleForm::Inputs::Base.new(self, attribute_name, column, input_type, options).label diff --git a/lib/simple_form/inputs/block_input.rb b/lib/simple_form/inputs/block_input.rb index d75b1cfe..36278789 100644 --- a/lib/simple_form/inputs/block_input.rb +++ b/lib/simple_form/inputs/block_input.rb @@ -1,8 +1,9 @@ module SimpleForm module Inputs class BlockInput < Base - def initialize(builder, block) - @builder, @block = builder, block + def initialize(*args, &block) + super + @block = block end def input diff --git a/test/components/error_test.rb b/test/components/error_test.rb index e737eafe..41f7a27b 100644 --- a/test/components/error_test.rb +++ b/test/components/error_test.rb @@ -4,12 +4,8 @@ class ErrorTest < ActionView::TestCase def with_error_for(object, attribute_name, type, options={}, &block) with_concat_form_for(object) do |f| - f.attribute_name = attribute_name - f.reflection = Association.new(Company, :company, {}) if options.delete(:setup_association) - f.input_type = type - f.options = options - - SimpleForm::Inputs::Base.new(f).error.to_s + f.reflection = Association.new(Company, :company, {}) if options.delete(:setup_association) + SimpleForm::Inputs::Base.new(f, attribute_name, nil, type, options).error.to_s end end diff --git a/test/components/hint_test.rb b/test/components/hint_test.rb index b74aebf4..64d1351e 100644 --- a/test/components/hint_test.rb +++ b/test/components/hint_test.rb @@ -4,12 +4,8 @@ class HintTest < ActionView::TestCase def with_hint_for(object, attribute_name, type, options={}, &block) with_concat_form_for(object) do |f| - f.attribute_name = attribute_name - f.reflection = Association.new(Company, :company, {}) if options.delete(:setup_association) - f.input_type = type - f.options = options - - SimpleForm::Inputs::Base.new(f).hint.to_s + f.reflection = Association.new(Company, :company, {}) if options.delete(:setup_association) + SimpleForm::Inputs::Base.new(f, attribute_name, nil, type, options).hint.to_s end end diff --git a/test/components/label_test.rb b/test/components/label_test.rb index ea0b0e76..4fafba06 100644 --- a/test/components/label_test.rb +++ b/test/components/label_test.rb @@ -2,19 +2,14 @@ require 'test_helper' class LabelTest < ActionView::TestCase - setup do SimpleForm::Inputs::Base.reset_i18n_cache :translate_required_html end def with_label_for(object, attribute_name, type, options={}) with_concat_form_for(object) do |f| - f.attribute_name = attribute_name - f.reflection = Association.new(Company, :company, {}) if options.delete(:setup_association) - f.input_type = type - f.options = options - - SimpleForm::Inputs::Base.new(f).label + f.reflection = Association.new(Company, :company, {}) if options.delete(:setup_association) + SimpleForm::Inputs::Base.new(f, attribute_name, nil, type, options).label end end diff --git a/test/inputs_test.rb b/test/inputs_test.rb index 9e0899b3..b7796753 100644 --- a/test/inputs_test.rb +++ b/test/inputs_test.rb @@ -222,7 +222,7 @@ class InputTest < ActionView::TestCase test 'mapping input should generate an error if type is not found' do with_concat_form_for(@user) do |f| assert_raise(RuntimeError, "Could not find method for nil") do - SimpleForm::Inputs::MappingInput.new(f).input + SimpleForm::Inputs::MappingInput.new(f, "unknown", nil, nil, {}).input end end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 64c577fd..3e665f24 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -28,7 +28,7 @@ else end class SimpleForm::FormBuilder - attr_accessor :attribute_name, :column, :reflection, :input_type, :options + attr_accessor :reflection, :options end class ActionView::TestCase