question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

API enhancement: get full route for nested resource

See original GitHub issue

If I use nested resources I sometimes have to deal with generic route names. Say I have a users and companies route and both have a nested resource address, which I can operate on with users/:id/address and companies/:id/address.

Now I want to enhance the restangularized element with custom logic inside Restangular.setOnElemRestangularized, but I need to treat the address of a user differently than the address of a company. I can’t just look into the route param, because it is address in both cases. I need to check elem.parentResource.route, too. It could look like this:

Restangular.setOnElemRestangularized(function(elem, isCollection, route) {
  if(elem.parentResource.route + '/' + route === 'companies/address') { /* do stuff */ }
  if(elem.parentResource.route + '/' + route === 'users/address') { /* do stuff */ }
});

This can become quite verbose and error prone. It becomes even worse with deeper nested resources or in more generic situations where I have to make sanity checks (e.g. is elem.parentResource defined?).

It would be very useful to get the full route of a resource like this:

Restangular.setOnElemRestangularized(function(elem, isCollection, route) {
  if(elem.getFullRoute() === 'companies/address') { /* do stuff */ }
  if(elem.getFullRoute() === 'users/address') { /* do stuff */ }
});

.getFullRoute() would basically recursively prepend every existing parentResource.route separated by a /. elem.getFullRoute() would be equal to elem.route for every non-nested resource.

function getFullRoute(elem) {
  var parentRoute = '';
  if(elem.parentResource) {
    parentRoute = getFullRoute(elem.parentResource) + '/';
  }
  return parentRoute + elem.route;
}

What do you think?

Issue Analytics

  • State:open
  • Created 9 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
vincentdieltienscommented, Dec 10, 2014

I ran into the same issue. I have customers/contacts and suppliers/contacts which are different.

The next solution seems to work well :

  1. Define the getFullRoute method (this is an iterative version. don’t know if recursive whould be better)
function getFullRoute(elem) {
  var currentElem = elem
  var fullRoute = [];

  do {
    fullRoute.splice(0, 0, currentElem.route);
    currentElem = currentElem.parentResource;
  }
  while(currentElem !== null);

  return fullRoute.join('/');
}
  1. Add line to config.transformElem to get the full route of the element and try to get the transformers of the full route before getting the transformers of the route.
config.transformElem = function(elem, isCollection, route, Restangular, force) {
    if (!force && !config.transformLocalElements && !elem[config.restangularFields.fromServer]) {
      return elem;
    }

    var fullRoute = getFullRoute(elem);
    var typeTransformers = config.transformers[fullRoute] || config.transformers[route];
    var changedElem = elem;
    if (typeTransformers) {
        _.each(typeTransformers, function(transformer) {
           changedElem = transformer(isCollection, changedElem);
        });
    }
    return config.onElemRestangularized(changedElem,
      isCollection, route, Restangular);
};

With that small changes, extendModel will work like this :

Restangular.extendModel('customers/contacts', function(model) {
  // ...
});
Restangular.extendModel('suppliers/contacts', function(model) {
  // ...
});

What do you think ?

0reactions
end-usercommented, Jul 20, 2016

This issue is still open and still seems relevant. Is there a workaround or a fix? How can I apply custom methods to nested model objects?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Improve nested routes API [SPR-14954] #19521 - GitHub
Sébastien Deleuze opened SPR-14954 and commented Nested routes API could perhaps be improved. Current syntax is : subroute(GET("/level1/**"), ...
Read more >
REST API Design Best Practices for Sub and Nested Resources
REST API design best practices for nested resources. ... a nested resource URL can convey that one resource belongs to another one.
Read more >
Rails Pattern For Including Nested Resources In API Responses
This Rails pattern provides a flexible way for clients to specify whether they want nested resources includes in the API response simply by ......
Read more >
ruby on rails - Override nested named route parameters
I will try to enhance an unaccepted answer in linked question: https://stackoverflow.com/a/29138733/2405850. try to use nesting with member ...
Read more >
How To Create Nested Resources for a Ruby on Rails ...
The routes and routing helpers that became available when we created a nested posts route. For a full list of example routes that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found