Skip to content

Commit

Permalink
Fix more ruby style issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hoppergee committed Jul 28, 2021
1 parent b8478a0 commit aa45ffa
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lib/loaf/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
16 changes: 14 additions & 2 deletions lib/loaf/view_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/integration/breadcrumb_trail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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('<a href="/onboard">Onboard</a>')
end
page.within "#breadcrumbs .selected" do
Expand Down
21 changes: 14 additions & 7 deletions spec/rails_app/app/controllers/onboard_controller.rb
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
14 changes: 7 additions & 7 deletions spec/rails_app/config/routes.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion spec/unit/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
expect(config.to_hash).to eq({
locales_path: "/",
match: :inclusive,
http_verbs: [:get, :head]
http_verbs: %i[get head]
})
end

Expand Down

0 comments on commit aa45ffa

Please sign in to comment.