DEV: Add save_user_preferences option to BookmarkManager (#16894)

When saving / creating bookmarks, we have code to save
the user's preference of bookmark_auto_delete_preference
to their user_options.

Unfortunately this can cause weirdness when plugins
have code using BookmarkManager to set the auto delete preference for
only a specific bookmark.

This commit introduces a save_user_preferences option (false
by default) so that this user preference is not saved unless
specified by the consumer of BookmarkManager, so plugins will
not have to worry about it.
This commit is contained in:
Martin Brennan
2022-05-24 11:13:21 +10:00
committed by GitHub
parent 0553788d3b
commit 1ee973e6e2
3 changed files with 64 additions and 21 deletions

View File

@ -227,6 +227,11 @@ RSpec.describe BookmarkManager do
expect(tu.bookmarked).to eq(true)
end
it "sets auto_delete_preference to never by default" do
bookmark = subject.create_for(bookmarkable_id: post.id, bookmarkable_type: "Post", name: name, reminder_at: reminder_at)
expect(bookmark.auto_delete_preference).to eq(Bookmark.auto_delete_preferences[:never])
end
context "when a reminder time is provided" do
it "saves the values correctly" do
subject.create_for(bookmarkable_id: post.id, bookmarkable_type: "Post", name: name, reminder_at: reminder_at)
@ -302,12 +307,41 @@ RSpec.describe BookmarkManager do
end
end
it "saves user's preference" do
subject.create_for(bookmarkable_id: post.id, bookmarkable_type: "Post", options: { auto_delete_preference: Bookmark.auto_delete_preferences[:when_reminder_sent] })
it "does not save user preference by default" do
user.user_option.update(bookmark_auto_delete_preference: Bookmark.auto_delete_preferences[:on_owner_reply])
subject.create_for(
bookmarkable_id: post.id,
bookmarkable_type: "Post",
options: { auto_delete_preference: Bookmark.auto_delete_preferences[:when_reminder_sent] }
)
expect(user.user_option.bookmark_auto_delete_preference).to eq(Bookmark.auto_delete_preferences[:on_owner_reply])
bookmark = Bookmark.find_by(user: user)
subject.update(
bookmark_id: bookmark.id,
name: "test",
reminder_at: 1.day.from_now,
options: { auto_delete_preference: Bookmark.auto_delete_preferences[:when_reminder_sent] }
)
expect(user.user_option.bookmark_auto_delete_preference).to eq(Bookmark.auto_delete_preferences[:on_owner_reply])
end
it "saves user's preference when save_user_preferences option is specified" do
user.user_option.update(bookmark_auto_delete_preference: Bookmark.auto_delete_preferences[:on_owner_reply])
subject.create_for(
bookmarkable_id: post.id,
bookmarkable_type: "Post",
options: { auto_delete_preference: Bookmark.auto_delete_preferences[:when_reminder_sent], save_user_preferences: true }
)
expect(user.user_option.bookmark_auto_delete_preference).to eq(Bookmark.auto_delete_preferences[:when_reminder_sent])
bookmark = Bookmark.find_by(user: user)
subject.update(bookmark_id: bookmark.id, name: "test", reminder_at: 1.day.from_now, options: { auto_delete_preference: Bookmark.auto_delete_preferences[:on_owner_reply] })
subject.update(
bookmark_id: bookmark.id,
name: "test",
reminder_at: 1.day.from_now,
options: { auto_delete_preference: Bookmark.auto_delete_preferences[:on_owner_reply], save_user_preferences: true }
)
expect(user.user_option.bookmark_auto_delete_preference).to eq(Bookmark.auto_delete_preferences[:on_owner_reply])
end
end