forked from nakajima/better-edit-in-place
-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.rb
38 lines (32 loc) · 1.38 KB
/
init.rb
1
2
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
module Nakajima
module BetterEditInPlace
def edit_in_place(resource, field, options={})
# Get record to be edited. If resource is an array, pull it out.
record = resource.is_a?(Array) ? resource.last : resource
if options.delete(:allow) == false
return record.send(field).blank? ? %(<i title="This information is missing.">[#{field.to_s.humanize}]</i>) : record.send(field)
end
options[:id] ||= "#{dom_id(record)}_#{field}"
options[:tag] ||= :span
options[:url] ||= url_for(resource)
options[:rel] ||= options.delete(:url)
options[:edit_blank] ||= true
options[:empty_message] ||= "[#{field.to_s.humanize}]"
options.delete(:url) # Just in case it wasn't cleared already
classes = options[:class].split(' ') rescue []
classes << 'editable' if classes.empty?
options[:class] = classes.uniq.join(' ')
if record.send(field).blank? && options.delete(:edit_blank)
options[:class] << ' novalue'
data = options.delete(:empty_message)
else
data = record.send(field)
end
# Cleanup options hash
options.delete(:edit_blank)
options.delete(:empty_message)
content_tag(:span, content_tag(options.delete(:tag), data, options), :class => 'edit-outer')
end
end
end
ActionView::Base.send :include, Nakajima::BetterEditInPlace