Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(161623) Do not show "deleted" projects in application #1478

Merged

Conversation

lozette
Copy link
Collaborator

@lozette lozette commented Apr 5, 2024

Changes

Now that we have the concept of "soft deleted" projects (projects which exist in the database but are in a deleted state) we need to ensure they never appear to users.

To ensure this, we have decided to use a default_scope for projects. Default scopes can be problematic as they can conceal issues and cause confusion - but the alternative is to add active and completed scopes to ALL calls to Project in the application.

This does mean that we lose access to the Project.deleted scope, but if we need to call on deleted projects specifically we can unscope the default scope and call Project.unscoped.where(state: :deleted)

We then go on to add some Policy changes and extra specs, to make sure deleted projects do not appear in listing pages, and cannot be shown/edited.

Checklist

  • Attach this pull request to the appropriate card in DevOps.
  • Update the CHANGELOG.md if needed.

@lozette lozette force-pushed the feature/161623-do-not-show-deleted-projects-in-application branch 5 times, most recently from 9b42e7c to 64670bb Compare April 10, 2024 12:58
@lozette lozette changed the title Feature/161623 do not show deleted projects in application (161623) Do not show "deleted" projects in application Apr 10, 2024
@lozette lozette marked this pull request as ready for review April 10, 2024 13:05
Adding default_scope to models can be problematic as default scopes can conceal
issues. However, now that we have a concept of "soft deleted" projects, we do
not want to return these projects ANYWHERE in the application - not in the UI,
exports or statistics. They should not be seen to exist.

To avoid explicitly asking for `active` and/or `completed` projects in all
our pages, use `default_scope` to exclude deleted projects.

If we want to view deleted projects in future, we will have to explicitly look
for them (e.g. using `Project.unscoped.where(state: :deleted)` as opposed to the
Project.deleted scope).
…edited

We don't want users viewing deleted projects by going to their `show` URL - so
ensure this doesn't happen using Policies.

This will return a 403 not a 404, which semantically isn't quite correct - the
project isn't deleted, so it's "found" but it's not viewable. But the user
shouldn't know it's not "deleted". However we think it will be rare for a user
to have a project URL and go to it directly.

The editing restricton is more of a theoretical need than a practical one, but
mark "soft deleted" projects as un-editable, just in case anyone manages to
access one and tries to edit it.
We were using ProjectPolicy.show? here but this is actually a project
_listing_ page, not a show page for an individual project. The naming on this
controller is a little obscure - it is _showing_ an individual month of projects
not an individual project. So `index?` is the appropriate Policy to call here.
Add some tests to prove our exports are not including any "soft deleted"
projects.
@lozette lozette force-pushed the feature/161623-do-not-show-deleted-projects-in-application branch from 64670bb to 965ce31 Compare April 10, 2024 13:07
Copy link

sonarcloud bot commented Apr 10, 2024

Copy link
Collaborator

@mec mec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh! default_scope again and me reviewing it again (we have a default scope on Note)! 😱

I suspect that, like you, I cannot come up with a good reason not to use it here, 'deleted' projects are a pretty odd concept, it's hard to understand what they are actually for.

For that reason, I am going to go against everything I have read and been told by better developers than me about never using default_scope and approve this!

@lozette
Copy link
Collaborator Author

lozette commented Apr 11, 2024

Yeah I have so many misgivings. I can see myself in the future frantically debugging something that's been caused by default_scope. But I also don't want to accidentally leave active and/or completed off a call to Project, and show users deleted projects 😬

@lozette lozette merged commit cf1e3ac into main Apr 11, 2024
7 checks passed
@lozette lozette deleted the feature/161623-do-not-show-deleted-projects-in-application branch April 11, 2024 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants