Module: Megatron::ApplicationHelper

Defined in:
app/helpers/megatron/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_class(string, *classes) ⇒ Object



114
115
116
117
118
# File 'app/helpers/megatron/application_helper.rb', line 114

def add_class(string, *classes)
  string ||= ''
  string << " #{classes.join(' ')}"
  string
end

#dasherize(input) ⇒ Object



152
153
154
# File 'app/helpers/megatron/application_helper.rb', line 152

def dasherize(input)
  input.gsub(/[\W,_]/, '-').gsub(/-{2,}/, '-')
end

#dev?Boolean

Mounted from dev-kit

Returns:

  • (Boolean)


5
6
7
# File 'app/helpers/megatron/application_helper.rb', line 5

def dev?
  __dir__.start_with?("/megatron") || ENV['DEVKIT']
end

#embed_svg(filename, options = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/helpers/megatron/application_helper.rb', line 86

def embed_svg(filename, options = {})
  group = 0
  file = File.read(Engine.root.join('app', 'assets', 'images', filename))
    .gsub(/<!--.+-->/, '')
    .gsub(/^\t{2,}\s*<\/?g>/, '')
    .gsub(/width=".+?"/, 'width="312"')
    .gsub(/\sheight.+$/, '')
    .gsub(/\t/, '  ')
    .gsub(/\n{3,}/m, "\n")
    .gsub(/^\s{2}<g>.+?^\s{2}<\/g>/m) { |g| 
      g.gsub(/^\s{4,}\s+/, '    ')
    }
    .gsub(/^<g>.+?^<\/g>/m) { |g|
      group += 1
      count = 0
      g.gsub(/^\s{2}<g>/) { |g| 
        count += 1
        %Q{  <g id="group-#{group}-cube-#{count}">}
      }
    }
  # doc = Nokogiri::HTML::DocumentFragment.parse file
  # svg = doc.at_css 'svg'
  # if options[:class].present?
  #   svg['class'] = options[:class]
  # end
  file.html_safe
end


54
55
56
57
58
59
60
# File 'app/helpers/megatron/application_helper.rb', line 54

def favicon_link_tag(source='favicon.ico', options={})
  tag('link', {
    :rel  => 'shortcut icon',
    :type => 'image/x-icon',
    :href => source
  }.merge!(options.symbolize_keys))
end


23
24
25
26
27
28
29
# File 'app/helpers/megatron/application_helper.rb', line 23

def link_up(href = nil, options = {}, html_options = nil, &block)
  here_if = options.delete(:here_if) || {}
  here_if[:path] = href if here_if.blank?
  options[:class] = add_class(options[:class], "here") if test_current_page(here_if)

  link_to(href, options, &block)
end

#megatron_asset_path(asset) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/helpers/megatron/application_helper.rb', line 9

def megatron_asset_path(asset)

  if ENV['LOCAL_DEV']
    alt_host = "http://localhost:5000"
  elsif dev?
    alt_host = "https://megatron.compose.devkit"
  end 

  alt_host ||= ENV['MEGATRON_ASSET_HOST'] 
  return "#{alt_host}/assets/megatron/#{asset}" if alt_host
  return "https://d11f55tj5eo9e5.cloudfront.net/assets/megatron/#{asset}" if Rails.env.production?
  return "/assets/megatron/#{asset}"
end

#megatron_assets_tagsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/helpers/megatron/application_helper.rb', line 31

def megatron_assets_tags
  version = "-#{Megatron::VERSION}"

  ext_suffix = Rails.env.production? ? '.gz' : ''

  pin_tab_icon(
    megatron_asset_path('logo.svg')
  ) +
  favicon_link_tag(
    megatron_asset_path('favicon.ico'), sizes: "32x32"
  ) +
  stylesheet_link_tag(
    megatron_asset_path("megatron#{version}.css#{ext_suffix}")
  ) +
  javascript_include_tag(
    megatron_asset_path("megatron#{version}.js#{ext_suffix}")
  )
end

#megatron_error_asset_tagObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/helpers/megatron/application_helper.rb', line 62

def megatron_error_asset_tag
  version = Megatron::VERSION
  ext_suffix = Rails.env.production? ? '.gz' : ''

  # Embed styles directly for these error codes since they are served from haproxy
  # and are likely to be served when the stylesheet server cannot be reached
  #
  if [408, 502, 503, 504].include?(@status_code)
    style== File.read("../public/assets/megatron/megatron-error-pages-#{version}.css")
  else
    stylesheet_link_tag(
      megatron_asset_path("megatron-error-pages-#{version}.css#{ext_suffix}")
    )
  end
end

#options_from_args(args) ⇒ Object



78
79
80
81
82
83
84
# File 'app/helpers/megatron/application_helper.rb', line 78

def options_from_args(args)
  if args.last.is_a? Hash
    args.pop
  else
    {}
  end
end

#parse_url(path) ⇒ Object



135
136
137
# File 'app/helpers/megatron/application_helper.rb', line 135

def parse_url(path)
  URI.parse(path.gsub(/(\?.+)/, '')).path
end

#pin_tab_icon(path) ⇒ Object



50
51
52
# File 'app/helpers/megatron/application_helper.rb', line 50

def pin_tab_icon(path)
  %Q{<link rel="mask-icon" mask href="#{path}" color="black">}.html_safe
end

#test_current_page(criteria) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/helpers/megatron/application_helper.rb', line 120

def test_current_page(criteria)
  return false unless criteria.present?
  
  test_params = criteria.delete(:params) || {}
  [:controller, :action, :path].each do |k|
    test_params[k] ||= criteria[k] if criteria[k].present?
  end

  fullpath = parse_url(request.fullpath)
  check_params = params.to_unsafe_hash.symbolize_keys.merge(path: fullpath)

  test_params.all? {|k, v| test_here_key_value(k, v, check_params) }

end

#test_here_key_value(key, value, check_params = params) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/helpers/megatron/application_helper.rb', line 139

def test_here_key_value(key, value, check_params = params)
  if value.is_a?(Hash)
    value.all? {|k,v| params[k].present? && test_here_key_value(k, v, params[k]) }
  elsif value.is_a?(Array)
    value.detect {|v| test_here_key_value(key, v) }.present?
  elsif value.is_a?(Regexp)
    (check_params[key] =~ value) != nil
  else
    value_to_check = key == :path ? parse_url(value) : value
    check_params[key] == value_to_check
  end
end