summaryrefslogtreecommitdiff
path: root/app/controllers/admin/topics_controller.rb
blob: b451928e5919a5709a14cb5df796a099ff6ba054 (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
# frozen_string_literal: true

class Admin::TopicsController < Admin::ApplicationController
  include SendFileUpload
  include PreviewMarkdown

  before_action :topic, only: [:edit, :update, :destroy]

  feature_category :projects

  def index
    @topics = Projects::TopicsFinder.new(params: params.permit(:search)).execute.page(params[:page]).without_count
  end

  def new
    @topic = Projects::Topic.new
  end

  def edit
  end

  def create
    @topic = Projects::Topic.new(topic_params)

    if @topic.save
      redirect_to edit_admin_topic_path(@topic), notice: _('Topic %{topic_name} was successfully created.') % { topic_name: @topic.name }
    else
      render "new"
    end
  end

  def update
    if @topic.update(topic_params)
      redirect_to edit_admin_topic_path(@topic), notice: _('Topic was successfully updated.')
    else
      render "edit"
    end
  end

  def destroy
    @topic.destroy!

    redirect_to admin_topics_path,
                status: :found,
                notice: _('Topic %{topic_name} was successfully removed.') % { topic_name: @topic.title_or_name }
  end

  private

  def topic
    @topic ||= Projects::Topic.find(params[:id])
  end

  def topic_params
    params.require(:projects_topic).permit(allowed_topic_params)
  end

  def allowed_topic_params
    [
      :avatar,
      :description,
      :name,
      :title
    ]
  end
end