Skip to content

Instantly share code, notes, and snippets.

@jejacks0n
Last active January 1, 2016 03:58
Show Gist options
  • Save jejacks0n/8088572 to your computer and use it in GitHub Desktop.
Save jejacks0n/8088572 to your computer and use it in GitHub Desktop.
Dummy::Application.routes.draw do
sections '/cms' do
section :bar, to: 'test#welcome2'
#section :bar do
# get ":id", to: 'test#welcome'
#end
get "/welcome" => 'test#welcome', section: :foo
root to: 'test#welcome'
end
end
require "singleton"
module Hg
class Routing
def self.setup(router, root = '/', &block)
raise Hg::MissingBlock unless block_given?
new(router, root, &block)
end
def initialize(router, root, &block)
@router = router
@root = root
@mapper = Mapper.new
context = self
scope(root) do
constraints(Hg::SectionConstraint.new(false)) do
context.instance_eval(&block)
end
constraints(Hg::SectionConstraint.new(true, @mapper)) do
get '*path', to: 'hg/section#show', format: false
end
end
end
def section(name, options)
@mapper[name] = options
end
def method_missing(method, *args, &block)
if @router.respond_to?(method, true)
@router.send(method, *args, &block)
else
super
end
end
class Mapper
def initialize
@sections = {}
end
def []=(name, options)
@sections[name] = options
end
def [](name)
@sections[name]
end
end
end
end
# inject our method into ActionDispatch::Routing::Mapper
module ActionDispatch
module Routing
class Mapper
def sections(root, &block)
::Hg::Routing.setup(self, root, &block)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment