versioncake icon indicating copy to clipboard operation
versioncake copied to clipboard

Default Version for A Specific Route

Open LBRapid opened this issue 8 years ago • 3 comments

Is there a way to set the default version just for one route/controller action?

I want to use the latest version available for all api resources except one. For this resource, I would like it to use version 1 of the api unless a specific api version is specified.

Let me know if you need more information to understand my question. Overriding the default_version config works for the time being, but I would like to implement this in a way that going forward it might work for multiple resources.

LBRapid avatar Mar 03 '16 17:03 LBRapid

Interesting use case! I don't see an easy way to support this now, but can think of a couple ways to go about this:

before_actions

class PostsController < ApplicationController
  prepend_before_action :override_latest_version, only: :show, if: :'request_version_missing?' # this API doesn't exist yet

  def index;end

  def show;end

  private

  def override_latest_version
    set_version 1
  end
end

configuration

VersionCake.setup do |config|
  config.resources do |r|
    r.resource %r{.*}, [], [], (1..4), 1 # overrides the global missing_version
  end
  config.extraction_strategy = :query_parameter # for simplicity
  config.missing_version = 4
end

What do you think?

bwillis avatar Mar 03 '16 20:03 bwillis

Both methods look pretty good.

I think I prefer the second method to the prepend_before_filter option. It would allow for all Versioncake configuration to stay together, rather than having to add extra things to a controller.

LBRapid avatar Mar 03 '16 22:03 LBRapid

Yeah, agree with keeping the config together and it allows for non-Rails apps to use the functionality easier.

bwillis avatar Mar 04 '16 07:03 bwillis