From 1e36f688606ca9f4ff32361510ab8c04a028506d Mon Sep 17 00:00:00 2001 From: Martin Schaflitzl Date: Thu, 21 Dec 2023 16:32:49 +0100 Subject: [PATCH] Fix MutationAfterCastError on #attributes for records after casting --- lib/active_type/util/unmutable_attributes.rb | 1 + spec/active_type/util_spec.rb | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/lib/active_type/util/unmutable_attributes.rb b/lib/active_type/util/unmutable_attributes.rb index 42b4d12..c923d93 100644 --- a/lib/active_type/util/unmutable_attributes.rb +++ b/lib/active_type/util/unmutable_attributes.rb @@ -10,6 +10,7 @@ module Util class UnmutableAttributes attr_reader :original_attributes + delegate :to_hash, to: :original_attributes def initialize(attributes) @original_attributes = attributes diff --git a/spec/active_type/util_spec.rb b/spec/active_type/util_spec.rb index 60ea486..bf4b5f4 100644 --- a/spec/active_type/util_spec.rb +++ b/spec/active_type/util_spec.rb @@ -282,6 +282,13 @@ class AssociatedRecord < ActiveRecord::Base end.not_to raise_error end + it 'still allows to access the attributes of the original record after cast via #attributes' do + base_record = UtilSpec::BaseRecord.create!(persisted_string: 'foo') + ActiveType::Util.cast(base_record, UtilSpec::ExtendedRecord) + + expect(base_record.attributes).to include('persisted_string' => 'foo') + end + context 'with option force: true' do it 'will not prevent changing or saving it' do base_record = UtilSpec::BaseRecord.create!(:persisted_string => 'old value')