Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 2 additions & 57 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,79 +6,24 @@
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 4
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: Max, AllowHeredoc, AllowURI, AllowQualifiedName, URISchemes, AllowRBSInlineAnnotation, AllowCopDirectives, AllowedPatterns, SplitStrings.
# URISchemes: http, https
Layout/LineLength:
Exclude:
- 'generators/navigation_config/templates/config/navigation.rb'
- 'spec/simple_navigation/helpers_spec.rb'

# Offense count: 1
# Configuration parameters: MaxRangeSize.
Lint/MissingCopEnableDirective:
Exclude:
- 'spec/simple_navigation/configuration_spec.rb'

# Offense count: 11
# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns.
# SupportedStyles: snake_case, normalcase, non_integer
# AllowedIdentifiers: TLS1_1, TLS1_2, capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64
Naming/VariableNumber:
Exclude:
- 'generators/navigation_config/templates/config/navigation.rb'
- 'spec/fake_app/config/navigation.rb'
- 'spec/simple_navigation/item_container_spec.rb'

# Offense count: 122
# Configuration parameters: Prefixes, AllowedPatterns.
# Prefixes: when, with, without
RSpec/ContextWording:
Exclude:
- 'spec/simple_navigation/adapters/rails_spec.rb'
- 'spec/simple_navigation/config_file_finder_spec.rb'
- 'spec/simple_navigation/configuration_spec.rb'
- 'spec/simple_navigation/helpers_spec.rb'
- 'spec/simple_navigation/item_adapter_spec.rb'
- 'spec/simple_navigation/item_container_spec.rb'
- 'spec/simple_navigation/item_spec.rb'
- 'spec/simple_navigation/items_provider_spec.rb'
- 'spec/simple_navigation/renderer/base_spec.rb'
- 'spec/simple_navigation/renderer/breadcrumbs_spec.rb'
- 'spec/simple_navigation/renderer/text_spec.rb'
- 'spec/simple_navigation_spec.rb'

# Offense count: 4
# Configuration parameters: Max, AllowedIdentifiers, AllowedPatterns.
RSpec/IndexedLet:
Exclude:
- 'spec/simple_navigation/item_container_spec.rb'

# Offense count: 46
# Configuration parameters: .
# SupportedStyles: have_received, receive
RSpec/MessageSpies:
EnforcedStyle: receive

# Offense count: 38
# Configuration parameters: EnforcedStyle, IgnoreSharedExamples.
# SupportedStyles: always, named_only
RSpec/NamedSubject:
Exclude:
- 'spec/simple_navigation_spec.rb'

# Offense count: 24
RSpec/SubjectStub:
Exclude:
- 'spec/simple_navigation/item_container_spec.rb'
- 'spec/simple_navigation/renderer/base_spec.rb'
- 'spec/simple_navigation_spec.rb'

# Offense count: 65
# Offense count: 69
# Configuration parameters: IgnoreNameless, IgnoreSymbolicNames.
RSpec/VerifiedDoubles:
Exclude:
- 'spec/simple_navigation/adapters/nanoc_spec.rb'
- 'spec/simple_navigation/adapters/padrino_spec.rb'
- 'spec/simple_navigation/adapters/rails_spec.rb'
- 'spec/simple_navigation/adapters/sinatra_spec.rb'
Expand Down
19 changes: 11 additions & 8 deletions generators/navigation_config/templates/config/navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,19 @@
# navigation.ignore_query_params_on_auto_highlight = true
# navigation.ignore_anchors_on_auto_highlight = true

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

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

# Add an item which has a sub navigation (same params, but with block)
primary.item :key_2, 'name', url, options do |sub_nav|
primary.item :key2, 'name', url, options do |sub_nav|
# Add an item to the sub navigation (same params again)
sub_nav.item :key_2_1, 'name', url, options
sub_nav.item :key21, 'name', url, options
end

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

# you can also specify html attributes to attach to this particular level
# works for all levels of the menu
Expand Down
2 changes: 2 additions & 0 deletions lib/simple_navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ def set_env(root, environment)
# Returns the current framework in which the plugin is running.
def framework
return :rails if defined?(Rails)
# :nocov:
return :padrino if defined?(Padrino)
return :sinatra if defined?(Sinatra)
return :nanoc if defined?(Nanoc3)

raise 'simple_navigation currently only works for Rails, Sinatra and Padrino apps'
# :nocov:
end

# Loads the adapter for the current framework
Expand Down
3 changes: 3 additions & 0 deletions lib/simple_navigation/adapters/nanoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

module SimpleNavigation
module Adapters
# @deprecated This adapter is for Nanoc3 (circa 2011-2012) and is no longer actively maintained.
# Nanoc3 is obsolete and has been replaced by Nanoc 4+. This adapter is kept for
# backward compatibility but should not be used for new projects.
class Nanoc < Base
class << self
def register(root)
Expand Down
4 changes: 2 additions & 2 deletions spec/fake_app/config/navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

SimpleNavigation::Configuration.run do |navigation|
navigation.items do |nav|
nav.item :item_1, 'Item 1', '/item_1', html: { class: 'item_1' }, link_html: { id: 'link_1' }
nav.item :item_2, 'Item 2', '/item_2', html: { class: 'item_2' }, link_html: { id: 'link_2' }
nav.item :item1, 'Item 1', '/item_1', html: { class: 'item_1' }, link_html: { id: 'link_1' }
nav.item :item2, 'Item 2', '/item_2', html: { class: 'item_2' }, link_html: { id: 'link_2' }
end
end
97 changes: 97 additions & 0 deletions spec/simple_navigation/adapters/nanoc_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# frozen_string_literal: true

# NOTE: This adapter is deprecated. It supports Nanoc3 (circa 2011-2012) which is obsolete.
# These tests are maintained for backward compatibility only.
RSpec.describe SimpleNavigation::Adapters::Nanoc do
let(:adapter) { described_class.new(context) }
let(:context) { double(:context, item: item) }
let(:item) { double(:item, path: '/test/path/') }

describe '.register' do
let(:nanoc_context) { double(:nanoc_context) }

before do
stub_const('Nanoc3::Context', nanoc_context)
allow(SimpleNavigation).to receive(:set_env)
allow(nanoc_context).to receive(:include)
end

it 'calls SimpleNavigation.set_env with root and development environment' do
expect(SimpleNavigation).to receive(:set_env).with('/root/path', 'development')
described_class.register('/root/path')
end

it 'includes SimpleNavigation::Helpers in Nanoc3::Context' do
expect(nanoc_context).to receive(:include).with(SimpleNavigation::Helpers)
described_class.register('/root/path')
end
end

describe '#initialize' do
it 'sets the context' do
expect(adapter.instance_variable_get(:@context)).to eq context
end
end

describe '#context_for_eval' do
it 'returns the context' do
expect(adapter.context_for_eval).to eq context
end
end

describe '#current_page?' do
context 'when the path matches the url (after chomping the trailing slash)' do
it 'returns true' do
expect(adapter.current_page?('/test/path')).to be true
end
end

context 'when the path does not match the url' do
it 'returns false' do
expect(adapter.current_page?('/other/path')).to be false
end
end

context 'when path is nil' do
let(:item) { double(:item, path: nil) }

it 'returns nil' do
expect(adapter.current_page?('/test/path')).to be_nil
end
end
end

describe '#link_to' do
it 'returns a link with the correct attributes' do
link = adapter.link_to('Test Link', '/url', class: 'nav-link', id: 'link1')
expect(link).to eq "<a href='/url' class='nav-link' id='link1'>Test Link</a>"
end

it 'handles options without values' do
link = adapter.link_to('Test', '/url', {})
expect(link).to eq "<a href='/url' >Test</a>"
end

it 'filters out nil values' do
link = adapter.link_to('Test', '/url', class: 'nav', id: nil)
expect(link).to eq "<a href='/url' class='nav'>Test</a>"
end
end

describe '#content_tag' do
it 'returns a tag with the correct type, content and attributes' do
tag = adapter.content_tag(:div, 'Content', class: 'container', id: 'main')
expect(tag).to eq "<div class='container' id='main'>Content</div>"
end

it 'handles tags without attributes' do
tag = adapter.content_tag(:span, 'Text', {})
expect(tag).to eq '<span >Text</span>'
end

it 'filters out nil values from attributes' do
tag = adapter.content_tag(:p, 'Paragraph', class: 'text', id: nil)
expect(tag).to eq "<p class='text'>Paragraph</p>"
end
end
end
23 changes: 23 additions & 0 deletions spec/simple_navigation/adapters/padrino_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@
let(:context) { double(:context, request: request) }
let(:request) { double(:request) }

describe '.register' do
let(:padrino_module) { Module.new }
let(:padrino_application) { Module.new }

before do
stub_const('Padrino', padrino_module)
stub_const('Padrino::Application', padrino_application)
allow(padrino_module).to receive_messages(root: '/padrino/root', env: 'production')
allow(SimpleNavigation).to receive(:set_env)
allow(padrino_application).to receive(:send)
end

it 'calls SimpleNavigation.set_env with Padrino root and env' do
expect(SimpleNavigation).to receive(:set_env).with('/padrino/root', 'production')
described_class.register(nil)
end

it 'includes SimpleNavigation::Helpers in Padrino::Application' do
expect(padrino_application).to receive(:send).with(:helpers, SimpleNavigation::Helpers)
described_class.register(nil)
end
end

describe '#link_to' do
it 'delegates to context' do
expect(context).to receive(:link_to).with('name', 'url', { my_option: true })
Expand Down
16 changes: 8 additions & 8 deletions spec/simple_navigation/adapters/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@
end

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

it "sets the adapter's template accordingly" do
expect(adapter.template).to be template
end
end

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

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

context 'and request.fullpath is defined' do
context 'when request.fullpath is defined' do
let(:request) { double(:request, fullpath: '/fullpath') }

it "sets the adapter's request_uri to the request.fullpath" do
expect(adapter.request_uri).to eq '/fullpath'
end
end

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

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

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

it "sets the adapter's context_for_eval to the template" do
expect(adapter.context_for_eval).to be template
end
end

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

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

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

it "sets the adapter's context_for_eval to the template" do
expect(adapter.context_for_eval).to be template
end
end

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

it 'raises an exception' do
Expand Down
13 changes: 13 additions & 0 deletions spec/simple_navigation/adapters/sinatra_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@

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

describe '.register' do
let(:sinatra_app) { double(:app, root: '/sinatra/root', environment: 'test') }

before do
allow(SimpleNavigation).to receive(:set_env)
end

it 'calls SimpleNavigation.set_env with app root and environment' do
expect(SimpleNavigation).to receive(:set_env).with('/sinatra/root', 'test')
described_class.register(sinatra_app)
end
end

describe '#context_for_eval' do
context "when adapter's context is not set" do
it 'raises an exception' do
Expand Down
Loading