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
|
|
|
|
|
2021-07-12 19:52:44 +00:00
|
|
|
# Obtiene el valor desde el documento.
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
def document_value
|
|
|
|
document.data[name.to_s]
|
|
|
|
end
|
|
|
|
|
2020-07-02 14:25:04 +00:00
|
|
|
def validate
|
|
|
|
super
|
|
|
|
|
2021-02-11 19:44:36 +00:00
|
|
|
errors << I18n.t('metadata.belongs_to.missing_post') unless 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
|
2021-05-13 22:44:21 +00:00
|
|
|
if belonged_to.present?
|
|
|
|
belonged_to[inverse].value = belonged_to[inverse].value.reject do |rej|
|
|
|
|
rej == post.uuid.value
|
|
|
|
end
|
|
|
|
end
|
2020-07-21 22:19:40 +00:00
|
|
|
|
2021-02-11 18:27:44 +00:00
|
|
|
# No duplicar las relaciones
|
2021-05-13 22:44:21 +00:00
|
|
|
belongs_to[inverse].value = (belongs_to[inverse].value.dup << 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
|
2021-02-11 19:44:36 +00:00
|
|
|
@inverse ||= layout.metadata.dig(name, 'inverse')&.to_sym
|
2020-07-21 22:19:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# El Post relacionado con este artículo
|
|
|
|
def belongs_to
|
2021-05-13 22:44:21 +00:00
|
|
|
posts.find(value, uuid: true) if value.present?
|
2020-07-21 22:19:40 +00:00
|
|
|
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-05-13 22:44:21 +00:00
|
|
|
posts.find(value_was, uuid: true) if value_was.present?
|
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
|
|
|
|
|
2021-05-17 20:27:01 +00:00
|
|
|
def indexable_values
|
|
|
|
belongs_to&.title&.value
|
|
|
|
end
|
|
|
|
|
2020-07-02 14:25:04 +00:00
|
|
|
private
|
|
|
|
|
2021-02-11 19:16:25 +00:00
|
|
|
def post_exists?
|
2021-02-11 20:57:12 +00:00
|
|
|
return true if sanitize(value).blank?
|
|
|
|
|
|
|
|
sanitize(value).present? && belongs_to.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
|