ensl.org/app/controllers/articles_controller.rb
2020-03-20 01:31:04 +02:00

84 lines
2.1 KiB
Ruby

class ArticlesController < ApplicationController
before_action :get_article, only: [:show, :edit, :update, :cleanup, :destroy]
def index
@categories = Category.ordered.nospecial.domain Category::DOMAIN_ARTICLES
end
def news_index
@news = Article.with_comments.ordered.nodrafts.onlynews.limit(10)
@categories = Category.ordered.domain(Category::DOMAIN_NEWS)
end
def news_archive
@news = Article.with_comments.ordered.nodrafts.onlynews
end
def admin
raise AccessError unless cuser and cuser.admin?
# FIXME: something better?
@articles = {"Drafts" => Article.drafts.ordered, "Special" => Article.category(Category::SPECIAL).ordered}
end
def show
raise AccessError unless @article.can_show? cuser
@article.mark_as_read! for: cuser if cuser
# OBSOLETE
# @article.record_view_count(request.remote_ip, cuser.nil?)
end
def new
@article = Article.new
raise AccessError unless @article.can_create? cuser
end
def edit
raise AccessError unless @article.can_update? cuser
@file = DataFile.new
@file.directory_id = Directory::ARTICLES
@file.article = @article
end
def create
@article = Article.new(Article.article_params(params, cuser))
@article.user = cuser
raise AccessError unless @article.can_create? cuser
if @article.save
flash[:notice] = t(:articles_create)
redirect_to @article
else
render :new
end
end
def update
raise AccessError unless @article.can_update?(cuser, Article.article_params(params, cuser))
if @article.update_attributes(Article.article_params(params, cuser))
flash[:notice] = t(:articles_update)
redirect_to @article
else
render :edit
end
end
# TODO: link it somewhere
def cleanup
raise AccessError unless @article.can_update? cuser
@article.text = strip(@article.text)
@article.save!
redirect_to @article
end
def destroy
raise AccessError unless @article.can_destroy? cuser
@article.destroy
redirect_to_back
end
private
def get_article
@article = Article.find params[:id]
end
end