Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. # frozen_string_literal: true
  2.  
  3. # Main Backoffice
  4. class Backoffice::AdminsController < BackofficeController
  5. before_action :set_admin, only: %i[edit update destroy]
  6. after_action :verify_authorized, only: :new
  7. after_action :verify_policy_scoped, only: :index
  8.  
  9. def index
  10. @admins = policy_scope(Admin)
  11. end
  12.  
  13. def new
  14. @admin = Admin.new
  15. authorize @admin
  16. end
  17.  
  18. def create
  19. @admin = Admin.new(params_admin)
  20. if @admin.save
  21. redirect_to backoffice_admins_path,
  22. notice: "Sucesso ao criar: Administrador (#{@admin.email})!"
  23. else
  24. render :new
  25. end
  26. end
  27.  
  28. def edit; end
  29.  
  30. def update
  31. if params_admin.values_at(:password, :password_confirmation).all?(&:blank?)
  32. admin_params = params_admin.except(%i[password password_confirmation])
  33. else
  34. admin_params = params_admin
  35. end
  36.  
  37. if @admin.update(admin_params)
  38. AdminMailer.update_email(current_admin, @admin).deliver_now
  39. redirect_to backoffice_admins_path,
  40. notice: "Sucesso ao alterar: Administrador (#{@admin.email})!"
  41. else
  42. render :edit
  43. end
  44. end
  45.  
  46. def destroy
  47. authorize @admin
  48. admin_email = @admin.email
  49. if @admin.destroy
  50. redirect_to backoffice_admins_path,
  51. notice: "Sucesso ao excluír: Administrador (#{admin_email})!"
  52. else
  53. render :index
  54. end
  55. end
  56.  
  57. private
  58.  
  59. def set_admin
  60. @admin = Admin.find(params[:id])
  61. end
  62.  
  63. def params_admin
  64. params.require(:admin).permit(
  65. :email, :name, :role, :password, :password_confirmation
  66. )
  67. end
  68. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement