1
0

rules_controller.rb 991 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # frozen_string_literal: true
  2. module Admin
  3. class RulesController < BaseController
  4. before_action :set_rule, except: [:index, :create]
  5. def index
  6. authorize :rule, :index?
  7. @rules = Rule.ordered
  8. @rule = Rule.new
  9. end
  10. def edit
  11. authorize @rule, :update?
  12. end
  13. def create
  14. authorize :rule, :create?
  15. @rule = Rule.new(resource_params)
  16. if @rule.save
  17. redirect_to admin_rules_path
  18. else
  19. @rules = Rule.ordered
  20. render :index
  21. end
  22. end
  23. def update
  24. authorize @rule, :update?
  25. if @rule.update(resource_params)
  26. redirect_to admin_rules_path
  27. else
  28. render :edit
  29. end
  30. end
  31. def destroy
  32. authorize @rule, :destroy?
  33. @rule.discard
  34. redirect_to admin_rules_path
  35. end
  36. private
  37. def set_rule
  38. @rule = Rule.find(params[:id])
  39. end
  40. def resource_params
  41. params.require(:rule).permit(:text, :priority)
  42. end
  43. end
  44. end