Class: SimpleNavigation::Adapters::Rails

Inherits:
Base
  • Object
show all
Defined in:
lib/simple_navigation/adapters/rails.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#context, #request

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Rails

Returns a new instance of Rails.



29
30
31
32
33
# File 'lib/simple_navigation/adapters/rails.rb', line 29

def initialize(context)
  @controller = extract_controller_from context
  @template = template_from @controller
  @request = @template.request if @template
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



4
5
6
# File 'lib/simple_navigation/adapters/rails.rb', line 4

def controller
  @controller
end

#templateObject (readonly)

Returns the value of attribute template.



4
5
6
# File 'lib/simple_navigation/adapters/rails.rb', line 4

def template
  @template
end

Class Method Details

.registerObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/simple_navigation/adapters/rails.rb', line 6

def self.register
  SimpleNavigation.set_env(::Rails.root, ::Rails.env)

  # Autoloading in initializers is deprecated on rails 6.0
  # This delays the hook initialization using the on_load
  # hooks, but does not change behaviour for existing
  # rails versions.
  if ::Rails::VERSION::MAJOR >= 6
    ActiveSupport.on_load(:action_controller_base) do
      SimpleNavigation::Adapters::Rails.register_controller_helpers
    end
  else
    register_controller_helpers
  end
end

.register_controller_helpersObject



22
23
24
25
26
27
# File 'lib/simple_navigation/adapters/rails.rb', line 22

def self.register_controller_helpers
  ActionController::Base.send(:include, SimpleNavigation::Helpers)
  SimpleNavigation::Helpers.instance_methods.each do |m|
    ActionController::Base.send(:helper_method, m.to_sym)
  end
end

Instance Method Details

#content_tag(type, content, options = {}) ⇒ Object



63
64
65
# File 'lib/simple_navigation/adapters/rails.rb', line 63

def (type, content, options = {})
  template && template.(type, html_safe(content), options)
end

#context_for_evalObject



49
50
51
52
53
# File 'lib/simple_navigation/adapters/rails.rb', line 49

def context_for_eval
  template   ||
  controller ||
  fail('no context set for evaluation the config file')
end

#current_page?(url) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/simple_navigation/adapters/rails.rb', line 55

def current_page?(url)
  template && template.current_page?(url)
end


59
60
61
# File 'lib/simple_navigation/adapters/rails.rb', line 59

def link_to(name, url, options = {})
  template && template.link_to(link_title(name), url, options)
end

#request_pathObject



45
46
47
# File 'lib/simple_navigation/adapters/rails.rb', line 45

def request_path
  request ? request.path : ''
end

#request_uriObject



35
36
37
38
39
40
41
42
43
# File 'lib/simple_navigation/adapters/rails.rb', line 35

def request_uri
  return '' unless request

  if request.respond_to?(:fullpath)
    request.fullpath
  else
    request.request_uri
  end
end