Module: Megatron::ApplicationHelper

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

Instance Method Summary collapse

Instance Method Details

#add_class(string, *classes) ⇒ Object



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

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

#dasherize(input) ⇒ Object



57
58
59
# File 'app/helpers/megatron/application_helper.rb', line 57

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

#get_root_urlObject



61
62
63
# File 'app/helpers/megatron/application_helper.rb', line 61

def get_root_url
  ENV["ROOT_URL"] || '/'
end

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/helpers/megatron/application_helper.rb', line 65

def inject_svg(filename, options = {})
  group = 0
  file = File.read(Megatron::Engine.root.join('app', 'assets', 'images', 'megatron', 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


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

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

#options_from_args(args) ⇒ Object



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

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

#parse_url(path) ⇒ Object



40
41
42
# File 'app/helpers/megatron/application_helper.rb', line 40

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

#test_current_page(criteria) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/helpers/megatron/application_helper.rb', line 25

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



44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/helpers/megatron/application_helper.rb', line 44

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