2020-05-26 22:26:19 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Los valores de este metadato son artículos en otros idiomas
|
2023-03-22 16:43:13 +00:00
|
|
|
class MetadataLocales < MetadataHasAndBelongsToMany
|
2020-05-26 22:26:19 +00:00
|
|
|
# Todos los valores posibles para cada idioma disponible
|
|
|
|
#
|
|
|
|
# @return { lang: { title: uuid } }
|
|
|
|
def values
|
|
|
|
@values ||= site.locales.map do |locale|
|
2023-03-22 17:26:06 +00:00
|
|
|
[locale, posts.where(lang: locale).map do |post|
|
2023-03-22 16:43:13 +00:00
|
|
|
[title(post), post.uuid.value]
|
2021-03-03 12:43:15 +00:00
|
|
|
end.to_h]
|
2020-11-07 23:51:00 +00:00
|
|
|
end.to_h
|
2020-05-26 22:26:19 +00:00
|
|
|
end
|
2023-03-22 17:26:06 +00:00
|
|
|
|
|
|
|
# Siempre hay una relación inversa
|
|
|
|
#
|
|
|
|
# @return [True]
|
|
|
|
def inverse?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
# El campo inverso se llama igual en el otro post
|
|
|
|
#
|
|
|
|
# @return [Symbol]
|
|
|
|
def inverse
|
|
|
|
:locales
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Obtiene todos los locales distintos a este post
|
|
|
|
#
|
|
|
|
# @return [Array]
|
|
|
|
def other_locales
|
|
|
|
site.locales.reject do |locale|
|
|
|
|
locale == post.lang.value.to_sym
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Obtiene todos los posts de los otros locales con el mismo layout
|
|
|
|
#
|
|
|
|
# @return [PostRelation]
|
|
|
|
def posts
|
|
|
|
other_locales.map do |locale|
|
|
|
|
site.posts(lang: locale).where(layout: post.layout.value)
|
2023-04-05 22:30:44 +00:00
|
|
|
end.reduce(&:concat) || PostRelation.new(site: site, lang: 'any')
|
2023-03-22 17:26:06 +00:00
|
|
|
end
|
2020-05-26 22:26:19 +00:00
|
|
|
end
|