Skip to content

Commit 4933db3

Browse files
authored
Merge pull request #236 from codeplant/rubocop
Rubocop / Coverage
2 parents 064d545 + c52d078 commit 4933db3

21 files changed

Lines changed: 342 additions & 243 deletions

.rubocop_todo.yml

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,79 +6,24 @@
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 4
10-
# This cop supports safe autocorrection (--autocorrect).
11-
# Configuration parameters: Max, AllowHeredoc, AllowURI, AllowQualifiedName, URISchemes, AllowRBSInlineAnnotation, AllowCopDirectives, AllowedPatterns, SplitStrings.
12-
# URISchemes: http, https
13-
Layout/LineLength:
14-
Exclude:
15-
- 'generators/navigation_config/templates/config/navigation.rb'
16-
- 'spec/simple_navigation/helpers_spec.rb'
17-
18-
# Offense count: 1
19-
# Configuration parameters: MaxRangeSize.
20-
Lint/MissingCopEnableDirective:
21-
Exclude:
22-
- 'spec/simple_navigation/configuration_spec.rb'
23-
24-
# Offense count: 11
25-
# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns.
26-
# SupportedStyles: snake_case, normalcase, non_integer
27-
# AllowedIdentifiers: TLS1_1, TLS1_2, capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64
28-
Naming/VariableNumber:
29-
Exclude:
30-
- 'generators/navigation_config/templates/config/navigation.rb'
31-
- 'spec/fake_app/config/navigation.rb'
32-
- 'spec/simple_navigation/item_container_spec.rb'
33-
34-
# Offense count: 122
35-
# Configuration parameters: Prefixes, AllowedPatterns.
36-
# Prefixes: when, with, without
37-
RSpec/ContextWording:
38-
Exclude:
39-
- 'spec/simple_navigation/adapters/rails_spec.rb'
40-
- 'spec/simple_navigation/config_file_finder_spec.rb'
41-
- 'spec/simple_navigation/configuration_spec.rb'
42-
- 'spec/simple_navigation/helpers_spec.rb'
43-
- 'spec/simple_navigation/item_adapter_spec.rb'
44-
- 'spec/simple_navigation/item_container_spec.rb'
45-
- 'spec/simple_navigation/item_spec.rb'
46-
- 'spec/simple_navigation/items_provider_spec.rb'
47-
- 'spec/simple_navigation/renderer/base_spec.rb'
48-
- 'spec/simple_navigation/renderer/breadcrumbs_spec.rb'
49-
- 'spec/simple_navigation/renderer/text_spec.rb'
50-
- 'spec/simple_navigation_spec.rb'
51-
52-
# Offense count: 4
53-
# Configuration parameters: Max, AllowedIdentifiers, AllowedPatterns.
54-
RSpec/IndexedLet:
55-
Exclude:
56-
- 'spec/simple_navigation/item_container_spec.rb'
57-
589
# Offense count: 46
5910
# Configuration parameters: .
6011
# SupportedStyles: have_received, receive
6112
RSpec/MessageSpies:
6213
EnforcedStyle: receive
6314

64-
# Offense count: 38
65-
# Configuration parameters: EnforcedStyle, IgnoreSharedExamples.
66-
# SupportedStyles: always, named_only
67-
RSpec/NamedSubject:
68-
Exclude:
69-
- 'spec/simple_navigation_spec.rb'
70-
7115
# Offense count: 24
7216
RSpec/SubjectStub:
7317
Exclude:
7418
- 'spec/simple_navigation/item_container_spec.rb'
7519
- 'spec/simple_navigation/renderer/base_spec.rb'
7620
- 'spec/simple_navigation_spec.rb'
7721

78-
# Offense count: 65
22+
# Offense count: 69
7923
# Configuration parameters: IgnoreNameless, IgnoreSymbolicNames.
8024
RSpec/VerifiedDoubles:
8125
Exclude:
26+
- 'spec/simple_navigation/adapters/nanoc_spec.rb'
8227
- 'spec/simple_navigation/adapters/padrino_spec.rb'
8328
- 'spec/simple_navigation/adapters/rails_spec.rb'
8429
- 'spec/simple_navigation/adapters/sinatra_spec.rb'

generators/navigation_config/templates/config/navigation.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,19 @@
3535
# navigation.ignore_query_params_on_auto_highlight = true
3636
# navigation.ignore_anchors_on_auto_highlight = true
3737

38-
# If this option is set to true, all item names will be considered as safe (passed through html_safe). Defaults to false.
38+
# If this option is set to true, all item names will be considered as safe
39+
# (passed through html_safe). Defaults to false.
3940
# navigation.consider_item_names_as_safe = false
4041

4142
# Define the primary navigation
4243
navigation.items do |primary|
4344
# Add an item to the primary navigation. The following params apply:
4445
# key - a symbol which uniquely defines your navigation item in the scope of the primary_navigation
4546
# name - will be displayed in the rendered navigation. This can also be a call to your I18n-framework.
46-
# url - the address that the generated item links to. You can also use url_helpers (named routes, restful routes helper, url_for etc.)
47-
# options - can be used to specify attributes that will be included in the rendered navigation item (e.g. id, class etc.)
47+
# url - the address that the generated item links to. You can also use url_helpers
48+
# (named routes, restful routes helper, url_for etc.)
49+
# options - can be used to specify attributes that will be included in the rendered
50+
# navigation item (e.g. id, class etc.)
4851
# some special options that can be set:
4952
# :html - Specifies html attributes that will be included in the rendered navigation item
5053
# :if - Specifies a proc to call to determine if the item should
@@ -58,19 +61,19 @@
5861
# when the item should be highlighted, you can set a regexp which is matched
5962
# against the current URI. You may also use a proc, or the symbol <tt>:subpath</tt>.
6063
#
61-
primary.item :key_1, 'name', url, options
64+
primary.item :key1, 'name', url, options
6265

6366
# Add an item which has a sub navigation (same params, but with block)
64-
primary.item :key_2, 'name', url, options do |sub_nav|
67+
primary.item :key2, 'name', url, options do |sub_nav|
6568
# Add an item to the sub navigation (same params again)
66-
sub_nav.item :key_2_1, 'name', url, options
69+
sub_nav.item :key21, 'name', url, options
6770
end
6871

6972
# You can also specify a condition-proc that needs to be fullfilled to display an item.
7073
# Conditions are part of the options. They are evaluated in the context of the views,
7174
# thus you can use all the methods and vars you have available in the views.
72-
primary.item :key_3, 'Admin', url, html: { class: 'special' }, if: -> { current_user.admin? }
73-
primary.item :key_4, 'Account', url, unless: -> { logged_in? }
75+
primary.item :key3, 'Admin', url, html: { class: 'special' }, if: -> { current_user.admin? }
76+
primary.item :key4, 'Account', url, unless: -> { logged_in? }
7477

7578
# you can also specify html attributes to attach to this particular level
7679
# works for all levels of the menu

lib/simple_navigation.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ def set_env(root, environment)
7373
# Returns the current framework in which the plugin is running.
7474
def framework
7575
return :rails if defined?(Rails)
76+
# :nocov:
7677
return :padrino if defined?(Padrino)
7778
return :sinatra if defined?(Sinatra)
7879
return :nanoc if defined?(Nanoc3)
7980

8081
raise 'simple_navigation currently only works for Rails, Sinatra and Padrino apps'
82+
# :nocov:
8183
end
8284

8385
# Loads the adapter for the current framework

lib/simple_navigation/adapters/nanoc.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
module SimpleNavigation
44
module Adapters
5+
# @deprecated This adapter is for Nanoc3 (circa 2011-2012) and is no longer actively maintained.
6+
# Nanoc3 is obsolete and has been replaced by Nanoc 4+. This adapter is kept for
7+
# backward compatibility but should not be used for new projects.
58
class Nanoc < Base
69
class << self
710
def register(root)

spec/fake_app/config/navigation.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
SimpleNavigation::Configuration.run do |navigation|
44
navigation.items do |nav|
5-
nav.item :item_1, 'Item 1', '/item_1', html: { class: 'item_1' }, link_html: { id: 'link_1' }
6-
nav.item :item_2, 'Item 2', '/item_2', html: { class: 'item_2' }, link_html: { id: 'link_2' }
5+
nav.item :item1, 'Item 1', '/item_1', html: { class: 'item_1' }, link_html: { id: 'link_1' }
6+
nav.item :item2, 'Item 2', '/item_2', html: { class: 'item_2' }, link_html: { id: 'link_2' }
77
end
88
end
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# frozen_string_literal: true
2+
3+
# NOTE: This adapter is deprecated. It supports Nanoc3 (circa 2011-2012) which is obsolete.
4+
# These tests are maintained for backward compatibility only.
5+
RSpec.describe SimpleNavigation::Adapters::Nanoc do
6+
let(:adapter) { described_class.new(context) }
7+
let(:context) { double(:context, item: item) }
8+
let(:item) { double(:item, path: '/test/path/') }
9+
10+
describe '.register' do
11+
let(:nanoc_context) { double(:nanoc_context) }
12+
13+
before do
14+
stub_const('Nanoc3::Context', nanoc_context)
15+
allow(SimpleNavigation).to receive(:set_env)
16+
allow(nanoc_context).to receive(:include)
17+
end
18+
19+
it 'calls SimpleNavigation.set_env with root and development environment' do
20+
expect(SimpleNavigation).to receive(:set_env).with('/root/path', 'development')
21+
described_class.register('/root/path')
22+
end
23+
24+
it 'includes SimpleNavigation::Helpers in Nanoc3::Context' do
25+
expect(nanoc_context).to receive(:include).with(SimpleNavigation::Helpers)
26+
described_class.register('/root/path')
27+
end
28+
end
29+
30+
describe '#initialize' do
31+
it 'sets the context' do
32+
expect(adapter.instance_variable_get(:@context)).to eq context
33+
end
34+
end
35+
36+
describe '#context_for_eval' do
37+
it 'returns the context' do
38+
expect(adapter.context_for_eval).to eq context
39+
end
40+
end
41+
42+
describe '#current_page?' do
43+
context 'when the path matches the url (after chomping the trailing slash)' do
44+
it 'returns true' do
45+
expect(adapter.current_page?('/test/path')).to be true
46+
end
47+
end
48+
49+
context 'when the path does not match the url' do
50+
it 'returns false' do
51+
expect(adapter.current_page?('/other/path')).to be false
52+
end
53+
end
54+
55+
context 'when path is nil' do
56+
let(:item) { double(:item, path: nil) }
57+
58+
it 'returns nil' do
59+
expect(adapter.current_page?('/test/path')).to be_nil
60+
end
61+
end
62+
end
63+
64+
describe '#link_to' do
65+
it 'returns a link with the correct attributes' do
66+
link = adapter.link_to('Test Link', '/url', class: 'nav-link', id: 'link1')
67+
expect(link).to eq "<a href='/url' class='nav-link' id='link1'>Test Link</a>"
68+
end
69+
70+
it 'handles options without values' do
71+
link = adapter.link_to('Test', '/url', {})
72+
expect(link).to eq "<a href='/url' >Test</a>"
73+
end
74+
75+
it 'filters out nil values' do
76+
link = adapter.link_to('Test', '/url', class: 'nav', id: nil)
77+
expect(link).to eq "<a href='/url' class='nav'>Test</a>"
78+
end
79+
end
80+
81+
describe '#content_tag' do
82+
it 'returns a tag with the correct type, content and attributes' do
83+
tag = adapter.content_tag(:div, 'Content', class: 'container', id: 'main')
84+
expect(tag).to eq "<div class='container' id='main'>Content</div>"
85+
end
86+
87+
it 'handles tags without attributes' do
88+
tag = adapter.content_tag(:span, 'Text', {})
89+
expect(tag).to eq '<span >Text</span>'
90+
end
91+
92+
it 'filters out nil values from attributes' do
93+
tag = adapter.content_tag(:p, 'Paragraph', class: 'text', id: nil)
94+
expect(tag).to eq "<p class='text'>Paragraph</p>"
95+
end
96+
end
97+
end

spec/simple_navigation/adapters/padrino_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,29 @@
66
let(:context) { double(:context, request: request) }
77
let(:request) { double(:request) }
88

9+
describe '.register' do
10+
let(:padrino_module) { Module.new }
11+
let(:padrino_application) { Module.new }
12+
13+
before do
14+
stub_const('Padrino', padrino_module)
15+
stub_const('Padrino::Application', padrino_application)
16+
allow(padrino_module).to receive_messages(root: '/padrino/root', env: 'production')
17+
allow(SimpleNavigation).to receive(:set_env)
18+
allow(padrino_application).to receive(:send)
19+
end
20+
21+
it 'calls SimpleNavigation.set_env with Padrino root and env' do
22+
expect(SimpleNavigation).to receive(:set_env).with('/padrino/root', 'production')
23+
described_class.register(nil)
24+
end
25+
26+
it 'includes SimpleNavigation::Helpers in Padrino::Application' do
27+
expect(padrino_application).to receive(:send).with(:helpers, SimpleNavigation::Helpers)
28+
described_class.register(nil)
29+
end
30+
end
31+
932
describe '#link_to' do
1033
it 'delegates to context' do
1134
expect(context).to receive(:link_to).with('name', 'url', { my_option: true })

spec/simple_navigation/adapters/rails_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@
7979
end
8080

8181
context "when the controller's template is stored as view_context (Rails3)" do
82-
context 'and the template is set' do
82+
context 'when the template is set' do
8383
before { allow(controller).to receive_messages(view_context: template) }
8484

8585
it "sets the adapter's template accordingly" do
8686
expect(adapter.template).to be template
8787
end
8888
end
8989

90-
context 'and the template is not set' do
90+
context 'when the template is not set' do
9191
before { allow(controller).to receive_messages(view_context: nil) }
9292

9393
it "sets the adapter's template to nil" do
@@ -101,15 +101,15 @@
101101
context "when the adapter's request is set" do
102102
before { allow(adapter).to receive_messages(request: request) }
103103

104-
context 'and request.fullpath is defined' do
104+
context 'when request.fullpath is defined' do
105105
let(:request) { double(:request, fullpath: '/fullpath') }
106106

107107
it "sets the adapter's request_uri to the request.fullpath" do
108108
expect(adapter.request_uri).to eq '/fullpath'
109109
end
110110
end
111111

112-
context 'and request.fullpath is not defined' do
112+
context 'when request.fullpath is not defined' do
113113
let(:request) { double(:request, request_uri: '/request_uri') }
114114

115115
before { allow(adapter).to receive_messages(request: request) }
@@ -153,15 +153,15 @@
153153
context "when the adapter's controller is set" do
154154
before { adapter.instance_variable_set(:@controller, controller) }
155155

156-
context "and the adapter's template is set" do
156+
context "when the adapter's template is set" do
157157
before { adapter.instance_variable_set(:@template, template) }
158158

159159
it "sets the adapter's context_for_eval to the template" do
160160
expect(adapter.context_for_eval).to be template
161161
end
162162
end
163163

164-
context "and the adapter's template is not set" do
164+
context "when the adapter's template is not set" do
165165
before { adapter.instance_variable_set(:@template, nil) }
166166

167167
it "sets the adapter's context_for_eval to the controller" do
@@ -173,15 +173,15 @@
173173
context "when the adapter's controller is not set" do
174174
before { adapter.instance_variable_set(:@controller, nil) }
175175

176-
context "and the adapter's template is set" do
176+
context "when the adapter's template is set" do
177177
before { adapter.instance_variable_set(:@template, template) }
178178

179179
it "sets the adapter's context_for_eval to the template" do
180180
expect(adapter.context_for_eval).to be template
181181
end
182182
end
183183

184-
context "and the adapter's template is not set" do
184+
context "when the adapter's template is not set" do
185185
before { adapter.instance_variable_set(:@template, nil) }
186186

187187
it 'raises an exception' do

spec/simple_navigation/adapters/sinatra_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@
77

88
before { allow(context).to receive_messages(request: request) }
99

10+
describe '.register' do
11+
let(:sinatra_app) { double(:app, root: '/sinatra/root', environment: 'test') }
12+
13+
before do
14+
allow(SimpleNavigation).to receive(:set_env)
15+
end
16+
17+
it 'calls SimpleNavigation.set_env with app root and environment' do
18+
expect(SimpleNavigation).to receive(:set_env).with('/sinatra/root', 'test')
19+
described_class.register(sinatra_app)
20+
end
21+
end
22+
1023
describe '#context_for_eval' do
1124
context "when adapter's context is not set" do
1225
it 'raises an exception' do

0 commit comments

Comments
 (0)