Skip to content

Instantly share code, notes, and snippets.

@iamjwc
Created May 22, 2013 14:37
Show Gist options
  • Save iamjwc/5628021 to your computer and use it in GitHub Desktop.
Save iamjwc/5628021 to your computer and use it in GitHub Desktop.
Question for @apotonick
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
@apotonick
Copy link

The ThingRepresentation is supposed to represent a Thing entity (instance), not a collection. If you want that, you could either have a separate ThingsRepresenter or go with a "lonely collection": https://github.com/apotonick/representable/#lonely-collections

I am not sure if that works with Decorator, yet, let me find this out.

And, yes trailblazer/representable#22 is closely related to this!

@apotonick
Copy link

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