-
Type: Bug
-
Resolution: Unresolved
-
Priority: Unknown
-
None
-
Affects Version/s: None
-
Component/s: Attributes
-
None
-
Ruby Drivers
(Seen in mongoid 8.1.5 and 9.0.1)
Having two classes A and B that are subclasses from Base, declaring attr_readonly in the context of A or B leaks that declaration into the entire class hierarchy:
irb(main):001> require 'mongoid' => true irb(main):002* class Base irb(main):003* include Mongoid::Document irb(main):004* field :a, type: String irb(main):005* field :b, type: String irb(main):006> end => #<Mongoid::Fields::Standard:0x00007b0dd3154300 @default_val=nil, @label=nil, @name="b", @options={:type=>String, :klass=>Base}> irb(main):007* class A < Base irb(main):008* attr_readonly :b irb(main):009> end => [:b] irb(main):010* class B < Base irb(main):011* attr_readonly :a irb(main):012> end => [:a] irb(main):013> A.readonly_attributes => #<Set: {"b", "a"}>
The expectation would be, that a declaration in a class would inherit to its child-classes (e.g. from Base to A and B), but not the other way around (i.e. not from A to Base, and especially not from A to B via Base).
A workaround is to set readonly_attributes in each subclass explicitly:
class A < Base self.readonly_attributes = Set.new attr_readonly :b end
however this is fragile and prone to errors of oversight when extending the class hierarchy.