simple-navigation 3.14.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.rspec +1 -0
  4. data/.travis.yml +10 -2
  5. data/CHANGELOG.md +400 -0
  6. data/Guardfile +4 -2
  7. data/README.md +1 -1
  8. data/gemfiles/.bundle/config +2 -0
  9. data/gemfiles/rails-3-2-stable.gemfile +10 -0
  10. data/gemfiles/rails-4-1-stable.gemfile +6 -0
  11. data/gemfiles/rails-4-2-stable.gemfile +7 -0
  12. data/generators/navigation_config/navigation_config_generator.rb +0 -1
  13. data/generators/navigation_config/templates/config/navigation.rb +18 -15
  14. data/lib/simple_navigation.rb +25 -42
  15. data/lib/simple_navigation/adapters/rails.rb +1 -24
  16. data/lib/simple_navigation/adapters/sinatra.rb +2 -11
  17. data/lib/simple_navigation/config_file.rb +36 -0
  18. data/lib/simple_navigation/config_file_finder.rb +42 -0
  19. data/lib/simple_navigation/{core/configuration.rb → configuration.rb} +7 -1
  20. data/lib/simple_navigation/{rendering/helpers.rb → helpers.rb} +0 -4
  21. data/lib/simple_navigation/{core/item.rb → item.rb} +76 -85
  22. data/lib/simple_navigation/{core/item_adapter.rb → item_adapter.rb} +3 -17
  23. data/lib/simple_navigation/{core/item_container.rb → item_container.rb} +23 -14
  24. data/lib/simple_navigation/{core/items_provider.rb → items_provider.rb} +0 -0
  25. data/lib/simple_navigation/railtie.rb +7 -0
  26. data/lib/simple_navigation/renderer.rb +12 -0
  27. data/lib/simple_navigation/{rendering/renderer → renderer}/base.rb +1 -1
  28. data/lib/simple_navigation/{rendering/renderer → renderer}/breadcrumbs.rb +0 -0
  29. data/lib/simple_navigation/{rendering/renderer → renderer}/json.rb +2 -0
  30. data/lib/simple_navigation/{rendering/renderer → renderer}/links.rb +0 -0
  31. data/lib/simple_navigation/{rendering/renderer → renderer}/list.rb +0 -0
  32. data/lib/simple_navigation/{rendering/renderer → renderer}/text.rb +0 -0
  33. data/lib/simple_navigation/version.rb +1 -1
  34. data/simple-navigation.gemspec +5 -4
  35. data/spec/fake_app/config/navigation.rb +6 -0
  36. data/spec/fake_app/rails_app.rb +35 -0
  37. data/spec/initializers/coveralls.rb +3 -0
  38. data/spec/initializers/have_css_matcher.rb +8 -3
  39. data/spec/initializers/memfs.rb +7 -0
  40. data/spec/initializers/rails.rb +4 -0
  41. data/spec/initializers/rspec.rb +7 -0
  42. data/spec/integration/rendering_navigation_spec.rb +12 -0
  43. data/spec/{lib/simple_navigation → simple_navigation}/adapters/padrino_spec.rb +0 -2
  44. data/spec/{lib/simple_navigation → simple_navigation}/adapters/rails_spec.rb +36 -93
  45. data/spec/{lib/simple_navigation → simple_navigation}/adapters/sinatra_spec.rb +4 -6
  46. data/spec/simple_navigation/config_file_finder_spec.rb +50 -0
  47. data/spec/simple_navigation/config_file_spec.rb +25 -0
  48. data/spec/{lib/simple_navigation/core → simple_navigation}/configuration_spec.rb +29 -19
  49. data/spec/{lib/simple_navigation/rendering → simple_navigation}/helpers_spec.rb +10 -13
  50. data/spec/{lib/simple_navigation/core → simple_navigation}/item_adapter_spec.rb +14 -11
  51. data/spec/{lib/simple_navigation/core → simple_navigation}/item_container_spec.rb +130 -42
  52. data/spec/simple_navigation/item_spec.rb +467 -0
  53. data/spec/{lib/simple_navigation/core → simple_navigation}/items_provider_spec.rb +1 -3
  54. data/spec/{lib/simple_navigation/rendering → simple_navigation}/renderer/base_spec.rb +34 -36
  55. data/spec/{lib/simple_navigation/rendering → simple_navigation}/renderer/breadcrumbs_spec.rb +4 -7
  56. data/spec/{lib/simple_navigation/rendering → simple_navigation}/renderer/json_spec.rb +5 -11
  57. data/spec/{lib/simple_navigation/rendering → simple_navigation}/renderer/links_spec.rb +5 -8
  58. data/spec/{lib/simple_navigation/rendering → simple_navigation}/renderer/list_spec.rb +4 -7
  59. data/spec/{lib/simple_navigation/rendering → simple_navigation}/renderer/text_spec.rb +4 -7
  60. data/spec/simple_navigation_spec.rb +190 -0
  61. data/spec/spec_helper.rb +29 -35
  62. metadata +103 -68
  63. data/CHANGELOG +0 -292
  64. data/lib/simple_navigation/core.rb +0 -5
  65. data/lib/simple_navigation/rails_controller_methods.rb +0 -164
  66. data/lib/simple_navigation/rendering.rb +0 -12
  67. data/rails/init.rb +0 -1
  68. data/spec/lib/simple_navigation/core/item_spec.rb +0 -703
  69. data/spec/lib/simple_navigation/rails_controller_methods_spec.rb +0 -270
  70. data/spec/lib/simple_navigation_spec.rb +0 -300
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  [![Code Climate](https://codeclimate.com/github/codeplant/simple-navigation.png)](https://codeclimate.com/github/codeplant/simple-navigation)
6
6
  [![Coverage Status](https://coveralls.io/repos/codeplant/simple-navigation/badge.png)](https://coveralls.io/r/codeplant/simple-navigation)
7
7
 
8
- Simple Navigation is a ruby library for creating navigations (with multiple levels) for your Rails 2, Rails 3, Rails 4, Sinatra or Padrino applications. It runs with all ruby versions (including ruby 2.0).
8
+ Simple Navigation is a ruby library for creating navigations (with multiple levels) for your Rails 2, Rails 3, Rails 4, Sinatra or Padrino applications. It runs with all ruby versions (including ruby 2.x).
9
9
 
10
10
  ## Documentation
11
11
 
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_JOBS: 4
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'railties', '~> 3.2.0'
4
+ gem 'rspec-rails', '~> 3.2.1'
5
+
6
+ if RUBY_VERSION >= '2.2.0'
7
+ gem 'test-unit', '~> 3.0'
8
+ end
9
+
10
+ gemspec path: '../'
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'railties', '~> 4.1.0'
4
+ gem 'rspec-rails', '~> 3.2.1'
5
+
6
+ gemspec path: '../'
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'launchy'
4
+ gem 'railties', '~> 4.2.0'
5
+ gem 'rspec-rails', '~> 3.2.1'
6
+
7
+ gemspec path: '../'
@@ -2,7 +2,6 @@ class NavigationConfigGenerator < Rails::Generator::Base
2
2
  def manifest
3
3
  record do |m|
4
4
  m.file 'config/navigation.rb', 'config/navigation.rb'
5
- m.readme '../../../README.md'
6
5
  end
7
6
  end
8
7
  end
@@ -4,35 +4,38 @@ SimpleNavigation::Configuration.run do |navigation|
4
4
  # Specify a custom renderer if needed.
5
5
  # The default renderer is SimpleNavigation::Renderer::List which renders HTML lists.
6
6
  # The renderer can also be specified as option in the render_navigation call.
7
- # navigation.renderer = Your::Custom::Renderer
7
+ #navigation.renderer = Your::Custom::Renderer
8
8
 
9
- # Specify the class that will be applied to active navigation items.
10
- # Defaults to 'selected' navigation.selected_class = 'your_selected_class'
9
+ # Specify the class that will be applied to active navigation items. Defaults to 'selected'
10
+ #navigation.selected_class = 'selected'
11
11
 
12
12
  # Specify the class that will be applied to the current leaf of
13
13
  # active navigation items. Defaults to 'simple-navigation-active-leaf'
14
- # navigation.active_leaf_class = 'your_active_leaf_class'
14
+ #navigation.active_leaf_class = 'simple-navigation-active-leaf'
15
15
 
16
- # Item keys are normally added to list items as id.
17
- # This setting turns that off
18
- # navigation.autogenerate_item_ids = false
16
+ # Specify if item keys are added to navigation items as id. Defaults to true
17
+ #navigation.autogenerate_item_ids = true
19
18
 
20
19
  # You can override the default logic that is used to autogenerate the item ids.
21
20
  # To do this, define a Proc which takes the key of the current item as argument.
22
21
  # The example below would add a prefix to each key.
23
- # navigation.id_generator = Proc.new {|key| "my-prefix-#{key}"}
22
+ #navigation.id_generator = Proc.new {|key| "my-prefix-#{key}"}
24
23
 
25
24
  # If you need to add custom html around item names, you can define a proc that
26
25
  # will be called with the name you pass in to the navigation.
27
26
  # The example below shows how to wrap items spans.
28
- # navigation.name_generator = Proc.new {|name, item| "<span>#{name}</span>"}
27
+ #navigation.name_generator = Proc.new {|name, item| "<span>#{name}</span>"}
29
28
 
30
- # The auto highlight feature is turned on by default.
31
- # This turns it off globally (for the whole plugin)
32
- # navigation.auto_highlight = false
29
+ # Specify if the auto highlight feature is turned on (globally, for the whole navigation). Defaults to true
30
+ #navigation.auto_highlight = true
31
+
32
+ # Specifies whether auto highlight should ignore query params and/or anchors when
33
+ # comparing the navigation items with the current URL. Defaults to true
34
+ #navigation.ignore_query_params_on_auto_highlight = true
35
+ #navigation.ignore_anchors_on_auto_highlight = true
33
36
 
34
37
  # If this option is set to true, all item names will be considered as safe (passed through html_safe). Defaults to false.
35
- # navigation.consider_item_names_as_safe = false
38
+ #navigation.consider_item_names_as_safe = false
36
39
 
37
40
  # Define the primary navigation
38
41
  navigation.items do |primary|
@@ -69,9 +72,9 @@ SimpleNavigation::Configuration.run do |navigation|
69
72
 
70
73
  # you can also specify html attributes to attach to this particular level
71
74
  # works for all levels of the menu
72
- # primary.dom_attributes = {id: 'menu-id', class: 'menu-class'}
75
+ #primary.dom_attributes = {id: 'menu-id', class: 'menu-class'}
73
76
 
74
77
  # You can turn off auto highlighting for a specific level
75
- # primary.auto_highlight = false
78
+ #primary.auto_highlight = false
76
79
  end
77
80
  end
@@ -4,9 +4,15 @@ require 'active_support/core_ext/hash'
4
4
  require 'active_support/core_ext/module/attribute_accessors'
5
5
 
6
6
  require 'simple_navigation/version'
7
- require 'simple_navigation/core'
8
- require 'simple_navigation/rendering'
7
+ require 'simple_navigation/configuration'
8
+ require 'simple_navigation/item_adapter'
9
+ require 'simple_navigation/item'
10
+ require 'simple_navigation/item_container'
11
+ require 'simple_navigation/items_provider'
12
+ require 'simple_navigation/renderer'
9
13
  require 'simple_navigation/adapters'
14
+ require 'simple_navigation/config_file_finder'
15
+ require 'simple_navigation/railtie' if defined?(::Rails)
10
16
 
11
17
  require 'forwardable'
12
18
 
@@ -55,7 +61,7 @@ module SimpleNavigation
55
61
  def set_env(root, environment)
56
62
  self.root = root
57
63
  self.environment = environment
58
- config_file_paths << SimpleNavigation.default_config_file_path
64
+ config_file_paths << default_config_file_path
59
65
  end
60
66
 
61
67
  # Returns the current framework in which the plugin is running.
@@ -89,30 +95,6 @@ module SimpleNavigation
89
95
  File.join(root, 'config')
90
96
  end
91
97
 
92
- # Returns true if the config_file for specified context does exist.
93
- def config_file?(navigation_context = :default)
94
- !!config_file(navigation_context)
95
- end
96
-
97
- # Returns the path to the config file for the given navigation context or
98
- # nil if no matching config file can be found.
99
- # If multiple config_paths are set, it returns the first matching path.
100
- def config_file(navigation_context = :default)
101
- config_file_paths
102
- .map { |path| File.join(path, config_file_name(navigation_context)) }
103
- .find { |full_path| File.exist?(full_path) }
104
- end
105
-
106
- # Returns the name of the config file for the given navigation_context
107
- def config_file_name(navigation_context = :default)
108
- prefix = if navigation_context == :default
109
- ''
110
- else
111
- "#{navigation_context.to_s.underscore}_"
112
- end
113
- "#{prefix}navigation.rb"
114
- end
115
-
116
98
  # Resets the list of config_file_paths to the specified path
117
99
  def config_file_path=(path)
118
100
  self.config_file_paths = [path]
@@ -121,20 +103,10 @@ module SimpleNavigation
121
103
  # Reads the config_file for the specified navigation_context and stores it
122
104
  # for later evaluation.
123
105
  def load_config(navigation_context = :default)
124
- unless config_file?(navigation_context)
125
- fail "Config file '#{config_file_name(navigation_context)}' not " \
126
- "found in path(s) #{config_file_paths.join(', ')}!"
127
- end
128
-
129
- # FIXME: what about update_config and update_config! methods ?
130
106
  if environment == 'production'
131
- config_files[navigation_context] ||= begin
132
- IO.read(config_file(navigation_context))
133
- end
107
+ update_config(navigation_context)
134
108
  else
135
- config_files[navigation_context] = begin
136
- IO.read(config_file(navigation_context))
137
- end
109
+ update_config!(navigation_context)
138
110
  end
139
111
  end
140
112
 
@@ -186,9 +158,20 @@ module SimpleNavigation
186
158
 
187
159
  private
188
160
 
189
- def apply_defaults(options)
190
- options[:level] = options.delete(:levels) if options[:levels]
191
- { context: :default, level: :all }.merge(options)
161
+ def config_file(navigation_context)
162
+ ConfigFileFinder.new(config_file_paths).find(navigation_context)
163
+ end
164
+
165
+ def read_config(navigation_context)
166
+ File.read config_file(navigation_context)
167
+ end
168
+
169
+ def update_config(navigation_context)
170
+ config_files[navigation_context] ||= read_config(navigation_context)
171
+ end
172
+
173
+ def update_config!(navigation_context)
174
+ config_files[navigation_context] = read_config(navigation_context)
192
175
  end
193
176
  end
194
177
  end
@@ -4,7 +4,7 @@ module SimpleNavigation
4
4
  attr_reader :controller, :template
5
5
 
6
6
  def self.register
7
- SimpleNavigation.set_env(rails_root, rails_env)
7
+ SimpleNavigation.set_env(::Rails.root, ::Rails.env)
8
8
  ActionController::Base.send(:include, SimpleNavigation::Helpers)
9
9
  SimpleNavigation::Helpers.instance_methods.each do |m|
10
10
  ActionController::Base.send(:helper_method, m.to_sym)
@@ -51,18 +51,6 @@ module SimpleNavigation
51
51
 
52
52
  protected
53
53
 
54
- def self.rails_root
55
- gte_rails3? ? ::Rails.root : ::RAILS_ROOT
56
- end
57
-
58
- def self.rails_env
59
- gte_rails3? ? ::Rails.env : ::RAILS_ENV
60
- end
61
-
62
- def self.gte_rails3?
63
- ::Rails::VERSION::MAJOR >= 3
64
- end
65
-
66
54
  def template_from(controller)
67
55
  if controller.respond_to?(:view_context)
68
56
  controller.view_context
@@ -97,14 +85,3 @@ module SimpleNavigation
97
85
  end
98
86
  end
99
87
  end
100
-
101
- # Initializer for Rails3
102
- if defined?(Rails) && SimpleNavigation::Adapters::Rails.gte_rails3?
103
- module SimpleNavigation
104
- class Railtie < Rails::Railtie
105
- initializer 'simple_navigation.register' do |app|
106
- SimpleNavigation.register
107
- end
108
- end
109
- end
110
- end
@@ -3,9 +3,8 @@ require 'cgi'
3
3
  module SimpleNavigation
4
4
  module Adapters
5
5
  class Sinatra < Base
6
- def self.register
7
- SimpleNavigation.set_env(sinatra_root, sinatra_environment)
8
- ::Sinatra::Application.send(:helpers, SimpleNavigation::Helpers)
6
+ def self.register(app)
7
+ SimpleNavigation.set_env(app.root, app.environment)
9
8
  end
10
9
 
11
10
  def initialize(context)
@@ -50,14 +49,6 @@ module SimpleNavigation
50
49
 
51
50
  protected
52
51
 
53
- def self.sinatra_root
54
- ::Sinatra::Application.root
55
- end
56
-
57
- def self.sinatra_environment
58
- ::Sinatra::Application.environment
59
- end
60
-
61
52
  def to_attributes(options)
62
53
  options.map { |k, v| v.nil? ? '' : " #{k}='#{v}'" }.join
63
54
  end
@@ -0,0 +1,36 @@
1
+ require 'active_support/core_ext/string'
2
+
3
+ module SimpleNavigation
4
+ # Internal: Encapsulates the config file naming knowledge.
5
+ class ConfigFile
6
+ # Internal: Initializes a ConfigFile.
7
+ #
8
+ # context - The navigation context for this ConfigFile.
9
+ def initialize(context)
10
+ @prefix = prefix_for_context(context)
11
+ end
12
+
13
+ # Internal: Returns the name of the configuration file on disk.
14
+ #
15
+ # Based on the the initialization context the outcome may differ.
16
+ #
17
+ # Examples
18
+ #
19
+ # ConfigFile.new.name # => "navigation.rb"
20
+ # ConfigFile.new(:default).name # => "navigation.rb"
21
+ # ConfigFile.new(:other).name # => "other_navigation.rb"
22
+ #
23
+ # Returns a String representing the name of the configuration file on disk.
24
+ def name
25
+ @name ||= "#{prefix}navigation.rb"
26
+ end
27
+
28
+ private
29
+
30
+ attr_reader :prefix
31
+
32
+ def prefix_for_context(context)
33
+ context == :default ? '' : "#{context.to_s.underscore}_"
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,42 @@
1
+ require 'simple_navigation/config_file'
2
+
3
+ module SimpleNavigation
4
+ # Internal: Encapsulates the configuration file finding logic.
5
+ class ConfigFileFinder
6
+ # Internal: Initializes a ConfigFileFinder.
7
+ #
8
+ # paths - an enumerable list of paths in which to look for configuration
9
+ # files.
10
+ def initialize(paths)
11
+ @paths = paths
12
+ end
13
+
14
+ # Internal: Searches a configuration file for the given context in the
15
+ # initialization paths.
16
+ #
17
+ # context - The navigation context for which to look the configuration file.
18
+ #
19
+ # Returns a String representing the full path of the configuation file.
20
+ # Raises StandardError if no file is found.
21
+ def find(context)
22
+ config_file_name = config_file_name_for_context(context)
23
+
24
+ find_config_file(config_file_name) ||
25
+ fail("Config file '#{config_file_name}' not found in " \
26
+ "path(s) #{paths.join(', ')}!")
27
+ end
28
+
29
+ private
30
+
31
+ attr_reader :paths
32
+
33
+ def config_file_name_for_context(context)
34
+ ConfigFile.new(context).name
35
+ end
36
+
37
+ def find_config_file(config_file_name)
38
+ paths.map { |path| File.join(path, config_file_name) }
39
+ .find { |full_path| File.exist?(full_path) }
40
+ end
41
+ end
42
+ end
@@ -7,7 +7,10 @@ module SimpleNavigation
7
7
 
8
8
  attr_accessor :autogenerate_item_ids,
9
9
  :auto_highlight,
10
- :consider_item_names_as_safe
10
+ :consider_item_names_as_safe,
11
+ :highlight_on_subpath,
12
+ :ignore_query_params_on_auto_highlight,
13
+ :ignore_anchors_on_auto_highlight
11
14
 
12
15
  attr_reader :primary_navigation
13
16
 
@@ -33,6 +36,9 @@ module SimpleNavigation
33
36
  @autogenerate_item_ids = true
34
37
  @auto_highlight = true
35
38
  @consider_item_names_as_safe = false
39
+ @highlight_on_subpath = false
40
+ @ignore_anchors_on_auto_highlight = true
41
+ @ignore_query_params_on_auto_highlight = true
36
42
  end
37
43
 
38
44
  def active_leaf_class
@@ -34,10 +34,6 @@ module SimpleNavigation
34
34
  SimpleNavigation.config.items(options[:items], &block)
35
35
  end
36
36
 
37
- if SimpleNavigation.respond_to?(:handle_explicit_navigation)
38
- SimpleNavigation.handle_explicit_navigation
39
- end
40
-
41
37
  unless SimpleNavigation.primary_navigation
42
38
  fail 'no primary navigation defined, either use a navigation config ' \
43
39
  'file or pass items directly to render_navigation'
@@ -2,27 +2,23 @@ module SimpleNavigation
2
2
  # Represents an item in your navigation.
3
3
  # Gets generated by the item method in the config-file.
4
4
  class Item
5
- attr_reader :highlights_on,
6
- :key,
7
- :method,
5
+ attr_reader :key,
6
+ :name,
8
7
  :sub_navigation,
9
8
  :url
10
9
 
11
- attr_writer :html_options
12
-
13
10
  # see ItemContainer#item
14
11
  #
15
12
  # The subnavigation (if any) is either provided by a block or
16
13
  # passed in directly as <tt>items</tt>
17
- def initialize(container, key, name, url_or_options = {}, options_or_nil = {}, items = nil, &sub_nav_block)
18
- options = setup_url_and_options(url_or_options, options_or_nil)
19
-
20
- @key = key
21
- @method = options.delete(:method)
22
- @name = name
23
- @container = setup_container(container, options)
14
+ def initialize(container, key, name, url = nil, opts = {}, &sub_nav_block)
15
+ self.container = container
16
+ self.key = key
17
+ self.name = name.respond_to?(:call) ? name.call : name
18
+ self.url = url.respond_to?(:call) ? url.call : url
19
+ self.options = opts
24
20
 
25
- setup_sub_navigation(items, &sub_nav_block)
21
+ setup_sub_navigation(options[:items], &sub_nav_block)
26
22
  end
27
23
 
28
24
  # Returns the item's name.
@@ -32,8 +28,8 @@ module SimpleNavigation
32
28
  #
33
29
  def name(options = {})
34
30
  options = { apply_generator: true }.merge(options)
35
- if (options[:apply_generator])
36
- SimpleNavigation.config.name_generator.call(@name, self)
31
+ if options[:apply_generator]
32
+ config.name_generator.call(@name, self)
37
33
  else
38
34
  @name
39
35
  end
@@ -42,35 +38,32 @@ module SimpleNavigation
42
38
  # Returns true if this navigation item should be rendered as 'selected'.
43
39
  # An item is selected if
44
40
  #
45
- # * it has been explicitly selected in a controller or
46
41
  # * it has a subnavigation and one of its subnavigation items is selected or
47
42
  # * its url matches the url of the current request (auto highlighting)
48
43
  #
49
44
  def selected?
50
- @selected ||= selected_by_config? ||
51
- selected_by_subnav? ||
52
- selected_by_condition?
45
+ @selected ||= selected_by_subnav? || selected_by_condition?
53
46
  end
54
47
 
55
48
  # Returns the html-options hash for the item, i.e. the options specified
56
49
  # for this item in the config-file.
57
50
  # It also adds the 'selected' class to the list of classes if necessary.
58
51
  def html_options
59
- options = @html_options
60
- options[:id] ||= autogenerated_item_id if autogenerate_item_ids?
52
+ html_opts = options.fetch(:html) { Hash.new }
53
+ html_opts[:id] ||= autogenerated_item_id
61
54
 
62
- classes = [@html_options[:class], selected_class, active_leaf_class]
55
+ classes = [html_opts[:class], selected_class, active_leaf_class]
63
56
  classes = classes.flatten.compact.join(' ')
64
- options[:class] = classes unless classes.nil? || classes.empty?
57
+ html_opts[:class] = classes if classes && !classes.empty?
65
58
 
66
- options
59
+ html_opts
67
60
  end
68
61
 
69
62
  # Returns the configured active_leaf_class if the item is the selected leaf,
70
63
  # nil otherwise
71
64
  def active_leaf_class
72
65
  if !selected_by_subnav? && selected_by_condition?
73
- SimpleNavigation.config.active_leaf_class
66
+ config.active_leaf_class
74
67
  end
75
68
  end
76
69
 
@@ -78,10 +71,26 @@ module SimpleNavigation
78
71
  # nil otherwise
79
72
  def selected_class
80
73
  if selected?
81
- container.selected_class || SimpleNavigation.config.selected_class
74
+ container.selected_class || config.selected_class
82
75
  end
83
76
  end
84
77
 
78
+ # Returns the :highlights_on option as set at initialization
79
+ def highlights_on
80
+ @highlights_on ||= options[:highlights_on]
81
+ end
82
+
83
+ # Returns the :method option as set at initialization
84
+ def method
85
+ @method ||= options[:method]
86
+ end
87
+
88
+ # Returns the html attributes for the link as set with the :link_html option
89
+ # at initialization
90
+ def link_html_options
91
+ @link_html_options ||= options[:link_html]
92
+ end
93
+
85
94
  protected
86
95
 
87
96
  # Returns true if item has a subnavigation and
@@ -90,10 +99,6 @@ module SimpleNavigation
90
99
  sub_navigation && sub_navigation.selected?
91
100
  end
92
101
 
93
- def selected_by_config?
94
- false
95
- end
96
-
97
102
  # Returns true if the item's url matches the request's current url.
98
103
  def selected_by_condition?
99
104
  highlights_on ? selected_by_highlights_on? : selected_by_autohighlight?
@@ -104,86 +109,72 @@ module SimpleNavigation
104
109
  url == '/' && SimpleNavigation.request_path == '/'
105
110
  end
106
111
 
107
- # Returns true if the item's id should be added to the rendered output.
108
- def autogenerate_item_ids?
109
- SimpleNavigation.config.autogenerate_item_ids
110
- end
111
-
112
112
  # Returns the item's id which is added to the rendered output.
113
113
  def autogenerated_item_id
114
- SimpleNavigation.config.id_generator.call(key)
114
+ config.id_generator.call(key) if config.autogenerate_item_ids
115
115
  end
116
116
 
117
117
  # Return true if auto_highlight is on for this item.
118
118
  def auto_highlight?
119
- SimpleNavigation.config.auto_highlight && container.auto_highlight
120
- end
121
-
122
- def url_without_anchor
123
- url && url.split('#').first
119
+ config.auto_highlight && container.auto_highlight
124
120
  end
125
121
 
126
122
  private
127
123
 
128
- attr_reader :container
124
+ attr_accessor :container,
125
+ :options
129
126
 
130
- attr_writer :highlights_on,
127
+ attr_writer :key,
128
+ :name,
131
129
  :sub_navigation,
132
130
  :url
133
131
 
132
+ def config
133
+ SimpleNavigation.config
134
+ end
135
+
136
+ def request_uri
137
+ SimpleNavigation.request_uri
138
+ end
139
+
140
+ def remove_anchors(url_with_anchors)
141
+ url_with_anchors && url_with_anchors.split('#').first
142
+ end
143
+
144
+ def remove_query_params(url_with_params)
145
+ url_with_params && url_with_params.split('?').first
146
+ end
147
+
148
+ def url_for_autohighlight
149
+ relevant_url = remove_anchors(self.url) if config.ignore_anchors_on_auto_highlight
150
+ relevant_url = remove_query_params(relevant_url) if config.ignore_query_params_on_auto_highlight
151
+ relevant_url
152
+ end
153
+
134
154
  def selected_by_autohighlight?
135
- auto_highlight? &&
136
- (root_path_match? ||
137
- (url_without_anchor &&
138
- SimpleNavigation.current_page?(url_without_anchor)))
155
+ return false unless auto_highlight?
156
+ root_path_match? ||
157
+ SimpleNavigation.current_page?(url_for_autohighlight) ||
158
+ autohighlight_by_subpath?
139
159
  end
140
160
 
141
- def selected_by_highlights_on?
142
- return false unless highlights_on
161
+ def autohighlight_by_subpath?
162
+ config.highlight_on_subpath && selected_by_subpath?
163
+ end
143
164
 
165
+ def selected_by_highlights_on?
144
166
  case highlights_on
145
- when Regexp then SimpleNavigation.request_uri =~ highlights_on
167
+ when Regexp then !!(request_uri =~ highlights_on)
146
168
  when Proc then highlights_on.call
147
- when :subpath
148
- escaped_url = Regexp.escape(url_without_anchor)
149
- !!(SimpleNavigation.request_uri =~ /^#{escaped_url}(\/|$|\?)/i)
169
+ when :subpath then selected_by_subpath?
150
170
  else
151
171
  fail ArgumentError, ':highlights_on must be a Regexp, Proc or :subpath'
152
172
  end
153
173
  end
154
174
 
155
- def setup_container(container, options = {})
156
- if options[:container_class]
157
- container.dom_class = options.delete(:container_class)
158
- end
159
-
160
- if options[:container_id]
161
- container.dom_id = options.delete(:container_id)
162
- end
163
-
164
- container.dom_attributes = if options[:container_attributes]
165
- options.delete(:container_attributes)
166
- else
167
- {}
168
- end
169
-
170
- if options[:selected_class]
171
- container.selected_class = options.delete(:selected_class)
172
- end
173
-
174
- container
175
- end
176
-
177
- def setup_url_and_options(url_or_options, options_or_nil)
178
- case url_or_options
179
- when Hash then options = url_or_options # there is no url
180
- when Proc then self.url = url_or_options.call
181
- else self.url = url_or_options
182
- end
183
-
184
- options ||= options_or_nil
185
- self.highlights_on = options.delete(:highlights_on)
186
- self.html_options = options
175
+ def selected_by_subpath?
176
+ escaped_url = Regexp.escape(url_for_autohighlight)
177
+ !!(request_uri =~ /^#{escaped_url}(\/|$||\?)/i)
187
178
  end
188
179
 
189
180
  def setup_sub_navigation(items = nil, &sub_nav_block)