Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion app/controllers/better_together/navigation_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def create # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
end
end

def update # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
def update
authorize @navigation_item

respond_to do |format|
Expand Down Expand Up @@ -102,6 +102,30 @@ def destroy

private

def handle_update_success
flash[:notice] = t('navigation_item.updated')
respond_to do |format|
format.html { redirect_to @navigation_area, notice: flash[:notice] }
format.turbo_stream { redirect_to @navigation_area, only_path: true }
end
end

def handle_update_failure
flash.now[:alert] = t('navigation_item.update_failed')
respond_to do |format|
format.html { render :edit, status: :unprocessable_entity }
format.turbo_stream do
render turbo_stream: [
turbo_stream.update('form_errors', partial: 'layouts/better_together/errors',
locals: { object: @navigation_item }),
turbo_stream.update('navigation_item_form', partial: 'better_together/navigation_items/form',
locals: { navigation_item: @navigation_item,
navigation_area: @navigation_area })
]
end
end
end

def available_parent_items
BetterTogether::NavigationItem.where.not(id: @navigation_item.id).includes(
:string_translations,
Expand Down
51 changes: 51 additions & 0 deletions spec/requests/better_together/navigation_items_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'NavigationItems', type: :request do
include BetterTogether::Engine.routes.url_helpers

let(:navigation_area) { create(:navigation_area) }
let(:navigation_item) { create(:navigation_item, navigation_area: navigation_area, title: 'Old Title') }

before do
allow_any_instance_of(BetterTogether::NavigationItemsController).to receive(:authorize)
end

describe 'PATCH /navigation_areas/:navigation_area_id/navigation_items/:id' do
context 'HTML format' do
it 'redirects on success' do
patch navigation_area_navigation_item_path(navigation_area, navigation_item, locale: I18n.default_locale),
params: { navigation_item: { title: 'New Title' } }

expect(response).to redirect_to(navigation_area_path(navigation_area, locale: I18n.default_locale))
expect(flash[:notice]).to be_present
end

it 'renders edit on failure' do
patch navigation_area_navigation_item_path(navigation_area, navigation_item, locale: I18n.default_locale),
params: { navigation_item: { title: '' } }

expect(response).to have_http_status(:unprocessable_entity)
expect(response.body).to include('navigation_item_form')
end
end

context 'Turbo Stream format' do
it 'redirects on success' do
patch navigation_area_navigation_item_path(navigation_area, navigation_item, locale: I18n.default_locale),
params: { navigation_item: { title: 'New Title' } }, as: :turbo_stream

expect(response).to redirect_to(navigation_area_path(navigation_area, locale: I18n.default_locale))
end

it 'renders turbo stream on failure' do
patch navigation_area_navigation_item_path(navigation_area, navigation_item, locale: I18n.default_locale),
params: { navigation_item: { title: '' } }, as: :turbo_stream

expect(response.media_type).to eq('text/vnd.turbo-stream.html')
expect(response.body).to include('form_errors')
end
end
end
end
Loading