Skip to content

Commit 2368688

Browse files
authored
* MONGOID-5474 add user test * MONGOID-5474 change to read-only * MOGOID-5474 switch order of flag docs
1 parent 584798b commit 2368688

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

lib/mongoid/config.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@ module Config
127127
# always return a Hash.
128128
option :legacy_attributes, default: false
129129

130-
# When this flag is true, a document is only readonly if it has been
131-
# projected using #only or #without, and readonly documents will not
132-
# be deletable/destroyable. When this flag is false, a document will
133-
# become readonly only once the #readonly! method is called, and an error
134-
# will be raised on attempting to save or update such documents, instead
135-
# of just on delete.
136-
# When this feature flag is turned on, the readonly state will be reset on
130+
# When this flag is false, a document will become read-only only once the
131+
# #readonly! method is called, and an error will be raised on attempting
132+
# to save or update such documents, instead of just on delete. When this
133+
# flag is true, a document is only read-only if it has been projected
134+
# using #only or #without, and read-only documents will not be
135+
# deletable/destroyable, but they will be savable/updatable.
136+
# When this feature flag is turned on, the read-only state will be reset on
137137
# reload, but when it is turned off, it won't be.
138138
option :legacy_readonly, default: false
139139

spec/mongoid/stateful_spec.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,36 @@
173173
op
174174
end
175175
end
176+
177+
context "when overriding readonly?" do
178+
179+
let(:doc) { ReadonlyModel.create! }
180+
181+
before do
182+
class ReadonlyModel
183+
include Mongoid::Document
184+
185+
attr_accessor :locked
186+
187+
def readonly?
188+
!!locked
189+
end
190+
end
191+
end
192+
193+
after do
194+
Object.send(:remove_const, :ReadonlyModel)
195+
end
196+
197+
it "raises when readonly? is true" do
198+
expect(doc.readonly?).to be false
199+
doc.locked = true
200+
expect(doc.readonly?).to be true
201+
expect do
202+
doc.destroy
203+
end.to raise_error(Mongoid::Errors::ReadonlyDocument)
204+
end
205+
end
176206
end
177207

178208
context "when legacy_readonly is false" do
@@ -206,6 +236,36 @@
206236
expect(document).to_not be_readonly
207237
end
208238
end
239+
240+
context "when overriding readonly?" do
241+
242+
let(:doc) { ReadonlyModel.new }
243+
244+
before do
245+
class ReadonlyModel
246+
include Mongoid::Document
247+
248+
attr_accessor :locked
249+
250+
def readonly?
251+
!!locked
252+
end
253+
end
254+
end
255+
256+
after do
257+
Object.send(:remove_const, :ReadonlyModel)
258+
end
259+
260+
it "raises when readonly? is true" do
261+
expect(doc.readonly?).to be false
262+
doc.locked = true
263+
expect(doc.readonly?).to be true
264+
expect do
265+
doc.save!
266+
end.to raise_error(Mongoid::Errors::ReadonlyDocument)
267+
end
268+
end
209269
end
210270
end
211271
end

0 commit comments

Comments
 (0)