broadcast_messages_controller.rb 1.56 KB
Newer Older
1
2
# frozen_string_literal: true

3
class Admin::BroadcastMessagesController < Admin::ApplicationController
4
5
  include BroadcastMessagesHelper

6
  before_action :finder, only: [:edit, :update, :destroy]
7

8
  # rubocop: disable CodeReuse/ActiveRecord
9
  def index
10
    @broadcast_messages = BroadcastMessage.order(ends_at: :desc).page(params[:page])
11
12
    @broadcast_message  = BroadcastMessage.new
  end
13
  # rubocop: enable CodeReuse/ActiveRecord
14
15

  def edit
16
17
18
  end

  def create
19
    @broadcast_message = BroadcastMessage.new(broadcast_message_params)
20
21

    if @broadcast_message.save
22
      redirect_to admin_broadcast_messages_path, notice: _('Broadcast Message was successfully created.')
23
24
25
26
27
    else
      render :index
    end
  end

28
29
  def update
    if @broadcast_message.update(broadcast_message_params)
30
      redirect_to admin_broadcast_messages_path, notice: _('Broadcast Message was successfully updated.')
31
32
33
34
35
    else
      render :edit
    end
  end

36
  def destroy
37
    @broadcast_message.destroy
38
39

    respond_to do |format|
40
      format.html { redirect_back_or_default(default: { action: 'index' }) }
41
      format.js { head :ok }
42
43
44
    end
  end

45
  def preview
46
47
    broadcast_message = BroadcastMessage.new(broadcast_message_params)
    render json: { message: render_broadcast_message(broadcast_message) }
48
49
  end

50
51
  protected

52
53
  def finder
    @broadcast_message = BroadcastMessage.find(params[:id])
54
  end
55
56

  def broadcast_message_params
57
58
59
60
61
62
    params.require(:broadcast_message).permit(%i(
      color
      ends_at
      font
      message
      starts_at
63
      target_path
64
      broadcast_type
65
    ))
66
  end
67
end