makandra / active_type

Make any Ruby object quack like ActiveRecord
MIT License
1.09k stars 74 forks source link

Calling to_yaml on an ActiveType::Object does not serialize correctly #127

Closed chriscz closed 3 years ago

chriscz commented 3 years ago

Calling to_yaml on a ActiveType::Object does not serialize its attributes whereas to_json does and works as expected.

I am willing to PR a fix for this by overriding the init_with, encode_with methods as defined in ActiveRecord::Core (used by Psych) and adding a Yaml encoder similar to ActiveModel::AttributeSet::YAMLEncoder to serialize the virtual_attributes field instead of @attributes as is currently done by the implementation in ActiveRecord.

Example

class Person < ActiveType::Object
  attribute :name
end
p = Person.new(name: "Chris")
p.to_yaml
# => "--- !ruby/object:Person\nconcise_attributes: []\nnew_record: true\nactive_record_yaml_version: 2\n"
kratob commented 3 years ago

Hi @chriscz. Sounds reasonable, feel free to send a PR.