-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #700 from arenoir/sparse_fieldsets
sparse fieldsets
- Loading branch information
Showing
10 changed files
with
131 additions
and
5 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,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 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
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,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 |