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

Custom marshaler doesn't trigger for load unless record is reloaded #358

Open
jezhou opened this issue Nov 25, 2019 · 1 comment
Open

Custom marshaler doesn't trigger for load unless record is reloaded #358

jezhou opened this issue Nov 25, 2019 · 1 comment

Comments

@jezhou
Copy link

jezhou commented Nov 25, 2019

I have a column encrypted by attr_encrypted, let's call this column data. I also have a custom Marshaler that loads/dumps data in the column.

attr_encrypted :data, marshal: true, marshaler: CustomMarshal

A problem arises if I try to immediately decrypt a column that I have set using attr_encrypted. It returns the value I've set, but this value hasn't gone through the CustomMarshal when I expect that it would.

# abc is the record

new_value = [{hello: 'how are you doing'}]

# CustomMarshal.dump is called; this works like I expect it to.
abc.data = new_value 

# Fails: abc.data has gone through the marshaler – new_value hasn't
expect(abc.data).to_not eq(new_value)

# Fails: forcing new_value through the marshaler should make it equal abc.data
marshalled_value = CustomMarshal.dump(new_value)
expect(abc.data).to eq(CustomMarshal.load(marshalled_value))

# Passes: the above tests will pass if I reload the record
abc.reload
expect(abc.data).to_not eq(new_value)
expect(abc.data).to eq(CustomMarshal.load(marshalled_value))

The culprit could be this line, but it looks like this behavior is intentional. Are my expectations here wrong? I feel like if I set a custom marshaler in the options, it should always use the marshaler to load and dump the values. Otherwise, after I set the attribute on the record, I have to explicitly reload it for the marshaller to actually work.

@jezhou
Copy link
Author

jezhou commented Nov 25, 2019

I want to add that using ActiveRecord's #serialize does what I expect it to – I don't have to reload the record for it to go through the custom marshaler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant