Sha256: 6125c0b8c742a517b3dacf7dab01ec1d896bf2a5f5c01918bcac6ff4982e3dc0

Contents?: true

Size: 1.96 KB

Versions: 11

Compression:

Stored size: 1.96 KB

Contents

module Blazer
  module BaseHelper
    def blazer_title(title = nil)
      if title
        content_for(:title) { title }
      else
        content_for?(:title) ? content_for(:title) : nil
      end
    end

    BLAZER_URL_REGEX = /\Ahttps?:\/\/[\S]+\z/
    BLAZER_IMAGE_EXT = %w[png jpg jpeg gif]

    def blazer_format_value(key, value)
      if value.is_a?(Numeric) && !key.to_s.end_with?("id") && !key.to_s.start_with?("id")
        number_with_delimiter(value, delimiter: ',', separator: '.')
      elsif value =~ BLAZER_URL_REGEX
        # see if image or link
        if Blazer.images && (key.include?("image") || BLAZER_IMAGE_EXT.include?(value.split(".").last.split("?").first.try(:downcase)))
          link_to value, target: "_blank" do
            image_tag value, referrerpolicy: "no-referrer"
          end
        else
          link_to value, value, target: "_blank"
        end
      elsif key.end_with?("_date")
        value.in_time_zone(Blazer.time_zone).strftime('%Y/%m/%d') rescue value
      elsif key.end_with?("_time")
        value.in_time_zone(Blazer.time_zone).strftime('%H:%M') rescue value
      else
        value
      end
    end

    def blazer_maps?
      Blazer.mapbox_access_token.present?
    end

    def blazer_js_var(name, value)
      "var #{name} = #{blazer_json_escape(value.to_json(root: false))};".html_safe
    end

    JSON_ESCAPE = { '&' => '\u0026', '>' => '\u003e', '<' => '\u003c', "\u2028" => '\u2028', "\u2029" => '\u2029' }
    JSON_ESCAPE_REGEXP = /[\u2028\u2029&><]/u

    # Prior to version 4.1 of rails double quotes were inadventently removed in json_escape.
    # This adds the correct json_escape functionality to rails versions < 4.1
    def blazer_json_escape(s)
      if Rails::VERSION::STRING < "4.1"
        result = s.to_s.gsub(JSON_ESCAPE_REGEXP, JSON_ESCAPE)
        s.html_safe? ? result.html_safe : result
      else
        json_escape(s)
      end
    end

    def blazer_series_name(k)
      k.nil? ? "null" : k.to_s
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
sql-jarvis-2.1.10 app/helpers/blazer/base_helper.rb
sql-jarvis-2.1.9 app/helpers/blazer/base_helper.rb
sql-jarvis-2.1.8 app/helpers/blazer/base_helper.rb
sql-jarvis-2.1.7 app/helpers/blazer/base_helper.rb
sql-jarvis-2.1.6 app/helpers/blazer/base_helper.rb
sql-jarvis-2.1.5 app/helpers/blazer/base_helper.rb
sql-jarvis-2.1.4 app/helpers/blazer/base_helper.rb
sql-jarvis-2.1.3 app/helpers/blazer/base_helper.rb
sql-jarvis-2.1.2 app/helpers/blazer/base_helper.rb
sql-jarvis-2.1.1 app/helpers/blazer/base_helper.rb
sql-jarvis-2.1 app/helpers/blazer/base_helper.rb