Skip to content
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

Inline Decorators #52

Closed
xaethos opened this issue Jul 16, 2013 · 13 comments
Closed

Inline Decorators #52

xaethos opened this issue Jul 16, 2013 · 13 comments

Comments

@xaethos
Copy link

xaethos commented Jul 16, 2013

They appear to be broken. I can create an anonymous class and assign it to the :decorator key, but when trying to use a block I get a NoMethodError on represented#to_hash. After some light skimming through the code, I suspect this may be a problem with #representer_engine not being defined on Representable::Decorator.

@apotonick
Copy link
Member

Can you give us the decorator class that is broken?

@xaethos
Copy link
Author

xaethos commented Jul 16, 2013

I haven't looked deeply into this yet, but my intuition says lib/representable/decorator.rb

@apotonick
Copy link
Member

I meant your decorator that breaks. It should work, check this test: https://github.com/apotonick/representable/blob/master/test/representable_test.rb#L465

@xaethos
Copy link
Author

xaethos commented Jul 16, 2013

Whoops, sorry!

This class does not work:

class ArticlesRepresenter < Roar::Decorator
  include Roar::Representer::JSON::HAL

  collection :all,
    as: :article,
    class: Article,
    embedded: true do
      property :latitude
      property :longitude
      link :self do
        { href: article_path(represented), title: represented.title }
      end
    end

end

The error it give is:

{
    "error": "NoMethodError",
    "message": "undefined method `to_hash' for #<Article:0x007f97f5066758>"
}

This one does:

class ArticlesRepresenter < Roar::Decorator
  include Roar::Representer::JSON::HAL

  itemDecorator = Class.new(Roar::Decorator) do
    include Roar::Representer::JSON::HAL
    property :latitude
    property :longitude
    link :self do
      { href: article_path(represented), title: represented.title }
    end
  end

  collection :all,
    as: :article,
    class: Article,
    decorator: itemDecorator,
    embedded: true

end

Full app at xaethos/roar-hal-demo

@apotonick
Copy link
Member

Ah, you were right, because HAL::representer_engine is missing. A test would be great :-D

@rsutphin
Copy link

rsutphin commented Aug 6, 2013

This may be fixed by #54.

@xaethos
Copy link
Author

xaethos commented Sep 4, 2013

Confirmed that the inline decorator described above now works.

@xaethos xaethos closed this as completed Sep 4, 2013
@apotonick
Copy link
Member

We still need a test for that.

@apotonick apotonick reopened this Sep 4, 2013
@jandudulski
Copy link

Similar problem is with items block:

works:

class RegionsRepresenter < Representable::Decorator
  include Representable::JSON::Collection

  item_decorator = Class.new(Representable::Decorator) do
    include Representable::JSON

    property :id
    property :name
  end

  items decorator: item_decorator
end

Raises Argument Error (0 for 1) for items

class RegionsRepresenter < Representable::Decorator
  include Representable::JSON::Collection

  items do
    property :id
    property :name
  end
end

I'm not sure if this is separate issue or it belongs here?

@apotonick
Copy link
Member

::items is a reserved method when mixing in JSON::Collection!!!111oneoneeleven

@apotonick apotonick reopened this May 22, 2014
@apotonick
Copy link
Member

Haha, I am stupid, it is early and I didn't read it properly. I don't think using an inline representer in combination with JSON::Collection::items is implemented, it's super simple to do that, but I guess I didn't think of that, yet. Do you wanna do it? I'll write the test.

@jandudulski
Copy link

👌

@apotonick
Copy link
Member

@jandudulski Fixed in 86ac6a6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants