Permalink
Browse files

Added simple alert() notifications for RJS exceptions when config.act…

…ion_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
  • Loading branch information...
1 parent 955583a commit 3589871de82c12123f62c80b4924d2696412065c @sstephenson sstephenson committed Mar 13, 2006
@@ -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]
@@ -162,6 +162,11 @@ class Base
# 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 = {}
@@ -370,12 +370,12 @@ def initialize(context, &block) #:nodoc:
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 @@ def include_helpers_from_context
# <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 @@ def append_to_function_chain!(call)
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 @@ def append_enumerable_function!(call)
class JavaScriptElementCollectionProxy < JavaScriptCollectionProxy #:nodoc:\
def initialize(generator, pattern)
- super(generator, "$$('#{pattern}')")
+ super(generator, "$$(#{pattern.to_json})")
end
end
end
@@ -141,7 +141,7 @@ def test_using_defaults
@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
@@ -393,7 +393,7 @@ def test_render_xml
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)})}});
@@ -230,31 +230,31 @@ def test_to_s
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 @@ def test_collection_first_and_last
@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 @@ def test_collection_proxy_with_each
@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 @@ def test_collection_proxy_on_collect
@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 @@ def test_collection_proxy_with_grep
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 @@ def test_collection_proxy_with_inject
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 @@ def test_collection_proxy_with_inject
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 @@ def test_collection_proxy_with_zip
});
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
+end

0 comments on commit 3589871

Please sign in to comment.