aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib
diff options
context:
space:
mode:
authorBob Remeika <bob.remeika@gmail.com>2009-11-03 23:18:25 -0800
committerStefan Penner <stefan.penner@gmail.com>2010-01-27 12:44:29 -0600
commit5ced1225fa2048b327c9afb8be48f99dff9841ae (patch)
tree52414c1514a033de930a18ff8e0124c537c06a6e /actionpack/lib
parentc1ce17a5f428dc48711299cfd5851798f1018a33 (diff)
downloadrails-5ced1225fa2048b327c9afb8be48f99dff9841ae.tar.gz
rails-5ced1225fa2048b327c9afb8be48f99dff9841ae.tar.bz2
rails-5ced1225fa2048b327c9afb8be48f99dff9841ae.zip
Applied Yehuda's patch; Sharing extract_object_name_for_form! between form_helper and ajax_helper; Added script_decorator helper
Diffstat (limited to 'actionpack/lib')
-rw-r--r--actionpack/lib/action_view/helpers/ajax_helper.rb52
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb36
2 files changed, 48 insertions, 40 deletions
diff --git a/actionpack/lib/action_view/helpers/ajax_helper.rb b/actionpack/lib/action_view/helpers/ajax_helper.rb
index f4da540f42..d6c43a1a15 100644
--- a/actionpack/lib/action_view/helpers/ajax_helper.rb
+++ b/actionpack/lib/action_view/helpers/ajax_helper.rb
@@ -3,6 +3,16 @@ module ActionView
module AjaxHelper
include UrlHelper
+ def remote_form_for(record_or_name_or_array, *args, &proc)
+ options = args.extract_options!
+ object_name = extract_object_name_for_form!(args, options, record_or_name_or_array)
+
+ concat(form_remote_tag(options))
+ fields_for(object_name, *(args << options), &proc)
+ concat('</form>'.html_safe!)
+ end
+ alias_method :form_remote_for, :remote_form_for
+
def form_remote_tag(options = {}, &block)
attributes = {}
attributes.merge!(extract_remote_attributes!(options))
@@ -50,41 +60,35 @@ module ActionView
end
def observe_field(name, options = {})
- if options[:url]
- options[:url] = options[:url].is_a?(Hash) ? url_for(options[:url]) : options[:url]
- end
+ url = options[:url]
+ options[:url] = url_for(url) if url && url.is_a?(Hash)
- if options[:frequency]
- case options[:frequency]
- when 0
- options.delete(:frequency)
- else
- options[:frequency] = options[:frequency].to_i
- end
+ frequency = options.delete(:frequency)
+ if frequency && frequency != 0
+ options[:frequency] = frequency.to_i
end
- if options[:with]
- if options[:with] !~ /[\{=(.]/
+ if with = options[:with]
+ if with !~ /[\{=(.]/
options[:with] = "'#{options[:with]}=' + encodeURIComponent(value)"
else
options[:with] ||= 'value' unless options[:function]
end
end
- if options[:function]
- statements = options[:function] # || remote_function(options) # TODO: Need to implement remote function - BR
+ if function = options[:function]
+ statements = function # || remote_function(options) # TODO: Need to implement remote function - BR
options[:function] = JSFunction.new(statements, "element", "value")
end
-
options[:name] = name
- <<-SCRIPT
- <script type="application/json" data-js-type="field_observer">
- //<![CDATA[
- #{options.to_json}
- // ]]>
- </script>
- SCRIPT
+ script_decorator("field_observer", options)
+ end
+
+ def script_decorator(js_type, options)
+ attributes = [%(type="application/json"), %(data-js-type="#{js_type}")]
+ attributes += options.map{|k, v| %(data-#{k}="#{v}")}
+ "<script " + attributes.join(" ") + "></script>"
end
module Rails2Compatibility
@@ -124,11 +128,11 @@ module ActionView
@statements, @arguments = statements, arguments
end
- def as_json(options = nil)
+ def to_s(options = nil)
"function(#{@arguments.join(", ")}) {#{@statements}}"
end
end
end
end
-end \ No newline at end of file
+end
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index 20e9916d62..1615f135b4 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -262,23 +262,8 @@ module ActionView
# FormTagHelper#form_tag.
def form_for(record_or_name_or_array, *args, &proc)
raise ArgumentError, "Missing block" unless block_given?
-
options = args.extract_options!
-
- case record_or_name_or_array
- when String, Symbol
- object_name = record_or_name_or_array
- when Array
- object = record_or_name_or_array.last
- object_name = ActionController::RecordIdentifier.singular_class_name(object)
- apply_form_for_options!(record_or_name_or_array, options)
- args.unshift object
- else
- object = record_or_name_or_array
- object_name = ActionController::RecordIdentifier.singular_class_name(object)
- apply_form_for_options!([object], options)
- args.unshift object
- end
+ object_name = extract_object_name_for_form!(args, options, record_or_name_or_array)
concat(form_tag(options.delete(:url) || {}, options.delete(:html) || {}))
fields_for(object_name, *(args << options), &proc)
@@ -742,6 +727,25 @@ module ActionView
def radio_button(object_name, method, tag_value, options = {})
InstanceTag.new(object_name, method, self, options.delete(:object)).to_radio_button_tag(tag_value, options)
end
+
+ private
+ def extract_object_name_for_form!(args, options, record_or_name_or_array)
+ case record_or_name_or_array
+ when String, Symbol
+ object_name = record_or_name_or_array
+ when Array
+ object = record_or_name_or_array.last
+ object_name = ActionController::RecordIdentifier.singular_class_name(object)
+ apply_form_for_options!(record_or_name_or_array, options)
+ args.unshift object
+ else
+ object = record_or_name_or_array
+ object_name = ActionController::RecordIdentifier.singular_class_name(object)
+ apply_form_for_options!([object], options)
+ args.unshift object
+ end
+ object_name
+ end
end
module InstanceTagMethods #:nodoc: