Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MutationAfterCastError on #attributes for records after casting #184

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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