From fb80a289ab6e2f13416f321bc64b93e3edc4f693 Mon Sep 17 00:00:00 2001 From: Vasiliy Ermolovich Date: Thu, 15 Feb 2018 09:31:18 +0300 Subject: [PATCH] Support SimpleForm/Formtastic forms without object. --- CHANGELOG.md | 1 + lib/enumerize/hooks/formtastic.rb | 2 +- lib/enumerize/hooks/simple_form.rb | 2 +- test/formtastic_test.rb | 8 ++++++++ test/simple_form_test.rb | 8 ++++++++ 5 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e8314ab..94216b20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### enhancements ### bug fix + Fix issue with SimpleForm/Formtastic forms without object. (by [@nashby](https://github.com/nashby)) ## 2.2.0 (February 13, 2018) diff --git a/lib/enumerize/hooks/formtastic.rb b/lib/enumerize/hooks/formtastic.rb index 4fb6549d..6d69c43c 100644 --- a/lib/enumerize/hooks/formtastic.rb +++ b/lib/enumerize/hooks/formtastic.rb @@ -5,7 +5,7 @@ module Hooks module FormtasticFormBuilderExtension def input(method, options={}) - klass = object.to_model.class + klass = object && object.to_model.class if klass.respond_to?(:enumerized_attributes) && (attr = klass.enumerized_attributes[method]) options[:collection] ||= attr.options diff --git a/lib/enumerize/hooks/simple_form.rb b/lib/enumerize/hooks/simple_form.rb index 28402867..07f5ee07 100644 --- a/lib/enumerize/hooks/simple_form.rb +++ b/lib/enumerize/hooks/simple_form.rb @@ -17,7 +17,7 @@ def input_field(attribute_name, options={}) private def add_input_options_for_enumerized_attribute(attribute_name, options) - klass = object.to_model.class + klass = object && object.to_model.class if klass.respond_to?(:enumerized_attributes) && (attr = klass.enumerized_attributes[attribute_name]) options[:collection] ||= attr.options diff --git a/test/formtastic_test.rb b/test/formtastic_test.rb index 86494db0..9e68cc8d 100644 --- a/test/formtastic_test.rb +++ b/test/formtastic_test.rb @@ -124,4 +124,12 @@ def persisted? assert_select 'input[type=text]' end + + it 'does not affect forms without object' do + concat(semantic_form_for('') do |f| + f.input(:name) + end) + + assert_select 'input[type=text]' + end end diff --git a/test/simple_form_test.rb b/test/simple_form_test.rb index 148b30f6..97675463 100644 --- a/test/simple_form_test.rb +++ b/test/simple_form_test.rb @@ -128,4 +128,12 @@ def persisted? assert_select 'input.string' end + + it 'does not affect forms without object' do + concat(simple_form_for('') do |f| + f.input(:name) + end) + + assert_select 'input.string' + end end