lib/hanami/model/associations/has_one.rb in hanami-model-1.1.0.rc1 vs lib/hanami/model/associations/has_one.rb in hanami-model-1.1.0
- old
+ new
@@ -1,5 +1,7 @@
+require "hanami/utils/hash"
+
module Hanami
module Model
module Associations
# Many-To-One association
#
@@ -47,27 +49,27 @@
scope.one
end
def create(data)
entity.new(
- command(:create, aggregate(target), use: [:timestamps]).call(data)
+ command(:create, aggregate(target), use: [:timestamps]).call(serialize(data))
)
rescue => e
raise Hanami::Model::Error.for(e)
end
def add(data)
- command(:create, relation(target), use: [:timestamps]).call(associate(data))
+ command(:create, relation(target), use: [:timestamps]).call(associate(serialize(data)))
rescue => e
raise Hanami::Model::Error.for(e)
end
def update(data)
command(:update, relation(target), use: [:timestamps])
.by_pk(
one.public_send(relation(target).primary_key)
- ).call(data)
+ ).call(serialize(data))
rescue => e
raise Hanami::Model::Error.for(e)
end
def delete
@@ -76,11 +78,11 @@
alias remove delete
def replace(data)
repository.transaction do
delete
- add(data)
+ add(serialize(data))
end
end
private
@@ -148,9 +150,15 @@
# @api private
def _build_scope
result = relation(target)
result = result.where(foreign_key => subject.fetch(primary_key)) unless subject.nil?
result.as(Model::MappedRelation.mapper_name)
+ end
+
+ # @since 1.1.0
+ # @api private
+ def serialize(data)
+ Utils::Hash.deep_serialize(data)
end
end
end
end
end