Class: Dokno::CategoriesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/dokno/categories_controller.rb

Instance Method Summary collapse

Methods included from PaginationConcern

#paginate

Methods included from UserConcern

#authorize, #can_edit?, #sanitized_user_obj_string, #user, #username

Instance Method Details

#createObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/dokno/categories_controller.rb', line 37

def create
  @category = Category.new(name: params[:name], parent: Category.find_by(code: params[:parent_category_code]))

  if @category.save
    flash[:green] = 'Category was created'
    redirect_to article_index_path(@category.code)
  else
    flash.now[:red]       = 'Category could not be created'
    @parent_category_code = params[:parent_category_code]
    render :new
  end
end

#destroyObject



63
64
65
66
67
68
# File 'app/controllers/dokno/categories_controller.rb', line 63

def destroy
  Category.find(params[:id].to_i).destroy!

  flash[:green] = 'Category was deleted'
  render json: {}, layout: false
end

#editObject



32
33
34
35
# File 'app/controllers/dokno/categories_controller.rb', line 32

def edit
  return redirect_to root_path if @category.blank?
  @parent_category_code = @category.parent&.code
end

#indexObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/dokno/categories_controller.rb', line 8

def index
  @search_term        = params[:search_term]
  @order              = params[:order]&.strip
  @order              = 'updated' unless %w(updated newest views alpha).include?(@order)
  @show_up_for_review = can_edit? && !request.path.include?(up_for_review_path)

  articles = if request.path.include? up_for_review_path
               Article.up_for_review(order: @order&.to_sym)
             elsif @search_term.present?
               Article.search(term: @search_term, category_id: @category&.id, order: @order&.to_sym)
             elsif @category.present?
               @category.articles_in_branch(order: @order&.to_sym)
             else
               Article.uncategorized(order: @order&.to_sym)
             end

  @articles = paginate(articles)
end

#newObject



27
28
29
30
# File 'app/controllers/dokno/categories_controller.rb', line 27

def new
  @category             = Category.new
  @parent_category_code = params[:parent_category_code]
end

#updateObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/dokno/categories_controller.rb', line 50

def update
  return redirect_to root_path if @category.blank?

  if @category.update(name: params[:name], parent: Category.find_by(code: params[:parent_category_code]))
    flash[:green] = 'Category was updated'
    redirect_to article_index_path(@category.code)
  else
    flash.now[:red]       = 'Category could not be updated'
    @parent_category_code = params[:parent_category_code]
    render :edit
  end
end