invites_controller.rb 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # frozen_string_literal: true
  2. class InvitesController < ApplicationController
  3. include Authorization
  4. layout 'admin'
  5. before_action :authenticate_user!
  6. before_action :set_body_classes
  7. before_action :set_cache_headers
  8. def index
  9. authorize :invite, :create?
  10. @invites = invites
  11. @invite = Invite.new
  12. end
  13. def create
  14. authorize :invite, :create?
  15. @invite = Invite.new(resource_params)
  16. @invite.user = current_user
  17. if @invite.save
  18. redirect_to invites_path
  19. else
  20. @invites = invites
  21. render :index
  22. end
  23. end
  24. def destroy
  25. @invite = invites.find(params[:id])
  26. authorize @invite, :destroy?
  27. @invite.expire!
  28. redirect_to invites_path
  29. end
  30. private
  31. def invites
  32. current_user.invites.order(id: :desc)
  33. end
  34. def resource_params
  35. params.require(:invite).permit(:max_uses, :expires_in, :autofollow, :comment)
  36. end
  37. def set_body_classes
  38. @body_classes = 'admin'
  39. end
  40. def set_cache_headers
  41. response.cache_control.replace(private: true, no_store: true)
  42. end
  43. end