From 320c483ab98cfa94d34a0189d52fc5f8c87c3649 Mon Sep 17 00:00:00 2001 From: Igor Bochkariov Date: Wed, 9 Oct 2013 19:03:57 +0400 Subject: [PATCH 01/10] fix RenderDemo to use configuration of rendering/extensions --- padrino-helpers/test/fixtures/markup_app/app.rb | 4 +--- padrino-helpers/test/fixtures/render_app/app.rb | 4 ---- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/padrino-helpers/test/fixtures/markup_app/app.rb b/padrino-helpers/test/fixtures/markup_app/app.rb index 3336848a7..baffeaa9d 100644 --- a/padrino-helpers/test/fixtures/markup_app/app.rb +++ b/padrino-helpers/test/fixtures/markup_app/app.rb @@ -2,9 +2,7 @@ require 'haml' require 'erubis' require 'slim' -require 'padrino-core/application/rendering/extensions/erubis' -require 'padrino-core/application/rendering/extensions/haml' -require 'padrino-core/application/rendering/extensions/slim' +require 'padrino-core/application/rendering' class MarkupDemo < Sinatra::Base register Padrino::Helpers diff --git a/padrino-helpers/test/fixtures/render_app/app.rb b/padrino-helpers/test/fixtures/render_app/app.rb index bf4bc057f..d2e6340fb 100644 --- a/padrino-helpers/test/fixtures/render_app/app.rb +++ b/padrino-helpers/test/fixtures/render_app/app.rb @@ -2,7 +2,6 @@ PADRINO_ENV = 'test' unless defined? PADRINO_ENV require 'padrino-core' -require 'slim' class RenderUser attr_accessor :name @@ -17,9 +16,6 @@ class RenderDemo < Padrino::Application set :logging, false set :padrino_logging, false set :environment, :test - set :erb, :engine_class => Padrino::Erubis::SafeBufferTemplate - set :haml, :escape_html => true - set :slim, :generator => Temple::Generators::RailsOutputBuffer end # get current engines from partials From 73b6bc6ee96e3a2c393974ed351dfa15f91f1167 Mon Sep 17 00:00:00 2001 From: Igor Bochkariov Date: Wed, 9 Oct 2013 19:13:03 +0400 Subject: [PATCH 02/10] fix MarkupDemo --- padrino-helpers/test/fixtures/markup_app/app.rb | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/padrino-helpers/test/fixtures/markup_app/app.rb b/padrino-helpers/test/fixtures/markup_app/app.rb index baffeaa9d..c0b1d078a 100644 --- a/padrino-helpers/test/fixtures/markup_app/app.rb +++ b/padrino-helpers/test/fixtures/markup_app/app.rb @@ -1,20 +1,14 @@ -require 'sinatra/base' -require 'haml' -require 'erubis' -require 'slim' -require 'padrino-core/application/rendering' +require 'padrino-core' class MarkupDemo < Sinatra::Base register Padrino::Helpers + register Padrino::Rendering configure do set :logging, false set :padrino_logging, false set :environment, :test set :root, File.dirname(__FILE__) - set :erb, :engine_class => Padrino::Erubis::SafeBufferTemplate - set :haml, :escape_html => true - set :slim, :generator => Temple::Generators::RailsOutputBuffer, :buffer => "out_buf" set :sessions, true set :protect_from_csrf, true end From 752c0700b0ff32d76d32b1a6d71af875763f3d16 Mon Sep 17 00:00:00 2001 From: Igor Bochkariov Date: Wed, 9 Oct 2013 19:50:08 +0400 Subject: [PATCH 03/10] use hash to store template handlers --- padrino-helpers/lib/padrino-helpers/output_helpers.rb | 3 ++- .../lib/padrino-helpers/output_helpers/abstract_handler.rb | 6 +++--- .../lib/padrino-helpers/output_helpers/erb_handler.rb | 3 ++- .../lib/padrino-helpers/output_helpers/haml_handler.rb | 2 +- .../lib/padrino-helpers/output_helpers/slim_handler.rb | 2 +- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/padrino-helpers/lib/padrino-helpers/output_helpers.rb b/padrino-helpers/lib/padrino-helpers/output_helpers.rb index 69e8e945a..5d29db12c 100644 --- a/padrino-helpers/lib/padrino-helpers/output_helpers.rb +++ b/padrino-helpers/lib/padrino-helpers/output_helpers.rb @@ -185,7 +185,8 @@ def content_blocks # find_proper_handler => # def find_proper_handler - OutputHelpers.handlers.map { |h| h.new(self) }.find { |h| h.engines.include?(current_engine) && h.is_type? } + handler_class = OutputHelpers.handlers[current_engine] + handler_class && handler_class.new(self) end ## diff --git a/padrino-helpers/lib/padrino-helpers/output_helpers/abstract_handler.rb b/padrino-helpers/lib/padrino-helpers/output_helpers/abstract_handler.rb index 0ba386002..dae939cbd 100644 --- a/padrino-helpers/lib/padrino-helpers/output_helpers/abstract_handler.rb +++ b/padrino-helpers/lib/padrino-helpers/output_helpers/abstract_handler.rb @@ -8,7 +8,7 @@ module OutputHelpers # OutputHelpers.handlers => [, ] # def self.handlers - @_template_handlers ||= [] + @_template_handlers ||= {} end ## @@ -17,8 +17,8 @@ def self.handlers # @example # OutputHelpers.register(OutputHelpers::HamlHandler) # - def self.register(handler) - handlers << handler + def self.register(engine, handler) + handlers[engine] = handler end # @abstract Extend this to create a template handler. diff --git a/padrino-helpers/lib/padrino-helpers/output_helpers/erb_handler.rb b/padrino-helpers/lib/padrino-helpers/output_helpers/erb_handler.rb index faedf02f5..be1a2c2af 100644 --- a/padrino-helpers/lib/padrino-helpers/output_helpers/erb_handler.rb +++ b/padrino-helpers/lib/padrino-helpers/output_helpers/erb_handler.rb @@ -72,7 +72,8 @@ def output_buffer=(val) template.instance_variable_set(:@_out_buf, val) end end - OutputHelpers.register(ErbHandler) + OutputHelpers.register(:erb, ErbHandler) + OutputHelpers.register(:erubis, ErbHandler) end end end diff --git a/padrino-helpers/lib/padrino-helpers/output_helpers/haml_handler.rb b/padrino-helpers/lib/padrino-helpers/output_helpers/haml_handler.rb index dd57fd97f..027a1534a 100644 --- a/padrino-helpers/lib/padrino-helpers/output_helpers/haml_handler.rb +++ b/padrino-helpers/lib/padrino-helpers/output_helpers/haml_handler.rb @@ -57,7 +57,7 @@ def engines @_engines ||= [:haml] end end - OutputHelpers.register(HamlHandler) + OutputHelpers.register(:haml, HamlHandler) end end end diff --git a/padrino-helpers/lib/padrino-helpers/output_helpers/slim_handler.rb b/padrino-helpers/lib/padrino-helpers/output_helpers/slim_handler.rb index dfdb90930..adb868cd3 100644 --- a/padrino-helpers/lib/padrino-helpers/output_helpers/slim_handler.rb +++ b/padrino-helpers/lib/padrino-helpers/output_helpers/slim_handler.rb @@ -72,7 +72,7 @@ def output_buffer=(val) template.instance_variable_set(:@_out_buf, val) end end - OutputHelpers.register(SlimHandler) + OutputHelpers.register(:slim, SlimHandler) end end end From 7f576c5f5fade24111efef3c33ca678b40c8e3ad Mon Sep 17 00:00:00 2001 From: Igor Bochkariov Date: Wed, 9 Oct 2013 20:57:41 +0400 Subject: [PATCH 04/10] move special code to template handlets --- padrino-helpers/lib/padrino-helpers/output_helpers.rb | 11 ++++------- .../lib/padrino-helpers/output_helpers/erb_handler.rb | 2 +- .../padrino-helpers/output_helpers/haml_handler.rb | 3 ++- .../padrino-helpers/output_helpers/slim_handler.rb | 2 +- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/padrino-helpers/lib/padrino-helpers/output_helpers.rb b/padrino-helpers/lib/padrino-helpers/output_helpers.rb index 5d29db12c..a874f0ff4 100644 --- a/padrino-helpers/lib/padrino-helpers/output_helpers.rb +++ b/padrino-helpers/lib/padrino-helpers/output_helpers.rb @@ -45,14 +45,11 @@ def render(engine, *) # # => "" # def capture_html(*args, &block) - handler = find_proper_handler - captured_block, captured_html = nil, "" - if handler && handler.is_type? && handler.block_is_type?(block) - captured_html, captured_block = handler.capture_from_template(*args, &block) + if handler = find_proper_handler + handler.capture_from_template(*args, &block) + else + block.call(*args) end - # invoking the block directly if there was no template - captured_html = block_given? && ( captured_block || block.call(*args) ) if captured_html.blank? - captured_html end alias :capture :capture_html diff --git a/padrino-helpers/lib/padrino-helpers/output_helpers/erb_handler.rb b/padrino-helpers/lib/padrino-helpers/output_helpers/erb_handler.rb index be1a2c2af..733d08783 100644 --- a/padrino-helpers/lib/padrino-helpers/output_helpers/erb_handler.rb +++ b/padrino-helpers/lib/padrino-helpers/output_helpers/erb_handler.rb @@ -33,7 +33,7 @@ def capture_from_template(*args, &block) captured_block = block.call(*args) ret = eval("@_out_buf", block.binding) self.output_buffer = _buf_was - [ ret, captured_block ] + ret.blank? ? captured_block : ret end ## diff --git a/padrino-helpers/lib/padrino-helpers/output_helpers/haml_handler.rb b/padrino-helpers/lib/padrino-helpers/output_helpers/haml_handler.rb index 027a1534a..ebc034e4b 100644 --- a/padrino-helpers/lib/padrino-helpers/output_helpers/haml_handler.rb +++ b/padrino-helpers/lib/padrino-helpers/output_helpers/haml_handler.rb @@ -33,7 +33,8 @@ def block_is_type?(block) # def capture_from_template(*args, &block) eval("_hamlout ||= @haml_buffer", block.binding) - template.capture_haml(*args, &block) + capture_html = template.capture_haml(*args, &block) + capture_html.blank? ? block.call(*args) : capture_html end ## diff --git a/padrino-helpers/lib/padrino-helpers/output_helpers/slim_handler.rb b/padrino-helpers/lib/padrino-helpers/output_helpers/slim_handler.rb index adb868cd3..a94bfe304 100644 --- a/padrino-helpers/lib/padrino-helpers/output_helpers/slim_handler.rb +++ b/padrino-helpers/lib/padrino-helpers/output_helpers/slim_handler.rb @@ -33,7 +33,7 @@ def capture_from_template(*args, &block) captured_block = block.call(*args) ret = eval("@_out_buf", block.binding) self.output_buffer = _buf_was - [ ret, captured_block ] + block_is_type?(block) ? ret : captured_block end ## From 5bab185208925bf19748932d390d4f189cf592a3 Mon Sep 17 00:00:00 2001 From: Igor Bochkariov Date: Wed, 9 Oct 2013 21:30:43 +0400 Subject: [PATCH 05/10] fix haml --- .../lib/padrino-helpers/output_helpers/haml_handler.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/padrino-helpers/lib/padrino-helpers/output_helpers/haml_handler.rb b/padrino-helpers/lib/padrino-helpers/output_helpers/haml_handler.rb index ebc034e4b..f5f48a466 100644 --- a/padrino-helpers/lib/padrino-helpers/output_helpers/haml_handler.rb +++ b/padrino-helpers/lib/padrino-helpers/output_helpers/haml_handler.rb @@ -32,9 +32,7 @@ def block_is_type?(block) # @handler.capture_from_template(&block) => "...html..." # def capture_from_template(*args, &block) - eval("_hamlout ||= @haml_buffer", block.binding) - capture_html = template.capture_haml(*args, &block) - capture_html.blank? ? block.call(*args) : capture_html + block_is_type?(block) ? template.capture_haml(*args, &block) : block.call(*args) end ## From 73ea9ca555617566aae4eab8fb2ef7fac815cc8e Mon Sep 17 00:00:00 2001 From: Igor Bochkariov Date: Wed, 9 Oct 2013 21:47:27 +0400 Subject: [PATCH 06/10] detect erb properly --- .../lib/padrino-helpers/output_helpers/erb_handler.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/padrino-helpers/lib/padrino-helpers/output_helpers/erb_handler.rb b/padrino-helpers/lib/padrino-helpers/output_helpers/erb_handler.rb index 733d08783..41e8b31cc 100644 --- a/padrino-helpers/lib/padrino-helpers/output_helpers/erb_handler.rb +++ b/padrino-helpers/lib/padrino-helpers/output_helpers/erb_handler.rb @@ -33,7 +33,7 @@ def capture_from_template(*args, &block) captured_block = block.call(*args) ret = eval("@_out_buf", block.binding) self.output_buffer = _buf_was - ret.blank? ? captured_block : ret + block_is_type?(block) ? ret : captured_block end ## From 6a276d96d46b9fb7fa4fab2ab947dc4d29e95c88 Mon Sep 17 00:00:00 2001 From: Igor Bochkariov Date: Wed, 9 Oct 2013 22:32:41 +0400 Subject: [PATCH 07/10] fix tests to use `=`, check for content_for double output --- .../test/fixtures/markup_app/views/button_to.haml | 4 ++-- .../fixtures/markup_app/views/capture_concat.haml | 8 ++++---- .../fixtures/markup_app/views/capture_concat.slim | 8 ++++---- .../test/fixtures/markup_app/views/content_for.erb | 2 +- .../test/fixtures/markup_app/views/content_for.slim | 2 +- .../test/fixtures/markup_app/views/content_tag.haml | 4 ++-- .../test/fixtures/markup_app/views/fields_for.haml | 8 ++++---- .../test/fixtures/markup_app/views/form_for.haml | 6 +++--- .../test/fixtures/markup_app/views/form_tag.haml | 12 ++++++------ .../test/fixtures/markup_app/views/link_to.haml | 2 +- .../fixtures/markup_app/views/simple_partial.slim | 2 +- .../fixtures/render_app/views/double_capture_erb.erb | 4 ++-- .../render_app/views/double_capture_haml.haml | 2 +- .../render_app/views/double_capture_slim.slim | 2 +- padrino-helpers/test/test_output_helpers.rb | 12 ++++++------ 15 files changed, 39 insertions(+), 39 deletions(-) diff --git a/padrino-helpers/test/fixtures/markup_app/views/button_to.haml b/padrino-helpers/test/fixtures/markup_app/views/button_to.haml index fa1898449..570db5a04 100644 --- a/padrino-helpers/test/fixtures/markup_app/views/button_to.haml +++ b/padrino-helpers/test/fixtures/markup_app/views/button_to.haml @@ -1,5 +1,5 @@ -- button_to 'Foo button', '/foo', :class => 'foo-form' do - - field_set_tag do += button_to 'Foo button', '/foo', :class => 'foo-form' do + = field_set_tag do = label_tag :username = content_tag(:p, 'button_to test', :id => 'test-point') = button_to 'Bar button', '/bar' diff --git a/padrino-helpers/test/fixtures/markup_app/views/capture_concat.haml b/padrino-helpers/test/fixtures/markup_app/views/capture_concat.haml index 527c0115d..4a28cbd71 100644 --- a/padrino-helpers/test/fixtures/markup_app/views/capture_concat.haml +++ b/padrino-helpers/test/fixtures/markup_app/views/capture_concat.haml @@ -1,12 +1,12 @@ -- @content = captured_content do += @content = captured_content do %span Captured Line 1 %span Captured Line 2 = @content -- concat_in_p('Concat Line 3') += concat_in_p('Concat Line 3') -- concat_if_block_is_template('haml') do += concat_if_block_is_template('haml') do %span This is haml %span This is haml -- concat_ruby_not_template_block += concat_ruby_not_template_block diff --git a/padrino-helpers/test/fixtures/markup_app/views/capture_concat.slim b/padrino-helpers/test/fixtures/markup_app/views/capture_concat.slim index 04186e7be..3827a9cda 100644 --- a/padrino-helpers/test/fixtures/markup_app/views/capture_concat.slim +++ b/padrino-helpers/test/fixtures/markup_app/views/capture_concat.slim @@ -1,12 +1,12 @@ -- @content = captured_content do += @content = captured_content do span Captured Line 1 span Captured Line 2 = @content -- concat_in_p('Concat Line 3') += concat_in_p('Concat Line 3') -- concat_if_block_is_template('slim') do += concat_if_block_is_template('slim') do span This is slim span This is slim -- concat_ruby_not_template_block \ No newline at end of file += concat_ruby_not_template_block diff --git a/padrino-helpers/test/fixtures/markup_app/views/content_for.erb b/padrino-helpers/test/fixtures/markup_app/views/content_for.erb index bede704d7..71f5c2529 100644 --- a/padrino-helpers/test/fixtures/markup_app/views/content_for.erb +++ b/padrino-helpers/test/fixtures/markup_app/views/content_for.erb @@ -11,4 +11,4 @@
<%= yield_content :demo2, "Johnny", "Smith" %>
<%= content_for?(:demo).to_s %> -
<%= content_for?(:fake).to_s %> \ No newline at end of file +
<%= content_for?(:fake).to_s %> diff --git a/padrino-helpers/test/fixtures/markup_app/views/content_for.slim b/padrino-helpers/test/fixtures/markup_app/views/content_for.slim index be66c854d..8da1ee288 100644 --- a/padrino-helpers/test/fixtures/markup_app/views/content_for.slim +++ b/padrino-helpers/test/fixtures/markup_app/views/content_for.slim @@ -9,4 +9,4 @@ .demo2= yield_content :demo2, "Johnny", "Smith" .demo_has_content= content_for?(:demo) -.fake_has_content= content_for?(:fake) \ No newline at end of file +.fake_has_content= content_for?(:fake) diff --git a/padrino-helpers/test/fixtures/markup_app/views/content_tag.haml b/padrino-helpers/test/fixtures/markup_app/views/content_tag.haml index 9a56dc189..55812685e 100644 --- a/padrino-helpers/test/fixtures/markup_app/views/content_tag.haml +++ b/padrino-helpers/test/fixtures/markup_app/views/content_tag.haml @@ -2,10 +2,10 @@ = content_tag :p, "Test 2" -- content_tag(:p, :class => 'test', :id => 'test3') do += content_tag(:p, :class => 'test', :id => 'test3') do %span Test 3 -- content_tag(:p) do += content_tag(:p) do %span Test 4 = content_tag_with_block diff --git a/padrino-helpers/test/fixtures/markup_app/views/fields_for.haml b/padrino-helpers/test/fixtures/markup_app/views/fields_for.haml index 6a67d79b7..9046b11c2 100644 --- a/padrino-helpers/test/fixtures/markup_app/views/fields_for.haml +++ b/padrino-helpers/test/fixtures/markup_app/views/fields_for.haml @@ -1,13 +1,13 @@ - @user = MarkupUser.new -- form_for @user , '/demo1', :id => 'demo-fields-for' do |f| += form_for @user , '/demo1', :id => 'demo-fields-for' do |f| = f.text_field :gender - - fields_for @user.permission do |permission| + = fields_for @user.permission do |permission| = permission.check_box :can_edit = permission.check_box :can_delete - - f.fields_for :telephone do |child_form| + = f.fields_for :telephone do |child_form| = child_form.label :number = child_form.text_field :number - - f.fields_for :addresses do |child_form| + = f.fields_for :addresses do |child_form| = child_form.label :name = child_form.text_field :name - unless child_form.object.new_record? diff --git a/padrino-helpers/test/fixtures/markup_app/views/form_for.haml b/padrino-helpers/test/fixtures/markup_app/views/form_for.haml index dfc71a00e..59e9573c6 100644 --- a/padrino-helpers/test/fixtures/markup_app/views/form_for.haml +++ b/padrino-helpers/test/fixtures/markup_app/views/form_for.haml @@ -1,4 +1,4 @@ -- form_for MarkupUser.new, '/demo', :id => 'demo' do |f| += form_for MarkupUser.new, '/demo', :id => 'demo' do |f| = f.error_messages(:header_message => "custom MarkupUser cannot be saved!") = f.hidden_field :session_id %p @@ -43,7 +43,7 @@ %p = f.image_submit "buttons/post.png", :class => 'success', :id => 'image-button' -- form_for MarkupUser.new, '/another_demo', :id => 'demo2', :method => 'get' do |f| += form_for MarkupUser.new, '/another_demo', :id => 'demo2', :method => 'get' do |f| = f.error_messages :header_message => "custom MarkupUser cannot be saved!" = f.hidden_field :session_id = f.text_field_block :username, { :class => 'input' }, { :caption => 'Nickname: ', :class => 'label' } @@ -55,5 +55,5 @@ = f.submit_block "Create", { :class => 'button' } = f.image_submit_block "buttons/ok.png", { :class => 'image' } -- form_for :markup_user, '/third_demo', :id => 'demo3', :method => 'get' do |f| += form_for :markup_user, '/third_demo', :id => 'demo3', :method => 'get' do |f| = f.text_field_block :username diff --git a/padrino-helpers/test/fixtures/markup_app/views/form_tag.haml b/padrino-helpers/test/fixtures/markup_app/views/form_tag.haml index 4cf41167d..b39a31318 100644 --- a/padrino-helpers/test/fixtures/markup_app/views/form_tag.haml +++ b/padrino-helpers/test/fixtures/markup_app/views/form_tag.haml @@ -1,6 +1,6 @@ -- form_tag '/simple', :class => 'simple-form' do += form_tag '/simple', :class => 'simple-form' do = error_messages_for nil - - field_set_tag do + = field_set_tag do = hidden_field_tag :session_id, :value => "__secret__" = label_tag :username = text_field_tag :username @@ -26,10 +26,10 @@ = check_box_tag :remember_me = submit_tag -- form_tag '/advanced', :id => 'advanced', :class => 'advanced-form', :method => 'get' do += form_tag '/advanced', :id => 'advanced', :class => 'advanced-form', :method => 'get' do = error_messages_for MarkupUser.new, :header_message => "There are problems with saving user!" = hidden_field_tag :session_id, :value => "__secret__" - - field_set_tag "Advanced", :class => 'advanced-field-set' do + = field_set_tag "Advanced", :class => 'advanced-field-set' do %p = label_tag :username, :class => 'first', :caption => "Nickname" = text_field_tag :username, :value => params[:username], :id => 'the_username' @@ -69,10 +69,10 @@ %p = range_field_tag('ranger_with_min_max', :min => 1, :max => 50) = range_field_tag('ranger_with_range', :range => 1..5) - - field_set_tag(:class => 'buttons') do + = field_set_tag(:class => 'buttons') do = submit_tag "Login" = button_tag "Cancel" = image_submit_tag "buttons/submit.png" -- form_tag '/dontprotect', :class => 'no-protection', :protect_from_csrf => false do += form_tag '/dontprotect', :class => 'no-protection', :protect_from_csrf => false do = submit_tag "Login" diff --git a/padrino-helpers/test/fixtures/markup_app/views/link_to.haml b/padrino-helpers/test/fixtures/markup_app/views/link_to.haml index b53277d4f..ee3174701 100644 --- a/padrino-helpers/test/fixtures/markup_app/views/link_to.haml +++ b/padrino-helpers/test/fixtures/markup_app/views/link_to.haml @@ -1,4 +1,4 @@ = link_to "Test 1 No Block", '/test1', :class => 'test', :id => 'test1' -- link_to("/test2", :class => 'test', :id => 'test2') do += link_to("/test2", :class => 'test', :id => 'test2') do %span Test 2 With Block diff --git a/padrino-helpers/test/fixtures/markup_app/views/simple_partial.slim b/padrino-helpers/test/fixtures/markup_app/views/simple_partial.slim index ca16c418e..698d9a0ef 100644 --- a/padrino-helpers/test/fixtures/markup_app/views/simple_partial.slim +++ b/padrino-helpers/test/fixtures/markup_app/views/simple_partial.slim @@ -1 +1 @@ -p.slim= partial 'partials/slim', :engine => "slim" \ No newline at end of file +p.slim= partial 'partials/slim', :engine => "slim" diff --git a/padrino-helpers/test/fixtures/render_app/views/double_capture_erb.erb b/padrino-helpers/test/fixtures/render_app/views/double_capture_erb.erb index c7f7e123c..aae2849a1 100644 --- a/padrino-helpers/test/fixtures/render_app/views/double_capture_erb.erb +++ b/padrino-helpers/test/fixtures/render_app/views/double_capture_erb.erb @@ -1,3 +1,3 @@ -<% form_for( :object, '/' ) do |f| %> +<% form_for( :object, '/' ) do |f| %> <%= $number_of_captures += 1 %> -<% end %> \ No newline at end of file +<% end %> diff --git a/padrino-helpers/test/fixtures/render_app/views/double_capture_haml.haml b/padrino-helpers/test/fixtures/render_app/views/double_capture_haml.haml index 26102762b..28951e3d9 100644 --- a/padrino-helpers/test/fixtures/render_app/views/double_capture_haml.haml +++ b/padrino-helpers/test/fixtures/render_app/views/double_capture_haml.haml @@ -1,2 +1,2 @@ -- form_for :object, '/' do |f| += form_for :object, '/' do |f| = $number_of_captures += 1 diff --git a/padrino-helpers/test/fixtures/render_app/views/double_capture_slim.slim b/padrino-helpers/test/fixtures/render_app/views/double_capture_slim.slim index 26102762b..28951e3d9 100644 --- a/padrino-helpers/test/fixtures/render_app/views/double_capture_slim.slim +++ b/padrino-helpers/test/fixtures/render_app/views/double_capture_slim.slim @@ -1,2 +1,2 @@ -- form_for :object, '/' do |f| += form_for :object, '/' do |f| = $number_of_captures += 1 diff --git a/padrino-helpers/test/test_output_helpers.rb b/padrino-helpers/test/test_output_helpers.rb index 2140bec26..81c75f496 100644 --- a/padrino-helpers/test/test_output_helpers.rb +++ b/padrino-helpers/test/test_output_helpers.rb @@ -9,20 +9,20 @@ def app context 'for #content_for method' do should 'work for erb templates' do visit '/erb/content_for' - assert_have_selector '.demo h1', :content => "This is content yielded from a content_for" - assert_have_selector '.demo2 h1', :content => "This is content yielded with name Johnny Smith" + assert_have_selector '.demo h1', :content => "This is content yielded from a content_for", :count => 1 + assert_have_selector '.demo2 h1', :content => "This is content yielded with name Johnny Smith", :count => 1 end should "work for haml templates" do visit '/haml/content_for' - assert_have_selector '.demo h1', :content => "This is content yielded from a content_for" - assert_have_selector '.demo2 h1', :content => "This is content yielded with name Johnny Smith" + assert_have_selector '.demo h1', :content => "This is content yielded from a content_for", :count => 1 + assert_have_selector '.demo2 h1', :content => "This is content yielded with name Johnny Smith", :count => 1 end should "work for slim templates" do visit '/slim/content_for' - assert_have_selector '.demo h1', :content => "This is content yielded from a content_for" - assert_have_selector '.demo2 h1', :content => "This is content yielded with name Johnny Smith" + assert_have_selector '.demo h1', :content => "This is content yielded from a content_for", :count => 1 + assert_have_selector '.demo2 h1', :content => "This is content yielded with name Johnny Smith", :count => 1 end end # content_for From 8ae11502f0e91aa22adc772b209bdacf40cc4a0b Mon Sep 17 00:00:00 2001 From: Igor Bochkariov Date: Wed, 9 Oct 2013 22:42:02 +0400 Subject: [PATCH 08/10] check output count, enable skipped tests --- .../markup_app/views/capture_concat.haml | 2 +- .../markup_app/views/capture_concat.slim | 2 +- padrino-helpers/test/test_output_helpers.rb | 29 +++++++++---------- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/padrino-helpers/test/fixtures/markup_app/views/capture_concat.haml b/padrino-helpers/test/fixtures/markup_app/views/capture_concat.haml index 4a28cbd71..37e0f3ddf 100644 --- a/padrino-helpers/test/fixtures/markup_app/views/capture_concat.haml +++ b/padrino-helpers/test/fixtures/markup_app/views/capture_concat.haml @@ -1,4 +1,4 @@ -= @content = captured_content do +- @content = captured_content do %span Captured Line 1 %span Captured Line 2 = @content diff --git a/padrino-helpers/test/fixtures/markup_app/views/capture_concat.slim b/padrino-helpers/test/fixtures/markup_app/views/capture_concat.slim index 3827a9cda..4a72b2ac1 100644 --- a/padrino-helpers/test/fixtures/markup_app/views/capture_concat.slim +++ b/padrino-helpers/test/fixtures/markup_app/views/capture_concat.slim @@ -1,4 +1,4 @@ -= @content = captured_content do +- @content = captured_content do span Captured Line 1 span Captured Line 2 = @content diff --git a/padrino-helpers/test/test_output_helpers.rb b/padrino-helpers/test/test_output_helpers.rb index 81c75f496..ba66ef772 100644 --- a/padrino-helpers/test/test_output_helpers.rb +++ b/padrino-helpers/test/test_output_helpers.rb @@ -49,20 +49,20 @@ def app context 'for #capture_html method' do should "work for erb templates" do visit '/erb/capture_concat' - assert_have_selector 'p span', :content => "Captured Line 1" - assert_have_selector 'p span', :content => "Captured Line 2" + assert_have_selector 'p span', :content => "Captured Line 1", :count => 1 + assert_have_selector 'p span', :content => "Captured Line 2", :count => 1 end should "work for haml templates" do visit '/haml/capture_concat' - assert_have_selector 'p span', :content => "Captured Line 1" - assert_have_selector 'p span', :content => "Captured Line 2" + assert_have_selector 'p span', :content => "Captured Line 1", :count => 1 + assert_have_selector 'p span', :content => "Captured Line 2", :count => 1 end should "work for slim templates" do visit '/slim/capture_concat' - assert_have_selector 'p span', :content => "Captured Line 1" - assert_have_selector 'p span', :content => "Captured Line 2" + assert_have_selector 'p span', :content => "Captured Line 1", :count => 1 + assert_have_selector 'p span', :content => "Captured Line 2", :count => 1 end end @@ -79,30 +79,27 @@ def app should "work for slim templates" do visit '/slim/capture_concat' - # TODO Get Slim concat working - # assert_have_selector 'p', :content => "Concat Line 3", :count => 1 + assert_have_selector 'p', :content => "Concat Line 3", :count => 1 end end context 'for #block_is_template?' do should "work for erb templates" do visit '/erb/capture_concat' - assert_have_selector 'p', :content => "The erb block passed in is a template", :class => 'is_template' - # TODO Get ERB template detection working (fix block_is_erb? method) - # assert_have_no_selector 'p', :content => "The ruby block passed in is a template", :class => 'is_template' + assert_have_selector 'p', :content => "The erb block passed in is a template", :class => 'is_template', :count => 1 + assert_have_no_selector 'p', :content => "The ruby block passed in is a template", :class => 'is_template', :count => 1 end should "work for haml templates" do visit '/haml/capture_concat' - assert_have_selector 'p', :content => "The haml block passed in is a template", :class => 'is_template' - assert_have_no_selector 'p', :content => "The ruby block passed in is a template", :class => 'is_template' + assert_have_selector 'p', :content => "The haml block passed in is a template", :class => 'is_template', :count => 1 + assert_have_no_selector 'p', :content => "The ruby block passed in is a template", :class => 'is_template', :count => 1 end should "work for slim templates" do visit '/slim/capture_concat' - # TODO Get Slim template detection working - # assert_have_selector 'p', :content => "The slim block passed in is a template", :class => 'is_template' - assert_have_no_selector 'p', :content => "The ruby block passed in is a template", :class => 'is_template' + assert_have_selector 'p', :content => "The slim block passed in is a template", :class => 'is_template', :count => 1 + assert_have_no_selector 'p', :content => "The ruby block passed in is a template", :class => 'is_template', :count => 1 end end From 25af5b5e30c5e51f50c3ee08d1374b3bff8e695b Mon Sep 17 00:00:00 2001 From: Igor Bochkariov Date: Thu, 10 Oct 2013 02:43:46 +0400 Subject: [PATCH 09/10] cleanup unused methods, clarify block_is_type? --- .../lib/padrino-helpers/output_helpers.rb | 7 ++-- .../output_helpers/abstract_handler.rb | 24 ++----------- .../output_helpers/erb_handler.rb | 35 +++++-------------- .../output_helpers/haml_handler.rb | 26 ++------------ .../output_helpers/slim_handler.rb | 35 +++++-------------- 5 files changed, 24 insertions(+), 103 deletions(-) diff --git a/padrino-helpers/lib/padrino-helpers/output_helpers.rb b/padrino-helpers/lib/padrino-helpers/output_helpers.rb index a874f0ff4..907b659fd 100644 --- a/padrino-helpers/lib/padrino-helpers/output_helpers.rb +++ b/padrino-helpers/lib/padrino-helpers/output_helpers.rb @@ -65,10 +65,9 @@ def capture_html(*args, &block) # concat_content("This will be output to the template buffer") # def concat_content(text="") - handler = find_proper_handler - if handler && handler.is_type? + if handler = find_proper_handler handler.concat_to_template(text) - else # theres no template to concat, return the text directly + else text end end @@ -102,7 +101,7 @@ def concat_safe_content(text="") # def block_is_template?(block) handler = find_proper_handler - block && handler && handler.block_is_type?(block) + block && handler && handler.engine_matches?(block) end ## diff --git a/padrino-helpers/lib/padrino-helpers/output_helpers/abstract_handler.rb b/padrino-helpers/lib/padrino-helpers/output_helpers/abstract_handler.rb index dae939cbd..776cb62f9 100644 --- a/padrino-helpers/lib/padrino-helpers/output_helpers/abstract_handler.rb +++ b/padrino-helpers/lib/padrino-helpers/output_helpers/abstract_handler.rb @@ -41,33 +41,13 @@ def template_extension # => "erb" end - ## - # Returns an array of engines used for the template. - # - # @example - # @handler.engines => [:erb, :erubis] - # - def engines - # Implemented in subclass. - end - - ## - # Returns true if the current template type is same as this handlers; false otherwise. - # - # @example - # @handler.is_type? => true - # - def is_type? - # Implemented in subclass. - end - ## # Returns true if the block given is of the handler's template type; false otherwise. # # @example - # @handler.block_is_type?(block) => true + # @handler.engine_matches?(block) => true # - def block_is_type?(block) + def engine_matches?(block) # Implemented in subclass. end diff --git a/padrino-helpers/lib/padrino-helpers/output_helpers/erb_handler.rb b/padrino-helpers/lib/padrino-helpers/output_helpers/erb_handler.rb index 41e8b31cc..7b5ce5fc7 100644 --- a/padrino-helpers/lib/padrino-helpers/output_helpers/erb_handler.rb +++ b/padrino-helpers/lib/padrino-helpers/output_helpers/erb_handler.rb @@ -12,16 +12,6 @@ def initialize(template) @output_buffer = template.instance_variable_get(:@_out_buf) end - ## - # Returns true if the current template type is same as this handlers; false otherwise. - # - # @example - # @handler.is_type? => true - # - def is_type? - !self.output_buffer.nil? - end - ## # Captures the html from a block of template code for this handler. # @@ -30,10 +20,10 @@ def is_type? # def capture_from_template(*args, &block) self.output_buffer, _buf_was = ActiveSupport::SafeBuffer.new, self.output_buffer - captured_block = block.call(*args) - ret = eval("@_out_buf", block.binding) + raw = block.call(*args) + captured = template.instance_variable_get(:@_out_buf) self.output_buffer = _buf_was - block_is_type?(block) ? ret : captured_block + engine_matches?(block) ? captured : raw end ## @@ -43,7 +33,7 @@ def capture_from_template(*args, &block) # @handler.concat_to_template("This will be output to the template buffer") # def concat_to_template(text="") - self.output_buffer << text if is_type? && text + self.output_buffer << text if text nil end @@ -51,23 +41,14 @@ def concat_to_template(text="") # Returns true if the block given is of the handler's template type; false otherwise. # # @example - # @handler.block_is_type?(block) => true + # @handler.engine_matches?(block) => true # - def block_is_type?(block) - block && eval('!!defined?(__in_erb_template)', block.binding) - end - - ## - # Returns an array of engines used for the template. - # - # @example - # @handler.engines => [:erb, :erubis] - # - def engines - @_engines ||= [:erb, :erubis] + def engine_matches?(block) + block.binding.eval('defined? __in_erb_template') end protected + def output_buffer=(val) template.instance_variable_set(:@_out_buf, val) end diff --git a/padrino-helpers/lib/padrino-helpers/output_helpers/haml_handler.rb b/padrino-helpers/lib/padrino-helpers/output_helpers/haml_handler.rb index f5f48a466..96502d4f9 100644 --- a/padrino-helpers/lib/padrino-helpers/output_helpers/haml_handler.rb +++ b/padrino-helpers/lib/padrino-helpers/output_helpers/haml_handler.rb @@ -5,23 +5,13 @@ module OutputHelpers # Handler for reading and writing from a haml template. # class HamlHandler < AbstractHandler - ## - # Returns true if the current template type is same as this handlers; false otherwise. - # - # @example - # @handler.is_type? => true - # - def is_type? - template.respond_to?(:is_haml?) && template.is_haml? - end - ## # Returns true if the block given is of the handler's template type; false otherwise. # # @example - # @handler.block_is_type?(block) => true + # @handler.engine_matches?(block) => true # - def block_is_type?(block) + def engine_matches?(block) template.block_is_haml?(block) end @@ -32,7 +22,7 @@ def block_is_type?(block) # @handler.capture_from_template(&block) => "...html..." # def capture_from_template(*args, &block) - block_is_type?(block) ? template.capture_haml(*args, &block) : block.call(*args) + engine_matches?(block) ? template.capture_haml(*args, &block) : block.call(*args) end ## @@ -45,16 +35,6 @@ def concat_to_template(text="") template.haml_concat(text) nil end - - ## - # Returns an array of engines used for the template. - # - # @example - # @handler.engines => [:haml] - # - def engines - @_engines ||= [:haml] - end end OutputHelpers.register(:haml, HamlHandler) end diff --git a/padrino-helpers/lib/padrino-helpers/output_helpers/slim_handler.rb b/padrino-helpers/lib/padrino-helpers/output_helpers/slim_handler.rb index a94bfe304..3cbbf97d4 100644 --- a/padrino-helpers/lib/padrino-helpers/output_helpers/slim_handler.rb +++ b/padrino-helpers/lib/padrino-helpers/output_helpers/slim_handler.rb @@ -12,16 +12,6 @@ def initialize(template) @output_buffer = template.instance_variable_get(:@_out_buf) end - ## - # Returns true if the current template type is same as this handlers; false otherwise. - # - # @example - # @handler.is_type? => true - # - def is_type? - !self.output_buffer.nil? - end - ## # Captures the html from a block of template code for this handler. # @@ -30,10 +20,10 @@ def is_type? # def capture_from_template(*args, &block) self.output_buffer, _buf_was = ActiveSupport::SafeBuffer.new, self.output_buffer - captured_block = block.call(*args) - ret = eval("@_out_buf", block.binding) + raw = block.call(*args) + captured = template.instance_variable_get(:@_out_buf) self.output_buffer = _buf_was - block_is_type?(block) ? ret : captured_block + engine_matches?(block) ? captured : raw end ## @@ -43,7 +33,7 @@ def capture_from_template(*args, &block) # @handler.concat_to_template("This will be output to the template buffer") # def concat_to_template(text="") - self.output_buffer << text if is_type? && text + self.output_buffer << text if text nil end @@ -51,23 +41,14 @@ def concat_to_template(text="") # Returns true if the block given is of the handler's template type; false otherwise. # # @example - # @handler.block_is_type?(block) => true + # @handler.engine_matches?(block) => true # - def block_is_type?(block) - block && eval('defined? __in_slim_template', block.binding) - end - - ## - # Returns an array of engines used for the template. - # - # @example - # @handler.engines => [:erb, :erubis] - # - def engines - @_engines ||= [:slim] + def engine_matches?(block) + block.binding.eval('defined? __in_slim_template') end protected + def output_buffer=(val) template.instance_variable_set(:@_out_buf, val) end From 80a0e2446454a69cc307aac9499ad08085a0c15d Mon Sep 17 00:00:00 2001 From: Igor Bochkariov Date: Thu, 10 Oct 2013 11:29:23 +0400 Subject: [PATCH 10/10] disable concatenating `-` in templates for Slim and Haml, add tests for this --- .../lib/padrino-helpers/form_helpers.rb | 4 +--- .../output_helpers/abstract_handler.rb | 6 +++++- .../output_helpers/haml_handler.rb | 11 ----------- .../output_helpers/slim_handler.rb | 11 ----------- padrino-helpers/test/fixtures/render_app/app.rb | 4 ++++ .../render_app/views/wrong_capture_erb.erb | 3 +++ .../render_app/views/wrong_capture_haml.haml | 2 ++ .../render_app/views/wrong_capture_slim.slim | 2 ++ padrino-helpers/test/test_render_helpers.rb | 16 ++++++++++++++++ 9 files changed, 33 insertions(+), 26 deletions(-) create mode 100644 padrino-helpers/test/fixtures/render_app/views/wrong_capture_erb.erb create mode 100644 padrino-helpers/test/fixtures/render_app/views/wrong_capture_haml.haml create mode 100644 padrino-helpers/test/fixtures/render_app/views/wrong_capture_slim.slim diff --git a/padrino-helpers/lib/padrino-helpers/form_helpers.rb b/padrino-helpers/lib/padrino-helpers/form_helpers.rb index a346fbfd9..aca22a304 100644 --- a/padrino-helpers/lib/padrino-helpers/form_helpers.rb +++ b/padrino-helpers/lib/padrino-helpers/form_helpers.rb @@ -90,9 +90,7 @@ def form_tag(url, options={}, &block) inner_form_html << csrf_token_field end inner_form_html << mark_safe(capture_html(&block)) - not_concat = options.delete(:not_concat) - form_html = content_tag(:form, inner_form_html, options) - not_concat ? form_html : concat_content(form_html) + concat_content content_tag(:form, inner_form_html, options) end ## diff --git a/padrino-helpers/lib/padrino-helpers/output_helpers/abstract_handler.rb b/padrino-helpers/lib/padrino-helpers/output_helpers/abstract_handler.rb index 776cb62f9..ee7709f90 100644 --- a/padrino-helpers/lib/padrino-helpers/output_helpers/abstract_handler.rb +++ b/padrino-helpers/lib/padrino-helpers/output_helpers/abstract_handler.rb @@ -64,11 +64,15 @@ def capture_from_template(*args, &block) ## # Outputs the given text to the templates buffer directly. # + # This method is called when template uses block-aware helpers. For Slim and Haml such + # helpers just return output to use with `=`. For Erb this method is implemented in + # ErbHandler by concatenating text captured from the block to output buffer. + # # @example # @handler.concat_to_template("This will be output to the template buffer") # def concat_to_template(text="") - # Implemented in subclass. + text end end end diff --git a/padrino-helpers/lib/padrino-helpers/output_helpers/haml_handler.rb b/padrino-helpers/lib/padrino-helpers/output_helpers/haml_handler.rb index 96502d4f9..23e5bc88c 100644 --- a/padrino-helpers/lib/padrino-helpers/output_helpers/haml_handler.rb +++ b/padrino-helpers/lib/padrino-helpers/output_helpers/haml_handler.rb @@ -24,17 +24,6 @@ def engine_matches?(block) def capture_from_template(*args, &block) engine_matches?(block) ? template.capture_haml(*args, &block) : block.call(*args) end - - ## - # Outputs the given text to the templates buffer directly. - # - # @example - # @handler.concat_to_template("This will be output to the template buffer") - # - def concat_to_template(text="") - template.haml_concat(text) - nil - end end OutputHelpers.register(:haml, HamlHandler) end diff --git a/padrino-helpers/lib/padrino-helpers/output_helpers/slim_handler.rb b/padrino-helpers/lib/padrino-helpers/output_helpers/slim_handler.rb index 3cbbf97d4..e350b2a8f 100644 --- a/padrino-helpers/lib/padrino-helpers/output_helpers/slim_handler.rb +++ b/padrino-helpers/lib/padrino-helpers/output_helpers/slim_handler.rb @@ -26,17 +26,6 @@ def capture_from_template(*args, &block) engine_matches?(block) ? captured : raw end - ## - # Outputs the given text to the templates buffer directly. - # - # @example - # @handler.concat_to_template("This will be output to the template buffer") - # - def concat_to_template(text="") - self.output_buffer << text if text - nil - end - ## # Returns true if the block given is of the handler's template type; false otherwise. # diff --git a/padrino-helpers/test/fixtures/render_app/app.rb b/padrino-helpers/test/fixtures/render_app/app.rb index d2e6340fb..f20fd1389 100644 --- a/padrino-helpers/test/fixtures/render_app/app.rb +++ b/padrino-helpers/test/fixtures/render_app/app.rb @@ -32,6 +32,10 @@ class RenderDemo < Padrino::Application render "double_capture_#{params[:ext]}" end + get '/wrong_capture_:ext' do + render "wrong_capture_#{params[:ext]}" + end + # partial with object get '/partial/object' do partial 'template/user', :object => RenderUser.new('John'), :locals => { :extra => "bar" } diff --git a/padrino-helpers/test/fixtures/render_app/views/wrong_capture_erb.erb b/padrino-helpers/test/fixtures/render_app/views/wrong_capture_erb.erb new file mode 100644 index 000000000..edcd6db32 --- /dev/null +++ b/padrino-helpers/test/fixtures/render_app/views/wrong_capture_erb.erb @@ -0,0 +1,3 @@ +<%= form_for( :object, '/' ) do |f| %> + <%= content_tag(:p, 'this is wrong') %> +<% end %> diff --git a/padrino-helpers/test/fixtures/render_app/views/wrong_capture_haml.haml b/padrino-helpers/test/fixtures/render_app/views/wrong_capture_haml.haml new file mode 100644 index 000000000..f40514d8d --- /dev/null +++ b/padrino-helpers/test/fixtures/render_app/views/wrong_capture_haml.haml @@ -0,0 +1,2 @@ +- form_for :object, '/' do |f| + = content_tag(:p, 'this is wrong') diff --git a/padrino-helpers/test/fixtures/render_app/views/wrong_capture_slim.slim b/padrino-helpers/test/fixtures/render_app/views/wrong_capture_slim.slim new file mode 100644 index 000000000..f40514d8d --- /dev/null +++ b/padrino-helpers/test/fixtures/render_app/views/wrong_capture_slim.slim @@ -0,0 +1,2 @@ +- form_for :object, '/' do |f| + = content_tag(:p, 'this is wrong') diff --git a/padrino-helpers/test/test_render_helpers.rb b/padrino-helpers/test/test_render_helpers.rb index c4dffb5aa..ec025ae36 100644 --- a/padrino-helpers/test/test_render_helpers.rb +++ b/padrino-helpers/test/test_render_helpers.rb @@ -90,5 +90,21 @@ def app visit '/double_capture_erb' assert_equal 1,$number_of_captures end + + should "fail on wrong erb usage" do + assert_raises(SyntaxError) do + visit '/wrong_capture_erb' + end + end + + should "ignore wrong haml usage" do + visit '/wrong_capture_haml' + assert_have_no_selector 'p', :content => 'this is wrong' + end + + should "ignore wrong slim usage" do + visit '/wrong_capture_slim' + assert_have_no_selector 'p', :content => 'this is wrong' + end end end