4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/view_mixins/form.rb', line 4
def ajax_form_for(record, options = {}, &proc)
raise ArgumentError, "Missing block" unless block_given?
options[:html] ||= {}
case record
when String, Symbol
object_name = record
object = nil
else
object = record.is_a?(Array) ? record.last : record
object_name = options[:as] || ActiveModel::Naming.param_key(object)
apply_form_for_options!(record, options)
end
caller_id = options[:html][:id]
options[:html][:remote] = options.delete(:remote) if options.has_key?(:remote)
options[:html][:method] = options.delete(:method) if options.has_key?(:method)
options[:html][:authenticity_token] = options.delete(:authenticity_token)
builder = options[:parent_builder] = instantiate_builder(object_name, object, options, &proc)
fields_for = fields_for(object_name, object, options, &proc)
default_options = builder.multipart? ? {:multipart => true} : {}
output = form_tag(options.delete(:url) || {}, default_options.merge!(options.delete(:html)))
if !object.blank? && !object.id.blank?
output << "<fieldset><input type='hidden' name='#{ActiveModel::Naming.param_key(record)}[control_against_overwrite_by_another_user]' value='#{Time.now.utc}' /></fieldset>".html_safe
end
output << fields_for
output.safe_concat(build_ajax_callback_code(caller_id))
output.safe_concat('</form>')
end
|