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