FIX: Do not allow null options for bookmark manager

This commit is contained in:
Martin Brennan 2020-05-08 15:24:37 +00:00
parent e83dc75238
commit 22dffe6f69
2 changed files with 19 additions and 2 deletions

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true
class BookmarkManager
DEFAULT_OPTIONS = { delete_when_reminder_sent: false }
include HasErrors
def initialize(user)
@ -22,7 +24,7 @@ class BookmarkManager
reminder_type: reminder_type,
reminder_at: reminder_at,
reminder_set_at: Time.zone.now
}.merge(options)
}.merge(default_options(options))
)
if bookmark.errors.any?
@ -82,7 +84,7 @@ class BookmarkManager
reminder_at: reminder_at,
reminder_type: reminder_type,
reminder_set_at: Time.zone.now
}.merge(options)
}.merge(default_options(options))
)
if bookmark.errors.any?
@ -102,4 +104,8 @@ class BookmarkManager
return if reminder_type.blank?
reminder_type.is_a?(Integer) ? reminder_type : Bookmark.reminder_types[reminder_type.to_sym]
end
def default_options(options)
DEFAULT_OPTIONS.merge(options) { |key, old, new| new.nil? ? old : new }
end
end

View File

@ -53,6 +53,17 @@ RSpec.describe BookmarkManager do
end
end
context "when options are provided with null values" do
let(:options) { { delete_when_reminder_sent: nil } }
it "saves defaults successfully" do
subject.create(post_id: post.id, name: name, options: options)
bookmark = Bookmark.find_by(user: user)
expect(bookmark.delete_when_reminder_sent).to eq(false)
end
end
context "when the bookmark already exists for the user & post" do
before do
Bookmark.create(post: post, user: user, topic: post.topic)