base_helper.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. module Blazer
  2. module BaseHelper
  3. def blazer_title(title = nil)
  4. if title
  5. content_for(:title) { title }
  6. else
  7. content_for?(:title) ? content_for(:title) : nil
  8. end
  9. end
  10. def blazer_format_value(key, value)
  11. if value.is_a?(Integer) && !key.to_s.end_with?("id")
  12. number_with_delimiter(value)
  13. else
  14. value
  15. end
  16. end
  17. def blazer_column_types(columns, rows, boom)
  18. columns.map do |k, _|
  19. v = (rows.find { |r| r[k] } || {})[k]
  20. if boom[k]
  21. "string"
  22. elsif v.is_a?(Numeric)
  23. "numeric"
  24. elsif v.is_a?(Time) || v.is_a?(Date)
  25. "time"
  26. elsif v.nil?
  27. nil
  28. else
  29. "string"
  30. end
  31. end
  32. end
  33. def blazer_maps?
  34. ENV["MAPBOX_ACCESS_TOKEN"].present?
  35. end
  36. JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003e', '<' => '\u003c', "\u2028" => '\u2028', "\u2029" => '\u2029' }
  37. JSON_ESCAPE_REGEXP = /[\u2028\u2029&><]/u
  38. # Prior to version 4.1 of rails double quotes were inadventently removed in json_escape.
  39. # This adds the correct json_escape functionality to rails versions < 4.1
  40. def blazer_json_escape(s)
  41. if Rails::VERSION::STRING < "4.1"
  42. result = s.to_s.gsub(JSON_ESCAPE_REGEXP, JSON_ESCAPE)
  43. s.html_safe? ? result.html_safe : result
  44. else
  45. json_escape(s)
  46. end
  47. end
  48. end
  49. end