Skip to content
This repository has been archived by the owner on Nov 5, 2021. It is now read-only.

How to inherit only few methods from v1 version #26

Open
brahamshakti opened this issue Sep 18, 2015 · 4 comments
Open

How to inherit only few methods from v1 version #26

brahamshakti opened this issue Sep 18, 2015 · 4 comments

Comments

@brahamshakti
Copy link

I don't know whether it is handled or not but I didn't find any documentation regarding inheriting only few methods from previous version.

eg.

api vendor_string: "myvendor", default_version: 1 do
   version 1 do
     cache as: 'v1' do
        resources :authorizations
     end
   end
   version 2 do
     inherit from: 'v1'
     # Now I want only `index` action to inherit from authorizations and want to modify other actions of authorization controller. How to do this?
   end
end

Thanks.

@davidcelis
Copy link
Member

Hm, it looks like there isn't a way to do this yet. Ideally you'd be able to do something like:

api vendor_string: "myvendor", default_version: 1 do
  version 1 do
    cache as: 'v1' do
      resources :authorizations
    end
  end

  version 2 do
    inherit from: 'v1' do
      resources :authorizations, only: [:index]
    end

    resources :authorizations, except: [:index]
  end
end

@brahamshakti
Copy link
Author

Yes sure you are right. This gem can ease the life of a developer if you provide this feature in your gem :-)

@SampsonCrowley
Copy link

SampsonCrowley commented Nov 4, 2021

To come back to this, it's about order of operations. rails reads routes from the top down. you are misunderstanding what "inherit from 'v1' means, it means add everything from v1, then add more. if you had to pick every route to inherit from, it would make the inheritance pointless. the "inheritance" is just syntactic sugar for automatically defining all routes that already exist.

also think about how ridiculously complex this would get for how to try to pick and choose what routes to inherit and what not to. How fine-grained is it supposed to be? the feature you are requesting is not really possible.

I actually make my newest version the source of truth in the routes file, then have older versions inherit from it so that they can override new routes with "does not exist for this version" errors. this satisfies the rails conventions of top-down routing

if you want to pick and choose what to define at all, define the resource before inheriting, or don't inherit

@davidcelis your example also makes no sense really. this is a routes file, not the controller, if you define all the routes except index in the inheritance, then define the index outside the inheritance, you are still ending up with the exact same available endpoints, pointing to the exact same controller methods, only through multiple definitions, and that index wont be considered until after all other routes in the list.

api vendor_string: "myvendor", default_version: 1 do
  version 1 do
    cache as: 'v1' do
      resources :authorizations
    end
  end

  version 2 do
    resources :authorizations, except: [:index]
    get "authorizations", to: "error_message"
   
    inherit from: 'v1'
  end
end

@SampsonCrowley
Copy link

You guys both seem to be confusing the routes with the controller. To override controller actions, define those methods again in the newer version controller

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants