ensl.org/app/controllers/categories_controller.rb

71 lines
1.8 KiB
Ruby
Raw Normal View History

class CategoriesController < ApplicationController
before_action :get_category, except: [:index, :new, :create]
def show
if [Category::DOMAIN_ARTICLES, Category::DOMAIN_NEWS].include? @category.domain
@articles = Article.with_comments.ordered.limited.nodrafts.category params[:id]
2020-03-15 18:31:00 +00:00
Category.find(params[:id]).mark_as_read! for: cuser if cuser
2014-03-31 21:33:16 +00:00
render partial: 'articles/article', collection: @articles.to_a
end
end
def index
@categories = Category.ordered
end
def new
@category = Category.new
raise AccessError unless @category.can_create? cuser
end
def edit
raise AccessError unless @category.can_update? cuser
end
def create
@category = Category.new Category.params(params, cuser)
raise AccessError unless @category.can_create? cuser
if @category.save
# FIXME: move to model
@category.update_attribute :sort, @category.id
flash[:notice] = t(:articles_category)
redirect_to :categories
2014-04-27 00:47:53 +00:00
else
render action: :new
end
end
def update
raise AccessError unless @category.can_update? cuser
if @category.update_attributes Category.params(params, cuser)
flash[:notice] = t(:articles_category_update)
redirect_to :categories
end
end
def up
raise AccessError unless @category.can_update? cuser
2020-03-29 23:16:34 +00:00
@category.move_up(Category.where(domain: @category.domain), 'sort')
redirect_to :categories
end
def down
raise AccessError unless @category.can_update? cuser
2020-03-29 23:16:34 +00:00
@category.move_down(Category.where(domain: @category.domain), 'sort')
redirect_to :categories
end
def destroy
raise AccessError unless @category.can_destroy? cuser
@category.destroy
redirect_to :categories
end
private
def get_category
@category = Category.find params[:id]
end
end