Fix routes to use singular resource for family

This commit is contained in:
Eugene Burmakin 2025-10-04 23:08:02 +02:00
parent 54661a1d52
commit e7df54d738
3 changed files with 23 additions and 28 deletions

View file

@ -62,7 +62,7 @@ export default class extends Controller {
try {
const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
const response = await fetch(`/families/${this.familyIdValue}/update_location_sharing`, {
const response = await fetch(`/family/update_location_sharing`, {
method: 'PATCH',
headers: {
'Accept': 'application/json',
@ -273,4 +273,4 @@ export default class extends Controller {
}
return false;
}
}
}

View file

@ -10,7 +10,7 @@
</div>
<div class="bg-base-200 rounded-lg p-6">
<%= form_with model: @family, local: true, class: "space-y-6" do |form| %>
<%= form_with url: family_path, model: @family, local: true, class: "space-y-6" do |form| %>
<% if @family.errors.any? %>
<div class="alert alert-error">
<div>
@ -63,4 +63,4 @@
<% end %>
</div>
</div>
</div>
</div>

View file

@ -2,7 +2,7 @@
require 'rails_helper'
RSpec.describe 'Families', type: :request do
RSpec.describe 'Family', type: :request do
let(:user) { create(:user) }
let(:other_user) { create(:user) }
let(:family) { create(:family, creator: user) }
@ -14,9 +14,6 @@ RSpec.describe 'Families', type: :request do
sign_in user
end
# GET /families route no longer exists - we use singular resource /family
# Users without a family should go to /family/new instead
describe 'GET /family' do
it 'shows the family page' do
get "/family"
@ -76,9 +73,10 @@ RSpec.describe 'Families', type: :request do
end
it 'redirects to the new family with success message' do
post '/families', params: valid_attributes
post '/family', params: valid_attributes
expect(response).to have_http_status(:found)
expect(response.location).to match(%r{/families/})
expect(response.location).to eq family_url
follow_redirect!
expect(response.body).to include('Family created successfully!')
end
@ -94,7 +92,7 @@ RSpec.describe 'Families', type: :request do
end
it 'renders the new template with errors' do
post '/families', params: invalid_attributes
post '/family', params: invalid_attributes
expect(response).to have_http_status(:unprocessable_content)
end
end
@ -155,10 +153,8 @@ RSpec.describe 'Families', type: :request do
describe 'DELETE /family' do
context 'when family has only one member' do
it 'deletes the family' do
expect do
delete "/family"
end.to change(Family, :count).by(-1)
expect(response).to redirect_to(family_path)
expect { delete '/family' }.to change(Family, :count).by(-1)
expect(response).to redirect_to(new_family_path)
end
end
@ -168,9 +164,7 @@ RSpec.describe 'Families', type: :request do
end
it 'does not delete the family' do
expect do
delete "/family"
end.not_to change(Family, :count)
expect { delete "/family" }.not_to change(Family, :count)
expect(response).to redirect_to(family_path)
follow_redirect!
expect(response.body).to include('Cannot delete family with members')
@ -196,22 +190,22 @@ RSpec.describe 'Families', type: :request do
it 'denies access to show when user is not in family' do
get "/family"
expect(response).to redirect_to(family_path)
expect(response).to redirect_to(new_family_path)
end
it 'redirects to families index when user is not in family for edit' do
it 'redirects to family page when user is not in family for edit' do
get "/family/edit"
expect(response).to redirect_to(family_path)
expect(response).to redirect_to(new_family_path)
end
it 'redirects to families index when user is not in family for update' do
it 'redirects to family page when user is not in family for update' do
patch "/family", params: { family: { name: 'Hacked' } }
expect(response).to redirect_to(family_path)
expect(response).to redirect_to(new_family_path)
end
it 'redirects to families index when user is not in family for destroy' do
it 'redirects to family page when user is not in family for destroy' do
delete "/family"
expect(response).to redirect_to(family_path)
expect(response).to redirect_to(new_family_path)
end
end
@ -220,7 +214,7 @@ RSpec.describe 'Families', type: :request do
before { sign_out user }
it 'redirects to login for index' do
get '/families'
get '/family'
expect(response).to redirect_to(new_user_session_path)
end
@ -230,12 +224,13 @@ RSpec.describe 'Families', type: :request do
end
it 'redirects to login for new' do
get '/families/new'
get '/family/new'
expect(response).to redirect_to(new_user_session_path)
end
it 'redirects to login for create' do
post '/families', params: { family: { name: 'Test' } }
post '/family', params: { family: { name: 'Test' } }
expect(response).to redirect_to(new_user_session_path)
end