Skip to content

Commit

Permalink
Merge 34f0847 into 47deb87
Browse files Browse the repository at this point in the history
  • Loading branch information
arenoir committed Oct 26, 2014
2 parents 47deb87 + 34f0847 commit a2a0b27
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 3 deletions.
9 changes: 8 additions & 1 deletion lib/active_model/serializer.rb
Expand Up @@ -124,7 +124,14 @@ def json_key
end

def attributes(options = {})
self.class._attributes.dup.each_with_object({}) do |name, hash|
attributes =
if options[:fields]
self.class._attributes & options[:fields]
else
self.class._attributes.dup
end

attributes.each_with_object({}) do |name, hash|
hash[name] = send(name)
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/active_model/serializer/adapter.rb
Expand Up @@ -17,6 +17,7 @@ def serializable_hash(options = {})
end

def to_json(options = {})
options[:fieldset] = ActiveModel::Serializer::Fieldset.new(serializer, options[:fields])
serializable_hash(options).to_json
end
end
Expand Down
10 changes: 9 additions & 1 deletion lib/active_model/serializer/adapter/json_api.rb
Expand Up @@ -10,9 +10,12 @@ def initialize(serializer, options = {})
def serializable_hash(options = {})
@root = (options[:root] || serializer.json_key).to_s.pluralize.to_sym
@hash = {}
@fieldset = options[:fieldset]

if serializer.respond_to?(:each)
@hash[@root] = serializer.map{|s| self.class.new(s).serializable_hash[@root] }
opt = @fieldset ? {fieldset: @fieldset} : {}

@hash[@root] = serializer.map{|s| self.class.new(s).serializable_hash(opt)[@root] }
else
@hash[@root] = attributes_for_serializer(serializer, {})

Expand Down Expand Up @@ -57,6 +60,11 @@ def add_link(name, serializer, options)
private

def attributes_for_serializer(serializer, options)

if fields = @fieldset && @fieldset.fields_for(serializer)
options[:fields] = fields
end

attributes = serializer.attributes(options)
attributes[:id] = attributes[:id].to_s if attributes[:id]
attributes
Expand Down
33 changes: 33 additions & 0 deletions lib/active_model/serializer/fieldset.rb
@@ -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
1 change: 1 addition & 0 deletions lib/active_model_serializers.rb
@@ -1,6 +1,7 @@
require "active_model"
require "active_model/serializer/version"
require "active_model/serializer"
require "active_model/serializer/fieldset"

begin
require 'action_controller'
Expand Down
54 changes: 54 additions & 0 deletions test/adapter/json_api/fieldset_test.rb
@@ -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
6 changes: 5 additions & 1 deletion test/serializers/attributes_test.rb
Expand Up @@ -12,7 +12,11 @@ def test_attributes_definition
assert_equal([:name, :description],
@profile_serializer.class._attributes)
end

def test_attributes_with_fields_option
assert_equal({name: 'Name 1'},
@profile_serializer.attributes( { fields: [:name] } ) )
end
end
end
end

0 comments on commit a2a0b27

Please sign in to comment.