Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@spannerinworks
Last active August 17, 2017 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spannerinworks/3c1df25529bfecd1bc2304e99ef7308b to your computer and use it in GitHub Desktop.
Save spannerinworks/3c1df25529bfecd1bc2304e99ef7308b to your computer and use it in GitHub Desktop.
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