Skip to content

Commit f571d59

Browse files
authored
Merge pull request #311 from brainspec/fix/issue-309
Support non-ActiveModel objects in SimpleForm/Formtastic integration.
2 parents b9cff3a + 0e27159 commit f571d59

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

lib/enumerize/hooks/formtastic.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ module Hooks
55
module FormtasticFormBuilderExtension
66

77
def input(method, options={})
8-
klass = object && object.to_model.class
8+
enumerized_object = convert_to_model(object)
9+
klass = enumerized_object.class
910

1011
if klass.respond_to?(:enumerized_attributes) && (attr = klass.enumerized_attributes[method])
1112
options[:collection] ||= attr.options

lib/enumerize/hooks/simple_form.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ def input_field(attribute_name, options={})
1717
private
1818

1919
def add_input_options_for_enumerized_attribute(attribute_name, options)
20-
klass = object && object.to_model.class
20+
enumerized_object = convert_to_model(object)
21+
klass = enumerized_object.class
2122

2223
if klass.respond_to?(:enumerized_attributes) && (attr = klass.enumerized_attributes[attribute_name])
2324
options[:collection] ||= attr.options

test/formtastic_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ def persisted?
4242
end
4343
end
4444

45+
class Registration < Struct.new(:sex)
46+
extend Enumerize
47+
48+
enumerize :sex, in: [:male, :female]
49+
end
50+
4551
before { $VERBOSE = nil }
4652
after { $VERBOSE = true }
4753

@@ -125,6 +131,15 @@ def persisted?
125131
assert_select 'input[type=text]'
126132
end
127133

134+
it 'renders select with enumerized values for non-ActiveModel object' do
135+
concat(semantic_form_for(Registration.new, as: 'registration', url: '/') do |f|
136+
f.input(:sex)
137+
end)
138+
139+
assert_select 'select option[value=male]'
140+
assert_select 'select option[value=female]'
141+
end
142+
128143
it 'does not affect forms without object' do
129144
concat(semantic_form_for('') do |f|
130145
f.input(:name)

test/simple_form_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ def persisted?
4040
end
4141
end
4242

43+
class Registration < Struct.new(:sex)
44+
extend Enumerize
45+
46+
enumerize :sex, in: [:male, :female]
47+
end
48+
4349
let(:user) { User.new }
4450
let(:post) { Post.new }
4551

@@ -129,6 +135,15 @@ def persisted?
129135
assert_select 'input.string'
130136
end
131137

138+
it 'renders select with enumerized values for non-ActiveModel object' do
139+
concat(simple_form_for(Registration.new, as: 'registration', url: '/') do |f|
140+
f.input(:sex)
141+
end)
142+
143+
assert_select 'select option[value=male]'
144+
assert_select 'select option[value=female]'
145+
end
146+
132147
it 'does not affect forms without object' do
133148
concat(simple_form_for('') do |f|
134149
f.input(:name)

0 commit comments

Comments
 (0)