From 851da77ce77c997ca2ef3396e0abdfa7598d8bcc Mon Sep 17 00:00:00 2001 From: f Date: Wed, 31 Jan 2018 17:29:27 -0300 Subject: [PATCH] wip de edicion de articulos --- app/assets/stylesheets/application.scss | 1 + app/controllers/posts_controller.rb | 22 ++++++++++++++++++++++ app/models/post.rb | 4 +++- app/views/posts/_form.haml | 24 ++++++++++++++++++++++++ app/views/posts/edit.haml | 9 +++++++++ app/views/posts/show.haml | 3 +++ config/locales/en.yml | 10 ++++++++++ config/locales/es.yml | 8 ++++++++ 8 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 app/views/posts/_form.haml create mode 100644 app/views/posts/edit.haml diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index eead430f..3818947a 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -35,4 +35,5 @@ body { textarea.post-content { min-height: 80vh; font-family: monospace; + font-size: 90%; } diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 3a98367a..9a017a29 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -9,4 +9,26 @@ class PostsController < ApplicationController @site = find_site @post = find_post(@site) end + + def edit + @site = find_site + @post = find_post(@site) + end + + def update + p = post_params + + # crear un array a partir de una cadena separada por comas + [:tags,:categories].each do |comma| + p[comma] = p.dig(comma).split(',').map(&:strip) + end + + binding.pry + end + + private + + def post_params + params.require(:post).permit(:title, :date, :tags, :categories, :content) + end end diff --git a/app/models/post.rb b/app/models/post.rb index 9e7af91c..39b8cf4c 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -15,7 +15,9 @@ class Post def initialize(site:, post:, front_matter: {}) @site = site @post = post - @errors = [] + # los errores tienen que ser un hash para que + # ActiveModel pueda traer los errores normalmente + @errors = {} @front_matter = front_matter new_post! unless @post diff --git a/app/views/posts/_form.haml b/app/views/posts/_form.haml new file mode 100644 index 00000000..ad9468f0 --- /dev/null +++ b/app/views/posts/_form.haml @@ -0,0 +1,24 @@ += form_tag site_post_path(@site, @post), method: :patch, class: 'form' do + .form-group + = submit_tag t('posts.save'), class: 'btn btn-success' + .form-group + = text_field 'post', 'title', value: @post.title, class: 'form-control', + placeholder: t('posts.title') + .form-group + = text_area_tag 'post[content]', @post.content, autofocus: true, + class: 'form-control post-content' + .form-group + = label_tag 'post_date', t('posts.date') + = date_field 'post', 'date', value: @post.date.strftime('%F'), + class: 'form-control' + %small.text-muted.form-text= t('posts.date_help') + .form-group + = label_tag 'post_categories', t('posts.categories') + = text_field 'post', 'categories', value: @post.categories.join(', '), + class: 'form-control' + %small.text-muted.form-text= t('posts.tags_help') + .form-group + = label_tag 'post_tags', t('posts.tags') + = text_field 'post', 'tags', value: @post.tags.join(', '), + class: 'form-control' + %small.text-muted.form-text= t('posts.tags_help') diff --git a/app/views/posts/edit.haml b/app/views/posts/edit.haml new file mode 100644 index 00000000..cc291978 --- /dev/null +++ b/app/views/posts/edit.haml @@ -0,0 +1,9 @@ +.container-fluid + .row + %h1 + = t('posts.editing') + = @post.title + + .row + .col + = render 'posts/form' diff --git a/app/views/posts/show.haml b/app/views/posts/show.haml index 7df0997b..ce7bcf2c 100644 --- a/app/views/posts/show.haml +++ b/app/views/posts/show.haml @@ -2,6 +2,9 @@ %h1= @post.title + = link_to t('posts.edit'), edit_site_post_path(@site, @post), + class: 'btn btn-info' + .content :markdown #{@post.content} diff --git a/config/locales/en.yml b/config/locales/en.yml index c28e990e..f2713112 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -7,3 +7,13 @@ en: title: 'Sites' footer: powered_by: 'is developed by' + posts: + edit: 'Edit' + save: 'Save' + date: 'Publication date' + date_help: 'This changes the articles order!' + title: 'Title' + tags: 'Tags' + tags_help: 'Comma separated!' + categories: 'Categories' + categories_help: 'Comma separated!' diff --git a/config/locales/es.yml b/config/locales/es.yml index 6092f60e..07ca386a 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -7,3 +7,11 @@ es: title: 'Sitios' footer: powered_by: 'es desarrollada por' + posts: + edit: 'Editar' + save: 'Guardar' + date: 'Fecha de publicación' + date_help: '¡Esto cambia el orden de los artículos!' + title: 'Título' + categories: 'Categorías' + categories_help: '¡Separadas por comas!'