Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added optional: true to reader's belongs_to [Fixes #120] #121

Merged
merged 2 commits into from
Oct 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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