mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
remove serializer dependency from fieldset
This commit is contained in:
parent
34f08477e4
commit
be54e0bc4f
@ -17,7 +17,10 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def to_json(options = {})
|
def to_json(options = {})
|
||||||
options[:fieldset] = ActiveModel::Serializer::Fieldset.new(serializer, options[:fields])
|
if fields = options.delete(:fields)
|
||||||
|
options[:fieldset] = ActiveModel::Serializer::Fieldset.new(fields, serializer.json_key)
|
||||||
|
end
|
||||||
|
|
||||||
serializable_hash(options).to_json
|
serializable_hash(options).to_json
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -2,26 +2,29 @@ module ActiveModel
|
|||||||
class Serializer
|
class Serializer
|
||||||
class Fieldset
|
class Fieldset
|
||||||
|
|
||||||
attr_accessor :fields, :root
|
attr_reader :fields, :root
|
||||||
|
|
||||||
def initialize(serializer, fields = {})
|
def initialize(fields, root = nil)
|
||||||
@root = serializer.json_key
|
@root = root
|
||||||
@fields = parse(fields)
|
@fields = parse(fields)
|
||||||
end
|
end
|
||||||
|
|
||||||
def fields_for(serializer)
|
def fields_for(serializer)
|
||||||
key = serializer.json_key || serializer.class.root_name
|
key = serializer.json_key || serializer.class.root_name
|
||||||
fields[key]
|
fields[key.to_sym]
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def parse(fields)
|
def parse(fields)
|
||||||
if fields.is_a?(Hash)
|
if fields.is_a?(Hash)
|
||||||
fields.inject({}) { |h,(k,v)| h[k.to_s] = v.map(&:to_sym); h}
|
fields.inject({}) { |h,(k,v)| h[k.to_sym] = v.map(&:to_sym); h}
|
||||||
elsif fields.is_a?(Array)
|
elsif fields.is_a?(Array)
|
||||||
|
if root.nil?
|
||||||
|
raise ArgumentError, 'The root argument must be specified if the fileds argument is an array.'
|
||||||
|
end
|
||||||
hash = {}
|
hash = {}
|
||||||
hash[root.to_s] = fields.map(&:to_sym)
|
hash[root.to_sym] = fields.map(&:to_sym)
|
||||||
hash
|
hash
|
||||||
else
|
else
|
||||||
{}
|
{}
|
||||||
|
|||||||
@ -17,7 +17,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_fieldset_with_fields_array
|
def test_fieldset_with_fields_array
|
||||||
fieldset = ActiveModel::Serializer::Fieldset.new(@serializer, ['title'])
|
fieldset = ActiveModel::Serializer::Fieldset.new(['title'], 'post')
|
||||||
|
|
||||||
assert_equal(
|
assert_equal(
|
||||||
{:title=>"New Post", :links=>{:comments=>["1", "2"]}},
|
{:title=>"New Post", :links=>{:comments=>["1", "2"]}},
|
||||||
@ -26,7 +26,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_fieldset_with_hash
|
def test_fieldset_with_hash
|
||||||
fieldset = ActiveModel::Serializer::Fieldset.new(@serializer, {post: [:body]})
|
fieldset = ActiveModel::Serializer::Fieldset.new({post: [:body]})
|
||||||
|
|
||||||
assert_equal(
|
assert_equal(
|
||||||
{:body=>"Body", :links=>{:comments=>["1", "2"]}},
|
{:body=>"Body", :links=>{:comments=>["1", "2"]}},
|
||||||
@ -35,7 +35,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_fieldset_with_multiple_hashes
|
def test_fieldset_with_multiple_hashes
|
||||||
fieldset = ActiveModel::Serializer::Fieldset.new(@serializer, {post: [:title], comment: [:body]})
|
fieldset = ActiveModel::Serializer::Fieldset.new({post: [:title], comment: [:body]})
|
||||||
|
|
||||||
assert_equal(
|
assert_equal(
|
||||||
[{:body=>"comment one" }, {:body=>"comment two"}],
|
[{:body=>"comment one" }, {:body=>"comment two"}],
|
||||||
|
|||||||
26
test/serializers/fieldset_test.rb
Normal file
26
test/serializers/fieldset_test.rb
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
module ActiveModel
|
||||||
|
class Serializer
|
||||||
|
class FieldsetTest < Minitest::Test
|
||||||
|
|
||||||
|
def test_fieldset_with_hash
|
||||||
|
fieldset = ActiveModel::Serializer::Fieldset.new({'post' => ['id', 'title'], 'coment' => ['body']})
|
||||||
|
|
||||||
|
assert_equal(
|
||||||
|
{:post=>[:id, :title], :coment=>[:body]},
|
||||||
|
fieldset.fields
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_fieldset_with_array_of_fields_and_root_name
|
||||||
|
fieldset = ActiveModel::Serializer::Fieldset.new(['title'], 'post')
|
||||||
|
|
||||||
|
assert_equal(
|
||||||
|
{:post => [:title]},
|
||||||
|
fieldset.fields
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue
Block a user