module BestInPlace module BestInPlaceHelpers def best_in_place(object, field, opts = {}) opts[:type] ||= :input opts[:collection] ||= [] field = field.to_s value = object.send(field).blank? ? "" : 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 = "" 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 br), :attributes => %w(id class href)) else out << ">#{sanitize(value.to_s, :tags => nil, :attributes => nil)}" end out << "" raw out end def best_in_place_if(condition, object, field, opts={}) if condition best_in_place(object, field, opts) else object.send field end end end end