Module: Blazer::BaseHelper

Defined in:
app/helpers/blazer/base_helper.rb

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#blazer_format_annotations(annotations) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/helpers/blazer/base_helper.rb', line 45

def blazer_format_annotations(annotations)
  return [] unless annotations.is_a?(Array)
  sorted = annotations.sort_by { |annotation| annotation[:min_date] }

  boxes = sorted.select { |annotation| annotation[:max_date] }.map.with_index do |annotation, index|
    {
      type: "box",
      xScaleID: "x",
      xMin: annotation[:min_date],
      xMax: annotation[:max_date],
      backgroundColor: annotation[:color] || blazer_map_annotation_box_colors(index),
    }
  end

  # chartjs annotations don't support labels for box annotations
  labels = sorted.select { |annotation| annotation[:label] }.map.with_index do |annotation, index|
    {
      type: "line",
      value: annotation[:min_date],
      mode: "vertical",
      scaleID: "x",
      adjustScaleRange: false,
      borderColor: annotation[:color] || '#00000050',
      drawTime: "afterDatasetsDraw",
      label: {
        content: annotation[:label],
        enabled: true,
        position: "bottom",
        yAdjust: 30 + (index * 30) % 60,
      },
    }
  end

  boxes + labels
end

#blazer_format_value(key, value) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/helpers/blazer/base_helper.rb', line 14

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)
  elsif value.is_a?(String) && 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
  else
    value
  end
end

#blazer_js_var(name, value) ⇒ Object



31
32
33
# File 'app/helpers/blazer/base_helper.rb', line 31

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

#blazer_series_name(k) ⇒ Object



35
36
37
# File 'app/helpers/blazer/base_helper.rb', line 35

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

#blazer_title(title = nil) ⇒ Object



3
4
5
6
7
8
9
# File 'app/helpers/blazer/base_helper.rb', line 3

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

#blazer_var_default?(key) ⇒ Boolean



39
40
41
42
43
# File 'app/helpers/blazer/base_helper.rb', line 39

def blazer_var_default?(key)
  value = request.query_parameters[key]
  return true if value.to_s == ""
  return true if @data_sources.any? { |source| source.variable_defaults[key].to_s == value.to_s }
end