Skip to content

Commit

Permalink
Merge pull request #311 from brainspec/fix/issue-309
Browse files Browse the repository at this point in the history
Support non-ActiveModel objects in SimpleForm/Formtastic integration.
  • Loading branch information
nashby authored Mar 2, 2018
2 parents b9cff3a + 0e27159 commit f571d59
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/enumerize/hooks/formtastic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/enumerize/hooks/simple_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions test/formtastic_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions test/simple_form_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f571d59

Please sign in to comment.