2018-01-29 22:19:10 +00:00
|
|
|
class PostsController < ApplicationController
|
|
|
|
before_action :authenticate!
|
|
|
|
|
|
|
|
def index
|
2018-02-22 19:01:11 +00:00
|
|
|
@site = find_site
|
2018-05-08 21:14:00 +00:00
|
|
|
@lang = find_lang(@site)
|
2018-02-19 21:16:48 +00:00
|
|
|
@category = session[:category] = params.dig(:category)
|
2018-02-22 19:01:11 +00:00
|
|
|
@posts = @site.posts_for(@lang)
|
2018-04-30 20:50:29 +00:00
|
|
|
|
|
|
|
if params[:sort_by].present?
|
|
|
|
@posts.sort_by! do |p|
|
|
|
|
p.send(params[:sort_by].to_s)
|
|
|
|
end
|
|
|
|
end
|
2018-01-29 22:19:10 +00:00
|
|
|
end
|
2018-01-30 15:20:19 +00:00
|
|
|
|
|
|
|
def show
|
|
|
|
@site = find_site
|
2018-05-08 21:14:00 +00:00
|
|
|
@lang = find_lang(@site)
|
2018-01-30 15:20:19 +00:00
|
|
|
@post = find_post(@site)
|
|
|
|
end
|
2018-01-31 20:29:27 +00:00
|
|
|
|
2018-02-03 22:37:09 +00:00
|
|
|
def new
|
|
|
|
@site = find_site
|
2018-05-08 21:14:00 +00:00
|
|
|
@lang = find_lang(@site)
|
2018-05-08 21:57:11 +00:00
|
|
|
@template = find_template(@site)
|
|
|
|
@post = Post.new(site: @site,
|
|
|
|
front_matter: { date: Time.now },
|
|
|
|
lang: @lang,
|
|
|
|
template: @template)
|
2018-02-03 22:37:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@site = find_site
|
2018-05-08 21:14:00 +00:00
|
|
|
@lang = find_lang(@site)
|
2018-02-26 18:58:56 +00:00
|
|
|
@post = Post.new(site: @site, front_matter: post_params.to_hash, lang: @lang)
|
2018-02-03 22:37:09 +00:00
|
|
|
|
|
|
|
if @post.save
|
2018-02-24 21:24:11 +00:00
|
|
|
redirect_to site_posts_path(@site, lang: @lang)
|
2018-02-03 22:37:09 +00:00
|
|
|
else
|
|
|
|
render 'posts/new'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-31 20:29:27 +00:00
|
|
|
def edit
|
|
|
|
@site = find_site
|
2018-05-08 21:14:00 +00:00
|
|
|
@lang = find_lang(@site)
|
2018-01-31 20:29:27 +00:00
|
|
|
@post = find_post(@site)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
p = post_params
|
2018-02-02 22:20:31 +00:00
|
|
|
@site = find_site
|
2018-05-08 21:14:00 +00:00
|
|
|
@lang = find_lang(@site)
|
2018-02-02 22:20:31 +00:00
|
|
|
@post = find_post(@site)
|
2018-01-31 20:29:27 +00:00
|
|
|
|
2018-02-02 22:20:31 +00:00
|
|
|
@post.update_attributes(p)
|
2018-02-03 22:37:09 +00:00
|
|
|
|
2018-02-02 22:20:31 +00:00
|
|
|
if @post.save
|
2018-02-24 21:24:11 +00:00
|
|
|
redirect_to site_posts_path(@site, category: session[:category], lang: @lang)
|
2018-02-02 22:20:31 +00:00
|
|
|
else
|
|
|
|
render 'posts/edit'
|
|
|
|
end
|
2018-01-31 20:29:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-02-02 22:20:31 +00:00
|
|
|
# Solo permitir cambiar estos atributos de cada articulo
|
2018-01-31 20:29:27 +00:00
|
|
|
def post_params
|
2018-02-03 23:41:02 +00:00
|
|
|
params.require(:post).permit(:title, :date, :content, :slug,
|
2018-02-10 16:09:42 +00:00
|
|
|
:cover, :layout, :permalink, :objetivos, :dir,
|
2018-02-08 19:11:53 +00:00
|
|
|
:duracion, :formato, :habilidades, :recomendaciones,
|
2018-02-08 14:05:05 +00:00
|
|
|
conocimientos: [], sesiones_ejercicios_relacionados: [],
|
2018-02-08 16:02:20 +00:00
|
|
|
materiales_requeridos: [], lang: {},
|
2018-02-03 23:41:02 +00:00
|
|
|
tags: [], categories: [])
|
2018-01-31 20:29:27 +00:00
|
|
|
end
|
2018-01-29 22:19:10 +00:00
|
|
|
end
|