Skip to content

Commit

Permalink
Change http_verbs to request_methods
Browse files Browse the repository at this point in the history
  • Loading branch information
hoppergee committed Oct 12, 2021
1 parent e28ea0f commit 72b1590
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 32 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ rails generate loaf:install
* [2.1.1 controller](#211-controller)
* [2.1.2 view](#212-view)
* [2.1.3 :match](#213-match)
* [2.1.4 :http_verbs](#214-http_verbs)
* [2.1.4 :request_methods](#214-request_methods)
* [2.2 breadcrumb_trail](#22-breadcrumb_trail)
* [3. Configuration](#3-configuration)
* [4. Translation](#4-translation)
Expand Down Expand Up @@ -208,11 +208,11 @@ To make a breadcrumb current based on the query parameters do:
breadcrumb "Posts", posts_path(order: :desc), match: {order: :desc}
```
#### 2.1.4 :http_verbs
#### 2.1.4 :request_methods
**Loaf** allows you to match on multiple HTTP verbs in order to make a breadcrumb current with the `:http_verbs` option.
**Loaf** allows you to match on multiple HTTP methods in order to make a breadcrumb current with the `:request_methods` option.
The `:http_verbs` key accepts `:all` or an array with following values:
The `:request_methods` key accepts `:all` or an array with following values:
* `:get`
* `:post`
Expand All @@ -230,9 +230,9 @@ Its default value is `%i[get head]`
For example:
```ruby
http_verbs: %i[get head]
http_verbs: %i[post get head]
http_verbs: :all
request_methods: %i[get head]
request_methods: %i[post get head]
request_methods: :all
```
### 2.2 breadcrumb_trail
Expand Down
6 changes: 3 additions & 3 deletions lib/loaf/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
module Loaf
class Configuration
VALID_ATTRIBUTES = [
:http_verbs,
:locales_path,
:match
:match,
:request_methods
].freeze

attr_accessor(*VALID_ATTRIBUTES)
Expand All @@ -14,7 +14,7 @@ class Configuration

DEFAULT_MATCH = :inclusive

DEFAULT_HTTP_VERBS = %i[get head].freeze
DEFAULT_REQUEST_METHODS = %i[get head].freeze

# Setup this configuration
#
Expand Down
4 changes: 2 additions & 2 deletions lib/loaf/crumb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class Crumb

attr_reader :match

attr_reader :http_verbs
attr_reader :request_methods

def initialize(name, url, options = {})
@name = name || raise_name_error
@url = url || raise_url_error
@match = options.fetch(:match, Loaf.configuration.match)
@http_verbs = options.fetch(:http_verbs, Loaf.configuration.http_verbs)
@request_methods = options.fetch(:request_methods, Loaf.configuration.request_methods)
freeze
end

Expand Down
16 changes: 8 additions & 8 deletions lib/loaf/view_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def breadcrumb_trail(options = {})
current = current_crumb?(
path,
options.fetch(:match) { crumb.match },
http_verbs: options.fetch(:http_verbs) { crumb.http_verbs }
request_methods: options.fetch(:request_methods) { crumb.request_methods }
)

yield(Loaf::Breadcrumb[name, path, current])
Expand All @@ -69,8 +69,8 @@ def breadcrumb_trail(options = {})
# the pattern to match on
#
# @api public
def current_crumb?(path, pattern = :inclusive, http_verbs: nil)
return false unless match_http_verbs(http_verbs)
def current_crumb?(path, pattern = :inclusive, request_methods: nil)
return false unless match_request_methods(request_methods)

origin_path = URI::DEFAULT_PARSER.unescape(path).force_encoding(Encoding::BINARY)

Expand Down Expand Up @@ -133,16 +133,16 @@ def _expand_url(url)
end
end

# Check if the HTTP verbs are allowed
# Check if the HTTP request methods are allowed
#
# @retun [Boolean]
#
# @api private
def match_http_verbs(http_verbs)
http_verbs ||= Loaf.configuration.http_verbs
return true if http_verbs == :all
def match_request_methods(request_methods)
request_methods ||= Loaf.configuration.request_methods
return true if request_methods == :all

http_verbs.any? { |verb| request.try("#{verb}?") }
request_methods.any? { |method| request.try("#{method}?") }
end
end # ViewExtensions
end # Loaf
12 changes: 6 additions & 6 deletions spec/rails_app/app/controllers/onboard_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ def setup
case params[:step]
when "1"
breadcrumb "Step 1", onboard_step_path(step: 1),
match: :exact, http_verbs: %i[get]
match: :exact, request_methods: %i[get]
render :step1
when "2"
breadcrumb "Step 2", onboard_step_path(step: 2),
match: :exact, http_verbs: %i[get post]
match: :exact, request_methods: %i[get post]
render :step2
when "3"
breadcrumb "Step 3", onboard_step_path(step: 3),
match: :exact, http_verbs: %i[get put]
match: :exact, request_methods: %i[get put]
render :step3
when "4"
breadcrumb "Step 4", onboard_step_path(step: 4),
match: :exact, http_verbs: %i[get patch]
match: :exact, request_methods: %i[get patch]
render :step4
when "5"
breadcrumb "Step 5", onboard_step_path(step: 5),
match: :exact, http_verbs: %i[get delete]
match: :exact, request_methods: %i[get delete]
render :step5
when "6"
breadcrumb "Step 6", onboard_step_path(step: 6),
match: :exact, http_verbs: :all
match: :exact, request_methods: :all
render :step6
else
render :setup
Expand Down
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: %i[get head]
request_methods: %i[get head]
})
end

Expand Down
10 changes: 5 additions & 5 deletions spec/unit/view_extensions/breadcrumb_trail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,17 +281,17 @@
expect(view.breadcrumb_trail(match: :exact).map(&:current?)).to eq([false, false, true])
end

it "match current path with :http_verbs" do
it "match current path with :request_methods" do
view = DummyView.new
view.breadcrumb("posts", "/posts", http_verbs: %i[get post])
view.breadcrumb("posts", "/posts", request_methods: %i[get post])
view.set_path("/posts")
view.set_request_method(:post)

trail = view.breadcrumb_trail.map(&:to_a)
expect(trail).to eq([["posts", "/posts", true]])
end

it "fail to match current path with :http_verbs" do
it "fail to match current path with :request_methods" do
view = DummyView.new
view.breadcrumb("posts", "/posts")
view.set_path("/posts")
Expand All @@ -301,9 +301,9 @@
expect(trail).to eq([["posts", "/posts", false]])
end

it "match current path with :http_verbs => :all" do
it "match current path with :request_methods => :all" do
view = DummyView.new
view.breadcrumb("posts", "/posts", http_verbs: :all)
view.breadcrumb("posts", "/posts", request_methods: :all)
view.set_path("/posts")
view.set_request_method(:delete)

Expand Down

0 comments on commit 72b1590

Please sign in to comment.