invites_controller.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # frozen_string_literal: true
  2. module Admin
  3. class InvitesController < BaseController
  4. def index
  5. authorize :invite, :index?
  6. @invites = filtered_invites.includes(user: :account).page(params[:page])
  7. @invite = Invite.new
  8. end
  9. def create
  10. authorize :invite, :create?
  11. @invite = Invite.new(resource_params)
  12. @invite.user = current_user
  13. if @invite.save
  14. redirect_to admin_invites_path
  15. else
  16. @invites = Invite.page(params[:page])
  17. render :index
  18. end
  19. end
  20. def destroy
  21. @invite = Invite.find(params[:id])
  22. authorize @invite, :destroy?
  23. @invite.expire!
  24. redirect_to admin_invites_path
  25. end
  26. def deactivate_all
  27. authorize :invite, :deactivate_all?
  28. Invite.available.in_batches.update_all(expires_at: Time.now.utc)
  29. redirect_to admin_invites_path
  30. end
  31. private
  32. def resource_params
  33. params.require(:invite).permit(:max_uses, :expires_in)
  34. end
  35. def filtered_invites
  36. InviteFilter.new(filter_params).results
  37. end
  38. def filter_params
  39. params.slice(*InviteFilter::KEYS).permit(*InviteFilter::KEYS)
  40. end
  41. end
  42. end