makandra / active_type

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

Implement YAML serialization #128

Closed chriscz closed 3 years ago

chriscz commented 3 years ago

This implements YAML serialization in a way that's (hopefully) completely compatible with ActiveRecord's current and previous implementation. Closes #127

chriscz commented 3 years ago

I just started thinking that maybe passing an option such as deep:true to to_yaml may be useful if someone is interested in including all the nests_one and nests_many associations as well. What do you think @kratob?

kratob commented 3 years ago

The PR itself is perfect, I'll merge and release it. Thank you very much.

As to the deep: true: I think this would probably get messy and is in most (maybe all?) cases not necessary.

Just looking at our nests_many example from the docs

class Holiday < ActiveRecord::Base
end

class HolidaysForm < ActiveType::Object
  nests_many :holidays, reject_if: :all_blank, default: proc { Holiday.all }
end

I don't think it makes much sense to serialize all Holiday records together with the ActiveType::Object, because it's totally unclear what you would even do when deserializing.

Maybe I'm missing a usecase here, so feel free to reopen if you have a good idea.

kratob commented 3 years ago

Released as 1.5.0.

chriscz commented 3 years ago

Thanks @kratob I see what you mean in terms of the forms example. We've been using the library in some web integrations for representing the remote resources and we then serialize responses to using YAML. Fortunately these models are mostly flat so I don't really care for the nests_{one,many}, but we do have certain tree-like structures in other places, regardless, this isn't currently a requirement for me!

Thanks again for merging and your work on maintaining this project!