Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Route toggling with contraints
module Mode
  class Constraint
    def initialize(mode)
      @mode = mode
    end

    def matches?(_request)
      @mode == Rails.application.config.mode # configured from the environment
    end
  end

  BOB = Mode::Constraint.new(:BOB)
  AMY = Mode::Constraint.new(:AMY)
end

config/routes.rb :

namespace :foo, constraints: Mode::BOB do
  get 'foo/foo' => 'foo#foo'
end

get 'bars/:id/bar' => 'bar#bar', constraints: Mode::AMY

resources :bazs, constraints: Mode::BOB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.