From 2581fe03623d4ee68093aac0300d62f4bd466ebb Mon Sep 17 00:00:00 2001 From: Wasif Hossain Date: Wed, 17 Jul 2019 15:51:15 +0600 Subject: [PATCH] Lookup fieldset using either string or symbol --- active_model_serializers.gemspec | 2 +- lib/active_model/serializer/fieldset.rb | 2 +- test/serializers/fieldset_test.rb | 14 ++++++++++++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/active_model_serializers.gemspec b/active_model_serializers.gemspec index a29ef9dc9..8630cd324 100644 --- a/active_model_serializers.gemspec +++ b/active_model_serializers.gemspec @@ -15,7 +15,7 @@ Gem::Specification.new do |spec| spec.homepage = 'https://github.com/rails-api/active_model_serializers' spec.license = 'MIT' - spec.files = Dir["CHANGELOG.md", "MIT-LICENSE", "README.md", "lib/**/*"] + spec.files = Dir['CHANGELOG.md', 'MIT-LICENSE', 'README.md', 'lib/**/*'] spec.require_paths = ['lib'] spec.executables = [] diff --git a/lib/active_model/serializer/fieldset.rb b/lib/active_model/serializer/fieldset.rb index 8ed99f899..78609a4e5 100644 --- a/lib/active_model/serializer/fieldset.rb +++ b/lib/active_model/serializer/fieldset.rb @@ -12,7 +12,7 @@ def fields end def fields_for(type) - fields[type.singularize.to_sym] || fields[type.pluralize.to_sym] + fields[type.to_s.singularize.to_sym] || fields[type.to_s.pluralize.to_sym] end protected diff --git a/test/serializers/fieldset_test.rb b/test/serializers/fieldset_test.rb index 8237f5d03..781696340 100644 --- a/test/serializers/fieldset_test.rb +++ b/test/serializers/fieldset_test.rb @@ -5,11 +5,21 @@ module ActiveModel class Serializer class FieldsetTest < ActiveSupport::TestCase + def setup + @fieldset = ActiveModel::Serializer::Fieldset.new('post' => %w(id title), 'comment' => ['body']) + end + def test_fieldset_with_hash - fieldset = ActiveModel::Serializer::Fieldset.new('post' => %w(id title), 'comment' => ['body']) expected = { post: [:id, :title], comment: [:body] } - assert_equal(expected, fieldset.fields) + assert_equal(expected, @fieldset.fields) + end + + def test_fields_for_accepts_string_or_symbol + expected = [:id, :title] + + assert_equal(expected, @fieldset.fields_for(:post)) + assert_equal(expected, @fieldset.fields_for('post')) end end end