From 2ebea1c02d10e0fea26bd98d297a8f4d41dc1aff Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Sun, 31 Jan 2010 16:27:24 +1100 Subject: deOMGifying Railties, Active Support, and Action Pack --- actionpack/lib/action_controller/metal/testing.rb | 2 +- actionpack/lib/action_view/template/resolver.rb | 3 +-- actionpack/test/abstract/layouts_test.rb | 18 +++++++------- actionpack/test/controller/filters_test.rb | 6 ++--- .../test/controller/new_base/render_action_test.rb | 28 +++++++++++----------- .../test/controller/new_base/render_layout_test.rb | 20 ++++++++-------- .../controller/new_base/render_partial_test.rb | 4 ++-- actionpack/test/controller/subscriber_test.rb | 4 ++-- actionpack/test/dispatch/mime_type_test.rb | 4 ++-- .../test/template/html-scanner/sanitizer_test.rb | 4 ++-- actionpack/test/template/subscriber_test.rb | 4 ++-- 11 files changed, 48 insertions(+), 49 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/metal/testing.rb b/actionpack/lib/action_controller/metal/testing.rb index d62269b9af..707ad968f4 100644 --- a/actionpack/lib/action_controller/metal/testing.rb +++ b/actionpack/lib/action_controller/metal/testing.rb @@ -4,7 +4,7 @@ module ActionController include RackDelegation - # OMG MEGA HAX + # TODO: Clean this up def process_with_new_base_test(request, response) @_request = request @_response = response diff --git a/actionpack/lib/action_view/template/resolver.rb b/actionpack/lib/action_view/template/resolver.rb index 340a6afe5e..af5d6f6d18 100644 --- a/actionpack/lib/action_view/template/resolver.rb +++ b/actionpack/lib/action_view/template/resolver.rb @@ -141,8 +141,7 @@ module ActionView end end - # OMG HAX - # TODO: remove hax + # TODO: remove hack class FileSystemResolverWithFallback < Resolver def initialize(path, options = {}) super(options) diff --git a/actionpack/test/abstract/layouts_test.rb b/actionpack/test/abstract/layouts_test.rb index bf02e5a864..b6d89ea489 100644 --- a/actionpack/test/abstract/layouts_test.rb +++ b/actionpack/test/abstract/layouts_test.rb @@ -13,7 +13,7 @@ module AbstractControllerTests "layouts/hello_override.erb" => "With Override <%= yield %>", "layouts/abstract_controller_tests/layouts/with_string_implied_child.erb" => "With Implied <%= yield %>", - "layouts/omg.erb" => "OMGHI2U <%= yield %>", + "layouts/overwrite.erb" => "Overwrite <%= yield %>", "layouts/with_false_layout.erb" => "False Layout <%= yield %>" )] end @@ -42,7 +42,7 @@ module AbstractControllerTests end def overwrite_string - render :_template => ActionView::Template::Text.new("Hello string!"), :layout => "omg" + render :_template => ActionView::Template::Text.new("Hello string!"), :layout => "overwrite" end def overwrite_skip @@ -68,7 +68,7 @@ module AbstractControllerTests end class WithProc < Base - layout proc { |c| "omg" } + layout proc { |c| "overwrite" } def index render :_template => ActionView::Template::Text.new("Hello proc!") @@ -83,7 +83,7 @@ module AbstractControllerTests end private def hello - "omg" + "overwrite" end end @@ -122,7 +122,7 @@ module AbstractControllerTests end class WithSymbolAndNoMethod < Base - layout :omg_no_method + layout :no_method def index render :_template => ActionView::Template::Text.new("Hello boom!") @@ -175,7 +175,7 @@ module AbstractControllerTests test "when layout is overwriten by string in render, render new layout" do controller = WithString.new controller.process(:overwrite_string) - assert_equal "OMGHI2U Hello string!", controller.response_body + assert_equal "Overwrite Hello string!", controller.response_body end test "when layout is overwriten by false in render, render no layout" do @@ -209,13 +209,13 @@ module AbstractControllerTests test "when layout is specified as a proc, call it and use the layout returned" do controller = WithProc.new controller.process(:index) - assert_equal "OMGHI2U Hello proc!", controller.response_body + assert_equal "Overwrite Hello proc!", controller.response_body end test "when layout is specified as a symbol, call the requested method and use the layout returned" do controller = WithSymbol.new controller.process(:index) - assert_equal "OMGHI2U Hello symbol!", controller.response_body + assert_equal "Overwrite Hello symbol!", controller.response_body end test "when layout is specified as a symbol and the method returns nil, don't use a layout" do @@ -266,7 +266,7 @@ module AbstractControllerTests test "raises an exception when specifying layout true" do assert_raises ArgumentError do Object.class_eval do - class ::BadOmgFailLolLayout < AbstractControllerTests::Layouts::Base + class ::BadFailLayout < AbstractControllerTests::Layouts::Base layout true end end diff --git a/actionpack/test/controller/filters_test.rb b/actionpack/test/controller/filters_test.rb index 8445428e8f..004e1369d3 100644 --- a/actionpack/test/controller/filters_test.rb +++ b/actionpack/test/controller/filters_test.rb @@ -435,7 +435,7 @@ class FilterTest < ActionController::TestCase end def non_yielding_filter - @filters << "zomg it didn't yield" + @filters << "it didn't yield" @filter_return_value end @@ -465,14 +465,14 @@ class FilterTest < ActionController::TestCase controller = NonYieldingAroundFilterController.new controller.instance_variable_set "@filter_return_value", false test_process(controller, "index") - assert_equal ["filter_one", "zomg it didn't yield"], controller.assigns['filters'] + assert_equal ["filter_one", "it didn't yield"], controller.assigns['filters'] end def test_after_filters_are_not_run_if_around_filter_does_not_yield controller = NonYieldingAroundFilterController.new controller.instance_variable_set "@filter_return_value", true test_process(controller, "index") - assert_equal ["filter_one", "zomg it didn't yield"], controller.assigns['filters'] + assert_equal ["filter_one", "it didn't yield"], controller.assigns['filters'] end def test_added_filter_to_inheritance_graph diff --git a/actionpack/test/controller/new_base/render_action_test.rb b/actionpack/test/controller/new_base/render_action_test.rb index 239f68659c..0804d7b09d 100644 --- a/actionpack/test/controller/new_base/render_action_test.rb +++ b/actionpack/test/controller/new_base/render_action_test.rb @@ -119,9 +119,9 @@ module RenderActionWithApplicationLayout # Set the view path to an application view structure with layouts self.view_paths = self.view_paths = [ActionView::FixtureResolver.new( "render_action_with_application_layout/basic/hello_world.html.erb" => "Hello World!", - "render_action_with_application_layout/basic/hello.html.builder" => "xml.p 'Omg'", - "layouts/application.html.erb" => "OHAI <%= yield %> KTHXBAI", - "layouts/greetings.html.erb" => "Greetings <%= yield %> Bai", + "render_action_with_application_layout/basic/hello.html.builder" => "xml.p 'Hello'", + "layouts/application.html.erb" => "Hi <%= yield %> OK, Bye", + "layouts/greetings.html.erb" => "Greetings <%= yield %> Bye", "layouts/builder.html.builder" => "xml.html do\n xml << yield\nend" )] @@ -156,14 +156,14 @@ module RenderActionWithApplicationLayout test "rendering implicit application.html.erb as layout" do get "/render_action_with_application_layout/basic/hello_world" - assert_body "OHAI Hello World! KTHXBAI" + assert_body "Hi Hello World! OK, Bye" assert_status 200 end test "rendering with layout => true" do get "/render_action_with_application_layout/basic/hello_world_with_layout" - assert_body "OHAI Hello World! KTHXBAI" + assert_body "Hi Hello World! OK, Bye" assert_status 200 end @@ -184,7 +184,7 @@ module RenderActionWithApplicationLayout test "rendering with layout => 'greetings'" do get "/render_action_with_application_layout/basic/hello_world_with_custom_layout" - assert_body "Greetings Hello World! Bai" + assert_body "Greetings Hello World! Bye" assert_status 200 end end @@ -194,7 +194,7 @@ module RenderActionWithApplicationLayout test "builder works with layouts" do get :with_builder_and_layout - assert_response "\n

Omg

\n\n" + assert_response "\n

Hello

\n\n" end end @@ -204,7 +204,7 @@ module RenderActionWithControllerLayout class BasicController < ActionController::Base self.view_paths = self.view_paths = [ActionView::FixtureResolver.new( "render_action_with_controller_layout/basic/hello_world.html.erb" => "Hello World!", - "layouts/render_action_with_controller_layout/basic.html.erb" => "With Controller Layout! <%= yield %> KTHXBAI" + "layouts/render_action_with_controller_layout/basic.html.erb" => "With Controller Layout! <%= yield %> Bye" )] def hello_world @@ -234,14 +234,14 @@ module RenderActionWithControllerLayout test "render hello_world and implicitly use .html.erb as a layout." do get "/render_action_with_controller_layout/basic/hello_world" - assert_body "With Controller Layout! Hello World! KTHXBAI" + assert_body "With Controller Layout! Hello World! Bye" assert_status 200 end test "rendering with layout => true" do get "/render_action_with_controller_layout/basic/hello_world_with_layout" - assert_body "With Controller Layout! Hello World! KTHXBAI" + assert_body "With Controller Layout! Hello World! Bye" assert_status 200 end @@ -265,8 +265,8 @@ module RenderActionWithBothLayouts class BasicController < ActionController::Base self.view_paths = [ActionView::FixtureResolver.new({ "render_action_with_both_layouts/basic/hello_world.html.erb" => "Hello World!", - "layouts/application.html.erb" => "OHAI <%= yield %> KTHXBAI", - "layouts/render_action_with_both_layouts/basic.html.erb" => "With Controller Layout! <%= yield %> KTHXBAI" + "layouts/application.html.erb" => "Oh Hi <%= yield %> Bye", + "layouts/render_action_with_both_layouts/basic.html.erb" => "With Controller Layout! <%= yield %> Bye" })] def hello_world @@ -292,14 +292,14 @@ module RenderActionWithBothLayouts test "rendering implicitly use .html.erb over application.html.erb as a layout" do get "/render_action_with_both_layouts/basic/hello_world" - assert_body "With Controller Layout! Hello World! KTHXBAI" + assert_body "With Controller Layout! Hello World! Bye" assert_status 200 end test "rendering with layout => true" do get "/render_action_with_both_layouts/basic/hello_world_with_layout" - assert_body "With Controller Layout! Hello World! KTHXBAI" + assert_body "With Controller Layout! Hello World! Bye" assert_status 200 end diff --git a/actionpack/test/controller/new_base/render_layout_test.rb b/actionpack/test/controller/new_base/render_layout_test.rb index 6a9668b81a..372fb66b88 100644 --- a/actionpack/test/controller/new_base/render_layout_test.rb +++ b/actionpack/test/controller/new_base/render_layout_test.rb @@ -3,10 +3,10 @@ require 'abstract_unit' module ControllerLayouts class ImplicitController < ::ApplicationController self.view_paths = [ActionView::FixtureResolver.new( - "layouts/application.html.erb" => "OMG <%= yield %> KTHXBAI", + "layouts/application.html.erb" => "Main <%= yield %> Layout", "layouts/override.html.erb" => "Override! <%= yield %>", "basic.html.erb" => "Hello world!", - "controller_layouts/implicit/layout_false.html.erb" => "hai(layout_false.html.erb)" + "controller_layouts/implicit/layout_false.html.erb" => "hi(layout_false.html.erb)" )] def index @@ -27,7 +27,7 @@ module ControllerLayouts class ImplicitNameController < ::ApplicationController self.view_paths = [ActionView::FixtureResolver.new( - "layouts/controller_layouts/implicit_name.html.erb" => "OMGIMPLICIT <%= yield %> KTHXBAI", + "layouts/controller_layouts/implicit_name.html.erb" => "Implicit <%= yield %> Layout", "basic.html.erb" => "Hello world!" )] @@ -40,14 +40,14 @@ module ControllerLayouts test "rendering a normal template, but using the implicit layout" do get "/controller_layouts/implicit/index" - assert_body "OMG Hello world! KTHXBAI" + assert_body "Main Hello world! Layout" assert_status 200 end test "rendering a normal template, but using an implicit NAMED layout" do get "/controller_layouts/implicit_name/index" - assert_body "OMGIMPLICIT Hello world! KTHXBAI" + assert_body "Implicit Hello world! Layout" assert_status 200 end @@ -63,15 +63,15 @@ module ControllerLayouts test "rendering with :layout => false leaves out the implicit layout" do get :layout_false - assert_response "hai(layout_false.html.erb)" + assert_response "hi(layout_false.html.erb)" end end class MismatchFormatController < ::ApplicationController self.view_paths = [ActionView::FixtureResolver.new( "layouts/application.html.erb" => "<%= yield %>", - "controller_layouts/mismatch_format/index.js.rjs" => "page[:test].omg", - "controller_layouts/mismatch_format/implicit.rjs" => "page[:test].omg" + "controller_layouts/mismatch_format/index.js.rjs" => "page[:test].ext", + "controller_layouts/mismatch_format/implicit.rjs" => "page[:test].ext" )] def explicit @@ -84,12 +84,12 @@ module ControllerLayouts test "if JS is selected, an HTML template is not also selected" do get :index, "format" => "js" - assert_response "$(\"test\").omg();" + assert_response "$(\"test\").ext();" end test "if JS is implicitly selected, an HTML template is not also selected" do get :implicit - assert_response "$(\"test\").omg();" + assert_response "$(\"test\").ext();" end test "if an HTML template is explicitly provides for a JS template, an error is raised" do diff --git a/actionpack/test/controller/new_base/render_partial_test.rb b/actionpack/test/controller/new_base/render_partial_test.rb index 8fddcbcd57..1a1b36a65e 100644 --- a/actionpack/test/controller/new_base/render_partial_test.rb +++ b/actionpack/test/controller/new_base/render_partial_test.rb @@ -5,7 +5,7 @@ module RenderPartial class BasicController < ActionController::Base self.view_paths = [ActionView::FixtureResolver.new( - "render_partial/basic/_basic.html.erb" => "OMG!", + "render_partial/basic/_basic.html.erb" => "BasicPartial!", "render_partial/basic/basic.html.erb" => "<%= @test_unchanged = 'goodbye' %><%= render :partial => 'basic' %><%= @test_unchanged %>" )] @@ -20,7 +20,7 @@ module RenderPartial test "rendering a partial in ActionView doesn't pull the ivars again from the controller" do get :changing - assert_response("goodbyeOMG!goodbye") + assert_response("goodbyeBasicPartial!goodbye") end end diff --git a/actionpack/test/controller/subscriber_test.rb b/actionpack/test/controller/subscriber_test.rb index 152a0d0c04..119a18ebc5 100644 --- a/actionpack/test/controller/subscriber_test.rb +++ b/actionpack/test/controller/subscriber_test.rb @@ -13,7 +13,7 @@ module Another end def data_sender - send_data "cool data", :filename => "omg.txt" + send_data "cool data", :filename => "file.txt" end def xfile_sender @@ -121,7 +121,7 @@ class ACSubscriberTest < ActionController::TestCase wait assert_equal 3, logs.size - assert_match /Sent data omg\.txt/, logs[1] + assert_match /Sent data file\.txt/, logs[1] end def test_send_file diff --git a/actionpack/test/dispatch/mime_type_test.rb b/actionpack/test/dispatch/mime_type_test.rb index 0832943d4c..369212e2d0 100644 --- a/actionpack/test/dispatch/mime_type_test.rb +++ b/actionpack/test/dispatch/mime_type_test.rb @@ -23,7 +23,7 @@ class MimeTypeTest < ActiveSupport::TestCase end # Accept header send with user HTTP_USER_AGENT: Sunrise/0.42j (Windows XP) - test "parse crappy broken acceptlines" do + test "parse broken acceptlines" do accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/*,,*/*;q=0.5" expect = [Mime::HTML, Mime::XML, "image/*", Mime::TEXT, Mime::ALL] assert_equal expect, Mime::Type.parse(accept).collect { |c| c.to_s } @@ -31,7 +31,7 @@ class MimeTypeTest < ActiveSupport::TestCase # Accept header send with user HTTP_USER_AGENT: Mozilla/4.0 # (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; InfoPath.1) - test "parse crappy broken acceptlines2" do + test "parse other broken acceptlines" do accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, , pronto/1.00.00, sslvpn/1.00.00.00, */*" expect = ['image/gif', 'image/x-xbitmap', 'image/jpeg','image/pjpeg', 'application/x-shockwave-flash', 'application/vnd.ms-excel', 'application/vnd.ms-powerpoint', 'application/msword', 'pronto/1.00.00', 'sslvpn/1.00.00.00', Mime::ALL ] assert_equal expect, Mime::Type.parse(accept).collect { |c| c.to_s } diff --git a/actionpack/test/template/html-scanner/sanitizer_test.rb b/actionpack/test/template/html-scanner/sanitizer_test.rb index e85a5c7abf..a6e760b0b6 100644 --- a/actionpack/test/template/html-scanner/sanitizer_test.rb +++ b/actionpack/test/template/html-scanner/sanitizer_test.rb @@ -48,7 +48,7 @@ class SanitizerTest < ActionController::TestCase assert_sanitized "a b cd e f", "a b cd e f" end - # fucked + # TODO: Clean up def test_sanitize_js_handlers raw = %{onthis="do that" hello} assert_sanitized raw, %{onthis="do that" hello} @@ -193,7 +193,7 @@ class SanitizerTest < ActionController::TestCase assert_sanitized img_hack, "" end - # fucked + # TODO: Clean up def test_should_sanitize_attributes assert_sanitized %(blah), %(blah) end diff --git a/actionpack/test/template/subscriber_test.rb b/actionpack/test/template/subscriber_test.rb index 5db2b16ac1..8bacab7088 100644 --- a/actionpack/test/template/subscriber_test.rb +++ b/actionpack/test/template/subscriber_test.rb @@ -33,7 +33,7 @@ class AVSubscriberTest < ActiveSupport::TestCase end def test_render_text_template - @view.render(:text => "OMG") + @view.render(:text => "TEXT") wait assert_equal 1, @logger.logged(:info).size @@ -41,7 +41,7 @@ class AVSubscriberTest < ActiveSupport::TestCase end def test_render_inline_template - @view.render(:inline => "<%= 'OMG' %>") + @view.render(:inline => "<%= 'TEXT' %>") wait assert_equal 1, @logger.logged(:info).size -- cgit v1.2.3 From 59e9478f57216999c12580c42cb9149894090123 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 31 Jan 2010 13:37:43 -0600 Subject: Require ac/test_case from AV::TestCase --- actionpack/lib/action_view/test_case.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb index 16d66b6eca..3095d84e13 100644 --- a/actionpack/lib/action_view/test_case.rb +++ b/actionpack/lib/action_view/test_case.rb @@ -1,3 +1,5 @@ +require 'action_controller/test_case' + module ActionView class Base alias_method :initialize_without_template_tracking, :initialize -- cgit v1.2.3 From a3349f845ffa2415e12ac9e26b4f7300d7edd3ef Mon Sep 17 00:00:00 2001 From: "Stephen St. Martin" Date: Sun, 31 Jan 2010 14:42:40 -0500 Subject: form_for should pass :remote to form_tag through html_options --- actionpack/lib/action_view/helpers/form_helper.rb | 2 ++ actionpack/test/template/form_helper_test.rb | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 20e9916d62..54610fdc71 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -280,6 +280,8 @@ module ActionView args.unshift object end + options[:html][:remote] = true if options.delete(:remote) + concat(form_tag(options.delete(:url) || {}, options.delete(:html) || {})) fields_for(object_name, *(args << options), &proc) concat(''.html_safe!) diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index c97343fbe5..caeca9db10 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -451,6 +451,25 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, output_buffer end + def test_form_for_with_remote + form_for(:post, @post, :remote => true, :html => { :id => 'create-post', :method => :put }) do |f| + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) + end + + expected = + "
" + + "
" + + "" + + "" + + "" + + "" + + "
" + + assert_dom_equal expected, output_buffer + end + def test_form_for_without_object form_for(:post, :html => { :id => 'create-post' }) do |f| concat f.text_field(:title) -- cgit v1.2.3 From a9e89e82b67f721fe68ca58a2d371b666cdb7441 Mon Sep 17 00:00:00 2001 From: "Stephen St. Martin" Date: Sun, 31 Jan 2010 14:03:37 -0500 Subject: add :remote option to form_tag --- actionpack/lib/action_view/helpers/form_tag_helper.rb | 1 + actionpack/test/template/form_tag_helper_test.rb | 6 ++++++ 2 files changed, 7 insertions(+) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb index fb32f78e5b..81c76e090a 100644 --- a/actionpack/lib/action_view/helpers/form_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb @@ -441,6 +441,7 @@ module ActionView returning options.stringify_keys do |html_options| html_options["enctype"] = "multipart/form-data" if html_options.delete("multipart") html_options["action"] = url_for(url_for_options, *parameters_for_url) + html_options["data-remote"] = true if html_options.delete("remote") end end diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb index 01bde8ea04..553ec44fad 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -53,6 +53,12 @@ class FormTagHelperTest < ActionView::TestCase assert_dom_equal expected, actual end + def test_form_tag_with_remote + actual = form_tag({}, :remote => true) + expected = %(
) + assert_dom_equal expected, actual + end + def test_form_tag_with_block_in_erb __in_erb_template = '' form_tag("http://example.com") { concat "Hello world!" } -- cgit v1.2.3 From e1618b9ac397d1963e788f441fc4965cd3f9d4cf Mon Sep 17 00:00:00 2001 From: "Erik St. Martin" Date: Sun, 31 Jan 2010 15:40:40 -0500 Subject: data-remote needs to be on the form and not the submit when using button_to :remote=>true --- actionpack/lib/action_view/helpers/url_helper.rb | 4 +++- actionpack/test/template/url_helper_test.rb | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index bd179ef0b3..e56bfc7bfd 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -286,6 +286,8 @@ module ActionView form_method = method.to_s == 'get' ? 'get' : 'post' + remote = html_options.delete('remote') + request_token_tag = '' if form_method == 'post' && protect_against_forgery? request_token_tag = tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token) @@ -298,7 +300,7 @@ module ActionView html_options.merge!("type" => "submit", "value" => name) - ("
" + + ("
" + method_tag + tag("input", html_options) + request_token_tag + "
").html_safe! end diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb index b498ec8429..e904e88f49 100644 --- a/actionpack/test/template/url_helper_test.rb +++ b/actionpack/test/template/url_helper_test.rb @@ -81,6 +81,13 @@ class UrlHelperTest < ActionView::TestCase ) end + def test_button_to_with_remote_and_javascript_confirm + assert_dom_equal( + "
", + button_to("Hello", "http://www.example.com", :remote => true, :confirm => "Are you sure?") + ) + end + def test_button_to_enabled_disabled assert_dom_equal( "
", -- cgit v1.2.3 From c493370f332715dee0ef795a66e896d7f0471cbe Mon Sep 17 00:00:00 2001 From: Stefan Penner Date: Sun, 31 Jan 2010 16:30:21 -0600 Subject: UJS documentation. --- actionpack/lib/action_view/helpers/form_helper.rb | 27 +++++++++++ .../lib/action_view/helpers/form_tag_helper.rb | 29 ++++++++---- actionpack/lib/action_view/helpers/url_helper.rb | 55 ++++++++++++---------- 3 files changed, 78 insertions(+), 33 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 54610fdc71..a72357cf3d 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -225,6 +225,33 @@ module ActionView # ... # <% end %> # + # === Unobtrusive JavaScript + # + # Specifying: + # + # :remote => true + # + # in the options hash creates a form that will allow the unobtrusive JavaScript drivers to modify its + # behaviour. The expected default behaviour is an XMLHttpRequest in the background instead of the regular + # POST arrangement, but ultimately the behaviour is the choice of the JavaScript driver implementor. + # Even though it's using JavaScript to serialize the form elements, the form submission will work just like + # a regular submission as viewed by the receiving side (all elements available in params). + # + # Example: + # + # <% form_for(:post, @post, :remote => true, :html => { :id => 'create-post', :method => :put }) do |f| %> + # ... + # <% end %> + # + # The HTML generated for this would be: + # + #
+ #
+ # + #
+ # ... + #
+ # # === Customized form builders # # You can also build forms using a customized FormBuilder class. Subclass diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb index 81c76e090a..597cf311cb 100644 --- a/actionpack/lib/action_view/helpers/form_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb @@ -19,6 +19,8 @@ module ActionView # If "put", "delete", or another verb is used, a hidden input with name _method # is added to simulate the verb over post. # * A list of parameters to feed to the URL the form will be posted to. + # * :remote - If set to true, will allow the Unobtrusive JavaScript drivers to control the + # submit behaviour. By default this behaviour is an ajax submit. # # ==== Examples # form_tag('/posts') @@ -30,10 +32,14 @@ module ActionView # form_tag('/upload', :multipart => true) # # =>
# - # <% form_tag '/posts' do -%> + # <% form_tag('/posts')do -%> #
<%= submit_tag 'Save' %>
# <% end -%> # # =>
+ # + # <% form_tag('/posts', :remote => true) %> + # # =>
+ # def form_tag(url_for_options = {}, options = {}, *parameters_for_url, &block) html_options = html_options_for_form(url_for_options, options, *parameters_for_url) if block_given? @@ -333,12 +339,13 @@ module ActionView # Creates a submit button with the text value as the caption. # # ==== Options - # * :confirm => 'question?' - This will add a JavaScript confirm - # prompt with the question specified. If the user accepts, the form is - # processed normally, otherwise no action is taken. + # * :confirm => 'question?' - If present the unobtrusive JavaScript + # drivers will provide a prompt with the question specified. If the user accepts, + # the form is processed normally, otherwise no action is taken. # * :disabled - If true, the user will not be able to use this input. - # * :disable_with - Value of this parameter will be used as the value for a disabled version - # of the submit button when the form is submitted. + # * :disable_with - Value of this parameter will be used as the value for a + # disabled version of the submit button when the form is submitted. This feature is + # provided by the unobtrusive JavaScript driver. # * Any other key creates standard HTML options for the tag. # # ==== Examples @@ -351,16 +358,22 @@ module ActionView # submit_tag "Save edits", :disabled => true # # => # + # # submit_tag "Complete sale", :disable_with => "Please wait..." - # # => # # submit_tag nil, :class => "form_submit" # # => # # submit_tag "Edit", :disable_with => "Editing...", :class => "edit-button" - # # => + # + # submit_tag "Save", :confirm => "Are you sure?" + # # => + # def submit_tag(value = "Save changes", options = {}) options.stringify_keys! diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index e56bfc7bfd..4301647a2c 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -117,8 +117,8 @@ module ActionView # end # # ==== Options - # * :confirm => 'question?' - This will add a JavaScript confirm - # prompt with the question specified. If the user accepts, the link is + # * :confirm => 'question?' - This will allow the unobtrusive JavaScript + # driver to prompt with the question specified. If the user accepts, the link is # processed normally, otherwise no action is taken. # * :method => symbol of HTTP verb - This modifier will dynamically # create an HTML form and immediately submit the form for processing using @@ -195,18 +195,15 @@ module ActionView # link_to "Nonsense search", searches_path(:foo => "bar", :baz => "quux") # # => Nonsense search # - # The three options specific to +link_to+ (:confirm and :method) are used as follows: + # The two options specific to +link_to+ (:confirm and :method) are used as follows: # # link_to "Visit Other Site", "http://www.rubyonrails.org/", :confirm => "Are you sure?" - # # => Visit Other Site - # - # link_to "Delete Image", @image, :confirm => "Are you sure?", :method => :delete - # # => Delete Image + # # => Visit Other Site + # + # link_to("Destroy", "http://www.example.com", :method => :delete, :confirm => "Are you sure?") + # # => Destroy + + # def link_to(*args, &block) if block_given? options = args.first || {} @@ -256,9 +253,11 @@ module ActionView # There are a few special +html_options+: # * :method - Specifies the anchor name to be appended to the path. # * :disabled - Specifies the anchor name to be appended to the path. - # * :confirm - This will add a JavaScript confirm + # * :confirm - This will use the unobtrusive JavaScript driver to # prompt with the question specified. If the user accepts, the link is # processed normally, otherwise no action is taken. + # * :remote - If set to true, will allow the Unobtrusive JavaScript drivers to control the + # submit behaviour. By default this behaviour is an ajax submit. # # ==== Examples # <%= button_to "New", :action => "new" %> @@ -266,15 +265,27 @@ module ActionView # #
# #
" # - # button_to "Delete Image", { :action => "delete", :id => @image.id }, - # :confirm => "Are you sure?", :method => :delete + # + # <%= button_to "Delete Image", { :action => "delete", :id => @image.id }, + # :confirm => "Are you sure?", :method => :delete %> # # => "
# #
# # - # # + # # # #
# #
" + # + # + # <%= button_to('Destroy', 'http://www.example.com', :confirm => 'Are you sure?', + # :method => "delete", :remote => true, :disable_with => 'loading...') %> + # # => "
+ # #
+ # # + # # + # #
+ # #
" + # # + def button_to(name, options = {}, html_options = {}) html_options = html_options.stringify_keys convert_boolean_attributes!(html_options, %w( disabled )) @@ -567,14 +578,8 @@ module ActionView method, href = html_options.delete("method"), html_options['href'] - if confirm && method - add_confirm_to_attributes!(html_options, confirm) - add_method_to_attributes!(html_options, method) - elsif confirm - add_confirm_to_attributes!(html_options, confirm) - elsif method - add_method_to_attributes!(html_options, method) - end + add_confirm_to_attributes!(html_options, confirm) if confirm + add_method_to_attributes!(html_options, method) if method html_options["data-url"] = options[:url] if options.is_a?(Hash) && options[:url] -- cgit v1.2.3 From 9f01dff9c203821bf4ac6d7b885f1d6b018d5c79 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Sun, 31 Jan 2010 16:33:06 -0800 Subject: Get rails tests running on bundler 0.9 --- actionpack/test/abstract_unit.rb | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) (limited to 'actionpack') diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb index 7b04638ccc..867e50d5b9 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -1,10 +1,4 @@ -begin - require File.expand_path('../../../vendor/gems/environment', __FILE__) -rescue LoadError -end - -lib = File.expand_path('../../lib', __FILE__) -$:.unshift(lib) unless $:.include?('lib') || $:.include?(lib) +require File.expand_path('../../../load_paths', __FILE__) $:.unshift(File.dirname(__FILE__) + '/lib') $:.unshift(File.dirname(__FILE__) + '/fixtures/helpers') @@ -20,9 +14,6 @@ require 'action_view/base' require 'action_dispatch' require 'fixture_template' require 'active_support/dependencies' - -activemodel_path = File.expand_path('../../../activemodel/lib', __FILE__) -$:.unshift(activemodel_path) if File.directory?(activemodel_path) && !$:.include?(activemodel_path) require 'active_model' begin -- cgit v1.2.3 From 4cbb9db0a5ff65b0a626a5b043331abefd89e717 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Sun, 31 Jan 2010 19:17:42 -0800 Subject: For performance reasons, you can no longer call html_safe! on Strings. Instead, all Strings are always not html_safe?. Instead, you can get a SafeBuffer from a String by calling #html_safe, which will SafeBuffer.new(self). * Additionally, instead of doing concat("".html_safe), you can do safe_concat(""), which will skip both the flag set, and the flag check. * For the first pass, I converted virtually all #html_safe!s to #html_safe, and the tests pass. A further optimization would be to try to use #safe_concat as much as possible, reducing the performance impact if we know up front that a String is safe. --- actionpack/lib/action_view.rb | 4 +- actionpack/lib/action_view/base.rb | 2 +- actionpack/lib/action_view/erb/util.rb | 49 ---------------------- .../lib/action_view/helpers/active_model_helper.rb | 11 +++-- .../lib/action_view/helpers/asset_tag_helper.rb | 6 +-- actionpack/lib/action_view/helpers/date_helper.rb | 14 +++---- actionpack/lib/action_view/helpers/debug_helper.rb | 4 +- actionpack/lib/action_view/helpers/form_helper.rb | 4 +- .../lib/action_view/helpers/form_options_helper.rb | 2 +- .../lib/action_view/helpers/form_tag_helper.rb | 6 +-- .../lib/action_view/helpers/number_helper.rb | 2 +- .../lib/action_view/helpers/prototype_helper.rb | 2 +- .../lib/action_view/helpers/raw_output_helper.rb | 2 +- .../lib/action_view/helpers/sanitize_helper.rb | 12 +----- actionpack/lib/action_view/helpers/tag_helper.rb | 8 ++-- actionpack/lib/action_view/helpers/text_helper.rb | 10 ++--- .../lib/action_view/helpers/translation_helper.rb | 2 +- actionpack/lib/action_view/helpers/url_helper.rb | 8 ++-- actionpack/lib/action_view/render/partials.rb | 2 +- actionpack/lib/action_view/safe_buffer.rb | 27 ------------ actionpack/lib/action_view/test_case.rb | 2 +- actionpack/test/controller/caching_test.rb | 4 +- actionpack/test/controller/output_escaping_test.rb | 2 +- actionpack/test/template/erb_util_test.rb | 2 +- actionpack/test/template/form_helper_test.rb | 2 +- actionpack/test/template/form_tag_helper_test.rb | 6 +-- actionpack/test/template/safe_buffer_test.rb | 2 +- actionpack/test/template/test_case_test.rb | 2 +- 28 files changed, 58 insertions(+), 141 deletions(-) delete mode 100644 actionpack/lib/action_view/erb/util.rb delete mode 100644 actionpack/lib/action_view/safe_buffer.rb (limited to 'actionpack') diff --git a/actionpack/lib/action_view.rb b/actionpack/lib/action_view.rb index 93aa69c060..20a2a63621 100644 --- a/actionpack/lib/action_view.rb +++ b/actionpack/lib/action_view.rb @@ -56,7 +56,9 @@ module ActionView autoload :TestCase, 'action_view/test_case' end -require 'action_view/erb/util' +require 'active_support/core_ext/string/output_safety' require 'action_view/base' +ActionView::SafeBuffer = ActiveSupport::SafeBuffer + I18n.load_path << "#{File.dirname(__FILE__)}/action_view/locale/en.yml" diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index 07ef3f2140..148d507f1c 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -284,7 +284,7 @@ module ActionView #:nodoc: @helpers = self.class.helpers || Module.new @_controller = controller - @_content_for = Hash.new {|h,k| h[k] = ActionView::SafeBuffer.new } + @_content_for = Hash.new {|h,k| h[k] = ActiveSupport::SafeBuffer.new } @_virtual_path = nil self.view_paths = view_paths end diff --git a/actionpack/lib/action_view/erb/util.rb b/actionpack/lib/action_view/erb/util.rb deleted file mode 100644 index aef859b3ac..0000000000 --- a/actionpack/lib/action_view/erb/util.rb +++ /dev/null @@ -1,49 +0,0 @@ -require 'erb' - -class ERB - module Util - HTML_ESCAPE = { '&' => '&', '>' => '>', '<' => '<', '"' => '"' } - JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003E', '<' => '\u003C' } - - # A utility method for escaping HTML tag characters. - # This method is also aliased as h. - # - # In your ERb templates, use this method to escape any unsafe content. For example: - # <%=h @person.name %> - # - # ==== Example: - # puts html_escape("is a > 0 & a < 10?") - # # => is a > 0 & a < 10? - def html_escape(s) - s = s.to_s - if s.html_safe? - s - else - s.gsub(/[&"><]/) { |special| HTML_ESCAPE[special] }.html_safe! - end - end - - undef :h - alias h html_escape - - module_function :html_escape - module_function :h - - # A utility method for escaping HTML entities in JSON strings. - # This method is also aliased as j. - # - # In your ERb templates, use this method to escape any HTML entities: - # <%=j @person.to_json %> - # - # ==== Example: - # puts json_escape("is a > 0 & a < 10?") - # # => is a \u003E 0 \u0026 a \u003C 10? - def json_escape(s) - s.to_s.gsub(/[&"><]/) { |special| JSON_ESCAPE[special] } - end - - alias j json_escape - module_function :j - module_function :json_escape - end -end diff --git a/actionpack/lib/action_view/helpers/active_model_helper.rb b/actionpack/lib/action_view/helpers/active_model_helper.rb index 87b7adf6c4..e106bb0897 100644 --- a/actionpack/lib/action_view/helpers/active_model_helper.rb +++ b/actionpack/lib/action_view/helpers/active_model_helper.rb @@ -6,7 +6,7 @@ require 'active_support/core_ext/kernel/reporting' module ActionView class Base - @@field_error_proc = Proc.new{ |html_tag, instance| "
#{html_tag}
".html_safe! } + @@field_error_proc = Proc.new{ |html_tag, instance| "
#{html_tag}
".html_safe } cattr_accessor :field_error_proc end @@ -86,12 +86,11 @@ module ActionView submit_value = options[:submit_value] || options[:action].gsub(/[^\w]/, '').capitalize contents = form_tag({:action => action}, :method =>(options[:method] || 'post'), :enctype => options[:multipart] ? 'multipart/form-data': nil) - contents << hidden_field(record_name, :id) unless record.new_record? - contents << all_input_tags(record, record_name, options) + contents.safe_concat hidden_field(record_name, :id) unless record.new_record? + contents.safe_concat all_input_tags(record, record_name, options) yield contents if block_given? - contents << submit_tag(submit_value) - contents << '' - contents.html_safe! + contents.safe_concat submit_tag(submit_value) + contents.safe_concat('') end # Returns a string containing the error message attached to the +method+ on the +object+ if one exists. diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb index 4df99f8293..96976ce45f 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb @@ -293,7 +293,7 @@ module ActionView else sources = expand_javascript_sources(sources, recursive) ensure_javascript_sources!(sources) if cache - sources.collect { |source| javascript_src_tag(source, options) }.join("\n").html_safe! + sources.collect { |source| javascript_src_tag(source, options) }.join("\n").html_safe end end @@ -444,7 +444,7 @@ module ActionView else sources = expand_stylesheet_sources(sources, recursive) ensure_stylesheet_sources!(sources) if cache - sources.collect { |source| stylesheet_tag(source, options) }.join("\n").html_safe! + sources.collect { |source| stylesheet_tag(source, options) }.join("\n").html_safe end end @@ -588,7 +588,7 @@ module ActionView if sources.is_a?(Array) content_tag("video", options) do - sources.map { |source| tag("source", :src => source) }.join.html_safe! + sources.map { |source| tag("source", :src => source) }.join.html_safe end else options[:src] = path_to_video(sources) diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index 34f38b0a8a..8be2f76bd6 100644 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -616,7 +616,7 @@ module ActionView build_selects_from_types(order) else - "#{select_date}#{@options[:datetime_separator]}#{select_time}".html_safe! + "#{select_date}#{@options[:datetime_separator]}#{select_time}".html_safe end end @@ -835,7 +835,7 @@ module ActionView select_html << prompt_option_tag(type, @options[:prompt]) + "\n" if @options[:prompt] select_html << select_options_as_html.to_s - (content_tag(:select, select_html, select_options) + "\n").html_safe! + (content_tag(:select, select_html, select_options) + "\n").html_safe end # Builds a prompt option tag with supplied options or from default options @@ -865,7 +865,7 @@ module ActionView :id => input_id_from_type(type), :name => input_name_from_type(type), :value => value - }) + "\n").html_safe! + }) + "\n").html_safe end # Returns the name attribute for the input tag @@ -896,7 +896,7 @@ module ActionView separator = separator(type) unless type == order.first # don't add on last field select.insert(0, separator.to_s + send("select_#{type}").to_s) end - select.html_safe! + select.html_safe end # Returns the separator for a given datetime component @@ -916,15 +916,15 @@ module ActionView class InstanceTag #:nodoc: def to_date_select_tag(options = {}, html_options = {}) - datetime_selector(options, html_options).select_date.html_safe! + datetime_selector(options, html_options).select_date.html_safe end def to_time_select_tag(options = {}, html_options = {}) - datetime_selector(options, html_options).select_time.html_safe! + datetime_selector(options, html_options).select_time.html_safe end def to_datetime_select_tag(options = {}, html_options = {}) - datetime_selector(options, html_options).select_datetime.html_safe! + datetime_selector(options, html_options).select_datetime.html_safe end private diff --git a/actionpack/lib/action_view/helpers/debug_helper.rb b/actionpack/lib/action_view/helpers/debug_helper.rb index 885945fde3..e637dc1474 100644 --- a/actionpack/lib/action_view/helpers/debug_helper.rb +++ b/actionpack/lib/action_view/helpers/debug_helper.rb @@ -27,10 +27,10 @@ module ActionView def debug(object) begin Marshal::dump(object) - "
#{h(object.to_yaml).gsub("  ", "  ")}
".html_safe! + "
#{h(object.to_yaml).gsub("  ", "  ")}
".html_safe rescue Exception => e # errors from Marshal or YAML # Object couldn't be dumped, perhaps because of singleton methods -- this is the fallback - "#{h(object.inspect)}".html_safe! + "#{h(object.inspect)}".html_safe end end end diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index a72357cf3d..76998a6ea1 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -311,7 +311,7 @@ module ActionView concat(form_tag(options.delete(:url) || {}, options.delete(:html) || {})) fields_for(object_name, *(args << options), &proc) - concat(''.html_safe!) + safe_concat('') end def apply_form_for_options!(object_or_array, options) #:nodoc: @@ -879,7 +879,7 @@ module ActionView end hidden = tag("input", "name" => options["name"], "type" => "hidden", "value" => options['disabled'] && checked ? checked_value : unchecked_value) checkbox = tag("input", options) - (hidden + checkbox).html_safe! + (hidden + checkbox).html_safe end def to_boolean_select_tag(options = {}) diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index 02ad637509..0ffe770f5f 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -296,7 +296,7 @@ module ActionView options << %() end - options_for_select.join("\n").html_safe! + options_for_select.join("\n").html_safe end # Returns a string of option tags that have been compiled by iterating over the +collection+ and assigning the diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb index 597cf311cb..9b8471c6c6 100644 --- a/actionpack/lib/action_view/helpers/form_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb @@ -446,7 +446,7 @@ module ActionView concat(tag(:fieldset, options, true)) concat(content_tag(:legend, legend)) unless legend.blank? concat(content) - concat("".html_safe!) + safe_concat("") end private @@ -474,14 +474,14 @@ module ActionView def form_tag_html(html_options) extra_tags = extra_tags_for_form(html_options) - (tag(:form, html_options, true) + extra_tags).html_safe! + (tag(:form, html_options, true) + extra_tags).html_safe end def form_tag_in_block(html_options, &block) content = capture(&block) concat(form_tag_html(html_options)) concat(content) - concat("".html_safe!) + safe_concat("") end def token_tag diff --git a/actionpack/lib/action_view/helpers/number_helper.rb b/actionpack/lib/action_view/helpers/number_helper.rb index 64b71663c3..3d3502a08b 100644 --- a/actionpack/lib/action_view/helpers/number_helper.rb +++ b/actionpack/lib/action_view/helpers/number_helper.rb @@ -92,7 +92,7 @@ module ActionView :precision => precision, :delimiter => delimiter, :separator => separator) - ).gsub(/%u/, unit).html_safe! + ).gsub(/%u/, unit).html_safe rescue number end diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb index d335d89274..7eb6bceca0 100644 --- a/actionpack/lib/action_view/helpers/prototype_helper.rb +++ b/actionpack/lib/action_view/helpers/prototype_helper.rb @@ -610,7 +610,7 @@ module ActionView # page.hide 'spinner' # end def update_page(&block) - JavaScriptGenerator.new(@template, &block).to_s.html_safe! + JavaScriptGenerator.new(@template, &block).to_s.html_safe end # Works like update_page but wraps the generated JavaScript in a