2023-04-08 15:47:51 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-04-08 20:36:13 +00:00
|
|
|
class MetadataHasOne < MetadataBelongsTo
|
2023-04-08 20:09:40 +00:00
|
|
|
def had_one
|
|
|
|
return default_value if value_was.blank?
|
|
|
|
|
|
|
|
posts.find(uuid: value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_one
|
|
|
|
return default_value if value.blank?
|
|
|
|
|
|
|
|
posts.find(uuid: value)
|
|
|
|
end
|
|
|
|
|
2023-04-08 20:36:13 +00:00
|
|
|
def belonged_to; end
|
|
|
|
def belongs_to; end
|
2023-04-08 20:09:40 +00:00
|
|
|
|
|
|
|
def save
|
2023-04-08 20:36:13 +00:00
|
|
|
# XXX: DRY
|
|
|
|
if !changed?
|
|
|
|
self[:value] = document_value
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
self[:value] = sanitize value
|
2023-04-08 20:09:40 +00:00
|
|
|
|
|
|
|
return true unless changed?
|
|
|
|
return true unless inverse?
|
|
|
|
|
|
|
|
had_one&.value = nil
|
|
|
|
has_one&.value = post.uuid.value
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def related_methods
|
|
|
|
@related_methods ||= %i[has_one had_one].freeze
|
|
|
|
end
|
2023-04-08 20:36:13 +00:00
|
|
|
|
|
|
|
def indexable_values
|
|
|
|
has_one&.title&.value
|
|
|
|
end
|
2023-04-08 15:47:51 +00:00
|
|
|
end
|