Module: Megatron::ApplicationHelper
- Defined in:
- app/helpers/megatron/application_helper.rb
Instance Method Summary collapse
- #add_class(string, *classes) ⇒ Object
- #embed_svg(filename, options = {}) ⇒ Object
- #link_up(href = nil, options = {}, html_options = nil, &block) ⇒ Object
- #megatron_asset_path(asset) ⇒ Object
- #megatron_assets_tags ⇒ Object
- #megatron_error_asset_tag ⇒ Object
- #options_from_args(args) ⇒ Object
- #parse_url(path) ⇒ Object
- #pin_tab_icon(path) ⇒ Object
- #test_current_page(criteria) ⇒ Object
- #test_here_key_value(key, value, check_params = params) ⇒ Object
Instance Method Details
#add_class(string, *classes) ⇒ Object
97 98 99 100 101 |
# File 'app/helpers/megatron/application_helper.rb', line 97 def add_class(string, *classes) string ||= '' string << " #{classes.join(' ')}" string end |
#embed_svg(filename, options = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'app/helpers/megatron/application_helper.rb', line 69 def (filename, = {}) 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 |
#link_up(href = nil, options = {}, html_options = nil, &block) ⇒ Object
10 11 12 13 14 15 16 |
# File 'app/helpers/megatron/application_helper.rb', line 10 def link_up(href = nil, = {}, = nil, &block) here_if = .delete(:here_if) || {} here_if[:path] = href if here_if.blank? [:class] = add_class([:class], "here") if test_current_page(here_if) link_to(href, , &block) end |
#megatron_asset_path(asset) ⇒ Object
3 4 5 6 7 8 |
# File 'app/helpers/megatron/application_helper.rb', line 3 def megatron_asset_path(asset) 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_tags ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'app/helpers/megatron/application_helper.rb', line 18 def if ENV['MEGATRONDEV'] version = '' else version = "-#{Megatron::VERSION}" end 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_tag ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/helpers/megatron/application_helper.rb', line 45 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
61 62 63 64 65 66 67 |
# File 'app/helpers/megatron/application_helper.rb', line 61 def (args) if args.last.is_a? Hash args.pop else {} end end |
#parse_url(path) ⇒ Object
118 119 120 |
# File 'app/helpers/megatron/application_helper.rb', line 118 def parse_url(path) URI.parse(path.gsub(/(\?.+)/, '')).path end |
#pin_tab_icon(path) ⇒ Object
41 42 43 |
# File 'app/helpers/megatron/application_helper.rb', line 41 def pin_tab_icon(path) %Q{<link rel="mask-icon" mask href="#{path}" color="black">}.html_safe end |
#test_current_page(criteria) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'app/helpers/megatron/application_helper.rb', line 103 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
122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/helpers/megatron/application_helper.rb', line 122 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 |