Module: Megatron::ApplicationHelper
- Defined in:
- app/helpers/megatron/application_helper.rb
Instance Method Summary collapse
- #add_class(string, *classes) ⇒ Object
- #dasherize(input) ⇒ Object
- #get_root_url ⇒ Object
- #link_up(href = nil, options = {}, html_options = nil, &block) ⇒ Object
- #options_from_args(args) ⇒ Object
- #parse_url(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
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_url ⇒ Object
61 62 63 |
# File 'app/helpers/megatron/application_helper.rb', line 61 def get_root_url ENV["ROOT_URL"] || '/' end |
#link_up(href = nil, options = {}, html_options = nil, &block) ⇒ Object
3 4 5 6 7 8 9 |
# File 'app/helpers/megatron/application_helper.rb', line 3 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 |
#options_from_args(args) ⇒ Object
11 12 13 14 15 16 17 |
# File 'app/helpers/megatron/application_helper.rb', line 11 def (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 |