Skip to content

Commit

Permalink
Deprecate JavaScriptHelper#update_element_function, which is supersee…
Browse files Browse the repository at this point in the history
…ded by RJS [Thomas Fuchs]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5438 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
madrobby committed Nov 6, 2006
1 parent ba553f9 commit e52e803
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 99 deletions.
2 changes: 2 additions & 0 deletions actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*

* Deprecate JavaScriptHelper#update_element_function, which is superseeded by RJS [Thomas Fuchs]

* pluralize helper interprets nil as zero. #6474 [pope]

* Fix invalid test fixture exposed by stricter Ruby 1.8.5 multipart parsing. #6524 [Bob Silva]
Expand Down
34 changes: 34 additions & 0 deletions actionpack/lib/action_view/helpers/deprecated_helper.rb
@@ -0,0 +1,34 @@
module ActionView
module Helpers
module PrototypeHelper

def update_element_function(element_id, options = {}, &block)
content = escape_javascript(options[:content] || '')
content = escape_javascript(capture(&block)) if block

javascript_function = case (options[:action] || :update)
when :update
if options[:position]
"new Insertion.#{options[:position].to_s.camelize}('#{element_id}','#{content}')"
else
"$('#{element_id}').innerHTML = '#{content}'"
end

when :empty
"$('#{element_id}').innerHTML = ''"

when :remove
"Element.remove('#{element_id}')"

else
raise ArgumentError, "Invalid action, choose one of :update, :remove, :empty"
end

javascript_function << ";\n"
options[:binding] ? concat(javascript_function, options[:binding]) : javascript_function
end
deprecate :update_element_function => "use RJS instead"

end
end
end
75 changes: 0 additions & 75 deletions actionpack/lib/action_view/helpers/prototype_helper.rb
Expand Up @@ -204,81 +204,6 @@ def submit_to_remote(name, value, options = {})
tag("input", options[:html], false)
end

# Returns a JavaScript function (or expression) that'll update a DOM
# element according to the options passed.
#
# * <tt>:content</tt>: The content to use for updating. Can be left out
# if using block, see example.
# * <tt>:action</tt>: Valid options are :update (assumed by default),
# :empty, :remove
# * <tt>:position</tt> If the :action is :update, you can optionally
# specify one of the following positions: :before, :top, :bottom,
# :after.
#
# Examples:
# <%= javascript_tag(update_element_function("products",
# :position => :bottom, :content => "<p>New product!</p>")) %>
#
# <% replacement_function = update_element_function("products") do %>
# <p>Product 1</p>
# <p>Product 2</p>
# <% end %>
# <%= javascript_tag(replacement_function) %>
#
# This method can also be used in combination with remote method call
# where the result is evaluated afterwards to cause multiple updates on
# a page. Example:
#
# # Calling view
# <%= form_remote_tag :url => { :action => "buy" },
# :complete => evaluate_remote_response %>
# all the inputs here...
#
# # Controller action
# def buy
# @product = Product.find(1)
# end
#
# # Returning view
# <%= update_element_function(
# "cart", :action => :update, :position => :bottom,
# :content => "<p>New Product: #{@product.name}</p>")) %>
# <% update_element_function("status", :binding => binding) do %>
# You've bought a new product!
# <% end %>
#
# Notice how the second call doesn't need to be in an ERb output block
# since it uses a block and passes in the binding to render directly.
# This trick will however only work in ERb (not Builder or other
# template forms).
#
# See also JavaScriptGenerator and update_page.
def update_element_function(element_id, options = {}, &block)
content = escape_javascript(options[:content] || '')
content = escape_javascript(capture(&block)) if block

javascript_function = case (options[:action] || :update)
when :update
if options[:position]
"new Insertion.#{options[:position].to_s.camelize}('#{element_id}','#{content}')"
else
"$('#{element_id}').innerHTML = '#{content}'"
end

when :empty
"$('#{element_id}').innerHTML = ''"

when :remove
"Element.remove('#{element_id}')"

else
raise ArgumentError, "Invalid action, choose one of :update, :remove, :empty"
end

javascript_function << ";\n"
options[:binding] ? concat(javascript_function, options[:binding]) : javascript_function
end

# Returns 'eval(request.responseText)' which is the JavaScript function
# that form_remote_tag can call in :complete to evaluate a multiple
# update return document using update_element_function calls.
Expand Down
36 changes: 36 additions & 0 deletions actionpack/test/template/deprecated_helper_test.rb
@@ -0,0 +1,36 @@
require File.dirname(__FILE__) + '/../abstract_unit'

class DeprecatedHelperTest < Test::Unit::TestCase
include ActionView::Helpers::JavaScriptHelper
include ActionView::Helpers::CaptureHelper

def test_update_element_function
assert_deprecated 'update_element_function' do

assert_equal %($('myelement').innerHTML = 'blub';\n),
update_element_function('myelement', :content => 'blub')
assert_equal %($('myelement').innerHTML = 'blub';\n),
update_element_function('myelement', :action => :update, :content => 'blub')
assert_equal %($('myelement').innerHTML = '';\n),
update_element_function('myelement', :action => :empty)
assert_equal %(Element.remove('myelement');\n),
update_element_function('myelement', :action => :remove)

assert_equal %(new Insertion.Bottom('myelement','blub');\n),
update_element_function('myelement', :position => 'bottom', :content => 'blub')
assert_equal %(new Insertion.Bottom('myelement','blub');\n),
update_element_function('myelement', :action => :update, :position => :bottom, :content => 'blub')

_erbout = ""
assert_equal %($('myelement').innerHTML = 'test';\n),
update_element_function('myelement') { _erbout << "test" }

_erbout = ""
assert_equal %($('myelement').innerHTML = 'blockstuff';\n),
update_element_function('myelement', :content => 'paramstuff') { _erbout << "blockstuff" }

end
end

end

24 changes: 0 additions & 24 deletions actionpack/test/template/prototype_helper_test.rb
Expand Up @@ -143,30 +143,6 @@ def test_observe_form_using_function_for_callback
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Observer('cart', 2, function(element, value) {alert('Form changed')})\n//]]>\n</script>),
observe_form("cart", :frequency => 2, :function => "alert('Form changed')")
end

def test_update_element_function
assert_equal %($('myelement').innerHTML = 'blub';\n),
update_element_function('myelement', :content => 'blub')
assert_equal %($('myelement').innerHTML = 'blub';\n),
update_element_function('myelement', :action => :update, :content => 'blub')
assert_equal %($('myelement').innerHTML = '';\n),
update_element_function('myelement', :action => :empty)
assert_equal %(Element.remove('myelement');\n),
update_element_function('myelement', :action => :remove)

assert_equal %(new Insertion.Bottom('myelement','blub');\n),
update_element_function('myelement', :position => 'bottom', :content => 'blub')
assert_equal %(new Insertion.Bottom('myelement','blub');\n),
update_element_function('myelement', :action => :update, :position => :bottom, :content => 'blub')

_erbout = ""
assert_equal %($('myelement').innerHTML = 'test';\n),
update_element_function('myelement') { _erbout << "test" }

_erbout = ""
assert_equal %($('myelement').innerHTML = 'blockstuff';\n),
update_element_function('myelement', :content => 'paramstuff') { _erbout << "blockstuff" }
end

def test_update_page
block = Proc.new { |page| page.replace_html('foo', 'bar') }
Expand Down

0 comments on commit e52e803

Please sign in to comment.