about summary refs log tree commit diff
path: root/app/controllers/settings/keyword_mutes_controller.rb
blob: f79e1b320b78c4956b59332a762da23e169c1ac8 (plain) (blame)
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
# frozen_string_literal: true

class Settings::KeywordMutesController < ApplicationController
  layout 'admin'

  before_action :authenticate_user!
  before_action :load_keyword_mute, only: [:edit, :update, :destroy]

  def index
    @keyword_mutes = paginated_keyword_mutes_for_account
  end

  def new
    @keyword_mute = keyword_mutes_for_account.build
  end

  def create
    @keyword_mute = keyword_mutes_for_account.create(keyword_mute_params)

    if @keyword_mute.persisted?
      redirect_to settings_keyword_mutes_path, notice: I18n.t('generic.changes_saved_msg')
    else
      render :new
    end
  end

  def update
    if @keyword_mute.update(keyword_mute_params)
      redirect_to settings_keyword_mutes_path, notice: I18n.t('generic.changes_saved_msg')
    else
      render :edit
    end
  end

  def destroy
    @keyword_mute.destroy!

    redirect_to settings_keyword_mutes_path, notice: I18n.t('generic.changes_saved_msg')
  end

  def destroy_all
    keyword_mutes_for_account.delete_all

    redirect_to settings_keyword_mutes_path, notice: I18n.t('generic.changes_saved_msg')
  end

  private

  def keyword_mutes_for_account
    Glitch::KeywordMute.where(account: current_account)
  end

  def load_keyword_mute
    @keyword_mute = keyword_mutes_for_account.find(params[:id])
  end

  def keyword_mute_params
    params.require(:keyword_mute).permit(:keyword, :whole_word)
  end

  def paginated_keyword_mutes_for_account
    keyword_mutes_for_account.order(:keyword).page params[:page]
  end
end