Skip to content

Commit

Permalink
Added optional: true to reader's belongs_to [Fixes ledermann#120] (le…
Browse files Browse the repository at this point in the history
…dermann#121)

* Added optional: true to read_marks [Fixes ledermann#120]
* Added specs for the optional reader case [Fixes ledermann#120]
  • Loading branch information
k-rudy authored and HwakyoungLee committed May 12, 2023
1 parent ef5a7ed commit e045523
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/unread/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 28 additions & 0 deletions spec/unread/readable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e045523

Please sign in to comment.