2020-07-02 14:25:04 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Almacena el UUID de otro Post
|
|
|
|
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
|
|
|
|
|
|
|
|
# @see MetadataTemplate
|
|
|
|
def to_param
|
|
|
|
name
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate
|
|
|
|
super
|
|
|
|
|
2020-07-02 14:26:00 +00:00
|
|
|
errors << I18n.t('metadata.belongs_to.missing_post') unless post_exists?
|
2020-07-02 14:25:04 +00:00
|
|
|
|
|
|
|
errors.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def sanitize(uuid)
|
|
|
|
uuid.gsub(/[^a-f0-9\-]/, '')
|
|
|
|
end
|
2020-07-02 14:26:00 +00:00
|
|
|
|
|
|
|
def post_exists?
|
|
|
|
!value.blank? && posts.find(sanitize(value), uuid: true)
|
|
|
|
end
|
2020-07-02 14:25:04 +00:00
|
|
|
end
|