Permalink
Browse files

the initial import of the render_with plugin for rails

  • Loading branch information...
1 parent 1dbe805 commit 031b387506eb4b07b6d042bbd1d0f21213380784 @zdennis committed Apr 2, 2008
View
@@ -0,0 +1,20 @@
+Copyright (c) 2008 Zach Dennis, Mark VanHolstyn, Mutually Human Software
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
4 README
@@ -0,0 +1,4 @@
+render_with
+------------
+
+TODO: docs
View
@@ -0,0 +1,13 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run tests.'
+task :default => :test
+
+desc 'Test the render_with plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
View
@@ -0,0 +1 @@
+require 'render_with'
View
@@ -0,0 +1,39 @@
+class Renderer
+ def initialize(context, generator_factory=ActionView::Helpers::PrototypeHelper::JavaScriptGenerator)
+ # include_helpers_from(context)
+ @context = context
+ context.assigns.each_pair do |key,value|
+ instance_variable_set "@#{key}", value
+ end
+ @page = generator_factory.new(context) {}
+ end
+
+ def to_s
+ @page.to_s
+ end
+
+private
+ # def include_helpers_from(context)
+ # context.extended_by.each do |mod|
+ # extend mod
+ # end
+ # end
+
+ def method_missing(*args, &block)
+ if @page.respond_to?(args.first)
+ @page.send(*args, &block)
+ else
+ @context.send *args, &block
+ end
+ end
+
+end
+
+class ActionView::Base
+ def render_with(renderer_name, &block)
+ renderer = "#{renderer_name}_renderer".classify.constantize.new(@template)
+ yield renderer
+ page = eval("page", block.binding)
+ page << renderer.to_s
+ end
+end
@@ -0,0 +1,3 @@
+render_with :test do |renderer|
+ renderer.display_ivar
+end
@@ -0,0 +1,7 @@
+render_with :test do |renderer|
+ renderer.set_notice "This is the test notice"
+end
+
+render_with :example do |renderer|
+ renderer.set_notice "This is the example notice"
+end
@@ -0,0 +1,3 @@
+render_with :test do |renderer|
+ renderer.set_notice "This is the notice"
+end
View
@@ -0,0 +1,3 @@
+# path = File.expand_path(File.dirname(__FILE__) + "/../")
+# Dir.chdir path
+# system "rake spec"
View
@@ -0,0 +1,57 @@
+require File.dirname(__FILE__) + '/test_helper'
+
+class TestRenderer < Renderer
+ def display_ivar
+ replace :foo_container, @foo
+ end
+
+ def set_notice msg
+ replace_html :notice, msg
+ end
+end
+
+class ExampleRenderer < TestRenderer
+end
+
+
+class TestController < ActionController::Base
+ def render_rjs_with_ivars
+ @foo = 'Hello World!'
+ respond_to :js
+ end
+
+ def render_rjs_with_single_renderer
+ respond_to :js
+ end
+
+ def render_rjs_with_multiple_renderers
+ respond_to :js
+ end
+end
+TestController.view_paths = [ File.dirname(__FILE__) + "/fixtures/" ]
+
+class RenderWithRendererTest < Test::Unit::TestCase
+ def setup
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+ @controller = TestController.new
+
+ @request.host = "www.nextangle.com"
+ end
+
+ def test_it_renders_rjs_with_a_single_renderer
+ xhr :get, :render_rjs_with_single_renderer
+ assert_match 'Element.update("notice", "This is the notice")'.to_regexp, @response.body, "didn't have the expected RJS"
+ end
+
+ def test_it_renders_rjs_with_multiple_renderers
+ xhr :get, :render_rjs_with_multiple_renderers
+ assert_match 'Element.update("notice", "This is the test notice")'.to_regexp, @response.body, "didn't have the first renderer's RJS"
+ assert_match 'Element.update("notice", "This is the example notice")'.to_regexp, @response.body, "didn't have the second renderer's RJS"
+ end
+
+ def test_it_gives_access_to_controller_instance_variables
+ xhr :get, :render_rjs_with_ivars
+ assert_match 'Element.replace("foo_container", "Hello World!")'.to_regexp, @response.body, "didn't have the ivar's value in the renderered RJS"
+ end
+end
View
@@ -0,0 +1,8 @@
+require File.dirname(__FILE__) + '/../../../../config/environment'
+require File.dirname(__FILE__) + '/../lib/render_with'
+
+class String
+ def to_regexp
+ Regexp.new Regexp.escape(self), Regexp::IGNORECASE
+ end
+end

0 comments on commit 031b387

Please sign in to comment.