Skip to content

Commit

Permalink
Fix MutationAfterCastError on #attributes for records after casting
Browse files Browse the repository at this point in the history
  • Loading branch information
martinschaflitzl1 committed Jan 8, 2024
1 parent 31bea72 commit 1e36f68
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/active_type/util/unmutable_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Util
class UnmutableAttributes

attr_reader :original_attributes
delegate :to_hash, to: :original_attributes

def initialize(attributes)
@original_attributes = attributes
Expand Down
7 changes: 7 additions & 0 deletions spec/active_type/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 1e36f68

Please sign in to comment.