From 0e27159a6db5f11ff57f08e8ea8fd9878a51db49 Mon Sep 17 00:00:00 2001 From: Vasiliy Ermolovich Date: Fri, 2 Mar 2018 12:21:40 +0300 Subject: [PATCH] Support non-ActiveModel objects in SimpleForm/Formtastic integration. --- lib/enumerize/hooks/formtastic.rb | 3 ++- lib/enumerize/hooks/simple_form.rb | 3 ++- test/formtastic_test.rb | 15 +++++++++++++++ test/simple_form_test.rb | 15 +++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/enumerize/hooks/formtastic.rb b/lib/enumerize/hooks/formtastic.rb index 6d69c43c..3a7401be 100644 --- a/lib/enumerize/hooks/formtastic.rb +++ b/lib/enumerize/hooks/formtastic.rb @@ -5,7 +5,8 @@ module Hooks module FormtasticFormBuilderExtension def input(method, options={}) - klass = object && object.to_model.class + enumerized_object = convert_to_model(object) + klass = enumerized_object.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 07f5ee07..07c5bf85 100644 --- a/lib/enumerize/hooks/simple_form.rb +++ b/lib/enumerize/hooks/simple_form.rb @@ -17,7 +17,8 @@ def input_field(attribute_name, options={}) private def add_input_options_for_enumerized_attribute(attribute_name, options) - klass = object && object.to_model.class + enumerized_object = convert_to_model(object) + klass = enumerized_object.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 9e68cc8d..5048df1d 100644 --- a/test/formtastic_test.rb +++ b/test/formtastic_test.rb @@ -42,6 +42,12 @@ def persisted? end end + class Registration < Struct.new(:sex) + extend Enumerize + + enumerize :sex, in: [:male, :female] + end + before { $VERBOSE = nil } after { $VERBOSE = true } @@ -125,6 +131,15 @@ def persisted? assert_select 'input[type=text]' end + it 'renders select with enumerized values for non-ActiveModel object' do + concat(semantic_form_for(Registration.new, as: 'registration', url: '/') do |f| + f.input(:sex) + end) + + assert_select 'select option[value=male]' + assert_select 'select option[value=female]' + end + it 'does not affect forms without object' do concat(semantic_form_for('') do |f| f.input(:name) diff --git a/test/simple_form_test.rb b/test/simple_form_test.rb index 97675463..f1463154 100644 --- a/test/simple_form_test.rb +++ b/test/simple_form_test.rb @@ -40,6 +40,12 @@ def persisted? end end + class Registration < Struct.new(:sex) + extend Enumerize + + enumerize :sex, in: [:male, :female] + end + let(:user) { User.new } let(:post) { Post.new } @@ -129,6 +135,15 @@ def persisted? assert_select 'input.string' end + it 'renders select with enumerized values for non-ActiveModel object' do + concat(simple_form_for(Registration.new, as: 'registration', url: '/') do |f| + f.input(:sex) + end) + + assert_select 'select option[value=male]' + assert_select 'select option[value=female]' + end + it 'does not affect forms without object' do concat(simple_form_for('') do |f| f.input(:name)