Skip to content

Commit

Permalink
Lookup fieldset using either string or symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
wasifhossain committed Jul 17, 2019
1 parent 433c0c6 commit 2581fe0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion active_model_serializers.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down
2 changes: 1 addition & 1 deletion lib/active_model/serializer/fieldset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions test/serializers/fieldset_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2581fe0

Please sign in to comment.