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 { try {
const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content'); 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', method: 'PATCH',
headers: { headers: {
'Accept': 'application/json', 'Accept': 'application/json',

View file

@ -10,7 +10,7 @@
</div> </div>
<div class="bg-base-200 rounded-lg p-6"> <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? %> <% if @family.errors.any? %>
<div class="alert alert-error"> <div class="alert alert-error">
<div> <div>

View file

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