-
Notifications
You must be signed in to change notification settings - Fork 1.4k
sparse fieldsets #700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
sparse fieldsets #700
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
39bee48
implement sparse fieldsets http://jsonapi.org/format/#fetching-sparse…
arenoir 34f0847
fix tests, but need to understand how the serializer class attribute …
arenoir be54e0b
remove serializer dependency from fieldset
arenoir c9c58e3
merge upstream/master
arenoir fc1562c
add fields to adapter initialize function, pull in master, add tests …
arenoir 2ed52f9
merge upstream update fieldset
arenoir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,40 @@ | ||
module ActiveModel | ||
class Serializer | ||
class Fieldset | ||
|
||
def initialize(fields, root = nil) | ||
@root = root | ||
@raw_fields = fields | ||
end | ||
|
||
def fields | ||
@fields ||= parsed_fields | ||
end | ||
|
||
def fields_for(serializer) | ||
key = serializer.json_key || serializer.class.root_name | ||
fields[key.to_sym] | ||
end | ||
|
||
private | ||
|
||
attr_reader :raw_fields, :root | ||
|
||
def parsed_fields | ||
if raw_fields.is_a?(Hash) | ||
raw_fields.inject({}) { |h,(k,v)| h[k.to_sym] = v.map(&:to_sym); h} | ||
elsif raw_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[root.to_sym] = raw_fields.map(&:to_sym) | ||
hash | ||
else | ||
{} | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this change belongs into the serializer. The serializer should just return all attributes, where jsonapi adapter would then intersect the ones it needs. I think this should go inside
JsonApiAdapter
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would agree If it just returned the attribute names but it calls send for each attribute. It would be terribly inefficient to call the method just to filter it out later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kurko any suggestions?