Created
May 22, 2013 14:37
-
-
Save iamjwc/5628021 to your computer and use it in GitHub Desktop.
Question for @apotonick
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
class ThingRepresentation < Representable::Decorator | |
include Representable::JSON | |
property :field | |
end | |
# This works great. | |
ThingRepresentation.new(Thing.new).to_json # => { "field": "thing value" } | |
# This does not work. | |
ThingRepresentation.new([Thing.new, Thing.new]).to_json # => [{ "field": "thing1 value" }, { "field": "thing2 value" }] | |
# How can I achieve this? | |
# This seems to be close to what I want: https://github.com/apotonick/representable/pull/22 |
In representable-1.5.1 this will work.
class ThingsRepresenter < Representable::Decorator
include Representable::JSON::Collection
items decorator: ThingRepresentation
end
ThingsRepresentation.new([Thing.new, Thing.new]).to_json
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
ThingRepresentation
is supposed to represent aThing
entity (instance), not a collection. If you want that, you could either have a separateThingsRepresenter
or go with a "lonely collection": https://github.com/apotonick/representable/#lonely-collectionsI am not sure if that works with
Decorator
, yet, let me find this out.And, yes trailblazer/representable#22 is closely related to this!