From aa45ffa3d9ab03d25d2d3efbd50b07b2623e3151 Mon Sep 17 00:00:00 2001 From: Hopper Gee Date: Wed, 28 Jul 2021 09:11:01 +0800 Subject: [PATCH] Fix more ruby style issues --- README.md | 2 +- lib/loaf/configuration.rb | 2 +- lib/loaf/view_extensions.rb | 16 ++++++++++++-- spec/integration/breadcrumb_trail_spec.rb | 2 +- .../app/controllers/onboard_controller.rb | 21 ++++++++++++------- spec/rails_app/config/routes.rb | 14 ++++++------- spec/unit/configuration_spec.rb | 2 +- 7 files changed, 39 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 7cfb15e..82aded2 100644 --- a/README.md +++ b/README.md @@ -225,7 +225,7 @@ The `:http_verbs` key accepts `:all` or an array with following values: * `:unlink` * `:trace` -Its default value is `[:get, :head]` +Its default value is `%i[get head]` For example: diff --git a/lib/loaf/configuration.rb b/lib/loaf/configuration.rb index 25d69e2..28d7535 100644 --- a/lib/loaf/configuration.rb +++ b/lib/loaf/configuration.rb @@ -14,7 +14,7 @@ class Configuration DEFAULT_MATCH = :inclusive - DEFAULT_HTTP_VERBS = [:get, :head].freeze + DEFAULT_HTTP_VERBS = %i[get head].freeze # Setup this configuration # diff --git a/lib/loaf/view_extensions.rb b/lib/loaf/view_extensions.rb index 9cd87d8..f0c2a9a 100644 --- a/lib/loaf/view_extensions.rb +++ b/lib/loaf/view_extensions.rb @@ -69,8 +69,8 @@ def breadcrumb_trail(options = {}) # the pattern to match on # # @api public - def current_crumb?(path, pattern = :inclusive, http_verbs: Loaf.configuration.http_verbs) - return false unless http_verbs == :all || http_verbs.any? {|verb| request.try("#{verb}?")} + def current_crumb?(path, pattern = :inclusive, http_verbs: nil) + return false unless match_http_verbs(http_verbs) origin_path = URI::DEFAULT_PARSER.unescape(path).force_encoding(Encoding::BINARY) @@ -132,5 +132,17 @@ def _expand_url(url) url end end + + # Check if the HTTP verbs are allowed + # + # @retun [Boolean] + # + # @api private + def match_http_verbs(http_verbs) + http_verbs ||= Loaf.configuration.http_verbs + return true if http_verbs == :all + + http_verbs.any? { |verb| request.try("#{verb}?") } + end end # ViewExtensions end # Loaf diff --git a/spec/integration/breadcrumb_trail_spec.rb b/spec/integration/breadcrumb_trail_spec.rb index 90e9039..275c78c 100644 --- a/spec/integration/breadcrumb_trail_spec.rb +++ b/spec/integration/breadcrumb_trail_spec.rb @@ -81,7 +81,7 @@ click_link "Step 1" # GET expect(page).to have_selector("h1", text: "Step 1") - page.within '#breadcrumbs' do + page.within "#breadcrumbs" do expect(page.html).to include('Onboard') end page.within "#breadcrumbs .selected" do diff --git a/spec/rails_app/app/controllers/onboard_controller.rb b/spec/rails_app/app/controllers/onboard_controller.rb index a7a7757..cff3117 100644 --- a/spec/rails_app/app/controllers/onboard_controller.rb +++ b/spec/rails_app/app/controllers/onboard_controller.rb @@ -1,26 +1,33 @@ -class OnboardController < ApplicationController +# frozen_string_literal: true +class OnboardController < ApplicationController breadcrumb "Onboard", :onboard_path, match: :exact def setup case params[:step] when "1" - breadcrumb "Step 1", onboard_step_path(step: 1), match: :exact, http_verbs: [:get] + breadcrumb "Step 1", onboard_step_path(step: 1), + match: :exact, http_verbs: %i[get] render "step1" when "2" - breadcrumb "Step 2", onboard_step_path(step: 2), match: :exact, http_verbs: [:get, :post] + breadcrumb "Step 2", onboard_step_path(step: 2), + match: :exact, http_verbs: %i[get post] render "step2" when "3" - breadcrumb "Step 3", onboard_step_path(step: 3), match: :exact, http_verbs: [:get, :put] + breadcrumb "Step 3", onboard_step_path(step: 3), + match: :exact, http_verbs: %i[get put] render "step3" when "4" - breadcrumb "Step 4", onboard_step_path(step: 4), match: :exact, http_verbs: [:get, :patch] + breadcrumb "Step 4", onboard_step_path(step: 4), + match: :exact, http_verbs: %i[get patch] render "step4" when "5" - breadcrumb "Step 5", onboard_step_path(step: 5), match: :exact, http_verbs: [:get, :delete] + breadcrumb "Step 5", onboard_step_path(step: 5), + match: :exact, http_verbs: %i[get delete] render "step5" when "6" - breadcrumb "Step 6", onboard_step_path(step: 6), match: :exact, http_verbs: :all + breadcrumb "Step 6", onboard_step_path(step: 6), + match: :exact, http_verbs: :all render "step6" else render "setup" diff --git a/spec/rails_app/config/routes.rb b/spec/rails_app/config/routes.rb index 5d6c215..e4a2921 100644 --- a/spec/rails_app/config/routes.rb +++ b/spec/rails_app/config/routes.rb @@ -1,14 +1,14 @@ RailsApp::Application.routes.draw do - root :to => 'home#index' + root :to => "home#index" resources :posts do resources :comments end - get '/onboard', to: 'onboard#setup', as: :onboard - get '/onboard/step/:step', to: 'onboard#setup', as: :onboard_step - post '/onboard/step/:step', to: 'onboard#setup' - patch '/onboard/step/:step', to: 'onboard#setup' - put '/onboard/step/:step', to: 'onboard#setup' - delete '/onboard/step/:step', to: 'onboard#setup' + get "/onboard", to: "onboard#setup", as: :onboard + get "/onboard/step/:step", to: "onboard#setup", as: :onboard_step + post "/onboard/step/:step", to: "onboard#setup" + patch "/onboard/step/:step", to: "onboard#setup" + put "/onboard/step/:step", to: "onboard#setup" + delete "/onboard/step/:step", to: "onboard#setup" end diff --git a/spec/unit/configuration_spec.rb b/spec/unit/configuration_spec.rb index edb131c..ab08e03 100644 --- a/spec/unit/configuration_spec.rb +++ b/spec/unit/configuration_spec.rb @@ -20,7 +20,7 @@ expect(config.to_hash).to eq({ locales_path: "/", match: :inclusive, - http_verbs: [:get, :head] + http_verbs: %i[get head] }) end