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
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/best_in_place/helper.rb', line 7
def best_in_place(object, field, opts = {})
if opts[:display_as] && opts[:display_with]
raise ArgumentError, "Can't use both 'display_as' and 'display_with' options at the same time"
end
if opts[:display_with] && !opts[:display_with].is_a?(Proc) && !ViewHelpers.respond_to?(opts[:display_with])
raise ArgumentError, "Can't find helper #{opts[:display_with]}"
end
real_object = real_object_for object
opts[:type] ||= :input
opts[:collection] ||= []
field = field.to_s
display_value = build_value_for(real_object, field, opts)
collection = nil
value = nil
if opts[:type] == :select && !opts[:collection].blank?
value = real_object.send(field)
display_value = Hash[opts[:collection]].stringify_keys[value.to_s]
collection = opts[:collection].to_json
end
if opts[:type] == :checkbox
value = !!real_object.send(field)
if opts[:collection].blank? || opts[:collection].size != 2
opts[:collection] = ["No", "Yes"]
end
display_value = value ? opts[:collection][1] : opts[:collection][0]
collection = opts[:collection].to_json
end
classes = ["best_in_place"]
unless opts[:classes].nil?
classes << opts[:classes]
classes.flatten!
end
out = "<span class='#{classes.join(" ")}'"
out << " id='#{BestInPlace::Utils.build_best_in_place_id(real_object, field)}'"
out << " data-url='#{opts[:path].blank? ? url_for(object) : url_for(opts[:path])}'"
out << " data-object='#{opts[:object_name] || BestInPlace::Utils.object_to_key(real_object)}'"
out << " data-collection='#{attribute_escape(collection)}'" unless collection.blank?
out << " data-attribute='#{field}'"
out << " data-activator='#{opts[:activator]}'" unless opts[:activator].blank?
out << " data-ok-button='#{opts[:ok_button]}'" unless opts[:ok_button].blank?
out << " data-ok-button-class='#{opts[:ok_button_class]}'" unless opts[:ok_button_class].blank?
out << " data-cancel-button='#{opts[:cancel_button]}'" unless opts[:cancel_button].blank?
out << " data-cancel-button-class='#{opts[:cancel_button_class]}'" unless opts[:cancel_button_class].blank?
out << " data-nil='#{attribute_escape(opts[:nil])}'" unless opts[:nil].blank?
out << " data-use-confirm='#{opts[:use_confirm]}'" unless opts[:use_confirm].nil?
out << " data-type='#{opts[:type]}'"
out << " data-inner-class='#{opts[:inner_class]}'" if opts[:inner_class]
out << " data-html-attrs='#{opts[:html_attrs].to_json}'" unless opts[:html_attrs].blank?
out << " data-original-content='#{attribute_escape(real_object.send(field))}'" out << " data-value='#{attribute_escape(value)}'" if value
if opts[:data] && opts[:data].is_a?(Hash)
opts[:data].each do |k, v|
if !v.is_a?(String) && !v.is_a?(Symbol)
v = v.to_json
end
out << %( data-#{k.to_s.dasherize}="#{v}")
end
end
if !opts[:sanitize].nil? && !opts[:sanitize]
out << " data-sanitize='false'>"
out << display_value.to_s
else
out << ">#{h(display_value.to_s)}"
end
out << "</span>"
raw out
end
|