aboutsummaryrefslogtreecommitdiff
path: root/app/helpers/application_helper.rb
blob: 15d508213c82db75a8f4ac04da3341c0d5662c30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
# SPDX-FileCopyrightText: 2020 IN COMMON Collective <collective@incommon.cc>
#
# SPDX-License-Identifier: AGPL-3.0-or-later

module ApplicationHelper
  def current_agency
    current_user.agencies.where(agent: current_agent).first
  end
  %w(observer editor maintainer leader).each do |role|
    define_method :"current_user_#{role}?" do
      current_agency.send(:"#{role}?")
    end
  end

  def map_container(map = Map.first)
    raw tag.div(
          tag.div(id: 'map',
                  data: {
                    target: 'map.container'
                  }),
          data: {
            controller: 'map',
            'map-latitude': map.latitude,
            'map-longitude': map.longitude,
            'map-zoom': map.zoom
          })
  end

  # Markdown helper
  # Always use all extensions. Additional parser and render options may be
  # passed as a second argument.
  #
  # @param markdown (String) a string to parse as Markdown source
  # @param options (String or Array) an optional parser/renderer option
  # @return String HTML-formatted from Mardkown source
  def m(markdown, options = nil)
    tag.div(
      CommonMarker.render_doc(
        markdown.to_s,
        options.to_a + default_commonmarker_options,
        default_commonmarker_extensions
      ).to_html.html_safe, class: 'markdown')
  end

  private

  # CommonMarker extensions
  # See https://github.com/gjtorikian/commonmarker#extensions
  def default_commonmarker_extensions
    [
      :table,
      :tasklist,
      :strikethrough,
      :autolink,
      :tagfilter
    ]
  end

  # CommonMarker options
  # See https://github.com/gjtorikian/commonmarker#options
  def default_commonmarker_options
    [
      #:HARDBREAKS, # only seems to work with render_html, but then all others
                    # only work with render_doc
      :FOOTNOTES,
      :SMART,
      :STRIKETHROUGH_DOUBLE_TILDE,
      :VALIDATE_UTF8
    ]
  end
end