Skip to content

Instantly share code, notes, and snippets.

@apotonick
Created May 7, 2012 06:01
Show Gist options
  • Save apotonick/2626187 to your computer and use it in GitHub Desktop.
Save apotonick/2626187 to your computer and use it in GitHub Desktop.
Cells in Sinatra, Webmachine, Love
require 'cell/base'
class FakeRoutes # should be sinatra's url helper instance
def url_for(*)
end
def named_routes # required due to a fucking stupid line in AbstractController::UrlFor#25 (3.2.3)
# super - _routes.named_routes.helper_names
Object.new.instance_eval do
def helper_names
[]
end
self
end
end
end
module FakeHelpers
def fruit_url(model, *args)
"http://fruits/#{model.id}"
end
def fruit_path(model, *args)
"/fruits/#{model.id}"
end
end
module Cell
# Allows using many Rails gem in your cells outside of a Rails environment.
module RailsHelperAPI
module InternalHelpers
def protect_against_forgery? # used in form_tag_helper.rb:651
false
end
def _routes # FIXME: where is this set in rails?
self.class._routes
end
end
extend ActiveSupport::Concern
module ClassMethods
attr_accessor :_routes
def helper_modules
[_helpers, InternalHelpers]
end
def view_context_class
super
@view_context_class._routes = _routes
@view_context_class
end
end
end
end
require "active_record"
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => "development.sqlite3"
)
class Fruit < ActiveRecord::Base
end
Fruit.create(:title => "Banana")
require "simple_form"
class BassistCell < Cell::Base
include Cell::RailsHelperAPI
self._helpers = FakeHelpers
self._routes = FakeRoutes.new
def play
@tone = "C"
@fruit = Fruit.find(:first)
render
end
end
Cell::Base.append_view_path(".")
puts Cell::Base.render_cell_for(:bassist, :play)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment