Skip to content

Commit 90a5209

Browse files
committed
Enhance permitted attributes for navigation areas and items, ensuring persistence of core fields in controller specs
1 parent ce8aa22 commit 90a5209

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

app/models/better_together/navigation_area.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,16 @@ def top_level_nav_items_includes_children
4747
def to_s
4848
name
4949
end
50+
51+
def self.permitted_attributes(id: false, destroy: false)
52+
# Allow core fields for creating/updating navigation areas
53+
attrs = %i[
54+
name
55+
style
56+
visible
57+
]
58+
59+
super + attrs
60+
end
5061
end
5162
end

spec/requests/better_together/navigation_areas_controller_spec.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
describe 'POST /:locale/.../navigation_areas' do
2222
# rubocop:todo RSpec/MultipleExpectations
23-
it 'creates and redirects on valid params' do # rubocop:todo RSpec/ExampleLength, RSpec/MultipleExpectations
23+
it 'creates and redirects on valid params, persisting permitted fields' do # rubocop:todo RSpec/ExampleLength, RSpec/MultipleExpectations
2424
# rubocop:enable RSpec/MultipleExpectations
2525
post better_together.navigation_areas_path(locale:), params: {
2626
navigation_area: {
@@ -33,6 +33,11 @@
3333
expect(response).to have_http_status(:found)
3434
follow_redirect!
3535
expect(response).to have_http_status(:ok)
36+
37+
area = BetterTogether::NavigationArea.find_by(identifier: 'main-nav')
38+
expect(area).to be_present
39+
expect(area.style).to eq('primary')
40+
expect(area.visible).to eq(true)
3641
end
3742

3843
it 'renders new on invalid params (HTML 200)' do
@@ -45,14 +50,17 @@
4550
let!(:area) { create(:better_together_navigation_area, protected: false) }
4651

4752
# rubocop:todo RSpec/MultipleExpectations
48-
it 'updates and redirects on valid params' do # rubocop:todo RSpec/ExampleLength, RSpec/MultipleExpectations
53+
it 'updates and redirects on valid params, applying changes' do # rubocop:todo RSpec/ExampleLength, RSpec/MultipleExpectations
4954
# rubocop:enable RSpec/MultipleExpectations
5055
patch better_together.navigation_area_path(locale:, id: area.slug), params: {
51-
navigation_area: { style: 'secondary' }
56+
navigation_area: { style: 'secondary', visible: false }
5257
}
5358
expect(response).to have_http_status(:found)
5459
follow_redirect!
5560
expect(response).to have_http_status(:ok)
61+
62+
expect(area.reload.style).to eq('secondary')
63+
expect(area.reload.visible).to eq(false)
5664
end
5765

5866
it 'renders edit on invalid params (HTML 200)' do

spec/requests/better_together/navigation_items_controller_spec.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
end
3131

3232
# rubocop:todo RSpec/MultipleExpectations
33-
it 'creates a navigation item and redirects (HTML)' do # rubocop:todo RSpec/ExampleLength, RSpec/MultipleExpectations
33+
it 'creates a navigation item and redirects (HTML), persisting permitted fields' do # rubocop:todo RSpec/ExampleLength, RSpec/MultipleExpectations
3434
# rubocop:enable RSpec/MultipleExpectations
3535
post better_together.navigation_area_navigation_items_path(
3636
locale:,
@@ -40,6 +40,15 @@
4040
expect(response).to have_http_status(:found)
4141
follow_redirect!
4242
expect(response).to have_http_status(:ok)
43+
44+
# Verify attributes were persisted via strong params
45+
created = BetterTogether::NavigationItem.order(:created_at).last
46+
expect(created).to be_present
47+
expect(created.navigation_area_id).to eq(navigation_area.id)
48+
expect(created.title(locale:)).to eq(params[:navigation_item]["title_#{locale}"])
49+
expect(created.url).to eq(params[:navigation_item][:url])
50+
expect(created.visible).to eq(params[:navigation_item][:visible])
51+
expect(created.item_type).to eq(params[:navigation_item][:item_type])
4352
end
4453

4554
it 'renders errors on invalid params' do
@@ -62,7 +71,7 @@
6271
end
6372

6473
# rubocop:todo RSpec/MultipleExpectations
65-
it 'updates with valid params then redirects' do # rubocop:todo RSpec/ExampleLength, RSpec/MultipleExpectations
74+
it 'updates with valid params then redirects and applies changes' do # rubocop:todo RSpec/ExampleLength, RSpec/MultipleExpectations
6675
# rubocop:enable RSpec/MultipleExpectations
6776
put better_together.navigation_area_navigation_item_path(
6877
locale:,
@@ -73,6 +82,8 @@
7382
expect(response).to have_http_status(:found)
7483
follow_redirect!
7584
expect(response).to have_http_status(:ok)
85+
86+
expect(item.reload.title(locale:)).to eq('Updated Title')
7687
end
7788

7889
it 'renders edit on invalid params (422)' do # rubocop:todo RSpec/ExampleLength

0 commit comments

Comments
 (0)