Class: Undo::Serializer::ActiveModel
- Inherits:
-
Object
- Object
- Undo::Serializer::ActiveModel
- Defined in:
- lib/undo/serializer/active_model.rb
Constant Summary collapse
- VERSION =
"0.0.2"
Instance Method Summary collapse
- #deserialize(hash) ⇒ Object
-
#initialize(model_serializer = nil) ⇒ ActiveModel
constructor
A new instance of ActiveModel.
- #serialize(object) ⇒ Object
Constructor Details
#initialize(model_serializer = nil) ⇒ ActiveModel
Returns a new instance of ActiveModel.
6 7 8 |
# File 'lib/undo/serializer/active_model.rb', line 6 def initialize(model_serializer = nil) @model_serializer = model_serializer end |
Instance Method Details
#deserialize(hash) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/undo/serializer/active_model.rb', line 14 def deserialize(hash) hash.each do |object_class, data| next unless data.is_a? Hash data.stringify_keys! object_class = object_class.to_s.camelize.constantize object = object_class.where(id: data.fetch("id")).first_or_initialize data.each do |field, value| next if "id" == field && object.persisted? _, association = field.to_s.split("___") if association deserialize_association(association, value) else object.send "#{field}=", value # not public_send! end end object.save! return object end end |
#serialize(object) ⇒ Object
10 11 12 |
# File 'lib/undo/serializer/active_model.rb', line 10 def serialize(object) model_serializer(object).as_json end |