summaryrefslogtreecommitdiff
path: root/app/controllers/forumgroups_controller.rb
blob: 23446020f5558e5438d3f9148293b6137a929dd9 (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
80
81
82
83
84
85
class ForumgroupsController < ApplicationController

  def index
    redirect_to forums_path
  end

  def show
    if request.format.html?
      redirect_to forums_path + "#group-#{params[:id].to_i}"
    else
      fg = Forumgroup.find_by(id: params[:id])
      respond_to {|format| format.json {render json: (fg.attributes.to_json if fg.try(:can_read?, :current_user))}}
    end
  end

  def edit
    if admin?
      @group = Forumgroup.find(params[:id])
    else
      flash[:alert] = "You are not allowed to edit forum groups."
    end
  end

  def update
    if admin?
      @group = Forumgroup.find(params[:id])
      if @group.update_attributes(group_params)
        flash[:notice] = "Forum group updated"
        redirect_to @group
      else
        flash[:alert] = "Something went wrong"
        render :edit
      end
    else
      flash[:alert] = "You are not allowed to change forum groups"
    end
  end

  def new
    if admin?
      @group = Forumgroup.new
    else
      flash[:alert] = "You are not allowed to create forum groups."
      redirect_to forums_path
    end
  end

  def create
    if admin?
      @group = Forumgroup.new(group_params)
      if @group.save
        flash[:notice] = "Forum group created."
        redirect_to @group
      else
        flash[:alert] = "Something went wrong"
        render :new
      end
    else
      flash[:alert] = "You are not allowed to create forum groups."
      redirect_to forums_path
    end
  end

  def destroy
    if admin?
      @group = Forumgroup.find(params[:id])
      if @group.destroy
        flash[:notice] = "forum group deleted."
      else
        flash[:alert] = "Something went wrong"
      end
    else
      flash[:alert] = "You are not allowed to delete forum groups."
    end
    redirect_to forums_path
  end

  private

  def group_params(add = [])
    a = [:name, :position, :role_read_id, :role_write_id] + add
    params.require(:forumgroup).permit(a)
  end

end