Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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