base_helper.rb 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. BLAZER_URL_REGEX = /\Ahttps?:\/\/[\S]+\z/
  11. BLAZER_IMAGE_EXT = %w[png jpg jpeg gif]
  12. def blazer_format_value(key, value)
  13. if value.is_a?(Integer) && !key.to_s.end_with?("id") && !key.to_s.start_with?("id")
  14. number_with_delimiter(value)
  15. elsif value =~ BLAZER_URL_REGEX
  16. # see if image or link
  17. if Blazer.images && (key.include?("image") || BLAZER_IMAGE_EXT.include?(value.split(".").last.split("?").first.try(:downcase)))
  18. link_to value, target: "_blank" do
  19. image_tag value, referrerpolicy: "no-referrer"
  20. end
  21. else
  22. link_to value, value, target: "_blank"
  23. end
  24. else
  25. value
  26. end
  27. end
  28. def blazer_maps?
  29. ENV["MAPBOX_ACCESS_TOKEN"].present?
  30. end
  31. def blazer_js_var(name, value)
  32. "var #{name} = #{blazer_json_escape(value.to_json(root: false))};".html_safe
  33. end
  34. JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003e', '<' => '\u003c', "\u2028" => '\u2028', "\u2029" => '\u2029' }
  35. JSON_ESCAPE_REGEXP = /[\u2028\u2029&><]/u
  36. # Prior to version 4.1 of rails double quotes were inadventently removed in json_escape.
  37. # This adds the correct json_escape functionality to rails versions < 4.1
  38. def blazer_json_escape(s)
  39. if Rails::VERSION::STRING < "4.1"
  40. result = s.to_s.gsub(JSON_ESCAPE_REGEXP, JSON_ESCAPE)
  41. s.html_safe? ? result.html_safe : result
  42. else
  43. json_escape(s)
  44. end
  45. end
  46. def blazer_series_name(k)
  47. k.nil? ? "null" : k.to_s
  48. end
  49. end
  50. end