Allow JSON-style values for the :with option of observe_field. Closes #8557 [kommen]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6930 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Thomas Fuchs 2007-06-03 14:27:43 +00:00
parent 13058b0188
commit 3cfb894bac
3 changed files with 14 additions and 1 deletions

@ -1,5 +1,7 @@
*SVN*
* Allow JSON-style values for the :with option of observe_field. Closes #8557 [kommen]
* Remove RAILS_ROOT from backtrace paths. #8540 [Tim Pope]
* Routing: map.resource :logo routes to LogosController so the controller may be reused for multiple nestings or namespaces. [Jeremy Kemper]

@ -698,7 +698,7 @@ def method_option_to_s(method)
end
def build_observer(klass, name, options = {})
if options[:with] && (options[:with] !~ /[=(.]/)
if options[:with] && (options[:with] !~ /[\{=(.]/)
options[:with] = "'#{options[:with]}=' + value"
else
options[:with] ||= 'value' unless options[:function]

@ -174,6 +174,17 @@ def test_observe_field
observe_field("glass", :frequency => 5.minutes, :url => { :action => "reorder_if_empty" })
end
def test_observe_field_using_with_option
expected = %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.Observer('glass', 300, function(element, value) {new Ajax.Request('http://www.example.com/check_value', {asynchronous:true, evalScripts:true, parameters:'id=' + value})})\n//]]>\n</script>)
assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => 'id')
assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => "'id=' + value")
end
def test_observe_field_using_json_in_with_option
expected = %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.Observer('glass', 300, function(element, value) {new Ajax.Request('http://www.example.com/check_value', {asynchronous:true, evalScripts:true, parameters:{'id':value}})})\n//]]>\n</script>)
assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => "{'id':value}")
end
def test_observe_field_using_function_for_callback
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.Observer('glass', 300, function(element, value) {alert('Element changed')})\n//]]>\n</script>),
observe_field("glass", :frequency => 5.minutes, :function => "alert('Element changed')")