2019-08-06 23:17:29 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# La relación de un sitio con sus artículos, esto nos permite generar
|
|
|
|
# artículos como si estuviésemos usando ActiveRecord.
|
|
|
|
class PostRelation < Array
|
|
|
|
# No necesitamos cambiar el sitio
|
|
|
|
attr_reader :site
|
|
|
|
|
|
|
|
def initialize(site:)
|
|
|
|
@site = site
|
|
|
|
# Proseguimos la inicialización sin valores por defecto
|
|
|
|
super()
|
|
|
|
end
|
|
|
|
|
|
|
|
# Genera un artículo nuevo con los parámetros que le pasemos y lo suma
|
|
|
|
# al array
|
|
|
|
def build(**args)
|
2019-08-08 19:26:47 +00:00
|
|
|
args[:document] ||= build_document
|
|
|
|
args[:lang] ||= I18n.locale
|
|
|
|
args[:layout] = build_layout(args[:layout])
|
|
|
|
|
|
|
|
post = Post.new(site: site, **args)
|
|
|
|
|
|
|
|
self << post
|
|
|
|
post
|
2019-08-06 23:17:29 +00:00
|
|
|
end
|
|
|
|
|
2019-08-14 21:19:01 +00:00
|
|
|
def find(id)
|
|
|
|
super() do |p|
|
|
|
|
p.id == id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# Intenta guardar todos y devuelve true si pudo
|
|
|
|
def save_all
|
|
|
|
map(&:save).all?
|
|
|
|
end
|
2019-08-08 19:26:47 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-08-13 19:09:23 +00:00
|
|
|
def build_layout(layout = nil)
|
2019-08-08 19:26:47 +00:00
|
|
|
return layout if layout.is_a? Layout
|
|
|
|
|
2019-08-13 19:09:23 +00:00
|
|
|
site.layouts[layout || :post]
|
2019-08-08 19:26:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Devuelve una colección Jekyll que hace pasar el documento
|
|
|
|
def build_collection
|
|
|
|
Jekyll::Collection.new(site.jekyll, 'posts')
|
|
|
|
end
|
|
|
|
|
|
|
|
# Un documento borrador con algunas propiedades por defecto
|
|
|
|
def build_document
|
|
|
|
doc = Jekyll::Document.new('', site: site.jekyll,
|
|
|
|
collection: build_collection)
|
|
|
|
doc.data['date'] = Date.today.to_time
|
|
|
|
doc
|
|
|
|
end
|
2019-08-06 23:17:29 +00:00
|
|
|
end
|