aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_view/helpers/prototype_helper.rb15
-rw-r--r--actionpack/test/template/prototype_helper_test.rb8
2 files changed, 21 insertions, 2 deletions
diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb
index f1e67f05f4..a9a68f474b 100644
--- a/actionpack/lib/action_view/helpers/prototype_helper.rb
+++ b/actionpack/lib/action_view/helpers/prototype_helper.rb
@@ -485,10 +485,21 @@ module ActionView
def <<(javascript)
@lines << javascript
end
-
+
+ # Executes the content of the block after a delay of +seconds+. Example:
+ #
+ # page.delay(20) do
+ # page.visual_effect :fade, 'notice'
+ # end
+ def delay(seconds = 1)
+ record "setTimeout(function() {\n\n"
+ yield
+ record "}, #{(seconds * 1000).to_i})"
+ end
+
private
def method_missing(method, *arguments, &block)
- record @context.send(method, *arguments, &block)
+ record(@context.send(method, *arguments, &block))
end
def record(line)
diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb
index 56485bc21c..eef6841727 100644
--- a/actionpack/test/template/prototype_helper_test.rb
+++ b/actionpack/test/template/prototype_helper_test.rb
@@ -201,6 +201,14 @@ class JavaScriptGeneratorTest < Test::Unit::TestCase
@generator.redirect_to(:action => 'welcome')
end
+ def test_delay
+ @generator.delay(20) do
+ @generator.hide('foo')
+ end
+
+ assert_equal "setTimeout(function() {\n;\nElement.hide(\"foo\");\n}, 20000);", @generator.to_s
+ end
+
def test_to_s
@generator.insert_html(:top, 'element', '<p>This is a test</p>')
@generator.insert_html(:bottom, 'element', '<p>This is a test</p>')