-
Notifications
You must be signed in to change notification settings - Fork 86
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
Nested Attributes get not serialized correctly #60
Comments
Hi @kaelumania! Could you please provide the example of what format you're trying to get and what you see instead? Gist or failing spec would be really helpful |
Closing the issue for now |
I’m running into a similar issue. As far as I can tell, serialize and deserialize are never called on attributes with custom types. The only method called is cast. |
Yeah, I re–checked it quickly, and looks like a regular |
I just ran into this problem. Serialize is not called on save. Here is a simplified version of what I'm trying to do : class Shipment < ActiveRecord::Base
attribute :recipient, Shipment::Recipient.to_type
end
class Shipment::Recipient
include StoreModel::Model
attribute :country, :country
end
require 'countries' # https://github.com/countries/countries
class CountryType < ActiveModel::Type::Value
def cast(value)
ISO3166::Country.new(value)
end
def serialize(value)
value.alpha2
end
end
shipment = Shipment.create!(recipient: {country: 'FR'}) This should save {"country": ''FR'} in the recipient attribute in the database and shipment.recipient.country should be a ISO3166::Country. But instead the gem is trying to save a hash of all the data from ISO3166::Country without calling serialize. The CountryType works correctly when used with a direct string attribute. |
Yes this is because of store_model/lib/store_model/types/one.rb Lines 49 to 50 in 44071d2
TL;DR It calls directly |
I worked it around somehow including class Message < ApplicationRecord
attribute :payload, MessagePayload.to_type
end
class MessagePayload
include StoreModel::Model
# Workaround for: https://github.com/DmitryTsepelev/store_model/issues/60#issuecomment-962668573
# `as_json` will read values from `read_attribute_for_serialization`
include ActiveModel::Serializers::JSON
attribute :user, ActiveModel::Type::GlobalId.new
def read_attribute_for_serialization(attribute)
attribute_types[attribute].serialize(super)
end
end
module ActiveModel
module Type
# An ActiveModel::Type that serializes and deserializes GlobalID object
class GlobalId < Value
def type
:global_id
end
def serialize(value)
value&.to_gid.try(:to_s)
end
def cast_value(value)
value.is_a?(::String) ? GlobalID::Locator.locate(value) : value
end
def assert_valid_value(value)
value.nil? || value.respond_to?(:to_gid) || value.to_s.start_with?('gid://')
end
end
end
end |
Is there any news on this? I'm running into a similar problem, when trying to build a type that encrypts data when stored to the DB and encrypts it on the fly when read. |
@23tux @flop @kaelumania could you please take a look at #60? I created a custom type with the serialization method and it seems to work. What am I missing? |
@DmitryTsepelev I just stumbled upon this old issue, as I'm trying to implement some encryption handling into StoreModel (again). My approach seems to be fine for non-OneOf use cases. I tried to make a ready to use file that can be run with
As you can see, the Edit: store_model/lib/store_model/types/one_base.rb Lines 51 to 52 in 842a98c
Here, |
Hello,
I was wondering, why during serialization my custom types don't get used to determine the json format:
but during serialization, the default json serialisation of the given type is used.
The text was updated successfully, but these errors were encountered: