Skip to content

Instantly share code, notes, and snippets.

@ahawkins
Created July 24, 2010 09:18
Show Gist options
  • Save ahawkins/488564 to your computer and use it in GitHub Desktop.
Save ahawkins/488564 to your computer and use it in GitHub Desktop.
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
class MockResponse
def content_type
'text/html'
end
end
class RenderingController < ApplicationController
class << self
# this is going to need to be an option in the final method call
# it it used to determine the path of partials called like:
#
# render :partial => "something"
#
# where the actual path is "/app/views/controller_that_handled_request/_something.erb"
# so you can use this option/method to specify that relative directory
# that is generated in the standard request process
def controller_path
"feed_items"
end
end
def url_for(options = {})
options ||= {}
case options
when String
options
when Hash
ActionController::UrlRewriter.new(captured_request, {}).rewrite(rewrite_options(options))
else
polymorphic_url(options)
end
end
def captured_request
hash = {
"SERVER_NAME"=>"localhost",
"HTTP_ACCEPT"=>"application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
"HTTP_CACHE_CONTROL"=>"max-age=0",
"HTTP_HOST"=>"localhost:3000",
"HTTP_USER_AGENT"=>"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4",
"REQUEST_PATH"=>"/login", "rack.url_scheme"=>"http",
"SERVER_PROTOCOL"=>"HTTP/1.1", "HTTP_IF_NONE_MATCH"=>"\"31a7c7f10c0dcd41034ac24bd99aa6f4\"",
"HTTP_ACCEPT_LANGUAGE"=>"en-US,en;q=0.8",
"REMOTE_ADDR"=>"127.0.0.1", "PATH_INFO"=>"/login",
"SERVER_SOFTWARE"=>"Mongrel 1.1.5", "rack.run_once"=>false,
"rack.version"=>[1, 1], "SCRIPT_NAME"=>"",
"HTTP_COOKIE"=>"/settings/todo/ready=true; /settings/email_account/configured=true; /settings/sms/ready=true; last_page_visited=%2Flogin; _crm_session=BAh7CDoQX2NzcmZfdG9rZW4iMTh3SG0xQm5iL1RiTzFDNkxraHl4S0NJaHpZL3duZGlPTm5pSFpFZHRHaVE9Og9zZXNzaW9uX2lkIiVmNjUxYmQyZTEyOTc5MTA4Y2E2ZTk3ZmMzOGZjZjQ4MiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA%3D%3D--eed2f740b8a99cc34261a8c83c4863e1d19d3b79",
"HTTP_VERSION"=>"HTTP/1.1", "rack.multithread"=>false, "REQUEST_URI"=>"/login", "rack.multiprocess"=>false, "SERVER_PORT"=>"3000", "HTTP_ACCEPT_CHARSET"=>"ISO-8859-1,utf-8;q=0.7,*;q=0.3", "REQUEST_METHOD"=>"GET", "GATEWAY_INTERFACE"=>"CGI/1.2", "HTTP_ACCEPT_ENCODING"=>"gzip,deflate,sdch", "HTTP_CONNECTION"=>"keep-alive"
}
ActionController::Request.new(hash)
end
def response
@mock_response ||= MockResponse.new
end
def session
{}
end
def current_user
User.new(:name => 'Adam')
end
def current_account
Account.new
end
end
class Renderer < ActionView::Base
def initialize(view_paths = [], assigns_for_first_render = {}, controller = RenderingController.new)
super(view_paths, assigns_for_first_render, controller)
# this is the part where we do some trickery to mimic the normal template rendering process
# in rails
self.helpers.send :include, ApplicationController.master_helper_module
self.view_paths = determine_view_paths
@template_format = :html
end
def do_something
@output = render(:partial => 'feed_items/feed_item', :locals => {:feed_item => FeedItem.first })
end
def determine_view_paths
Dir[Rails.root.join('app','views','**','*')].map {|dir| File.dirname(dir)}.uniq
end
def output
@output.inspect
end
end
I18n.locale = 'en'
r = Renderer.new()
r.do_something
r.output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment