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

module Admin
  class InvitesController < BaseController
    def index
      authorize :invite, :index?

      @invites = filtered_invites.includes(user: :account).page(params[:page])
      @invite  = Invite.new
    end

    def create
      authorize :invite, :create?

      @invite      = Invite.new(resource_params)
      @invite.user = current_user

      if @invite.save
        redirect_to admin_invites_path
      else
        @invites = Invite.page(params[:page])
        render :index
      end
    end

    def destroy
      @invite = Invite.find(params[:id])
      authorize @invite, :destroy?
      @invite.expire!
      redirect_to admin_invites_path
    end

    def deactivate_all
      authorize :invite, :deactivate_all?
      Invite.available.in_batches.update_all(expires_at: Time.now.utc)
      redirect_to admin_invites_path
    end

    private

    def resource_params
      params.require(:invite).permit(:max_uses, :expires_in)
    end

    def filtered_invites
      InviteFilter.new(filter_params).results
    end

    def filter_params
      params.slice(*InviteFilter::KEYS).permit(*InviteFilter::KEYS)
    end
  end
end