base_helper.rb 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003e', '<' => '\u003c', "\u2028" => '\u2028', "\u2029" => '\u2029' }
  32. JSON_ESCAPE_REGEXP = /[\u2028\u2029&><]/u
  33. # Prior to version 4.1 of rails double quotes were inadventently removed in json_escape.
  34. # This adds the correct json_escape functionality to rails versions < 4.1
  35. def blazer_json_escape(s)
  36. if Rails::VERSION::STRING < "4.1"
  37. result = s.to_s.gsub(JSON_ESCAPE_REGEXP, JSON_ESCAPE)
  38. s.html_safe? ? result.html_safe : result
  39. else
  40. json_escape(s)
  41. end
  42. end
  43. end
  44. end