mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge pull request #2344 from rails-api/fieldset-lookup-using-symbol-or-string
Lookup fieldset using either string or symbol
This commit is contained in:
commit
7b544f3341
@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
|
|||||||
spec.homepage = 'https://github.com/rails-api/active_model_serializers'
|
spec.homepage = 'https://github.com/rails-api/active_model_serializers'
|
||||||
spec.license = 'MIT'
|
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.require_paths = ['lib']
|
||||||
spec.executables = []
|
spec.executables = []
|
||||||
|
|
||||||
|
|||||||
@ -12,7 +12,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def fields_for(type)
|
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
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|||||||
@ -5,11 +5,21 @@ require 'test_helper'
|
|||||||
module ActiveModel
|
module ActiveModel
|
||||||
class Serializer
|
class Serializer
|
||||||
class FieldsetTest < ActiveSupport::TestCase
|
class FieldsetTest < ActiveSupport::TestCase
|
||||||
|
def setup
|
||||||
|
@fieldset = ActiveModel::Serializer::Fieldset.new('post' => %w(id title), 'comment' => ['body'])
|
||||||
|
end
|
||||||
|
|
||||||
def test_fieldset_with_hash
|
def test_fieldset_with_hash
|
||||||
fieldset = ActiveModel::Serializer::Fieldset.new('post' => %w(id title), 'comment' => ['body'])
|
|
||||||
expected = { post: [: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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user