3
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/best_in_place/helper.rb', line 3
def best_in_place(object, field, opts = {})
best_in_place_assert_arguments(opts)
type = opts[:as] || :input
field = field.to_s
options = {}
options[:data] = HashWithIndifferentAccess.new(opts[:data])
options[:data]['bip-type'] = type
options[:data]['bip-attribute'] = field
real_object = best_in_place_real_object_for object
display_value = best_in_place_build_value_for(real_object, field, opts)
value = real_object.send(field)
if opts[:collection] or type == :checkbox
collection = opts[:collection]
value = value.to_s
collection = best_in_place_default_collection if collection.blank?
collection = best_in_place_collection_builder(type, collection)
display_value = collection.flat_map{|a| a[0].to_s == value ? a[1] : nil }.compact[0]
collection = collection.to_json
options[:data]['bip-collection'] = html_escape(collection)
end
options[:class] = ['best_in_place'] + Array(opts[:class] || opts[:classes])
options[:id] = opts[:id] || BestInPlace::Utils.build_best_in_place_id(real_object, field)
pass_through_html_options(opts, options)
options[:data]['bip-activator'] = opts[:activator].presence
options[:data]['bip-html-attrs'] = opts[:html_attrs].to_json unless opts[:html_attrs].blank?
options[:data]['bip-inner-class'] = opts[:inner_class].presence
options[:data]['bip-placeholder'] = html_escape(opts[:place_holder]).presence
options[:data]['bip-object'] = opts[:param] || BestInPlace::Utils.object_to_key(real_object)
options[:data]['bip-ok-button'] = opts[:ok_button].presence
options[:data]['bip-ok-button-class'] = opts[:ok_button_class].presence
options[:data]['bip-cancel-button'] = opts[:cancel_button].presence
options[:data]['bip-cancel-button-class'] = opts[:cancel_button_class].presence
options[:data]['bip-original-content'] = html_escape(opts[:value] || value).presence
options[:data]['bip-skip-blur'] = opts.has_key?(:skip_blur) ? opts[:skip_blur].presence : BestInPlace.skip_blur
options[:data]['bip-url'] = url_for(opts[:url] || object)
if real_object.respond_to?(:new_record?) and real_object.new_record?
options[:data]['bip-new-object'] = true
options[:data]['bip-extra-payload'] = Hash[real_object.changes.map { |k,v| [k, v[1]] }]
end
options[:data]['bip-confirm'] = opts[:confirm].presence
options[:data]['bip-value'] = html_escape(value).presence
if opts[:raw]
options[:data]['bip-raw'] = 'true'
end
options[:data].delete_if { |_, v| v.nil? }
container = opts[:container] || BestInPlace.container
content_tag(container, display_value, options, opts[:raw].blank?)
end
|