-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbot_actions.rb
98 lines (82 loc) · 2.81 KB
/
bot_actions.rb
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
86
87
88
89
90
91
92
93
94
95
96
97
98
# frozen_string_literal: true
# Common bot actions
module BotActions
extend ActiveSupport::Concern
included do
before_action proc { namespace }
before_action proc { access_levels }
before_action proc { bot_account }, only: %i[destroy destroy_confirmation]
before_action proc { bot_type }, only: %i[create]
before_action proc { bot_accounts }
end
def index
authorize! @namespace, to: :view_bot_accounts?
@pagy, @bot_accounts = pagy(@bot_accounts)
end
def new
authorize! @namespace, to: :create_bot_accounts?
@new_bot_account = User.new(first_name: @namespace.type, last_name: 'Bot')
respond_to do |format|
format.turbo_stream do
render status: :ok
end
end
end
def create # rubocop:disable Metrics/MethodLength
@new_bot_account = Bots::CreateService.new(current_user, @namespace, @bot_type, bot_params).execute
respond_to do |format|
format.turbo_stream do
if @new_bot_account[:bot_user_account].persisted?
render status: :ok, locals: {
type: 'success',
message: t('concerns.bot_actions.create.success'),
personal_access_token: @new_bot_account[:personal_access_token]
}
else
render status: :unprocessable_entity,
locals:
{ type: 'alert',
message: @new_bot_account[:bot_user_account].errors.full_messages.first,
bot_params: }
end
end
end
end
def destroy_confirmation
authorize! @namespace, to: :destroy_bot_accounts?
render turbo_stream: turbo_stream.update('bot_modal',
partial: 'destroy_confirmation_modal',
locals: {
open: true,
bot_account: @bot_account
}), status: :ok
end
def destroy
Bots::DestroyService.new(@bot_account, current_user).execute
respond_to do |format|
format.turbo_stream do
if @bot_account.deleted?
flash[:success] = t('concerns.bot_actions.destroy.success')
redirect_to redirect_path
else
render status: :unprocessable_entity,
locals: {
type: 'alert',
message: @bot_account.errors.full_messages.first
}
end
end
end
end
private
def bot_account
id = params[:bot_id] || params[:id]
@bot_account = @namespace.namespace_bots.find_by(id:) || not_found
end
def access_levels
@access_levels = Member::AccessLevel.access_level_options_for_user(@namespace, current_user)
end
def bot_accounts
@bot_accounts = @namespace.namespace_bots.includes(:user)
end
end