diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..7f69394c --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,84 @@ +--- +name: CI + +on: + push: + branches: + - '**' + pull_request: + branches: + - '**' + schedule: + - cron: '0 4 1 * *' + +jobs: + rubocop: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v5 + + - name: Setup Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: '3.0' + + - name: Bundler + run: bundle install + + - name: Rubocop + run: bin/rubocop + + rspec: + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + ruby: + - '4.0' + - '3.4' + - '3.3' + - '3.2' + - '3.1' + - '3.0' + rails: + - rails_8.1 + - rails_8.0 + - rails_7.2 + - rails_7.1 + - rails_7.0 + - rails_6.1 + + exclude: + - ruby: '3.0' + rails: 'rails_7.2' + - ruby: '3.0' + rails: 'rails_8.0' + - ruby: '3.0' + rails: 'rails_8.1' + + - ruby: '3.1' + rails: 'rails_8.0' + - ruby: '3.1' + rails: 'rails_8.1' + + env: + # $BUNDLE_GEMFILE must be set at the job level, so it is set for all steps + BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/${{ matrix.rails }}.gemfile + # Check for frozen strings + RUBYOPT: "--enable=frozen-string-literal --debug=frozen-string-literal" + + steps: + - name: Checkout + uses: actions/checkout@v5 + + - name: Setup Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + bundler-cache: true + + - name: RSpec + run: bin/rspec diff --git a/.rspec b/.rspec index 1892c522..ce0f67a2 100644 --- a/.rspec +++ b/.rspec @@ -1,3 +1,2 @@ ---color ---format=documentation --require spec_helper +--warnings diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 00000000..6cd43a2c --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,49 @@ +--- +plugins: + - rubocop-capybara + - rubocop-performance + - rubocop-rake + - rubocop-rspec + +inherit_from: + - .rubocop_todo.yml + +AllCops: + NewCops: enable + SuggestExtensions: true + TargetRubyVersion: 3.0 + Exclude: + - bin/* + - gemfiles/* + +Gemspec/RequireMFA: + Enabled: false + +Style/Documentation: + Enabled: false + +Naming/FileName: + Exclude: + - lib/simple-navigation.rb + +########### +# METRICS # +########### + +Metrics/ClassLength: + Exclude: + - spec/**/* + +Metrics/MethodLength: + Exclude: + - spec/**/* + +######### +# RSPEC # +######### + +RSpec/NestedGroups: + Max: 6 + +RSpec/MultipleMemoizedHelpers: + Max: 8 diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 00000000..252b05b5 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,39 @@ +--- + +Layout/LineLength: + Enabled: false + +Naming/VariableNumber: + Enabled: false + +Style/BlockDelimiters: + Enabled: false + +######### +# RSPEC # +######### + +RSpec/SubjectStub: + Enabled: false + +RSpec/NamedSubject: + Enabled: false + +RSpec/VerifiedDoubles: + Enabled: false + +RSpec/ContextWording: + Enabled: false + +RSpec/MessageSpies: + Enabled: false + +RSpec/IndexedLet: + Enabled: false + +############ +# CAPYBARA # +############ + +Capybara/NegationMatcher: + Enabled: false diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 73ad5af5..00000000 --- a/.travis.yml +++ /dev/null @@ -1,23 +0,0 @@ -before_install: - - gem install bundler - -matrix: - include: - - rvm: 2.5.8 - gemfile: gemfiles/rails-6-1-stable.gemfile - - rvm: 2.6.7 - gemfile: gemfiles/rails-6-1-stable.gemfile - - rvm: 2.7.3 - gemfile: gemfiles/rails-6-1-stable.gemfile - - rvm: 2.5.3 - gemfile: gemfiles/rails-6-0-stable.gemfile - - rvm: 2.6.0 - gemfile: gemfiles/rails-6-0-stable.gemfile - - rvm: 2.5.1 - gemfile: gemfiles/rails-5-2-stable.gemfile - - rvm: 2.4.5 - gemfile: gemfiles/rails-4-2-stable.gemfile - - rvm: 2.3.3 - gemfile: gemfiles/rails-4-1-stable.gemfile - - rvm: 2.3.3 - gemfile: gemfiles/rails-3-2-stable.gemfile diff --git a/Appraisals b/Appraisals new file mode 100644 index 00000000..dcdd1cb2 --- /dev/null +++ b/Appraisals @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +# To update files in gemfiles/ directory: +# rm gemfiles/* +# bin/appraisal generate + +# To run tests: +# bin/appraisal rails_8.1 bundle install +# bin/appraisal rails_8.1 rake +# bin/appraisal rails_8.1 rspec + +appraise 'rails_6.1' do + gem 'railties', '~> 6.1.0' + gem 'rspec-rails' + gem 'concurrent-ruby', '1.3.4' + + # Fix: + # warning: drb was loaded from the standard library, but is not part of the default gems starting from Ruby 3.4.0. + # You can add drb to your Gemfile or gemspec to silence this warning. + install_if '-> { Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.4.0") }' do + gem 'base64' + gem 'bigdecimal' + gem 'drb' + gem 'mutex_m' + end + + install_if '-> { Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("4.0.0") }' do + gem 'benchmark' + end +end + +appraise 'rails_7.0' do + gem 'railties', '~> 7.0.0' + gem 'rspec-rails' +end + +appraise 'rails_7.1' do + gem 'railties', '~> 7.1.0' + gem 'rspec-rails' +end + +appraise 'rails_7.2' do + gem 'railties', '~> 7.2.0' + gem 'rspec-rails' +end + +appraise 'rails_8.0' do + gem 'railties', '~> 8.0.0' + gem 'rspec-rails' +end + +appraise 'rails_8.1' do + gem 'railties', '~> 8.1.0' + gem 'rspec-rails' +end diff --git a/Gemfile b/Gemfile index fa75df15..ad06fe67 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,24 @@ +# frozen_string_literal: true + source 'https://rubygems.org' gemspec + +# Dev libs +gem 'appraisal', git: 'https://github.com/thoughtbot/appraisal.git' +gem 'capybara' +gem 'memfs', git: 'https://github.com/simonc/memfs.git' +gem 'rake' +gem 'rdoc' +gem 'rspec' +gem 'simplecov' +gem 'tzinfo' +gem 'warning' + +# Dev tools / linter +gem 'guard-rspec', require: false +gem 'rubocop', require: false +gem 'rubocop-capybara', require: false +gem 'rubocop-performance', require: false +gem 'rubocop-rake', require: false +gem 'rubocop-rspec', require: false diff --git a/Guardfile b/Guardfile index f4357e62..881f7b62 100644 --- a/Guardfile +++ b/Guardfile @@ -1,7 +1,9 @@ +# frozen_string_literal: true + guard :rspec, all_after_pass: true, failed_mode: :none do watch(%r{^spec/.+_spec\.rb$}) - watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } + watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } watch(%r{^spec/fake_app/.+\.rb$}) { 'spec/integration' } - watch('spec/spec_helper.rb') { 'spec' } + watch('spec/spec_helper.rb') { 'spec' } end diff --git a/README.md b/README.md index a12b96b4..422c587b 100644 --- a/README.md +++ b/README.md @@ -31,30 +31,19 @@ Fork, fix, then send a Pull Request. To run the test suite locally against all supported frameworks: - % bundle install - % rake spec:all +```sh +bundle install +bin/appraisal bundle install +bin/appraisal rspec +bin/appraisal rspec ./spec/requests/users_spec.rb +``` To target the test suite against one framework: - % rake spec:rails-4-2-stable - -You can find a list of supported spec tasks by running rake -T. You may also find it useful to run a specific test for a specific framework. To do so, you'll have to first make sure you have bundled everything for that configuration, then you can run the specific test: - -% BUNDLE_GEMFILE='gemfiles/rails-4-2-stable.gemfile' bundle install -j 4 -% BUNDLE_GEMFILE='gemfiles/rails-4-2-stable.gemfile' bundle exec rspec ./spec/requests/users_spec.rb - -### Rake and Bundler - -If you use a shell plugin (like oh-my-zsh:bundler) that auto-prefixes commands with `bundle exec` using the `rake` command will fail. - -Get the original command with `type -a rake`: - - % type -a rake - rake is an alias for bundled_rake - rake is /Users/username/.rubies/ruby-2.2.3/bin/rake - rake is /usr/bin/rake - -In this situation `/Users/username/.rubies/ruby-2.2.3/bin/rake` is the command you should use. +```sh +bin/appraisal rails_8.1 rspec +bin/appraisal rails_8.1 rspec ./spec/requests/users_spec.rb +``` ## License diff --git a/Rakefile b/Rakefile index feeb7998..1a7ff9b6 100644 --- a/Rakefile +++ b/Rakefile @@ -1,37 +1,12 @@ -require 'bundler' -Bundler::GemHelper.install_tasks +# frozen_string_literal: true -require 'rspec/core' +require 'bundler/gem_tasks' require 'rspec/core/rake_task' - require 'rdoc/task' RSpec::Core::RakeTask.new(:spec) - task default: 'spec' -namespace :spec do - mappers = %w[ - rails-3-2-stable - rails-4-1-stable - rails-4-2-stable - rails-5-2-stable - rails-6-0-stable - rails-6-1-stable - ] - - mappers.each do |gemfile| - desc "Run Tests against #{gemfile}" - task gemfile do - sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle -j 4 --quiet" - sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle exec rake -t spec" - end - end - - desc 'Run Tests against all ORMs' - task all: mappers -end - RDoc::Task.new do |rdoc| rdoc.rdoc_dir = 'rdoc' rdoc.title = 'SimpleNavigation' diff --git a/bin/_guard-core b/bin/_guard-core new file mode 100755 index 00000000..3b368e69 --- /dev/null +++ b/bin/_guard-core @@ -0,0 +1,16 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application '_guard-core' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) + +require "rubygems" +require "bundler/setup" + +load Gem.bin_path("guard", "_guard-core") diff --git a/bin/appraisal b/bin/appraisal new file mode 100755 index 00000000..620da794 --- /dev/null +++ b/bin/appraisal @@ -0,0 +1,16 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application 'appraisal' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) + +require "rubygems" +require "bundler/setup" + +load Gem.bin_path("appraisal", "appraisal") diff --git a/bin/guard b/bin/guard new file mode 100755 index 00000000..643c81f6 --- /dev/null +++ b/bin/guard @@ -0,0 +1,16 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application 'guard' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) + +require "rubygems" +require "bundler/setup" + +load Gem.bin_path("guard", "guard") diff --git a/bin/rake b/bin/rake new file mode 100755 index 00000000..9efbee99 --- /dev/null +++ b/bin/rake @@ -0,0 +1,16 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application 'rake' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) + +require "rubygems" +require "bundler/setup" + +load Gem.bin_path("rake", "rake") diff --git a/bin/rspec b/bin/rspec new file mode 100755 index 00000000..93e191c2 --- /dev/null +++ b/bin/rspec @@ -0,0 +1,16 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application 'rspec' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) + +require "rubygems" +require "bundler/setup" + +load Gem.bin_path("rspec-core", "rspec") diff --git a/bin/rubocop b/bin/rubocop new file mode 100755 index 00000000..d73598dc --- /dev/null +++ b/bin/rubocop @@ -0,0 +1,16 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application 'rubocop' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) + +require "rubygems" +require "bundler/setup" + +load Gem.bin_path("rubocop", "rubocop") diff --git a/gemfiles/rails-3-2-stable.gemfile b/gemfiles/rails-3-2-stable.gemfile deleted file mode 100644 index 5b62ac4b..00000000 --- a/gemfiles/rails-3-2-stable.gemfile +++ /dev/null @@ -1,11 +0,0 @@ -source 'https://rubygems.org' - -gem 'rake', '< 11.0' -gem 'railties', '~> 3.2.0' -gem 'rspec-rails', '~> 3.2.1' - -if RUBY_VERSION >= '2.2.0' - gem 'test-unit', '~> 3.0' -end - -gemspec path: '../' diff --git a/gemfiles/rails-4-1-stable.gemfile b/gemfiles/rails-4-1-stable.gemfile deleted file mode 100644 index 57c4af92..00000000 --- a/gemfiles/rails-4-1-stable.gemfile +++ /dev/null @@ -1,7 +0,0 @@ -source 'https://rubygems.org' - -gem 'rake', '< 11.0' -gem 'railties', '~> 4.1.0' -gem 'rspec-rails', '~> 3.2.1' - -gemspec path: '../' diff --git a/gemfiles/rails-4-2-stable.gemfile b/gemfiles/rails-4-2-stable.gemfile deleted file mode 100644 index 98206291..00000000 --- a/gemfiles/rails-4-2-stable.gemfile +++ /dev/null @@ -1,7 +0,0 @@ -source 'https://rubygems.org' - -gem 'rake', '< 11.0' -gem 'railties', '~> 4.2.0' -gem 'rspec-rails', '~> 3.2.1' - -gemspec path: '../' diff --git a/gemfiles/rails-5-2-stable.gemfile b/gemfiles/rails-5-2-stable.gemfile deleted file mode 100644 index a5fb0470..00000000 --- a/gemfiles/rails-5-2-stable.gemfile +++ /dev/null @@ -1,7 +0,0 @@ -source 'https://rubygems.org' - -gem 'rake', '< 11.0' -gem 'railties', '~> 5.2.0' -gem 'rspec-rails', '~> 3.8.2' - -gemspec path: '../' diff --git a/gemfiles/rails-6-0-stable.gemfile b/gemfiles/rails-6-0-stable.gemfile deleted file mode 100644 index 095cb349..00000000 --- a/gemfiles/rails-6-0-stable.gemfile +++ /dev/null @@ -1,9 +0,0 @@ -source 'https://rubygems.org' - -gem 'rake', '< 11.0' - -# not yet -gem 'railties', '~> 6.0.0' -gem 'rspec-rails', '~> 3.8.2' - -gemspec path: '../' diff --git a/gemfiles/rails-6-1-stable.gemfile b/gemfiles/rails-6-1-stable.gemfile deleted file mode 100644 index c2024a32..00000000 --- a/gemfiles/rails-6-1-stable.gemfile +++ /dev/null @@ -1,9 +0,0 @@ -source 'https://rubygems.org' - -gem 'rake', '< 11.0' - -# not yet -gem 'railties', '~> 6.1.0' -gem 'rspec-rails', '~> 3.8.2' - -gemspec path: '../' diff --git a/gemfiles/rails_6.1.gemfile b/gemfiles/rails_6.1.gemfile new file mode 100644 index 00000000..6a8fd9aa --- /dev/null +++ b/gemfiles/rails_6.1.gemfile @@ -0,0 +1,35 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "appraisal", git: "https://github.com/thoughtbot/appraisal.git" +gem "capybara" +gem "memfs", git: "https://github.com/simonc/memfs.git" +gem "rake" +gem "rdoc" +gem "rspec" +gem "simplecov" +gem "tzinfo" +gem "warning" +gem "guard-rspec", require: false +gem "rubocop", require: false +gem "rubocop-capybara", require: false +gem "rubocop-performance", require: false +gem "rubocop-rake", require: false +gem "rubocop-rspec", require: false +gem "railties", "~> 6.1.0" +gem "rspec-rails" +gem "concurrent-ruby", "1.3.4" + +install_if -> { Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.4.0") } do + gem "base64" + gem "bigdecimal" + gem "drb" + gem "mutex_m" +end + +install_if -> { Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("4.0.0") } do + gem "benchmark" +end + +gemspec path: "../" diff --git a/gemfiles/rails_7.0.gemfile b/gemfiles/rails_7.0.gemfile new file mode 100644 index 00000000..6ce788a9 --- /dev/null +++ b/gemfiles/rails_7.0.gemfile @@ -0,0 +1,23 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "appraisal", git: "https://github.com/thoughtbot/appraisal.git" +gem "capybara" +gem "memfs", git: "https://github.com/simonc/memfs.git" +gem "rake" +gem "rdoc" +gem "rspec" +gem "simplecov" +gem "tzinfo" +gem "warning" +gem "guard-rspec", require: false +gem "rubocop", require: false +gem "rubocop-capybara", require: false +gem "rubocop-performance", require: false +gem "rubocop-rake", require: false +gem "rubocop-rspec", require: false +gem "railties", "~> 7.0.0" +gem "rspec-rails" + +gemspec path: "../" diff --git a/gemfiles/rails_7.1.gemfile b/gemfiles/rails_7.1.gemfile new file mode 100644 index 00000000..fefba441 --- /dev/null +++ b/gemfiles/rails_7.1.gemfile @@ -0,0 +1,23 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "appraisal", git: "https://github.com/thoughtbot/appraisal.git" +gem "capybara" +gem "memfs", git: "https://github.com/simonc/memfs.git" +gem "rake" +gem "rdoc" +gem "rspec" +gem "simplecov" +gem "tzinfo" +gem "warning" +gem "guard-rspec", require: false +gem "rubocop", require: false +gem "rubocop-capybara", require: false +gem "rubocop-performance", require: false +gem "rubocop-rake", require: false +gem "rubocop-rspec", require: false +gem "railties", "~> 7.1.0" +gem "rspec-rails" + +gemspec path: "../" diff --git a/gemfiles/rails_7.2.gemfile b/gemfiles/rails_7.2.gemfile new file mode 100644 index 00000000..bc5f4ab6 --- /dev/null +++ b/gemfiles/rails_7.2.gemfile @@ -0,0 +1,23 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "appraisal", git: "https://github.com/thoughtbot/appraisal.git" +gem "capybara" +gem "memfs", git: "https://github.com/simonc/memfs.git" +gem "rake" +gem "rdoc" +gem "rspec" +gem "simplecov" +gem "tzinfo" +gem "warning" +gem "guard-rspec", require: false +gem "rubocop", require: false +gem "rubocop-capybara", require: false +gem "rubocop-performance", require: false +gem "rubocop-rake", require: false +gem "rubocop-rspec", require: false +gem "railties", "~> 7.2.0" +gem "rspec-rails" + +gemspec path: "../" diff --git a/gemfiles/rails_8.0.gemfile b/gemfiles/rails_8.0.gemfile new file mode 100644 index 00000000..7869e509 --- /dev/null +++ b/gemfiles/rails_8.0.gemfile @@ -0,0 +1,23 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "appraisal", git: "https://github.com/thoughtbot/appraisal.git" +gem "capybara" +gem "memfs", git: "https://github.com/simonc/memfs.git" +gem "rake" +gem "rdoc" +gem "rspec" +gem "simplecov" +gem "tzinfo" +gem "warning" +gem "guard-rspec", require: false +gem "rubocop", require: false +gem "rubocop-capybara", require: false +gem "rubocop-performance", require: false +gem "rubocop-rake", require: false +gem "rubocop-rspec", require: false +gem "railties", "~> 8.0.0" +gem "rspec-rails" + +gemspec path: "../" diff --git a/gemfiles/rails_8.1.gemfile b/gemfiles/rails_8.1.gemfile new file mode 100644 index 00000000..f9b31152 --- /dev/null +++ b/gemfiles/rails_8.1.gemfile @@ -0,0 +1,23 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "appraisal", git: "https://github.com/thoughtbot/appraisal.git" +gem "capybara" +gem "memfs", git: "https://github.com/simonc/memfs.git" +gem "rake" +gem "rdoc" +gem "rspec" +gem "simplecov" +gem "tzinfo" +gem "warning" +gem "guard-rspec", require: false +gem "rubocop", require: false +gem "rubocop-capybara", require: false +gem "rubocop-performance", require: false +gem "rubocop-rake", require: false +gem "rubocop-rspec", require: false +gem "railties", "~> 8.1.0" +gem "rspec-rails" + +gemspec path: "../" diff --git a/generators/navigation_config/navigation_config_generator.rb b/generators/navigation_config/navigation_config_generator.rb index be81087f..449cb454 100644 --- a/generators/navigation_config/navigation_config_generator.rb +++ b/generators/navigation_config/navigation_config_generator.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class NavigationConfigGenerator < Rails::Generator::Base def manifest record do |m| diff --git a/generators/navigation_config/templates/config/navigation.rb b/generators/navigation_config/templates/config/navigation.rb index 2a11b180..48589406 100644 --- a/generators/navigation_config/templates/config/navigation.rb +++ b/generators/navigation_config/templates/config/navigation.rb @@ -1,41 +1,42 @@ -# -*- coding: utf-8 -*- +# frozen_string_literal: true + # Configures your navigation SimpleNavigation::Configuration.run do |navigation| # Specify a custom renderer if needed. # The default renderer is SimpleNavigation::Renderer::List which renders HTML lists. # The renderer can also be specified as option in the render_navigation call. - #navigation.renderer = Your::Custom::Renderer + # navigation.renderer = Your::Custom::Renderer - # Specify the class that will be applied to active navigation items. Defaults to 'selected' - #navigation.selected_class = 'selected' + # Specify the class that will be applied to active navigation items. Defaults to 'selected' + # navigation.selected_class = 'selected' # Specify the class that will be applied to the current leaf of # active navigation items. Defaults to 'simple-navigation-active-leaf' - #navigation.active_leaf_class = 'simple-navigation-active-leaf' + # navigation.active_leaf_class = 'simple-navigation-active-leaf' # Specify if item keys are added to navigation items as id. Defaults to true - #navigation.autogenerate_item_ids = true + # navigation.autogenerate_item_ids = true # You can override the default logic that is used to autogenerate the item ids. # To do this, define a Proc which takes the key of the current item as argument. # The example below would add a prefix to each key. - #navigation.id_generator = Proc.new {|key| "my-prefix-#{key}"} + # navigation.id_generator = Proc.new {|key| "my-prefix-#{key}"} # If you need to add custom html around item names, you can define a proc that # will be called with the name you pass in to the navigation. # The example below shows how to wrap items spans. - #navigation.name_generator = Proc.new {|name, item| tag.span(name) } + # navigation.name_generator = Proc.new {|name, item| tag.span(name) } # Specify if the auto highlight feature is turned on (globally, for the whole navigation). Defaults to true - #navigation.auto_highlight = true - - # Specifies whether auto highlight should ignore query params and/or anchors when - # comparing the navigation items with the current URL. Defaults to true - #navigation.ignore_query_params_on_auto_highlight = true - #navigation.ignore_anchors_on_auto_highlight = true - + # navigation.auto_highlight = true + + # Specifies whether auto highlight should ignore query params and/or anchors when + # comparing the navigation items with the current URL. Defaults to true + # 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. - #navigation.consider_item_names_as_safe = false + # navigation.consider_item_names_as_safe = false # Define the primary navigation navigation.items do |primary| @@ -73,9 +74,9 @@ # you can also specify html attributes to attach to this particular level # works for all levels of the menu - #primary.dom_attributes = {id: 'menu-id', class: 'menu-class'} + # primary.dom_attributes = {id: 'menu-id', class: 'menu-class'} # You can turn off auto highlighting for a specific level - #primary.auto_highlight = false + # primary.auto_highlight = false end end diff --git a/init.rb b/init.rb deleted file mode 100644 index 470d4902..00000000 --- a/init.rb +++ /dev/null @@ -1 +0,0 @@ -require File.dirname(__FILE__) + '/rails/init' diff --git a/install.rb b/install.rb deleted file mode 100644 index a62f1560..00000000 --- a/install.rb +++ /dev/null @@ -1,5 +0,0 @@ -begin - puts IO.read(File.join(File.dirname(__FILE__), 'README')) -rescue Exception => e - puts "The following error ocurred while installing the plugin: #{e.message}" -end diff --git a/lib/generators/navigation_config/navigation_config_generator.rb b/lib/generators/navigation_config/navigation_config_generator.rb index f56ddf4d..c3004d9a 100644 --- a/lib/generators/navigation_config/navigation_config_generator.rb +++ b/lib/generators/navigation_config/navigation_config_generator.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class NavigationConfigGenerator < Rails::Generators::Base def self.source_root @source_root ||= begin diff --git a/lib/simple-navigation.rb b/lib/simple-navigation.rb index dc7685de..4a0f8b31 100644 --- a/lib/simple-navigation.rb +++ b/lib/simple-navigation.rb @@ -1 +1,3 @@ -require 'simple_navigation' +# frozen_string_literal: true + +require_relative 'simple_navigation' diff --git a/lib/simple_navigation.rb b/lib/simple_navigation.rb index 1522fb20..eaf57fe9 100644 --- a/lib/simple_navigation.rb +++ b/lib/simple_navigation.rb @@ -1,24 +1,30 @@ -# cherry picking active_support stuff +# frozen_string_literal: true + +# require ruby dependencies +require 'cgi/escape' +require 'forwardable' +require 'json' +require 'singleton' + +# require external dependencies require 'active_support/core_ext/array' require 'active_support/core_ext/hash' +require 'active_support/core_ext/string' require 'active_support/core_ext/module/attribute_accessors' +require 'zeitwerk' -require 'simple_navigation/version' -require 'simple_navigation/configuration' -require 'simple_navigation/item_adapter' -require 'simple_navigation/item' -require 'simple_navigation/item_container' -require 'simple_navigation/items_provider' -require 'simple_navigation/renderer' -require 'simple_navigation/adapters' -require 'simple_navigation/config_file_finder' -require 'simple_navigation/railtie' if defined?(::Rails::Railtie) - -require 'forwardable' +# load zeitwerk +Zeitwerk::Loader.for_gem.tap do |loader| + loader.ignore("#{__dir__}/generators") + loader.ignore("#{__dir__}/simple-navigation.rb") + loader.setup +end # A plugin for generating a simple navigation. See README for resources on # usage instructions. module SimpleNavigation + require_relative 'simple_navigation/railtie' if defined?(Rails::Railtie) + mattr_accessor :adapter, :adapter_class, :config_files, @@ -38,21 +44,21 @@ module SimpleNavigation # Maps renderer keys to classes. The keys serve as shortcut in the # render_navigation calls (renderer: :list) self.registered_renderers = { - list: SimpleNavigation::Renderer::List, - links: SimpleNavigation::Renderer::Links, + list: SimpleNavigation::Renderer::List, + links: SimpleNavigation::Renderer::Links, breadcrumbs: SimpleNavigation::Renderer::Breadcrumbs, - text: SimpleNavigation::Renderer::Text, - json: SimpleNavigation::Renderer::Json + text: SimpleNavigation::Renderer::Text, + json: SimpleNavigation::Renderer::Json } class << self extend Forwardable def_delegators :adapter, :context_for_eval, - :current_page?, - :request, - :request_path, - :request_uri + :current_page?, + :request, + :request_path, + :request_uri def_delegators :adapter_class, :register @@ -70,8 +76,8 @@ def framework return :padrino if defined?(Padrino) return :sinatra if defined?(Sinatra) return :nanoc if defined?(Nanoc3) - fail 'simple_navigation currently only works for Rails, Sinatra and ' \ - 'Padrino apps' + + raise 'simple_navigation currently only works for Rails, Sinatra and Padrino apps' end # Loads the adapter for the current framework @@ -138,7 +144,7 @@ def active_item_container_for(level) when Integer then primary_navigation.active_item_container_for(level) when Range then primary_navigation.active_item_container_for(level.min) else - fail ArgumentError, "Invalid navigation level: #{level}" + raise ArgumentError, "Invalid navigation level: #{level}" end end diff --git a/lib/simple_navigation/adapters.rb b/lib/simple_navigation/adapters.rb deleted file mode 100644 index 84764597..00000000 --- a/lib/simple_navigation/adapters.rb +++ /dev/null @@ -1,10 +0,0 @@ -require 'simple_navigation/adapters/base' - -module SimpleNavigation - module Adapters - autoload :Rails, 'simple_navigation/adapters/rails' - autoload :Padrino, 'simple_navigation/adapters/padrino' - autoload :Sinatra, 'simple_navigation/adapters/sinatra' - autoload :Nanoc, 'simple_navigation/adapters/nanoc' - end -end diff --git a/lib/simple_navigation/adapters/base.rb b/lib/simple_navigation/adapters/base.rb index 7ff57798..e8324861 100644 --- a/lib/simple_navigation/adapters/base.rb +++ b/lib/simple_navigation/adapters/base.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module SimpleNavigation module Adapters # This is the base class for all adapters. diff --git a/lib/simple_navigation/adapters/nanoc.rb b/lib/simple_navigation/adapters/nanoc.rb index a3b83682..ba7811fb 100644 --- a/lib/simple_navigation/adapters/nanoc.rb +++ b/lib/simple_navigation/adapters/nanoc.rb @@ -1,14 +1,16 @@ +# frozen_string_literal: true + module SimpleNavigation module Adapters class Nanoc < Base class << self def register(root) SimpleNavigation.set_env(root, 'development') - Nanoc3::Context.send(:include, SimpleNavigation::Helpers) + Nanoc3::Context.include SimpleNavigation::Helpers end end - def initialize(ctx) + def initialize(ctx) # rubocop:disable Lint/MissingSuper @context = ctx end @@ -39,7 +41,7 @@ def content_tag(type, content, options = {}) private def to_attributes(options) - options.map { |k, v| v.nil? ? nil : "#{k}='#{v}'" }.compact.join(' ') + options.filter_map { |k, v| v.nil? ? nil : "#{k}='#{v}'" }.join(' ') end end end diff --git a/lib/simple_navigation/adapters/padrino.rb b/lib/simple_navigation/adapters/padrino.rb index 4dd7359d..28b0e4b9 100644 --- a/lib/simple_navigation/adapters/padrino.rb +++ b/lib/simple_navigation/adapters/padrino.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + module SimpleNavigation module Adapters class Padrino < Sinatra - def self.register(app) + def self.register(_app) SimpleNavigation.set_env(::Padrino.root, ::Padrino.env) ::Padrino::Application.send(:helpers, SimpleNavigation::Helpers) end diff --git a/lib/simple_navigation/adapters/rails.rb b/lib/simple_navigation/adapters/rails.rb index 7b513da7..b809a247 100644 --- a/lib/simple_navigation/adapters/rails.rb +++ b/lib/simple_navigation/adapters/rails.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module SimpleNavigation module Adapters class Rails < Base @@ -10,23 +12,19 @@ def self.register # 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 + ActiveSupport.on_load(:action_controller_base) do + SimpleNavigation::Adapters::Rails.register_controller_helpers end end def self.register_controller_helpers - ActionController::Base.send(:include, SimpleNavigation::Helpers) + ActionController::Base.include SimpleNavigation::Helpers SimpleNavigation::Helpers.instance_methods.each do |m| ActionController::Base.send(:helper_method, m.to_sym) end end - def initialize(context) + def initialize(context) # rubocop:disable Lint/MissingSuper @controller = extract_controller_from context @template = template_from @controller @request = @template.request if @template @@ -47,21 +45,21 @@ def request_path end def context_for_eval - template || - controller || - fail('no context set for evaluation the config file') + template || + controller || + raise('no context set for evaluation the config file') end def current_page?(url) - template && template.current_page?(url) + template&.current_page?(url) end def link_to(name, url, options = {}) - template && template.link_to(link_title(name), url, options) + template&.link_to(link_title(name), url, options) end def content_tag(type, content, options = {}) - template && template.content_tag(type, html_safe(content), options) + template&.content_tag(type, html_safe(content), options) end protected diff --git a/lib/simple_navigation/adapters/sinatra.rb b/lib/simple_navigation/adapters/sinatra.rb index 44f04689..1d65ccd7 100644 --- a/lib/simple_navigation/adapters/sinatra.rb +++ b/lib/simple_navigation/adapters/sinatra.rb @@ -1,4 +1,4 @@ -require 'cgi' +# frozen_string_literal: true module SimpleNavigation module Adapters @@ -7,13 +7,13 @@ def self.register(app) SimpleNavigation.set_env(app.root, app.environment) end - def initialize(context) + def initialize(context) # rubocop:disable Lint/MissingSuper @context = context @request = context.request end def context_for_eval - context || fail('no context set for evaluation the config file') + context || raise('no context set for evaluation the config file') end def request_uri @@ -32,9 +32,7 @@ def current_page?(url) request_uri.split('?').first end - if url_string =~ %r(^\w+://) - uri = "#{request.scheme}://#{request.host_with_port}#{uri}" - end + uri = "#{request.scheme}://#{request.host_with_port}#{uri}" if %r{^\w+://}.match?(url_string) url_string == CGI.unescape(uri) end diff --git a/lib/simple_navigation/config_file.rb b/lib/simple_navigation/config_file.rb index a392a9b7..92de1bd0 100644 --- a/lib/simple_navigation/config_file.rb +++ b/lib/simple_navigation/config_file.rb @@ -1,4 +1,4 @@ -require 'active_support/core_ext/string' +# frozen_string_literal: true module SimpleNavigation # Internal: Encapsulates the config file naming knowledge. diff --git a/lib/simple_navigation/config_file_finder.rb b/lib/simple_navigation/config_file_finder.rb index a82965f5..1004f92e 100644 --- a/lib/simple_navigation/config_file_finder.rb +++ b/lib/simple_navigation/config_file_finder.rb @@ -1,4 +1,4 @@ -require 'simple_navigation/config_file' +# frozen_string_literal: true module SimpleNavigation # Internal: Encapsulates the configuration file finding logic. @@ -22,8 +22,8 @@ def find(context) config_file_name = config_file_name_for_context(context) find_config_file(config_file_name) || - fail("Config file '#{config_file_name}' not found in " \ - "path(s) #{paths.join(', ')}!") + raise("Config file '#{config_file_name}' not found in " \ + "path(s) #{paths.join(', ')}!") end private diff --git a/lib/simple_navigation/configuration.rb b/lib/simple_navigation/configuration.rb index 3126dab7..b0478bca 100644 --- a/lib/simple_navigation/configuration.rb +++ b/lib/simple_navigation/configuration.rb @@ -1,4 +1,4 @@ -require 'singleton' +# frozen_string_literal: true module SimpleNavigation # Responsible for evaluating and handling the config/navigation.rb file. @@ -27,8 +27,8 @@ def self.eval_config(navigation_context = :default) end # Starts processing the configuration - def self.run(&block) - block.call Configuration.instance + def self.run + yield Configuration.instance end # Sets the config's default-settings @@ -74,14 +74,12 @@ def id_generator # See SimpleNavigation::ItemAdapter for more details. # def items(items_provider = nil, &block) - if (items_provider && block) || (items_provider.nil? && block.nil?) - fail('please specify either items_provider or block, but not both') - end + raise('please specify either items_provider or block, but not both') if (items_provider && block) || (items_provider.nil? && block.nil?) self.primary_navigation = ItemContainer.new if block - block.call primary_navigation + yield primary_navigation else primary_navigation.items = ItemsProvider.new(items_provider).items end diff --git a/lib/simple_navigation/helpers.rb b/lib/simple_navigation/helpers.rb index 8f5454db..fc13341f 100644 --- a/lib/simple_navigation/helpers.rb +++ b/lib/simple_navigation/helpers.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module SimpleNavigation # View helpers to render the navigation. # @@ -30,14 +32,12 @@ def self.load_config(options, includer, &block) SimpleNavigation.load_config context SimpleNavigation::Configuration.eval_config context - if block_given? || options[:items] - SimpleNavigation.config.items(options[:items], &block) - end + SimpleNavigation.config.items(options[:items], &block) if block_given? || options[:items] - unless SimpleNavigation.primary_navigation - fail 'no primary navigation defined, either use a navigation config ' \ - 'file or pass items directly to render_navigation' - end + return if SimpleNavigation.primary_navigation + + raise 'no primary navigation defined, either use a navigation config ' \ + 'file or pass items directly to render_navigation' end def self.apply_defaults(options) @@ -85,7 +85,7 @@ def self.apply_defaults(options) # def render_navigation(options = {}, &block) container = active_navigation_item_container(options, &block) - container && container.render(options) + container&.render(options) end # Returns the name of the currently active navigation item belonging to the @@ -138,9 +138,7 @@ def active_navigation_item_key(options = {}) # by default) if no active item can be found for the specified # options def active_navigation_item(options = {}, value_for_nil = nil) - if options[:level].nil? || options[:level] == :all - options[:level] = :leaves - end + options[:level] = :leaves if options[:level].nil? || options[:level] == :all container = active_navigation_item_container(options) if container && (item = container.selected_item) block_given? ? yield(item) : item diff --git a/lib/simple_navigation/item.rb b/lib/simple_navigation/item.rb index 49235406..580428f3 100644 --- a/lib/simple_navigation/item.rb +++ b/lib/simple_navigation/item.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + module SimpleNavigation # Represents an item in your navigation. # Gets generated by the item method in the config-file. - class Item + class Item # rubocop:disable Metrics/ClassLength attr_reader :key, :name, :sub_navigation, @@ -27,7 +29,7 @@ def initialize(container, key, name, url = nil, opts = {}, &sub_nav_block) # the name will be passed to the name_generator specified # in the configuration. # - def name(options = {}) + def name(options = {}) # rubocop:disable Lint/DuplicateMethods options = { apply_generator: true }.merge(options) if options[:apply_generator] config.name_generator.call(@name, self) @@ -50,7 +52,7 @@ def selected? # for this item in the config-file. # It also adds the 'selected' class to the list of classes if necessary. def html_options - html_opts = options.fetch(:html) { Hash.new } + html_opts = options.fetch(:html) { {} } html_opts[:id] ||= autogenerated_item_id classes = [html_opts[:class], selected_class, active_leaf_class] @@ -63,17 +65,17 @@ def html_options # Returns the configured active_leaf_class if the item is the selected leaf, # nil otherwise def active_leaf_class - if !selected_by_subnav? && selected_by_condition? - config.active_leaf_class - end + return unless !selected_by_subnav? && selected_by_condition? + + config.active_leaf_class end # Returns the configured selected_class if the item is selected, # nil otherwise def selected_class - if selected? - container.selected_class || config.selected_class - end + return unless selected? + + container.selected_class || config.selected_class end # Returns the :highlights_on option as set at initialization @@ -97,7 +99,7 @@ def link_html_options # Returns true if item has a subnavigation and # the sub_navigation is selected def selected_by_subnav? - sub_navigation && sub_navigation.selected? + sub_navigation&.selected? end # Returns true if the item's url matches the request's current url. @@ -139,26 +141,26 @@ def request_uri end def remove_anchors(url_with_anchors) - url_with_anchors && url_with_anchors.split('#').first + url_with_anchors&.split('#')&.first end def remove_query_params(url_with_params) - url_with_params && url_with_params.split('?').first + url_with_params&.split('?')&.first end def url_for_autohighlight - relevant_url = remove_anchors(self.url) if config.ignore_anchors_on_auto_highlight + relevant_url = remove_anchors(url) if config.ignore_anchors_on_auto_highlight relevant_url = remove_query_params(relevant_url) if config.ignore_query_params_on_auto_highlight relevant_url end def selected_by_autohighlight? return false unless auto_highlight? - return false unless self.url + return false unless url root_path_match? || - (url_for_autohighlight && SimpleNavigation.current_page?(url_for_autohighlight)) || - autohighlight_by_subpath? + (url_for_autohighlight && SimpleNavigation.current_page?(url_for_autohighlight)) || + autohighlight_by_subpath? end def autohighlight_by_subpath? @@ -171,13 +173,13 @@ def selected_by_highlights_on? when Proc then highlights_on.call when :subpath then selected_by_subpath? else - fail ArgumentError, ':highlights_on must be a Regexp, Proc or :subpath' + raise ArgumentError, ':highlights_on must be a Regexp, Proc or :subpath' end end def selected_by_subpath? escaped_url = Regexp.escape(url_for_autohighlight) - !!(request_uri =~ /^#{escaped_url}(\/|$||\?)/i) + !!(request_uri =~ %r{^#{escaped_url}(/|$||\?)}i) end def setup_sub_navigation(items = nil, &sub_nav_block) @@ -186,7 +188,7 @@ def setup_sub_navigation(items = nil, &sub_nav_block) self.sub_navigation = ItemContainer.new(container.level + 1) if sub_nav_block - sub_nav_block.call sub_navigation + yield sub_navigation else sub_navigation.items = items end diff --git a/lib/simple_navigation/item_adapter.rb b/lib/simple_navigation/item_adapter.rb index 0593a8a8..7a0c6000 100644 --- a/lib/simple_navigation/item_adapter.rb +++ b/lib/simple_navigation/item_adapter.rb @@ -1,5 +1,4 @@ -require 'forwardable' -require 'ostruct' +# frozen_string_literal: true module SimpleNavigation # This class acts as an adapter to items that are not defined using the DSL @@ -30,20 +29,32 @@ class ItemAdapter attr_reader :item + class Item + attr_reader :key, :name, :url, :options, :items + + def initialize(item) + @key = item[:key] + @name = item[:name] + @url = item[:url] + @options = item[:options] || {} + @items = item[:items] || [] + end + end + def initialize(item) - @item = item.is_a?(Hash) ? OpenStruct.new(item) : item + @item = item.is_a?(Hash) ? Item.new(item) : item end # Returns the options for this item. If the wrapped item does not implement # an options method, an empty hash is returned. def options - item.respond_to?(:options) ? item.options : {} + item.options end # Returns the items (subnavigation) for this item if it responds to :items # and the items-collection is not empty. Returns nil otherwise. def items - item.items if item.respond_to?(:items) && item.items && item.items.any? + item.items if item.items&.any? end # Converts this Item into a SimpleNavigation::Item diff --git a/lib/simple_navigation/item_container.rb b/lib/simple_navigation/item_container.rb index eebfde3c..0e9a3b39 100644 --- a/lib/simple_navigation/item_container.rb +++ b/lib/simple_navigation/item_container.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + module SimpleNavigation # Holds the Items for a navigation 'level'. - class ItemContainer + class ItemContainer # rubocop:disable Metrics/ClassLength attr_accessor :auto_highlight, :dom_class, :dom_id, @@ -11,7 +13,7 @@ class ItemContainer attr_writer :dom_attributes - def initialize(level = 1) #:nodoc: + def initialize(level = 1) # :nodoc: @level = level @items ||= [] @renderer = SimpleNavigation.config.renderer @@ -24,7 +26,7 @@ def dom_attributes dom_id_and_class = { id: dom_id, class: dom_class - }.reject { |_, v| v.nil? } + }.compact @dom_attributes.merge(dom_id_and_class) end @@ -63,6 +65,7 @@ def dom_attributes # The block - if specified - will hold the item's sub_navigation. def item(key, name, url = nil, options = {}, &block) return unless should_add_item?(options) + item = Item.new(self, key, name, url, options, &block) add_item item, options end @@ -71,6 +74,7 @@ def items=(new_items) new_items.each do |item| item_adapter = ItemAdapter.new(item) next unless should_add_item?(item_adapter.options) + add_item item_adapter.to_simple_navigation_item(self), item_adapter.options end end @@ -91,10 +95,11 @@ def level_for_item(navi_key) items.each do |item| next unless item.sub_navigation + level = item.sub_navigation.level_for_item(navi_key) return level if level end - return nil + nil end # Renders the items in this ItemContainer using the configured renderer. @@ -152,7 +157,8 @@ def add_item(item, options) end def modify_dom_attributes(options) - return unless container_options = options[:container] + return unless (container_options = options[:container]) + self.dom_attributes = container_options.fetch(:attributes) { dom_attributes } self.dom_class = container_options.fetch(:class) { dom_class } self.dom_id = container_options.fetch(:id) { dom_id } @@ -178,13 +184,13 @@ def selected_sub_navigation? def should_add_item?(options) [options[:if]].flatten.compact.all? { |m| evaluate_method(m) } && - [options[:unless]].flatten.compact.none? { |m| evaluate_method(m) } + [options[:unless]].flatten.compact.none? { |m| evaluate_method(m) } end def evaluate_method(method) case method when Proc, Method then method.call - else fail(ArgumentError, ':if or :unless must be procs or lambdas') + else raise(ArgumentError, ':if or :unless must be procs or lambdas') end end end diff --git a/lib/simple_navigation/items_provider.rb b/lib/simple_navigation/items_provider.rb index 30d56c90..3de1eaa3 100644 --- a/lib/simple_navigation/items_provider.rb +++ b/lib/simple_navigation/items_provider.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module SimpleNavigation # Acts as a proxy to navigation items that are passed into the # SimpleNavigation::Configuration#items method. @@ -20,7 +22,7 @@ def initialize(provider) end # Returns the navigation items - def items + def items # rubocop:disable Metrics/MethodLength if provider.is_a?(Symbol) SimpleNavigation.context_for_eval.send(provider) elsif provider.respond_to?(:items) @@ -28,9 +30,9 @@ def items elsif provider.respond_to?(:each) provider else - fail('items_provider either must be a symbol specifying the ' \ - 'helper-method to call, an object with an items-method defined ' \ - 'or an enumerable representing the items') + raise('items_provider either must be a symbol specifying the ' \ + 'helper-method to call, an object with an items-method defined ' \ + 'or an enumerable representing the items') end end end diff --git a/lib/simple_navigation/railtie.rb b/lib/simple_navigation/railtie.rb index 473a2cf5..5a50b49c 100644 --- a/lib/simple_navigation/railtie.rb +++ b/lib/simple_navigation/railtie.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + module SimpleNavigation class Railtie < ::Rails::Railtie - initializer 'simple_navigation.register' do |app| + initializer 'simple_navigation.register' do |_app| SimpleNavigation.register end end diff --git a/lib/simple_navigation/renderer.rb b/lib/simple_navigation/renderer.rb deleted file mode 100644 index ebbe3207..00000000 --- a/lib/simple_navigation/renderer.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'simple_navigation/helpers' -require 'simple_navigation/renderer/base' - -module SimpleNavigation - module Renderer - autoload :List, 'simple_navigation/renderer/list' - autoload :Links, 'simple_navigation/renderer/links' - autoload :Breadcrumbs, 'simple_navigation/renderer/breadcrumbs' - autoload :Text, 'simple_navigation/renderer/text' - autoload :Json, 'simple_navigation/renderer/json' - end -end diff --git a/lib/simple_navigation/renderer/base.rb b/lib/simple_navigation/renderer/base.rb index 85625fef..5d3ce7d3 100644 --- a/lib/simple_navigation/renderer/base.rb +++ b/lib/simple_navigation/renderer/base.rb @@ -1,4 +1,4 @@ -require 'forwardable' +# frozen_string_literal: true module SimpleNavigation module Renderer @@ -13,7 +13,7 @@ class Base def_delegators :adapter, :link_to, :content_tag - def initialize(options) #:nodoc: + def initialize(options) # :nodoc: @options = options @adapter = SimpleNavigation.adapter end @@ -44,7 +44,7 @@ def render_sub_navigation_for(item) # include_sub_navigation? to determine whether an item's sub_navigation # should be rendered or not. def render(item_container) - fail NotImplementedError, 'subclass responsibility' + raise NotImplementedError, 'subclass responsibility' end protected @@ -89,11 +89,11 @@ def options_for(item) end # Extracts the options relevant for the generated link - def link_options_for(item) + def link_options_for(item) # rubocop:disable Metrics/MethodLength special_options = { method: item.method, class: item.selected_class - }.reject { |_, v| v.nil? } + }.compact link_options = item.link_html_options diff --git a/lib/simple_navigation/renderer/breadcrumbs.rb b/lib/simple_navigation/renderer/breadcrumbs.rb index c2e64800..3537411b 100644 --- a/lib/simple_navigation/renderer/breadcrumbs.rb +++ b/lib/simple_navigation/renderer/breadcrumbs.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module SimpleNavigation module Renderer # Renders an ItemContainer as a
element and its containing items as @@ -25,11 +27,10 @@ def render(item_container) def a_tags(item_container) item_container.items.each_with_object([]) do |item, list| next unless item.selected? + list << tag_for(item) - if include_sub_navigation?(item) - list.concat a_tags(item.sub_navigation) - end + list.concat a_tags(item.sub_navigation) if include_sub_navigation?(item) end end diff --git a/lib/simple_navigation/renderer/json.rb b/lib/simple_navigation/renderer/json.rb index 31dba364..d546bfd7 100644 --- a/lib/simple_navigation/renderer/json.rb +++ b/lib/simple_navigation/renderer/json.rb @@ -1,4 +1,4 @@ -require 'json' +# frozen_string_literal: true module SimpleNavigation module Renderer diff --git a/lib/simple_navigation/renderer/links.rb b/lib/simple_navigation/renderer/links.rb index 0dbc8f42..281739a5 100644 --- a/lib/simple_navigation/renderer/links.rb +++ b/lib/simple_navigation/renderer/links.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module SimpleNavigation module Renderer # Renders an ItemContainer as a
element and its containing items as diff --git a/lib/simple_navigation/renderer/list.rb b/lib/simple_navigation/renderer/list.rb index f37bbe7a..a681c9c2 100644 --- a/lib/simple_navigation/renderer/list.rb +++ b/lib/simple_navigation/renderer/list.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module SimpleNavigation module Renderer # Renders an ItemContainer as a