Skip to content

Instantly share code, notes, and snippets.

@bernat
Created February 3, 2011 11:00
Show Gist options
  • Save bernat/809348 to your computer and use it in GitHub Desktop.
Save bernat/809348 to your computer and use it in GitHub Desktop.
helper best in place
module BestInPlace
module BestInPlaceHelpers
def best_in_place(object, field, opts = {})
opts[:type] ||= :input
opts[:collection] ||= []
opts[:nil] ||= "-"
field = field.to_s
value = object.send(field).blank? ? opts[:nil].to_s : object.send(field)
collection = nil
if opts[:type] == :select && !opts[:collection].blank?
v = object.send(field)
value = Hash[opts[:collection]][!!(v =~ /^[0-9]+$/) ? v.to_i : v]
collection = opts[:collection].to_json
end
if opts[:type] == :checkbox
fieldValue = !!object.send(field)
if opts[:collection].blank? || opts[:collection].size != 2
opts[:collection] = ["No", "Yes"]
end
value = fieldValue ? opts[:collection][1] : opts[:collection][0]
collection = opts[:collection].to_json
end
out = "<span class='best_in_place'"
out << " id='best_in_place_#{object.class.to_s.gsub("::", "_").underscore}_#{field}'"
out << " data-url='#{opts[:path].blank? ? url_for(object).to_s : url_for(opts[:path])}'"
out << " data-object='#{object.class.to_s.gsub("::", "_").underscore}'"
out << " data-collection='#{collection}'" unless collection.blank?
out << " data-attribute='#{field}'"
out << " data-activator='#{opts[:activator]}'" unless opts[:activator].blank?
out << " data-type='#{opts[:type].to_s}'>"
unless opts[:sanitize].nil? && !!opts[:sanitize]
out << " data-sanitize='#{!!opts[:sanitize]}'"
out << sanitize(value.to_s, :tags => %w(b i u s a strong em p h1 h2 h3 h4 h5 ul li ol hr pre span img), :attributes => %w(id class))
else
out << value.to_s
end
out << "</span>"
end
end
end
ActionView::Base.send(:include, BestInPlace::BestInPlaceHelpers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment