From 5a640dcbdd6a04cd135adad7c320f865589e928e Mon Sep 17 00:00:00 2001 From: Javan Makhmali Date: Tue, 17 May 2011 23:42:34 -0400 Subject: [PATCH] partials --- lib/copy/server.rb | 14 ++++++++++---- test/server_test.rb | 6 ++++++ test/test_app/views/_one.html.erb | 1 + test/test_app/views/_three.html.erb | 1 + test/test_app/views/about/_two.html.erb | 2 ++ test/test_app/views/renders_partials.html.erb | 4 ++++ 6 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 test/test_app/views/_one.html.erb create mode 100644 test/test_app/views/_three.html.erb create mode 100644 test/test_app/views/about/_two.html.erb create mode 100644 test/test_app/views/renders_partials.html.erb diff --git a/lib/copy/server.rb b/lib/copy/server.rb index 2f2440d..7fc5f14 100644 --- a/lib/copy/server.rb +++ b/lib/copy/server.rb @@ -68,6 +68,12 @@ def copy(name, options = {}, &block) # Append the output buffer. @_out_buf << format_text(name, content, options) end + + def partial(template) + template_array = template.to_s.split('/') + template = template_array[0..-2].join('/') + "/_#{template_array[-1]}.#{@_route.format}" + send(@_route.renderer, template.to_sym, :layout => false) + end end def self.config(&block) @@ -105,11 +111,11 @@ def self.config(&block) end get '*' do - route = Copy::Router.new(params[:splat].first, settings.views) - if route.success? + @_route = Copy::Router.new(params[:splat].first, settings.views) + if @_route.success? set_cache_control_header - content_type(route.format) - send(route.renderer, route.template, :layout => route.layout) + content_type(@_route.format) + send(@_route.renderer, @_route.template, :layout => @_route.layout) else not_found end diff --git a/test/server_test.rb b/test/server_test.rb index bbe0257..e36b487 100644 --- a/test/server_test.rb +++ b/test/server_test.rb @@ -112,6 +112,12 @@ class ServerCopyHelperTest < Test::Unit::TestCase assert last_response.ok? assert_match %Q(Important!), last_response.body end + + test "partial rendering" do + get 'renders_partials' + assert last_response.ok?, last_response.errors + assert_match "before\none\ntwo\nthree\nafter", last_response.body + end end class ServerAdminTest < Test::Unit::TestCase diff --git a/test/test_app/views/_one.html.erb b/test/test_app/views/_one.html.erb new file mode 100644 index 0000000..5626abf --- /dev/null +++ b/test/test_app/views/_one.html.erb @@ -0,0 +1 @@ +one diff --git a/test/test_app/views/_three.html.erb b/test/test_app/views/_three.html.erb new file mode 100644 index 0000000..2bdf67a --- /dev/null +++ b/test/test_app/views/_three.html.erb @@ -0,0 +1 @@ +three diff --git a/test/test_app/views/about/_two.html.erb b/test/test_app/views/about/_two.html.erb new file mode 100644 index 0000000..efa4c53 --- /dev/null +++ b/test/test_app/views/about/_two.html.erb @@ -0,0 +1,2 @@ +two +<%= partial :three %> diff --git a/test/test_app/views/renders_partials.html.erb b/test/test_app/views/renders_partials.html.erb new file mode 100644 index 0000000..21af589 --- /dev/null +++ b/test/test_app/views/renders_partials.html.erb @@ -0,0 +1,4 @@ +before +<%= partial 'one' %> +<%= partial 'about/two' %> +after \ No newline at end of file