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

What's the strategy to create multiple representations for the same class based on criteria? #116

Open
konung opened this issue May 8, 2015 · 6 comments

Comments

@konung
Copy link

konung commented May 8, 2015

Hello

This is actually a best practices / usage question, as I didn't find any examples of that. Here are two scenarios. I use decorator pattern.

Scenario 1:

Let's say I have a Product class that has multiple attributes & association. By default when somebody makes a request to api.com/products/1 - I want to show everything I've got, but if somebody makes a request to another action like so api.com/products/1/inventory_details - I only want to show limited view that pertains to inventory ( to make it faster for inventory lookups) or if somebody makes a request to api.com/products/1/assembly_details - I want to return a matrix of related sub assemblies along with some relevant product details.

Questions for Scenario 1 :

  1. Do I create a specific representer for each case like ProductRepresenter, ProductInventoryDetailRepresenter, ProductAssemblyDetailRepresenter or do I use some kind of flow control in the ProductRepresenter?
  2. If I create multiple representers, for the same class, how can I use represents / respond_with pattern vs respond_to / render ?
  3. Can I override this on action level?

Scenario 2:

Let's say I have api.com/products/1 that both my internal application can call but that I also want to expose to my clients. However I don't want my clients to see some of the attributes, like inventory details or maybe just one or two attributes. Also depending on the level access of employee, I want to limit their view / representation.

Questions for Scenario 2 :

  1. Do I create a specific representer for each case like ProductRepresenter, ProductClientViewRepresenter or do I use some kind of flow control in the ProductRepresenter?
  2. If I create multiple representers, for the same class, how can I use represents / respond_with pattern vs respond_to / render ?
  3. Can I override this on an action level - based on the access type like: admin vs inventory_user vs shipping_user?

Any advice would be appriciated. (I'll cross-post this on stackoverflow - http://stackoverflow.com/questions/30127077/strategy-to-create-multiple-representations-for-the-same-class-based-on-criteria)

@apotonick
Copy link
Member

I responded on SO.

@konung
Copy link
Author

konung commented May 9, 2015

Hey, Nick.
Hitting you up here as you suggested.
So for the admin access ( FYI I'm using a combination of AuthLogic / ActiveLDAP ( for AD authentication/ and CanCanCan ( might be switching away to something else) vs other level access - the problem is that since my authentication scheme for my users is based on AD and groups they belong to in AD, I have potentially 4-6 different levels / groups of access.
Implementaion Details in my app actually don't matter: but here is an example of different views I have to consider for inventory related part of the application:
admin/ superadmin
top_management
management
supervisor
inventory_group
shipping_group
billing_group
external_clients
regular_users

As you can see this can really become a mess of the representers really fast ( I can organize them in subfolders, but still) - I'm working on the bare bones of the app right now, but I will need to start implementing this fairly soon - so just trying to step ahead of the game.

@konung
Copy link
Author

konung commented May 9, 2015

Also on the "If I create multiple representers, for the same class, how can I use represents / respond_with pattern vs respond_to / render ?" part of the question - can you put up a small example illustrating this please? - Non of the examples seem to combine both.

Thanks.

@apotonick
Copy link
Member

Looks like you have a lot of different contexts. Do they all share the same document structure and then just hide certain parts, or does the document structure change reg. different contexts?

I am still playing with how to solve this in a beautiful way, since this is one of the major points of Trailblazer (polymorphic handling of different contexts for business logic, representers, forms, and authorization). We'll talk about that in the book, too (feel free to buy it now). 😉

@apotonick
Copy link
Member

@konung Please paste some scenarios here! I'm really interested in handling different contexts easier and more streamlined in Representable. 😬

@jaydave
Copy link

jaydave commented Dec 18, 2015

@apotonick Did you find any more beautiful solution ?

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

3 participants