Class: Undo::Serializer::ActiveModel
- Inherits:
-
Object
- Object
- Undo::Serializer::ActiveModel
- Defined in:
- lib/undo/serializer/active_model.rb
Instance Method Summary collapse
- #deserialize(hash) ⇒ Object
-
#initialize(*args) ⇒ ActiveModel
constructor
A new instance of ActiveModel.
- #serialize(object) ⇒ Object
Constructor Details
#initialize(*args) ⇒ ActiveModel
Returns a new instance of ActiveModel.
6 7 8 9 10 11 |
# File 'lib/undo/serializer/active_model.rb', line 6 def initialize(*args) = args. @serializer = args.first @serializer_source = .fetch :serializer, ->(object) { object.active_model_serializer.new object } end |
Instance Method Details
#deserialize(hash) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/undo/serializer/active_model.rb', line 17 def deserialize(hash) object_handler, data = hash.first return unless data.is_a? Hash data.stringify_keys! ActiveRecord::Base.transaction do initialize_object(object_handler, data).tap do |object| data.each do |field, value| next if "id" == field && object.persisted? case field when /___(.*)/ then deserialize_association object, $1, value else deserialize_field object, field, value end end object.save! end end end |
#serialize(object) ⇒ Object
13 14 15 |
# File 'lib/undo/serializer/active_model.rb', line 13 def serialize(object) serializer(object).as_json end |