Skip to content

AO3-5556 Don't change collections on preview - Follow up PR#5820

Open
pmonfort wants to merge 10 commits into
otwcode:masterfrom
pmonfort:AO3-5556-collection-persists-on-preview-cancel
Open

AO3-5556 Don't change collections on preview - Follow up PR#5820
pmonfort wants to merge 10 commits into
otwcode:masterfrom
pmonfort:AO3-5556-collection-persists-on-preview-cancel

Conversation

@pmonfort

Copy link
Copy Markdown
Contributor

Pull Request Checklist

Issue

https://otwarchive.atlassian.net/browse/AO3-5556

Purpose

Follow-up to #4501 by @tickinginstant, adopting and completing the work from that PR.

Original PR (#4501)

  • Generalize the function assign_tags_of_type to a function assign_through_association defined in ApplicationRecord that can be used for any type of association, not just tags.
  • Use that function to assign collections, so that setting collection_names will only set up the changes to be performed on save instead of performing them immediately. Similarly with collections_to_add and collections_to_remove.
  • Change the approve_automatically and set_anonymous_and_unrevealed callbacks in the CollectionItem class to run before_validation instead of before_save, so that we get the correct values for anonymous, unrevealed, user_approval_status, and collection_approval_status after validation.
  • Make the Collectible module a concern and move it to the correct directory.
  • Rewrite the Collectible function set_anon_unrevealed so that it uses the status of the collection items to compute whether the work should be anonymous/unrevealed, and make it run as an after_validation callback so that it has access to the collection item statuses set before validation.
  • Rewrite BookmarksController#update to use the collection handling from Collectible, and add a function akin to WorksController#in_moderated_collection so that the message on adding a moderated collection stays the same as it was before the change.
  • Stop calling save(validate: false) so much, and replace it with a reindex_item callback in the CollectionItem class. The item association on CollectionItems has touch: true, so changes to collection items will already update the work/bookmark timestamp. And indexing is the only other real purpose that calling save(validate: false) serves.

This PR

  • Resolved merge conflicts with master (~2 years of divergence)
  • Reorder callbacks in CollectionItem to follow Rails convention (before_validationbefore_saveafter_saveafter_create_commitafter_destroyafter_commit)
  • Fix annotation style in Collectible (# Note:# NOTE:) per Hound/Rubocop convention
  • Extract assign_through_association from ApplicationRecord into an AssociationAssignment concern, included only in Collectible and Taggable
  • Extract collections_meta_tag helper in WorksHelper to replace variable assignment in works/_meta.html.erb
  • Fix cucumber steps: Cancel is a link (I follow), not a button (I press)

Testing Instructions

Steps to reproduce the bug

  1. Post a work
  2. Edit the work
  3. Add a collection to the work, especially an anonymous or unrevealed collection
  4. Press "Preview"
  5. Note that in the preview, the collection is listed in the work meta, and the byline is updated to "Anonymous" if the collection is anonymous (there will be no
    indication on the preview page if the collection is unrevealed)
  6. Press "Cancel"
  7. The collection should not be added to the work

Automated tests

  • bundle exec rspec spec/models/collection_item_spec.rb spec/models/concerns/collectible_spec.rb spec/lib/works_owner_spec.rb — 65 examples, 0 failures
  • bundle exec cucumber features/collections/work_preview_collections.feature — 6 scenarios, 6 passed

References

Follow-up to #4501

Credit

Pablo Monfort (he/him))

@pmonfort pmonfort marked this pull request as draft May 16, 2026 01:22
@pmonfort pmonfort force-pushed the AO3-5556-collection-persists-on-preview-cancel branch 3 times, most recently from 0e88cf1 to 809c2e6 Compare May 17, 2026 01:15
pmonfort added 3 commits May 17, 2026 02:01
- Extract AssociationAssignment into its own concern
- Restore update_bookmarker_collections_index in Bookmarkable
- Fix set_anon_unrevealed for new records in Collectible
- Reorder CollectionItem callbacks per rubocop-rails 2.12.4
- Normalize locale files and add missing i18n keys
- Update specs to use item: instead of work: for CollectionItem
- Fix stale tags cache in rake spec with collection.reload
- Update bookmark controller spec for simplified flash message
- Fix cucumber scenario: remove non-existent flash assertion
@pmonfort pmonfort force-pushed the AO3-5556-collection-persists-on-preview-cancel branch from 809c2e6 to 4ef0c3f Compare May 17, 2026 20:09
@pmonfort pmonfort marked this pull request as ready for review May 17, 2026 20:37
Comment thread app/helpers/works_helper.rb Outdated
distinct.joins(:approved_collection_items).merge(collection.all_items)
}

after_validation :set_anon_unrevealed, if: -> { collection_items.loaded? }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

To make sure I understand the flow here, why do we need the if condition?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's a performance guard, without it, every save (even unrelated ones like editing tags) would trigger a query. If collection_items is loaded, there's a chance it was updated. It may be confusing though, I can remove it if you'd like.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nah, I think a comment for clarity would be enough 👍

Comment thread app/models/collection_item.rb
Comment thread spec/lib/works_owner_spec.rb
Comment thread spec/controllers/bookmarks_controller_spec.rb
Comment thread config/locales/controllers/en.yml
Comment thread app/controllers/bookmarks_controller.rb
Comment thread app/controllers/works_controller.rb Outdated
@pmonfort pmonfort force-pushed the AO3-5556-collection-persists-on-preview-cancel branch from 22fa86c to 7f8693c Compare June 6, 2026 03:16
@pmonfort

pmonfort commented Jun 6, 2026

Copy link
Copy Markdown
Contributor Author

@brianjaustin thanks for your code review! The code is ready for a second review, let me know your thoughts.

@pmonfort pmonfort requested a review from brianjaustin June 6, 2026 03:49

@brianjaustin brianjaustin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! Nothing blocking from my side

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants