Module: EcsRails::Entity::ImmutableIdentity

Included in:
EcsRails::Entity
Defined in:
lib/ecs_rails/entity.rb

Overview

Makes assignment to a readonly attribute raise on a persisted record.

ActiveRecord ships this behaviour, but attr_readonly only installs it when ActiveRecord.raise_on_assign_to_attr_readonly is true. That config defaults to false in bare ActiveRecord, and in a Rails app it is applied by the railtie only after this file has already been required — so by the time attr_readonly runs below, the host’s setting is not yet visible and cannot be relied on either way. RFC-0001 requires the raise unconditionally, so we install our own guard rather than inherit a race.

Harmless if ActiveRecord’s own guard is also installed: both raise the same error.

Instance Method Summary collapse

Instance Method Details

#_write_attribute(attr_name, value) ⇒ Object

The internal writer, which every generated attr= method funnels into.



53
54
55
56
# File 'lib/ecs_rails/entity.rb', line 53

def _write_attribute(attr_name, value)
  guard_readonly_attribute!(attr_name)
  super
end

#write_attribute(attr_name, value) ⇒ Object

Rejects writes to readonly attributes once the row exists. New records are untouched, so create — including ActiveRecord assigning the database-generated id — still works.

Both writers are guarded because the public #write_attribute does not call #_write_attribute; it writes to the attribute set directly.



47
48
49
50
# File 'lib/ecs_rails/entity.rb', line 47

def write_attribute(attr_name, value)
  guard_readonly_attribute!(attr_name)
  super
end