Module: Megatron::ApplicationHelper

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

Instance Method Summary collapse

Instance Method Details

#add_class(string, *classes) ⇒ Object



67
68
69
70
71
# File 'app/helpers/megatron/application_helper.rb', line 67

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

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



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
# File 'app/helpers/megatron/application_helper.rb', line 39

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

#icon(name, options = {}) ⇒ Object



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

def icon(name, options={})
  options[:class] ||= ''
  (:span, class: "#{name}_icon #{options[:class]}", 'aria-hidden' => true) {  }
end


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

def link_up(name = nil, options = nil, html_options = nil, &block)
  # append a class to html_options[:class] if the href thing works
  options ||= {}
  here_if = options.delete(:here_if) || {}
  here_if[:path] ||= name
  options[:class] = add_class(options[:class], "here") if test_current_page(here_if)

  link_to(name, options, html_options, &block)
end

#megatron_asset_path(asset) ⇒ Object



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

def megatron_asset_path(asset)
  return "#{ENV['MEGATRON_ASSET_HOST']}/assets/megatron/#{asset}" if ENV['MEGATRON_ASSET_HOST']
  return "https://d11f55tj5eo9e5.cloudfront.net/assets/megatron/#{asset}" if Rails.env.production?
  return "/assets/megatron/#{asset}"
end

#megatron_assets_tagsObject



19
20
21
22
23
24
# File 'app/helpers/megatron/application_helper.rb', line 19

def megatron_assets_tags
  version = Megatron::VERSION
  favicon_link_tag(megatron_asset_path('favicon.ico')) + 
  stylesheet_link_tag(megatron_asset_path("megatron-#{version}.css")) +
  javascript_include_tag(megatron_asset_path("megatron-#{version}.js"))
end

#options_from_args(args) ⇒ Object



31
32
33
34
35
36
37
# File 'app/helpers/megatron/application_helper.rb', line 31

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

#parse_url(path) ⇒ Object



88
89
90
# File 'app/helpers/megatron/application_helper.rb', line 88

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

#test_current_page(criteria) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/helpers/megatron/application_helper.rb', line 73

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



92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/helpers/megatron/application_helper.rb', line 92

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