Correct way to implement API versioning with active_model_serializers
<p>I know there are already some questions and also <a href="https://github.com/rails-api/active_model_serializers/issues/886" rel="nofollow">this is a open issue regarding AMS not handling namespaces too efficiently</a> (which is used by this versioning approach) but I wanted to be sure I am in the right track within the current constraints.</p>

<p>Right now I am using Rails 5 and AMS 0.10.1, so I did the following:</p>

<pre><code># config/initializers/active_model_serializer.rb
ActiveModelSerializers.config.serializer_lookup_enabled = false
</code></pre>

<p>to disable default serializer lookup (which didn't work anyway); and</p>

<pre><code># app/controllers/application_controller.rb
class ApplicationController &lt; ActionController::API
  def get_serializer(resource, options = {})
    unless options[:each_serializer] || options[:serializer] then
      serializer = (self.class.name.gsub("Controller","").singularize + "Serializer").constantize
      resource.respond_to?(:to_ary) ? options[:each_serializer] = serializer : options[:serializer] = serializer
    end
    super(resource, options)
  end
end
</code></pre>

<p>to override how serializers are found by default; my controllers and serializer are like this:</p>

<pre><code># app/controllers/api/v2/api_controller.rb
module Api::V2
  class ApiController &lt; ApplicationController
    ...

# app/controllers/api/v2/users_controller.rb
module Api::V2
  class UsersController &lt; ApiController
    ...
</code></pre>

<p>and</p>

<pre><code># app/serializers/api/v2/user_serializer.rb
module Api::V2
  class UserSerializer &lt; ActiveModel::Serializer
    ...    
</code></pre>

<p>Now, things like <code>ActiveModel::Serializer.serializer_for(object)</code> won't work, so I had to also monkey patch my request specs using <code>example.metadata[:api_version]</code> to set the API version before each test and raising and error if the example didn't set it.</p>

<p>So:</p>

<ol>
<li>Is there a better way documented?</li>
<li>Is this any close to being correct? </li>
<li>Will I be facing problem further in with this approach? </li>
<li>How can it be improved?</li>
</ol>