Skip to content

Commit 7a0392b

Browse files
committed
Refactor title methods in NavigationItem to remove locale parameter and improve fallback logic
1 parent 57b7b02 commit 7a0392b

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

app/models/better_together/navigation_item.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,16 @@ def set_position
180180
max_position ? max_position + 1 : 0
181181
end
182182

183-
def title(options = {}, locale: I18n.locale)
183+
def title(options = {})
184184
return linkable.title(**options) if linkable.present? && linkable.respond_to?(:title)
185185

186-
super(**options, locale:)
186+
super(**options)
187187
end
188188

189-
def title=(arg, options = {}, locale: I18n.locale)
189+
def title=(arg, options = {})
190190
linkable.public_send :title=, arg, locale: locale, **options if linkable.present? && linkable.respond_to?(:title=)
191191

192-
super(arg, locale:, **options)
192+
super(arg, **options)
193193
end
194194

195195
def to_s

spec/models/better_together/navigation_item_spec.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,61 @@
11
# frozen_string_literal: true
22

3+
require 'rails_helper'
4+
5+
RSpec.describe BetterTogether::NavigationItem, type: :model do
6+
let(:navigation_area) { create(:navigation_area) }
7+
8+
context 'title fallbacks' do
9+
it 'returns nav item translation when present' do
10+
nav = described_class.build(navigation_area:, title: 'Nav Title', slug: 'nav-title', visible: true)
11+
12+
expect(nav.title).to eq('Nav Title')
13+
end
14+
15+
it 'falls back to linkable title when nav item title blank and linkable present' do
16+
page = create(:page, title: 'Page Title')
17+
nav = described_class.build(navigation_area:, title: '', slug: 'nav-title', visible: true, linkable: page)
18+
19+
expect(nav.title).to eq('Page Title')
20+
end
21+
22+
it 'returns blank when nav item title blank and no linkable' do
23+
nav = described_class.build(navigation_area:, title: '', slug: 'nav-title', visible: true)
24+
25+
expect(nav.title).to be_blank
26+
end
27+
28+
it 'prefers linkable title when set' do
29+
page = create(:page, title: 'Page Title')
30+
nav = described_class.build(navigation_area:, title: 'Nav Title', slug: 'nav-title', visible: true,
31+
linkable: page)
32+
33+
expect(nav.title).to eq('Page Title')
34+
end
35+
36+
it 'returns translation for requested locale when available' do
37+
I18n.with_locale(:es) do
38+
nav = described_class.build(navigation_area:, title: 'Título Nav', slug: 'nav-title', visible: true)
39+
40+
expect(nav.title(locale: :es)).to eq('Título Nav')
41+
end
42+
end
43+
44+
it 'falls back to linkable translation for a missing nav translation' do
45+
page = create(:page)
46+
# set page spanish title
47+
page.public_send(:title=, 'Título Página', locale: :es)
48+
49+
nav = described_class.build(navigation_area:, title: '', slug: 'nav-title', visible: true, linkable: page)
50+
51+
I18n.with_locale(:es) do
52+
expect(nav.title(locale: :es)).to eq('Título Página')
53+
end
54+
end
55+
end
56+
end
57+
# frozen_string_literal: true
58+
359
# spec/models/better_together/navigation_item_spec.rb
460

561
require 'rails_helper'

0 commit comments

Comments
 (0)