File tree Expand file tree Collapse file tree 2 files changed +27
-3
lines changed Expand file tree Collapse file tree 2 files changed +27
-3
lines changed Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ module Readonly
23
23
# @return [ true | false ] If the document is new, or if the field is not
24
24
# readonly.
25
25
def attribute_writable? ( name )
26
- new_record? || ( !readonly_attributes . include? ( name ) && _loaded? ( name ) )
26
+ new_record? || ( !self . class . readonly_attributes . include? ( name ) && _loaded? ( name ) )
27
27
end
28
28
29
29
private
@@ -63,12 +63,17 @@ module ClassMethods
63
63
# end
64
64
#
65
65
# @param [ Symbol... ] *names The names of the fields.
66
+ # @note When a parent class contains readonly attributes and is then
67
+ # inherited by a child class, the child class will inherit the
68
+ # parent's readonly attributes at the time of its creation.
69
+ # Updating the parent does not propagate down to child classes after wards.
66
70
def attr_readonly ( *names )
71
+ self . readonly_attributes = self . readonly_attributes . dup
67
72
names . each do |name |
68
- readonly_attributes << database_field_name ( name )
73
+ self . readonly_attributes << database_field_name ( name )
69
74
end
70
75
end
71
76
end
72
77
end
73
78
end
74
- end
79
+ end
Original file line number Diff line number Diff line change 266
266
expect ( child . mother ) . to be_nil
267
267
end
268
268
end
269
+ end
270
+
271
+ context "when a subclass inherits readonly fields" do
272
+ let ( :attributes ) do
273
+ [ :title , :terms ]
274
+ end
275
+
276
+ before do
277
+ class OldPerson < Person
278
+ attr_readonly :age
279
+ end
280
+ end
269
281
282
+ it "ensures subclass inherits the readonly attributes from parent" do
283
+ expect ( OldPerson . readonly_attributes . to_a ) . to include ( "title" , "terms" )
284
+ end
285
+
286
+ it "ensures subclass does not modify parent's readonly attributes" do
287
+ expect ( Person . readonly_attributes . to_a ) . not_to include ( "age" )
288
+ end
270
289
end
271
290
end
272
291
end
You can’t perform that action at this time.
0 commit comments