-
-
Save joshbuddy/128923 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "active_support/core_ext" | |
require "action_controller/new_base/http" | |
require "rack/router" | |
module Rails | |
class Sinatra < ActionController::Http | |
include AbstractController::Renderer | |
include AbstractController::Callbacks | |
include AbstractController::Helpers | |
extlib_inheritable_accessor :router | |
class << self | |
def _routes() @_routes ||= [] end | |
def get(uri, opts={}, &block) route(:get, uri, opts, &block) end | |
def post(uri, opts={}, &block) route(:post, uri, opts, &block) end | |
def put(uri, opts={}, &block) route(:put, uri, opts, &block) end | |
def delete(uri, opts={}, &block) route(:delete, uri, opts, &block) end | |
def route(method, uri, opts, &block) | |
method = method.to_s.downcase | |
action_name = "[#{method}] #{uri}" | |
_routes << { :method => method, :uri => uri, :options => opts } | |
define_method(uri, &block) | |
end | |
def set(config, value) | |
self._view_paths = ActionView::Base.process_view_paths(value) if config == :views | |
end | |
def to_app(fallback = nil) | |
self.router = Rack::Router.new(fallback) do |r| | |
_routes.each { |route| | |
r.map route[:uri], route[:method], (route[:options] || {}).merge(:to => action(route[:uri])) | |
} | |
end | |
end | |
end | |
module Helpers | |
def url(*args) controller.class.router.url(*args) end | |
def link_to(text, url) %[<a href="#{url}">#{text}</a>] end | |
end | |
helper Helpers | |
def halt(msg) | |
throw :halt, msg | |
end | |
def render(name, options = {}) | |
options[:_template_name] = name | |
super(options) | |
end | |
def render_to_body(options = {}) | |
options[:_prefix] = "" | |
super | |
end | |
def process_action(*) | |
retval = catch(:halt) { super } | |
self.response_body ||= retval | |
self.headers["Content-Type"] ||= "text/html" | |
self.status ||= 200 | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyApp < Rails::Sinatra | |
set :views, File.dirname(__FILE__) | |
get "/hello", :name => :hello do | |
render :greetings | |
end | |
get "/world" do | |
@name = "Carl" | |
render :awesome | |
end | |
end | |
class MyOtherApp < Rails::Sinatra | |
before_filter do | |
halt "EJECTED!" | |
end | |
get "/goodbye" do | |
"Nope" | |
end | |
end | |
run Rack::Router.new { |r| | |
r.map "/", :to => MyApp.to_app | |
r.map "/other", :to => MyOtherApp.to_app | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment