aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Stephenson <sam@37signals.com>2006-03-13 02:11:59 +0000
committerSam Stephenson <sam@37signals.com>2006-03-13 02:11:59 +0000
commit3589871de82c12123f62c80b4924d2696412065c (patch)
tree8f420e61f86e6860a63cb9bbcc812962954d359f
parent955583aed4668981ee32dadcdc7e3c35e83d3adf (diff)
downloadrails-3589871de82c12123f62c80b4924d2696412065c.tar.gz
rails-3589871de82c12123f62c80b4924d2696412065c.tar.bz2
rails-3589871de82c12123f62c80b4924d2696412065c.zip
Added simple alert() notifications for RJS exceptions when config.action_view.debug_rjs = true. Set debug_rjs = true for the default development environment.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3856 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--actionpack/CHANGELOG2
-rw-r--r--actionpack/lib/action_view/base.rb5
-rw-r--r--actionpack/lib/action_view/helpers/prototype_helper.rb21
-rw-r--r--actionpack/test/controller/mime_responds_test.rb2
-rw-r--r--actionpack/test/controller/new_render_test.rb2
-rw-r--r--actionpack/test/template/prototype_helper_test.rb44
6 files changed, 48 insertions, 28 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 5924b240da..aa1ddd1a08 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added simple alert() notifications for RJS exceptions when config.action_view.debug_rjs = true. [Sam Stephenson]
+
* Added :content_type option to render, so you can change the content type on the fly [DHH]. Example: render :action => "atom.rxml", :content_type => "application/atom+xml"
* CHANGED DEFAULT: The default content type for .rxml is now application/xml instead of type/xml, see http://www.xml.com/pub/a/2004/07/21/dive.html for reason [DHH]
diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb
index 95afff0e84..94e7a47232 100644
--- a/actionpack/lib/action_view/base.rb
+++ b/actionpack/lib/action_view/base.rb
@@ -162,6 +162,11 @@ module ActionView #:nodoc:
# shortly.
@@local_assigns_support_string_keys = true
cattr_accessor :local_assigns_support_string_keys
+
+ # Specify whether RJS responses should be wrapped in a try/catch block
+ # that alert()s the caught exception (and then re-raises it).
+ @@debug_rjs = false
+ cattr_accessor :debug_rjs
@@template_handlers = {}
diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb
index 1f6aa274db..7781612180 100644
--- a/actionpack/lib/action_view/helpers/prototype_helper.rb
+++ b/actionpack/lib/action_view/helpers/prototype_helper.rb
@@ -370,12 +370,12 @@ module ActionView
end
private
- def include_helpers_from_context
- @context.extended_by.each do |mod|
- extend mod unless mod.name =~ /^ActionView::Helpers/
+ def include_helpers_from_context
+ @context.extended_by.each do |mod|
+ extend mod unless mod.name =~ /^ActionView::Helpers/
+ end
+ extend GeneratorMethods
end
- extend GeneratorMethods
- end
# JavaScriptGenerator generates blocks of JavaScript code that allow you
# to change the content and presentation of multiple DOM elements. Use
@@ -425,7 +425,12 @@ module ActionView
# <script> tag.
module GeneratorMethods
def to_s #:nodoc:
- @lines * $/
+ returning javascript = @lines * $/ do
+ if ActionView::Base.debug_rjs
+ javascript.replace "try {\n#{javascript}\n} catch (e) "
+ javascript << "{ alert('RJS error:\\n\\n' + e.toString()); throw e }"
+ end
+ end
end
# Returns a element reference by finding it through +id+ in the DOM. This element can then be
@@ -748,7 +753,7 @@ module ActionView
class JavaScriptElementProxy < JavaScriptProxy #:nodoc:
def initialize(generator, id)
@id = id
- super(generator, "$('#{id}')")
+ super(generator, "$(#{id.to_json})")
end
def replace_html(*options_for_render)
@@ -874,7 +879,7 @@ module ActionView
class JavaScriptElementCollectionProxy < JavaScriptCollectionProxy #:nodoc:\
def initialize(generator, pattern)
- super(generator, "$$('#{pattern}')")
+ super(generator, "$$(#{pattern.to_json})")
end
end
end
diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb
index 6fcaaf302d..94273cf4af 100644
--- a/actionpack/test/controller/mime_responds_test.rb
+++ b/actionpack/test/controller/mime_responds_test.rb
@@ -141,7 +141,7 @@ class MimeControllerTest < Test::Unit::TestCase
@request.env["HTTP_ACCEPT"] = "text/javascript"
get :using_defaults
- assert_equal "$('body').visualEffect(\"highlight\");", @response.body
+ assert_equal '$("body").visualEffect("highlight");', @response.body
@request.env["HTTP_ACCEPT"] = "application/xml"
get :using_defaults
diff --git a/actionpack/test/controller/new_render_test.rb b/actionpack/test/controller/new_render_test.rb
index 5bdb8ec7f8..961269da59 100644
--- a/actionpack/test/controller/new_render_test.rb
+++ b/actionpack/test/controller/new_render_test.rb
@@ -393,7 +393,7 @@ class NewRenderTest < Test::Unit::TestCase
def test_enum_rjs_test
get :enum_rjs_test
assert_equal <<-EOS.strip, @response.body
-$$('.product').each(function(value, index) {
+$$(".product").each(function(value, index) {
new Effect.Highlight(element,{});
new Effect.Highlight(value,{});
Sortable.create(value, {onUpdate:function(){new Ajax.Request('/test/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize(value)})}});
diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb
index 253a8bac1c..e0746f62e6 100644
--- a/actionpack/test/template/prototype_helper_test.rb
+++ b/actionpack/test/template/prototype_helper_test.rb
@@ -230,31 +230,31 @@ Element.update("baz", "<p>This is a test</p>");
end
def test_element_access
- assert_equal %($('hello');), @generator['hello']
+ assert_equal %($("hello");), @generator['hello']
end
def test_element_proxy_one_deep
@generator['hello'].hide
- assert_equal %($('hello').hide();), @generator.to_s
+ assert_equal %($("hello").hide();), @generator.to_s
end
def test_element_proxy_assignment
@generator['hello'].width = 400
- assert_equal %($('hello').width = 400;), @generator.to_s
+ assert_equal %($("hello").width = 400;), @generator.to_s
end
def test_element_proxy_two_deep
@generator['hello'].hide("first").clean_whitespace
- assert_equal %($('hello').hide("first").cleanWhitespace();), @generator.to_s
+ assert_equal %($("hello").hide("first").cleanWhitespace();), @generator.to_s
end
def test_select_access
- assert_equal %($$('div.hello');), @generator.select('div.hello')
+ assert_equal %($$("div.hello");), @generator.select('div.hello')
end
def test_select_proxy_one_deep
@generator.select('p.welcome b').first.hide
- assert_equal %($$('p.welcome b').first().hide();), @generator.to_s
+ assert_equal %($$("p.welcome b").first().hide();), @generator.to_s
end
def test_visual_effect
@@ -286,8 +286,8 @@ Element.update("baz", "<p>This is a test</p>");
@generator.select('p.welcome b').first.hide()
@generator.select('p.welcome b').last.show()
assert_equal <<-EOS.strip, @generator.to_s
-$$('p.welcome b').first().hide();
-$$('p.welcome b').last().show();
+$$("p.welcome b").first().hide();
+$$("p.welcome b").last().show();
EOS
end
@@ -299,10 +299,10 @@ $$('p.welcome b').last().show();
@generator.visual_effect :highlight, value
end
assert_equal <<-EOS.strip, @generator.to_s
-$$('p.welcome b').each(function(value, index) {
+$$("p.welcome b").each(function(value, index) {
value.removeClassName("selected");
});
-$$('p.welcome b').each(function(value, index) {
+$$("p.welcome b").each(function(value, index) {
new Effect.Highlight(value,{});
});
EOS
@@ -312,10 +312,10 @@ new Effect.Highlight(value,{});
@generator.select('p').collect('a') { |para| para.show }
@generator.select('p').collect { |para| para.hide }
assert_equal <<-EOS.strip, @generator.to_s
-var a = $$('p').collect(function(value, index) {
+var a = $$("p").collect(function(value, index) {
return value.show();
});
-$$('p').collect(function(value, index) {
+$$("p").collect(function(value, index) {
return value.hide();
});
EOS
@@ -332,10 +332,10 @@ return value.hide();
end
assert_equal <<-EOS.strip, @generator.to_s
-var a = $$('p').grep(/^a/, function(value, index) {
+var a = $$("p").grep(/^a/, function(value, index) {
return (value.className == "welcome");
});
-var b = $$('p').grep(/b$/, function(value, index) {
+var b = $$("p").grep(/b$/, function(value, index) {
alert(value);
return (value.className == "welcome");
});
@@ -352,10 +352,10 @@ return (value.className == "welcome");
end
assert_equal <<-EOS.strip, @generator.to_s
-var a = $$('p').inject([], function(memo, value, index) {
+var a = $$("p").inject([], function(memo, value, index) {
return (value.className == "welcome");
});
-var b = $$('p').inject(null, function(memo, value, index) {
+var b = $$("p").inject(null, function(memo, value, index) {
alert(memo);
return (value.className == "welcome");
});
@@ -364,7 +364,7 @@ return (value.className == "welcome");
def test_collection_proxy_with_pluck
@generator.select('p').pluck('a', 'className')
- assert_equal %(var a = $$('p').pluck("className");), @generator.to_s
+ assert_equal %(var a = $$("p").pluck("className");), @generator.to_s
end
def test_collection_proxy_with_zip
@@ -380,9 +380,17 @@ return array.reverse();
});
EOS
end
+
+ def test_debug_rjs
+ ActionView::Base.debug_rjs = true
+ @generator['welcome'].replace_html 'Welcome'
+ assert_equal "try {\n$(\"welcome\").update(\"Welcome\");\n} catch (e) { alert('RJS error:\\n\\n' + e.toString()); throw e }", @generator.to_s
+ ensure
+ ActionView::Base.debug_rjs = false
+ end
def test_class_proxy
@generator.form.focus('my_field')
assert_equal "Form.focus(\"my_field\");", @generator.to_s
end
-end \ No newline at end of file
+end