summaryrefslogtreecommitdiff
path: root/app/controllers/admin/forums_controller.rb
blob: 3ca11ce8a3918112da5b4ebe5d3794c352df936c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# frozen_string_literal: true

class Admin::ForumsController < ApplicationController
  before_action :check_auth
  before_action :set_forum, only: [:show, :edit, :update, :destroy]

  def index
    @title = 'Admin - Forums'
    @forums = Forum.order(name: :asc)
    respond_to do |format|
      format.html
      format.json { render json: @forums }
    end
  end

  def new
    @title = 'New Forum'
    @forum = Forum.new
    respond_to do |format|
      format.html
      format.json { render json: @forum }
    end
  end

  def edit
    @title = "Editing Forum: #{@forum.name}"
  end

  def show
    @title = "Details for Forum: #{@forum.name}"
  end

  def create
    @forum = Forum.new(forum_params)
    respond_to do |format|
      if @forum.save
        format.html { redirect_to admin_forums_path, notice: 'Forum was successfully created.' }
        format.json { render json: @forum, status: :created, location: admin_forums_path }
      else
        format.html { render action: 'new' }
        format.json { render json: @forum.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @forum.update(forum_params)
        format.html { redirect_to admin_forums_path, notice: 'Forum was successfully updated.' }
        format.json { render json: @forum, status: :created, location: admin_forums_path }
      else
        format.html { render action: 'new' }
        format.json { render json: @forum.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @forum.destroy
    respond_to do |format|
      format.html { redirect_to admin_forums_url }
      format.json { head :ok }
    end
  end

  private

  def set_forum
    @forum = Forum.find_by(short_name: params[:id])
  end

  def check_auth
    authorize! :manage, Forum
  end

  def forum_params
    params.require(:forum).permit(:name, :short_name, :description, :access_level, :post_access_level)
  end
end