Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
111 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module ActiveModel | ||
class Serializer | ||
class Fieldset | ||
|
||
attr_accessor :fields, :root | ||
|
||
def initialize(serializer, fields = {}) | ||
@root = serializer.json_key | ||
@fields = parse(fields) | ||
end | ||
|
||
def fields_for(serializer) | ||
key = serializer.json_key || serializer.class.root_name | ||
fields[key] | ||
end | ||
|
||
private | ||
|
||
def parse(fields) | ||
if fields.is_a?(Hash) | ||
fields.inject({}) { |h,(k,v)| h[k.to_s] = v.map(&:to_sym); h} | ||
elsif fields.is_a?(Array) | ||
hash = {} | ||
hash[root.to_s] = fields.map(&:to_sym) | ||
hash | ||
else | ||
{} | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require 'test_helper' | ||
|
||
module ActiveModel | ||
class Serializer | ||
class Adapter | ||
class JsonApi | ||
class FieldsetTest < Minitest::Test | ||
def setup | ||
@post = Post.new(title: 'New Post', body: 'Body') | ||
comment_1 = Comment.new(id: 1, body: 'comment one') | ||
comment_2 = Comment.new(id: 2, body: 'comment two') | ||
@post.comments = [comment_1, comment_2] | ||
|
||
@serializer = PostSerializer.new(@post) | ||
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer) | ||
|
||
end | ||
|
||
def test_fieldset_with_fields_array | ||
fieldset = ActiveModel::Serializer::Fieldset.new(@serializer, ['title']) | ||
|
||
assert_equal( | ||
{:title=>"New Post", :links=>{:comments=>["1", "2"]}}, | ||
@adapter.serializable_hash({fieldset: fieldset})[:posts] | ||
) | ||
end | ||
|
||
def test_fieldset_with_hash | ||
fieldset = ActiveModel::Serializer::Fieldset.new(@serializer, {post: [:body]}) | ||
|
||
assert_equal( | ||
{:body=>"Body", :links=>{:comments=>["1", "2"]}}, | ||
@adapter.serializable_hash({fieldset: fieldset})[:posts] | ||
) | ||
end | ||
|
||
def test_fieldset_with_multiple_hashes | ||
fieldset = ActiveModel::Serializer::Fieldset.new(@serializer, {post: [:title], comment: [:body]}) | ||
|
||
assert_equal( | ||
[{:body=>"comment one" }, {:body=>"comment two"}], | ||
@adapter.serializable_hash({fieldset: fieldset})[:linked][:comments] | ||
) | ||
|
||
#don't understand how this is getting set. | ||
@serializer.class._associations[:comments][:options] = {} | ||
|
||
end | ||
|
||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters