Guest User

Untitled

a guest
Jul 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. module BestInPlace
  2. module BestInPlaceHelpers
  3. def best_in_place(object, field, opts = {})
  4. opts[:type] ||= :input
  5. opts[:collection] ||= []
  6. opts[:nil] ||= "-"
  7. field = field.to_s
  8. value = object.send(field).blank? ? opts[:nil].to_s : object.send(field)
  9. collection = nil
  10. if opts[:type] == :select && !opts[:collection].blank?
  11. v = object.send(field)
  12. value = Hash[opts[:collection]][!!(v =~ /^[0-9]+$/) ? v.to_i : v]
  13. collection = opts[:collection].to_json
  14. end
  15. if opts[:type] == :checkbox
  16. fieldValue = !!object.send(field)
  17. if opts[:collection].blank? || opts[:collection].size != 2
  18. opts[:collection] = ["No", "Yes"]
  19. end
  20. value = fieldValue ? opts[:collection][1] : opts[:collection][0]
  21. collection = opts[:collection].to_json
  22. end
  23. out = "<span class='best_in_place'"
  24. out << " id='best_in_place_#{object.class.to_s.gsub("::", "_").underscore}_#{field}'"
  25. out << " data-url='#{opts[:path].blank? ? url_for(object).to_s : url_for(opts[:path])}'"
  26. out << " data-object='#{object.class.to_s.gsub("::", "_").underscore}'"
  27. out << " data-collection='#{collection}'" unless collection.blank?
  28. out << " data-attribute='#{field}'"
  29. out << " data-activator='#{opts[:activator]}'" unless opts[:activator].blank?
  30. out << " data-type='#{opts[:type].to_s}'>"
  31. unless opts[:sanitize].nil? && !!opts[:sanitize]
  32. out << " data-sanitize='#{!!opts[:sanitize]}'"
  33. 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))
  34. else
  35. out << value.to_s
  36. end
  37. out << "</span>"
  38. end
  39. end
  40. end
  41.  
  42.  
  43. ActionView::Base.send(:include, BestInPlace::BestInPlaceHelpers)
Add Comment
Please, Sign In to add comment