2020-07-02 14:25:04 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-07-21 22:19:40 +00:00
|
|
|
# Almacena el UUID de otro Post y actualiza el valor en el Post
|
|
|
|
# relacionado.
|
2020-07-02 14:25:04 +00:00
|
|
|
class MetadataBelongsTo < MetadataRelatedPosts
|
|
|
|
# TODO: Convertir algunos tipos de valores en módulos para poder
|
|
|
|
# implementar varios tipos de campo sin repetir código
|
|
|
|
#
|
|
|
|
# @include MetadataString
|
|
|
|
#
|
|
|
|
# Una string vacía
|
|
|
|
def default_value
|
|
|
|
''
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate
|
|
|
|
super
|
|
|
|
|
2021-02-11 18:27:44 +00:00
|
|
|
errors << I18n.t('metadata.belongs_to.missing_post') if value.present? && !post_exists?
|
2020-07-02 14:25:04 +00:00
|
|
|
|
|
|
|
errors.empty?
|
|
|
|
end
|
|
|
|
|
2020-07-21 22:19:40 +00:00
|
|
|
# Guardar y guardar la relación inversa también, eliminando la
|
|
|
|
# relación anterior si existía.
|
|
|
|
def save
|
2021-02-11 19:16:25 +00:00
|
|
|
super
|
|
|
|
|
2021-02-11 18:27:44 +00:00
|
|
|
# Si no hay relación inversa, no hacer nada más
|
2021-02-11 19:16:25 +00:00
|
|
|
return true unless changed?
|
|
|
|
return true unless inverse?
|
2020-07-21 22:19:40 +00:00
|
|
|
|
2021-02-11 18:27:44 +00:00
|
|
|
# Si estamos cambiando la relación, tenemos que eliminar la relación
|
|
|
|
# anterior
|
|
|
|
belonged_to[inverse].value.delete post.uuid.value if changed? && belonged_to.present?
|
2020-07-21 22:19:40 +00:00
|
|
|
|
2021-02-11 18:27:44 +00:00
|
|
|
# No duplicar las relaciones
|
|
|
|
belongs_to[inverse].value << post.uuid.value unless belongs_to.blank? || included?
|
2020-07-21 22:19:40 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
# El Post actual está incluido en la relación inversa?
|
|
|
|
def included?
|
2021-02-11 18:27:44 +00:00
|
|
|
belongs_to[inverse].value.include?(post.uuid.value)
|
2020-07-21 22:19:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Hay una relación inversa y el artículo existe?
|
|
|
|
def inverse?
|
2021-02-11 18:27:44 +00:00
|
|
|
inverse.present?
|
2020-07-21 22:19:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# El campo que es la relación inversa de este
|
|
|
|
def inverse
|
|
|
|
layout.metadata.dig name, 'inverse'
|
|
|
|
end
|
|
|
|
|
|
|
|
# El Post relacionado con este artículo
|
|
|
|
#
|
|
|
|
# XXX: Memoizamos usando el valor para tener el valor siempre
|
|
|
|
# actualizado.
|
|
|
|
def belongs_to
|
|
|
|
return if value.blank?
|
|
|
|
|
|
|
|
@belongs_to ||= {}
|
|
|
|
@belongs_to[value] ||= posts.find(value, uuid: true)
|
|
|
|
end
|
|
|
|
|
2021-02-11 18:27:44 +00:00
|
|
|
# El artículo relacionado anterior
|
2020-07-21 22:19:40 +00:00
|
|
|
def belonged_to
|
2021-02-11 18:27:44 +00:00
|
|
|
return if value_was.blank?
|
2020-07-21 22:19:40 +00:00
|
|
|
|
2021-02-11 18:27:44 +00:00
|
|
|
@belonged_to ||= posts.find(value_was, uuid: true)
|
2020-07-21 22:19:40 +00:00
|
|
|
end
|
|
|
|
|
2020-07-22 23:35:43 +00:00
|
|
|
def related_posts?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def related_methods
|
|
|
|
@related_methods ||= %i[belongs_to belonged_to].freeze
|
|
|
|
end
|
|
|
|
|
2020-07-02 14:25:04 +00:00
|
|
|
private
|
|
|
|
|
2021-02-11 19:16:25 +00:00
|
|
|
def post_exists?
|
|
|
|
posts.find(value, uuid: true).present?
|
2020-07-02 14:25:04 +00:00
|
|
|
end
|
2020-07-02 14:26:00 +00:00
|
|
|
|
2021-02-11 19:16:25 +00:00
|
|
|
def sanitize(uuid)
|
|
|
|
uuid.to_s.gsub(/[^a-f0-9\-]/i, '')
|
2020-07-02 14:26:00 +00:00
|
|
|
end
|
2020-07-02 14:25:04 +00:00
|
|
|
end
|