Skip to content

Commit 2581fe0

Browse files
committed
Lookup fieldset using either string or symbol
1 parent 433c0c6 commit 2581fe0

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

active_model_serializers.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
1515
spec.homepage = 'https://github.com/rails-api/active_model_serializers'
1616
spec.license = 'MIT'
1717

18-
spec.files = Dir["CHANGELOG.md", "MIT-LICENSE", "README.md", "lib/**/*"]
18+
spec.files = Dir['CHANGELOG.md', 'MIT-LICENSE', 'README.md', 'lib/**/*']
1919
spec.require_paths = ['lib']
2020
spec.executables = []
2121

lib/active_model/serializer/fieldset.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def fields
1212
end
1313

1414
def fields_for(type)
15-
fields[type.singularize.to_sym] || fields[type.pluralize.to_sym]
15+
fields[type.to_s.singularize.to_sym] || fields[type.to_s.pluralize.to_sym]
1616
end
1717

1818
protected

test/serializers/fieldset_test.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@
55
module ActiveModel
66
class Serializer
77
class FieldsetTest < ActiveSupport::TestCase
8+
def setup
9+
@fieldset = ActiveModel::Serializer::Fieldset.new('post' => %w(id title), 'comment' => ['body'])
10+
end
11+
812
def test_fieldset_with_hash
9-
fieldset = ActiveModel::Serializer::Fieldset.new('post' => %w(id title), 'comment' => ['body'])
1013
expected = { post: [:id, :title], comment: [:body] }
1114

12-
assert_equal(expected, fieldset.fields)
15+
assert_equal(expected, @fieldset.fields)
16+
end
17+
18+
def test_fields_for_accepts_string_or_symbol
19+
expected = [:id, :title]
20+
21+
assert_equal(expected, @fieldset.fields_for(:post))
22+
assert_equal(expected, @fieldset.fields_for('post'))
1323
end
1424
end
1525
end

0 commit comments

Comments
 (0)