From 98d71596c04399216304231967967cb3b441b835 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 23 Feb 2018 16:20:51 -0300 Subject: [PATCH] editar por idioma --- app/controllers/application_controller.rb | 6 ++++- app/controllers/posts_controller.rb | 7 ++++- app/models/post.rb | 32 ++++++++++++++++------- app/models/site.rb | 16 +++++++----- app/views/posts/_form.haml | 4 +-- app/views/posts/show.haml | 3 ++- 6 files changed, 48 insertions(+), 20 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ec46252..72679cf 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -22,11 +22,15 @@ class ApplicationController < ActionController::Base def find_post(site) id = params[:post_id] || params[:id] - lang = params.fetch(:lang, I18n.locale.to_s) + lang = find_lang posts = site.posts_for(lang) posts.find do |p| p.id == id end end + + def find_lang + params.fetch(:lang, I18n.locale.to_s) + end end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 7c09931..26b7b04 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -2,23 +2,26 @@ class PostsController < ApplicationController before_action :authenticate! def index + @lang = find_lang @site = find_site @category = session[:category] = params.dig(:category) - @lang = params.fetch(:lang, I18n.locale.to_s) @posts = @site.posts_for(@lang) end def show + @lang = find_lang @site = find_site @post = find_post(@site) end def new + @lang = find_lang @site = find_site @post = Post.new(site: @site, front_matter: { date: Time.now }) end def create + @lang = find_lang @site = find_site @post = Post.new(site: @site, front_matter: post_params.to_hash) @@ -30,12 +33,14 @@ class PostsController < ApplicationController end def edit + @lang = find_lang @site = find_site @post = find_post(@site) end def update p = post_params + @lang = find_lang @site = find_site @post = find_post(@site) diff --git a/app/models/post.rb b/app/models/post.rb index 203303c..63b9a16 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -36,13 +36,20 @@ class Post class: Jekyll::Document) end - @lang = lang || I18n.locale.to_s @site = site @post = post # los errores tienen que ser un hash para que # ActiveModel pueda traer los errores normalmente @errors = {} + # Si el sitio está traducido, trabajamos con la colección del + # idioma, sino, con posts + if @site.i18n? + @collection = @lang = lang || I18n.locale.to_s + else + @collection = 'posts' + end + # sincronizar los datos del document if new? @front_matter = {} @@ -66,7 +73,7 @@ class Post # Determina si fue traducido, buscando los slugs de su front_matter # lang en otras colecciones def translated? - get_front_matter('lang').present? + @site.i18n? && get_front_matter('lang').present? end def translations @@ -84,6 +91,7 @@ class Post # todos los datos para escribir el archivo, que es la condición # necesaria para poder crearlo :P def save + binding.pry cleanup! return false unless valid? @@ -120,7 +128,11 @@ class Post # Devuelve la ruta del post, si se cambió alguno de los datos, # generamos una ruta nueva para tener siempre la ruta actualizada. def path - basename_changed? ? File.join(@site.path, '_posts', basename_from_front_matter) : @post.try(:path) + if basename_changed? + File.join(@site.path, "_#{@collection}", basename_from_front_matter) + else + @post.try(:path) + end end # TODO los slugs se pueden repetir, el identificador real sería @@ -174,7 +186,7 @@ class Post # Genera un post nuevo y lo agrega a la colección del sitio. def new_post - opts = { site: @site.jekyll, collection: @site.jekyll.posts } + opts = { site: @site.jekyll, collection: @site.jekyll.collections[@collection] } @post = Jekyll::Document.new(path, opts) end @@ -182,11 +194,13 @@ class Post # # TODO no sería la forma correcta de hacerlo en Rails def add_post_to_site! - @site.jekyll.posts.docs << @post - @site.jekyll.posts.docs.sort! + @site.jekyll.collections[@collection].docs << @post + @site.jekyll.collections[@collection].docs.sort! - @site.posts << self unless @site.posts.include? self - @site.posts.sort! + unless @site.collections[@collection].include? self + @site.collections[@collection] << self + @site.collections[@collection].sort! + end end # Los define, asegurandose que las llaves siempre son strings, para no @@ -210,7 +224,7 @@ class Post # Reemplaza el post en el sitio por uno nuevo def replace_post! - @old_post = @site.jekyll.posts.docs.delete @post + @old_post = @site.jekyll.collections[@lang].docs.delete @post new_post end diff --git a/app/models/site.rb b/app/models/site.rb index 2f51e8b..97f37b1 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -2,12 +2,13 @@ # Usuaria class Site - attr_accessor :jekyll + attr_accessor :jekyll, :collections attr_reader :path def initialize(jekyll:, path: nil) @jekyll = jekyll @path = path || @jekyll.config['source'] + @collections = {} end # Determina si el sitio está en varios idiomas @@ -61,6 +62,10 @@ class Site @jekyll.config end + def collections + @collections + end + # Los posts de este sitio, si el sitio está traducido, trae los posts # del idioma actual, porque _posts va a estar vacío def posts @@ -77,9 +82,8 @@ class Site def posts_for(collection) return unless @jekyll.config['collections'].key? collection - # TODO tal vez no necesitamos esta magia - instance = instance_variable_get("@#{collection}") - return instance if instance + _collection = @collections[collection] + return _collection if _collection Rails.logger.info "Procesando #{collection}" @@ -92,9 +96,9 @@ class Site # Los convertimos a una clase intermedia capaz de acceder a sus # datos y modificarlos - instance_variable_set("@#{collection}", col.map do |post| + @collections[collection] = col.map do |post| Post.new(site: self, post: post, lang: collection) - end) + end end def categories diff --git a/app/views/posts/_form.haml b/app/views/posts/_form.haml index 4481863..9696bf0 100644 --- a/app/views/posts/_form.haml +++ b/app/views/posts/_form.haml @@ -10,10 +10,10 @@ - field_class = "form-control #{direction}" -# TODO habilitar form_for - if @post.new? - - url = site_posts_path(@site) + - url = site_posts_path(@site, lang: @lang) - method = :post - else - - url = site_post_path(@site, @post) + - url = site_post_path(@site, @post, lang: @lang) - method = :patch = form_tag url, method: method, class: 'form' do .form-group diff --git a/app/views/posts/show.haml b/app/views/posts/show.haml index 165df24..8d9eef2 100644 --- a/app/views/posts/show.haml +++ b/app/views/posts/show.haml @@ -13,7 +13,8 @@ .row .col - = link_to t('posts.edit'), edit_site_post_path(@site, @post), + = link_to t('posts.edit'), + edit_site_post_path(@site, @post, lang: @lang), class: 'btn btn-info' .row