Method Level Rails API Versions

In .NET, we managed to update our APIs at the method level, annotating them and only duplicating methods that differed between versions. I switched to the product group using Rails and am trying to figure out if there is anything that can help me do this in Rails. Everything that I have seen so far suggests proxying your entire controller (s) in the module using V1, V2, etc. When it can be said that only one method is different between V1 and V2, which makes it possible to significantly reduce the number of duplication is necessary.

Thanks.

+5
source share
1 answer

Ruby is an extremely flexible and rich language, and it should not be difficult to avoid code duplication.

One dirty and easy way to deal with your problem is with a route to action. In .NET, you actually route requests using the annotation method and do not use any special language features.

# API::V2 implements only special_method get '/api/v2/special_method', controller: 'API::V2', action: 'special_method' # all other actions from V2 are processed by V1 mount API::V1 => '/api/v1' mount API::V1 => '/api/v2' 

Another, more advanced way is to use modules to implement controller actions (instance methods). You can then reuse the same module in V2 of your API and override this method that you want to change.

 class API::V1 # implementaton of API::V1 include ImplV1 end class API::V2 # all the methods from V1 are available in V2 too include ImplV1 # override methods from V1 here include ImplV2 end 

Finally, a much simpler but less flexible way is to directly inherit V2 from V1:

 class API::V2 < API::V1 def special_method # ... end end 
+3
source

Source: https://habr.com/ru/post/1232557/


All Articles