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

Collection Representer seem boiler plate, any way to auto generate them? #58

Open
dtshepherd opened this issue Nov 22, 2013 · 4 comments

Comments

@dtshepherd
Copy link

Hi, I've been playing around with roar and roar-rails. Great job by the way, exactly what I was looking for.

I'm interested to know if there is a way to represent collections without having to create the boilerplate representer code that just reuses the singular form and provides the "collect" call. Is that possible now and I'm missing something? If not, I think just have a template with the plural collection and helper method, then use the singular form as the class and representer.

Related to this, is there a way to auto-discover the property and collection representers too? That seems like boiler plate (i.e. just do the same search that is performed with the controller "represents" logic.

Let me know if I'm way off base here...

@apotonick
Copy link
Member

Not sure if I got it right - do you mean something like that? trailblazer/representable#22

In @timoschilling's proposal, you kinda have one representer that automatically knows how to handle collections, too.

Auto-discovery as in not having to specify :class and :extend in the representer module for nested objects?

Oh, and thanks, glad you like it 😁

@dtshepherd
Copy link
Author

Yep, something like that for the collection. Obviously, having some rails magic to dynamically call collection_wapper and specifying the proper :class if the plural form is requested (like from an index action).

And yes again to the not having to specify :class and :extend. Or at least making that optional with a config. Obviously that won't work well if it creates a circular reference. Maybe an option to turn on and off per property/collection. This helps keep the reepresenters more decoupled from the model class. I'd rather specify something like :represented => true for the properties.

Does that make sense? If I can get some time away from my day job, I'm willing to take a stab at this feature set if you think it's a good idea. :)

@apotonick
Copy link
Member

I definitely like the "collection wrapper" idea from @timoschilling and will try to get that working.

Can you give some examples how the automatic :class/:extend guessing would work (with nested models, in different contexts like index, etc)? That would be great.

@dtshepherd
Copy link
Author

I actually played around with getting the infer_representer to create a dynamic module like "collection wrapper" did. Seems to work well actually. I'll try to push an example to you. I'm wondering if the same idea of dynamic module creation can fix #30? For example,

module MyRepresenter
  property :name
  property :company
end

repr_format = "Roar::Representer::" + format.to_s.upcase
r = Module.new do
  include repr_format.constantize
  include MyModule
end
Object.const_set(format.to_s.upcase + "::" + MyRepresenter.name, r)

Obviously, some generalization is needed for multiple representers, etc.

If I get some time, I'll make sure to push some examples for the automatic guessing.

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

2 participants