about summary refs log tree commit diff
path: root/app/controllers/admin/pending_accounts_controller.rb
blob: f297b7c9d0ec534f529e5047433ea57b4c09eef9 (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
65
66
# frozen_string_literal: true

module Admin
  class PendingAccountsController < BaseController
    before_action :set_accounts, only: :index

    def index
      @form = Form::AccountBatch.new
    end

    def batch
      names = Account.where(id: form_account_batch_params['account_ids'].map(&:to_i)).pluck(:username)

      @form = Form::AccountBatch.new(form_account_batch_params.merge(current_account: current_account, action: action_from_button))
      @form.save

      if action_from_button == 'approve'
        user_friendly_action_log(current_account, :approve_registration, names)
      else
        user_friendly_action_log(current_account, :reject_registration, names)
      end
    rescue ActionController::ParameterMissing
      flash[:alert] = I18n.t('admin.accounts.no_account_selected')
    ensure
      redirect_to admin_pending_accounts_path(current_params)
    end

    def approve_all
      account_ids = User.pending.pluck(:account_id)
      names = Account.where(id: account_ids).pluck(:username)
      Form::AccountBatch.new(current_account: current_account, account_ids: account_ids, action: 'approve').save
      user_friendly_action_log(current_account, :approve_registration, names, "Approved all peneding accounts.")
      redirect_to admin_pending_accounts_path(current_params)
    end

    def reject_all
      account_ids = User.pending.pluck(:account_id)
      names = Account.where(id: account_ids).pluck(:username)
      Form::AccountBatch.new(current_account: current_account, account_ids: account_ids, action: 'reject').save
      user_friendly_action_log(current_account, :reject_registration, names, "Rejected all pending accounts.")
      redirect_to admin_pending_accounts_path(current_params)
    end

    private

    def set_accounts
      @accounts = Account.joins(:user).merge(User.pending.recent).includes(user: :invite_request).page(params[:page])
    end

    def form_account_batch_params
      params.require(:form_account_batch).permit(:action, account_ids: [])
    end

    def action_from_button
      if params[:approve]
        'approve'
      elsif params[:reject]
        'reject'
      end
    end

    def current_params
      params.slice(:page).permit(:page)
    end
  end
end