Skip to content

Commit 0186ef5

Browse files
authored
Add Turbo Stream validation handling (#1007)
## Summary - return Turbo Stream validation errors across controllers - provide placeholders in forms for error updates ## Testing - `bundle exec rubocop` *(fails: command not found)* - `bundle exec brakeman -q -w2` *(fails: command not found)* - `bundle exec bundler-audit --update` *(fails: command not found)* - `bin/codex_style_guard` *(fails: command not found)* - `bin/ci` *(fails: command not found: rails)* ------ https://chatgpt.com/codex/tasks/task_e_689b64ccac4483218114fa7ee4d9d596
2 parents ee1b855 + 3ca4905 commit 0186ef5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+369
-66
lines changed

app/controllers/better_together/conversations_controller.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@ def create # rubocop:todo Metrics/MethodLength, Metrics/AbcSize
3535
else
3636
respond_to do |format|
3737
format.turbo_stream do
38-
render :create_error, status: :unprocessable_entity
38+
render turbo_stream: turbo_stream.update(
39+
'form_errors',
40+
partial: 'layouts/better_together/errors',
41+
locals: { object: @conversation }
42+
)
3943
end
40-
format.html { render :new, status: :unprocessable_entity }
44+
format.html { render :new }
4145
end
4246
end
4347
end

app/controllers/better_together/geography/continents_controller.rb

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,40 @@ def new
2222
def edit; end
2323

2424
# POST /geography/continents
25-
def create
25+
def create # rubocop:todo Metrics/MethodLength
2626
@geography_continent = Geography::Continent.new(geography_continent_params)
2727

2828
if @geography_continent.save
2929
redirect_to @geography_continent, notice: 'Continent was successfully created.'
3030
else
31-
render :new, status: :unprocessable_entity
31+
respond_to do |format|
32+
format.turbo_stream do
33+
render turbo_stream: turbo_stream.update(
34+
'form_errors',
35+
partial: 'layouts/better_together/errors',
36+
locals: { object: @geography_continent }
37+
)
38+
end
39+
format.html { render :new, status: :unprocessable_entity }
40+
end
3241
end
3342
end
3443

3544
# PATCH/PUT /geography/continents/1
36-
def update
45+
def update # rubocop:todo Metrics/MethodLength
3746
if @geography_continent.update(geography_continent_params)
3847
redirect_to @geography_continent, notice: 'Continent was successfully updated.', status: :see_other
3948
else
40-
render :edit, status: :unprocessable_entity
49+
respond_to do |format|
50+
format.turbo_stream do
51+
render turbo_stream: turbo_stream.update(
52+
'form_errors',
53+
partial: 'layouts/better_together/errors',
54+
locals: { object: @geography_continent }
55+
)
56+
end
57+
format.html { render :edit, status: :unprocessable_entity }
58+
end
4159
end
4260
end
4361

app/controllers/better_together/geography/countries_controller.rb

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,41 @@ def new
2828
def edit; end
2929

3030
# POST /geography/countries
31-
def create
31+
def create # rubocop:todo Metrics/MethodLength
3232
@geography_country = resource_class.new(geography_country_params)
3333
authorize_geography_country
3434

3535
if @geography_country.save
3636
redirect_to @geography_country, notice: 'Country was successfully created.', status: :see_other
3737
else
38-
render :new, status: :unprocessable_entity
38+
respond_to do |format|
39+
format.turbo_stream do
40+
render turbo_stream: turbo_stream.update(
41+
'form_errors',
42+
partial: 'layouts/better_together/errors',
43+
locals: { object: @geography_country }
44+
)
45+
end
46+
format.html { render :new, status: :unprocessable_entity }
47+
end
3948
end
4049
end
4150

4251
# PATCH/PUT /geography/countries/1
43-
def update
52+
def update # rubocop:todo Metrics/MethodLength
4453
if @geography_country.update(geography_country_params)
4554
redirect_to @geography_country, notice: 'Country was successfully updated.', status: :see_other
4655
else
47-
render :edit, status: :unprocessable_entity
56+
respond_to do |format|
57+
format.turbo_stream do
58+
render turbo_stream: turbo_stream.update(
59+
'form_errors',
60+
partial: 'layouts/better_together/errors',
61+
locals: { object: @geography_country }
62+
)
63+
end
64+
format.html { render :edit, status: :unprocessable_entity }
65+
end
4866
end
4967
end
5068

app/controllers/better_together/geography/region_settlements_controller.rb

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,41 @@ def new
2222
def edit; end
2323

2424
# POST /geography/region_settlements
25-
def create
25+
def create # rubocop:todo Metrics/MethodLength
2626
@geography_region_settlement = Geography::RegionSettlement.new(geography_region_settlement_params)
2727

2828
if @geography_region_settlement.save
2929
redirect_to @geography_region_settlement, notice: 'Region settlement was successfully created.'
3030
else
31-
render :new, status: :unprocessable_entity
31+
respond_to do |format|
32+
format.turbo_stream do
33+
render turbo_stream: turbo_stream.update(
34+
'form_errors',
35+
partial: 'layouts/better_together/errors',
36+
locals: { object: @geography_region_settlement }
37+
)
38+
end
39+
format.html { render :new, status: :unprocessable_entity }
40+
end
3241
end
3342
end
3443

3544
# PATCH/PUT /geography/region_settlements/1
36-
def update
45+
def update # rubocop:todo Metrics/MethodLength
3746
if @geography_region_settlement.update(geography_region_settlement_params)
3847
redirect_to @geography_region_settlement, notice: 'Region settlement was successfully updated.',
3948
status: :see_other
4049
else
41-
render :edit, status: :unprocessable_entity
50+
respond_to do |format|
51+
format.turbo_stream do
52+
render turbo_stream: turbo_stream.update(
53+
'form_errors',
54+
partial: 'layouts/better_together/errors',
55+
locals: { object: @geography_region_settlement }
56+
)
57+
end
58+
format.html { render :edit, status: :unprocessable_entity }
59+
end
4260
end
4361
end
4462

app/controllers/better_together/geography/regions_controller.rb

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,41 @@ def new
2828
def edit; end
2929

3030
# POST /geography/regions
31-
def create
31+
def create # rubocop:todo Metrics/MethodLength
3232
@geography_region = resource_class.new(geography_region_params)
3333
authorize_geography_region
3434

3535
if @geography_region.save
3636
redirect_to @geography_region, notice: 'Region was successfully created.'
3737
else
38-
render :new, status: :unprocessable_entity
38+
respond_to do |format|
39+
format.turbo_stream do
40+
render turbo_stream: turbo_stream.update(
41+
'form_errors',
42+
partial: 'layouts/better_together/errors',
43+
locals: { object: @geography_region }
44+
)
45+
end
46+
format.html { render :new, status: :unprocessable_entity }
47+
end
3948
end
4049
end
4150

4251
# PATCH/PUT /geography/regions/1
43-
def update
52+
def update # rubocop:todo Metrics/MethodLength
4453
if @geography_region.update(geography_region_params)
4554
redirect_to @geography_region, notice: 'Region was successfully updated.', status: :see_other
4655
else
47-
render :edit, status: :unprocessable_entity
56+
respond_to do |format|
57+
format.turbo_stream do
58+
render turbo_stream: turbo_stream.update(
59+
'form_errors',
60+
partial: 'layouts/better_together/errors',
61+
locals: { object: @geography_region }
62+
)
63+
end
64+
format.html { render :edit, status: :unprocessable_entity }
65+
end
4866
end
4967
end
5068

app/controllers/better_together/geography/settlements_controller.rb

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,41 @@ def new
2828
def edit; end
2929

3030
# POST /geography/settlements
31-
def create
31+
def create # rubocop:todo Metrics/MethodLength
3232
@geography_settlement = resource_class.new(geography_settlement_params)
3333
authorize_geography_settlement
3434

3535
if @geography_settlement.save
3636
redirect_to @geography_settlement, notice: 'Settlement was successfully created.'
3737
else
38-
render :new, status: :unprocessable_entity
38+
respond_to do |format|
39+
format.turbo_stream do
40+
render turbo_stream: turbo_stream.update(
41+
'form_errors',
42+
partial: 'layouts/better_together/errors',
43+
locals: { object: @geography_settlement }
44+
)
45+
end
46+
format.html { render :new, status: :unprocessable_entity }
47+
end
3948
end
4049
end
4150

4251
# PATCH/PUT /geography/settlements/1
43-
def update
52+
def update # rubocop:todo Metrics/MethodLength
4453
if @geography_settlement.update(geography_settlement_params)
4554
redirect_to @geography_settlement, notice: 'Settlement was successfully updated.', status: :see_other
4655
else
47-
render :edit, status: :unprocessable_entity
56+
respond_to do |format|
57+
format.turbo_stream do
58+
render turbo_stream: turbo_stream.update(
59+
'form_errors',
60+
partial: 'layouts/better_together/errors',
61+
locals: { object: @geography_settlement }
62+
)
63+
end
64+
format.html { render :edit, status: :unprocessable_entity }
65+
end
4866
end
4967
end
5068

app/controllers/better_together/geography/states_controller.rb

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,41 @@ def new
2626
def edit; end
2727

2828
# POST /geography/states
29-
def create
29+
def create # rubocop:todo Metrics/MethodLength
3030
@geography_state = ::BetterTogether::Geography::State.new(geography_state_params)
3131
authorize_geography_state
3232

3333
if @geography_state.save
3434
redirect_to @geography_state, notice: 'State was successfully created.', status: :see_other
3535
else
36-
render :new, status: :unprocessable_entity
36+
respond_to do |format|
37+
format.turbo_stream do
38+
render turbo_stream: turbo_stream.update(
39+
'form_errors',
40+
partial: 'layouts/better_together/errors',
41+
locals: { object: @geography_state }
42+
)
43+
end
44+
format.html { render :new, status: :unprocessable_entity }
45+
end
3746
end
3847
end
3948

4049
# PATCH/PUT /geography/states/1
41-
def update
50+
def update # rubocop:todo Metrics/MethodLength
4251
if @geography_state.update(geography_state_params)
4352
redirect_to @geography_state, notice: 'State was successfully updated.', status: :see_other
4453
else
45-
render :edit, status: :unprocessable_entity
54+
respond_to do |format|
55+
format.turbo_stream do
56+
render turbo_stream: turbo_stream.update(
57+
'form_errors',
58+
partial: 'layouts/better_together/errors',
59+
locals: { object: @geography_state }
60+
)
61+
end
62+
format.html { render :edit, status: :unprocessable_entity }
63+
end
4664
end
4765
end
4866

app/controllers/better_together/navigation_areas_controller.rb

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,42 @@ def edit
4545
authorize @navigation_area
4646
end
4747

48-
def create
48+
def create # rubocop:todo Metrics/MethodLength
4949
@navigation_area = resource_class.new(navigation_area_params)
5050
authorize @navigation_area
5151

5252
if @navigation_area.save
5353
redirect_to @navigation_area, only_path: true, notice: 'Navigation area was successfully created.'
5454
else
55-
render :new
55+
respond_to do |format|
56+
format.turbo_stream do
57+
render turbo_stream: turbo_stream.update(
58+
'form_errors',
59+
partial: 'layouts/better_together/errors',
60+
locals: { object: @navigation_area }
61+
)
62+
end
63+
format.html { render :new }
64+
end
5665
end
5766
end
5867

59-
def update
68+
def update # rubocop:todo Metrics/MethodLength
6069
authorize @navigation_area
6170

6271
if @navigation_area.update(navigation_area_params)
6372
redirect_to @navigation_area, only_path: true, notice: 'Navigation area was successfully updated.'
6473
else
65-
render :edit
74+
respond_to do |format|
75+
format.turbo_stream do
76+
render turbo_stream: turbo_stream.update(
77+
'form_errors',
78+
partial: 'layouts/better_together/errors',
79+
locals: { object: @navigation_area }
80+
)
81+
end
82+
format.html { render :edit }
83+
end
6684
end
6785
end
6886

app/controllers/better_together/pages_controller.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,23 @@ def new
3232
authorize @page
3333
end
3434

35-
def create
35+
def create # rubocop:todo Metrics/MethodLength
3636
@page = resource_class.new(page_params)
3737
authorize @page
3838

3939
if @page.save
4040
redirect_to edit_page_path(@page), notice: t('flash.generic.created', resource: t('resources.page'))
4141
else
42-
render :new
42+
respond_to do |format|
43+
format.turbo_stream do
44+
render turbo_stream: turbo_stream.update(
45+
'form_errors',
46+
partial: 'layouts/better_together/errors',
47+
locals: { object: @page }
48+
)
49+
end
50+
format.html { render :new }
51+
end
4352
end
4453
end
4554

0 commit comments

Comments
 (0)