roar-rails icon indicating copy to clipboard operation
roar-rails copied to clipboard

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

Open konung opened this issue 10 years ago • 6 comments

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)

konung avatar May 08 '15 15:05 konung

I responded on SO.

apotonick avatar May 08 '15 22:05 apotonick

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 avatar May 09 '15 08:05 konung

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.

konung avatar May 09 '15 08:05 konung

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). :wink:

apotonick avatar May 09 '15 08:05 apotonick

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

apotonick avatar May 11 '15 03:05 apotonick

@apotonick Did you find any more beautiful solution ?

jaydave avatar Dec 18 '15 02:12 jaydave