XML changes: Avoid to set tag name from representer. Use as option instead.
I have a case when I need to use same decorator for different properties. Just now I using subclasses for it what is annoying when you have a lot of same attribute types. With this PR I will be able to use as option for each property.
Can you make an example how this helps, I didn't really understand it by looking at the tests. Thanks, man!
@apotonick I added test case with example. So I think you will get the point.
@apotonick any progress?
Sorry to disappoint you, Alex, but XML support in Representable has a really low priority right now in my life - I promise you to look over it this week! :heart:
This PR adds the :as option so the wrap for nested representers can be specified from the composing representer. It doesn't look at the representation_wrap if :as is provided.
Is that correct?
Yes, but as I understand :as provided all time.
Yeah, I remember :as was there, before. But this didn't work with nested representers??
@apotonick no. And inside :as option all time present. Even if you not set it. I suppose it taken from class/module name.
@apotonick another thing. It doesn't look at the representation_wrap if :as is provided. is not true.
It replace tag name with option what comes in :as. So no need in representation_wrap option.
I sloooooowly start to remember this problem! It's great you came up with it! :+1:
@apotonick So I fixed it property? I think not fully because still not possible to set wrapper tag for collections, but I think it could be possible through something like this:
nested :songs do
collection :songs, as: :song, decorator: ::SongDecorator
end
I am working on fixing this - it's a terrible mess in the current implementation and I might have to break the API.
So, to sum up what you need.
class InvoiceDecorator < Representable::Decorator
include XML
property :start_date, wrap: :start_date, decorator: DateDecorator
end
Where the path would end up as invoice/start_date and not as currently, invoice/start_date/start_date.
@apotonick exactly! I think what my fix is not help you.
I think there's two problems in the current XML implementation.
- As you said, the
as:option doesn't work - I think this would fix your problem. - We support a weird
:wrapoption that could easily be achieved with::nested. In JSON/Hash, I introduced a:wrapoption that allows to suppress the representation_wrap of an inline representer, and that should be the same in XML.