Skip to content

Commit 509b23b

Browse files
committed
- Refactor breadcrumbs data structure
1 parent 6ecf43d commit 509b23b

3 files changed

Lines changed: 16 additions & 21 deletions

File tree

app/views/_breadcrumbs.slim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
- unless breadcrumbs.empty?
22
.breadcrumbs
33
- breadcrumbs.each do |item|
4-
- if item.last
5-
span = item.label
6-
- else
4+
- if item.href
75
a href="#{item.href}" = item.label
86
= " / "
7+
- else
8+
span = item.label

lib/madness/breadcrumbs.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module Madness
44
class Breadcrumbs
55
using StringRefinements
66

7+
Breadcrumb = Struct.new :label, :href, keyword_init: true
8+
79
attr_reader :path
810

911
def initialize(path)
@@ -17,20 +19,17 @@ def links
1719
private
1820

1921
def breadcrumbs
20-
home = OpenStruct.new({ label: 'Home', href: "#{config.base_uri}/" })
22+
home = Breadcrumb.new label: 'Home', href: "#{config.base_uri}/"
2123
result = breadcrumbs_maker(path).reverse.unshift home
22-
result.last.last = true
24+
result.last.href = nil
2325
result
2426
end
2527

2628
def breadcrumbs_maker(partial_path)
2729
parent, basename = File.split partial_path
28-
item = OpenStruct.new(
29-
{
30-
label: basename.to_label,
31-
href: "#{config.base_uri}/#{partial_path}/",
32-
}
33-
)
30+
href = "#{config.base_uri}/#{partial_path}"
31+
href = "#{href}/" unless href.end_with? '/'
32+
item = Breadcrumb.new label: basename.to_label, href: href
3433
result = [item]
3534
result += breadcrumbs_maker parent unless parent == '.'
3635
result

spec/madness/breadcrumbs_spec.rb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,24 @@
44
describe '#links' do
55
let(:links) { subject.links }
66

7-
it 'returns an array of OpenStructs' do
7+
it 'returns an array of Breadcrumb structs' do
88
expect(links).to be_an Array
99
expect(links.count).to eq 4
10-
expect(links.first).to be_an OpenStruct
10+
expect(links.first).to be_a described_class::Breadcrumb
1111
end
1212

1313
it 'adds a home link' do
1414
expect(links.first.label).to eq 'Home'
1515
expect(links.first.href).to eq '/'
1616
end
1717

18-
it 'adds a link to each element' do
18+
it 'adds a link to all elements except the last one' do
1919
expect(links[1].label).to eq 'one'
2020
expect(links[1].href).to eq '/one/'
2121
expect(links[2].label).to eq 'two'
2222
expect(links[2].href).to eq '/one/two/'
2323
expect(links[3].label).to eq 'three'
24-
expect(links[3].href).to eq '/one/two/three/'
25-
end
26-
27-
it 'adds a last attribute to last element' do
28-
expect(links.last.last).to be true
24+
expect(links[3].href).to be_nil
2925
end
3026

3127
context 'with sorted elements' do
@@ -37,7 +33,7 @@
3733
expect(links[2].label).to eq 'two'
3834
expect(links[2].href).to eq '/1. one/2. two/'
3935
expect(links[3].label).to eq 'three'
40-
expect(links[3].href).to eq '/1. one/2. two/3. three/'
36+
expect(links[3].href).to be_nil
4137
end
4238
end
4339

@@ -48,7 +44,7 @@
4844
it 'prepends the links with the base_uri' do
4945
expect(links[1].href).to eq '/docs/one/'
5046
expect(links[2].href).to eq '/docs/one/two/'
51-
expect(links[3].href).to eq '/docs/one/two/three/'
47+
expect(links[3].href).to be_nil
5248
end
5349
end
5450
end

0 commit comments

Comments
 (0)