From 2c4ec96acaaf820e2c662495da6c04a2a89737bc Mon Sep 17 00:00:00 2001 From: Konstantin Rudy Date: Thu, 29 Oct 2020 18:09:19 +0300 Subject: [PATCH 1/2] Added optional: true to read_marks [Fixes #120] --- lib/unread/read_mark.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/unread/read_mark.rb b/lib/unread/read_mark.rb index da3fbfe..9e17214 100644 --- a/lib/unread/read_mark.rb +++ b/lib/unread/read_mark.rb @@ -1,5 +1,5 @@ class ReadMark < ActiveRecord::Base - belongs_to :readable, polymorphic: true, inverse_of: :read_marks + belongs_to :readable, polymorphic: true, inverse_of: :read_marks, optional: true validates_presence_of :reader_id, :reader_type, :readable_type From da170631006f47404025c00313764ec3285ac3e3 Mon Sep 17 00:00:00 2001 From: Konstantin Rudy Date: Fri, 30 Oct 2020 20:01:52 +0300 Subject: [PATCH 2/2] Added specs for the optional reader case [Fixes #120] --- lib/unread/base.rb | 6 +++++- lib/unread/read_mark.rb | 2 +- spec/unread/readable_spec.rb | 28 ++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/unread/base.rb b/lib/unread/base.rb index 3cfa080..a1289c6 100644 --- a/lib/unread/base.rb +++ b/lib/unread/base.rb @@ -8,7 +8,11 @@ def acts_as_reader ReadMark.reader_classes ||= [] unless ReadMark.reader_classes.include?(self) - ReadMark.belongs_to :reader, polymorphic: true, inverse_of: :read_marks + if ActiveRecord::VERSION::MAJOR < 5 + ReadMark.belongs_to :reader, polymorphic: true, inverse_of: :read_marks + else + ReadMark.belongs_to :reader, polymorphic: true, inverse_of: :read_marks, optional: true + end has_many :read_marks, dependent: :delete_all, as: :reader, inverse_of: :reader diff --git a/lib/unread/read_mark.rb b/lib/unread/read_mark.rb index 9e17214..da3fbfe 100644 --- a/lib/unread/read_mark.rb +++ b/lib/unread/read_mark.rb @@ -1,5 +1,5 @@ class ReadMark < ActiveRecord::Base - belongs_to :readable, polymorphic: true, inverse_of: :read_marks, optional: true + belongs_to :readable, polymorphic: true, inverse_of: :read_marks validates_presence_of :reader_id, :reader_type, :readable_type diff --git a/spec/unread/readable_spec.rb b/spec/unread/readable_spec.rb index c8add8c..ef275fc 100644 --- a/spec/unread/readable_spec.rb +++ b/spec/unread/readable_spec.rb @@ -253,6 +253,34 @@ expect(@reader.read_marks.single.count).to eq 1 end + + context 'when the reader class defines a default_scope that excludes tha reader instance' do + before { ReadMark.stub(belongs_to_required_by_default: true) } + + let!(:reader_class) do + CustomReader = Class.new(ActiveRecord::Base) do + self.primary_key = 'number' + self.table_name = 'readers' + + acts_as_reader + + default_scope { where.not(name: 'foo') } + end + end + let!(:reader) { reader_class.create!(name: 'foo') } + let(:document) { Document.create! } + + before do + wait + document + end + + subject { document.mark_as_read!(for: reader) } + + it 'does not raise_error' do + expect { subject }.not_to raise_error + end + end end describe '.mark_as_read!' do