You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.4 KiB

9 months ago
4 months ago
9 months ago
4 months ago
9 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
9 months ago
8 months ago
9 months ago
8 months ago
8 months ago
9 months ago
9 months ago
9 months ago
  1. # ScoresController define Score and Grade interactions
  2. class ScoresController < ApplicationController
  3. include Pagy::Backend
  4. include AnsiColors
  5. before_action :set_score, only: %i[show edit update destroy]
  6. def grayshade(n)
  7. shade = eval("TEXT_GRAY_#{n}")
  8. shade += n.to_s
  9. end
  10. # GET /scores
  11. def index
  12. @pagy, @scores = pagy(Score.all)
  13. # logger.debug 'this is a debug message'
  14. # logger.info 'this is a super long message which should be wrapped. ' * 5
  15. shades = [800, 700, 600, 500, 400, 300, 200, 100].map { |n| grayshade(n) }.join(' ')
  16. logger.info(BG_BLACK + shades)
  17. logger.info(BG_GRAY + shades)
  18. logger.info(BG_WHITE + shades)
  19. logger.info(BG_BLACK + TEXT_GRAY_200 + "this is #{BOLD}a message in bold#{CLEAR}#{TEXT_GRAY_200}, isnt'it?")
  20. logger.info(BG_BLACK + TEXT_GRAY_200 + "this is #{TEXT_WHITE}a message in bold#{CLEAR}#{TEXT_GRAY_200}, isnt'it?")
  21. logger.info(BG_BLACK + TEXT_GRAY_200 + "this is #{BOLD}#{TEXT_WHITE}a message in bold#{CLEAR}#{TEXT_GRAY_200}, isnt'it?")
  22. logger.info(BG_GRAY + TEXT_GRAY_200 + "this is #{BOLD}a message in bold#{CLEAR}#{TEXT_GRAY_200}, isnt'it?")
  23. logger.info(BG_WHITE + TEXT_GRAY_800 + "this is #{BOLD}a message in bold#{CLEAR}, isnt'it?")
  24. logger.info(BG_WHITE + TEXT_GRAY_600 + "this is #{BOLD}a message in bold#{CLEAR}, isnt'it?")
  25. logger.info(BG_WHITE + TEXT_GRAY_400 + "this is #{BOLD}a message in bold#{CLEAR}, isnt'it?")
  26. logger.info(BG_WHITE + TEXT_GRAY_300 + "this is #{BOLD}a message in bold#{CLEAR}, isnt'it?")
  27. logger.info(BG_WHITE + TEXT_GRAY_200 + "this is #{BOLD}a message in bold#{CLEAR}, isnt'it?")
  28. logger.info(BG_WHITE + TEXT_GRAY_100 + "this is #{BOLD}a message in bold#{CLEAR}, isnt'it?")
  29. logger.info({ one: 1, two: 2 })
  30. logger.info 'this is an information', { four: 4, five: 5 }
  31. logger.debug BigDecimal('0.0003')
  32. logger.warn 'scores are', @scores
  33. logger.warn "this is a #{BOLD}warning#{CLEAR}#{BG_YELLOW}#{TEXT_BLACK}\n yop\n cool"
  34. logger.warn "this is a warning\n yop\n cool"
  35. logger.error 'this is an error message'
  36. logger.error "error message line #1\nerror message line #2\nerror message line #3\n\n"
  37. logger.info 'end of normal message'
  38. logger.debug @scores
  39. # sleep 0.5
  40. end
  41. # GET /scores/1
  42. def show; end
  43. # GET /scores/new
  44. def new
  45. @score = Score.new
  46. flash.now[:notice] = 'Unique name is mandatory!'
  47. flash.now[:alert] = 'Your book was not found'
  48. sleep 0.3
  49. end
  50. # GET /scores/1/edit
  51. def edit; end
  52. # POST /scores
  53. def create
  54. @score = Score.new(score_params)
  55. if @score.save
  56. redirect_to @score, notice: 'Score was successfully created.'
  57. else
  58. render :new, status: :unprocessable_entity
  59. end
  60. end
  61. # PATCH/PUT /scores/1
  62. def update
  63. if @score.update(score_params)
  64. redirect_to @score, notice: 'Score was successfully updated.', status: :see_other
  65. else
  66. render :edit, status: :unprocessable_entity
  67. end
  68. end
  69. # DELETE /scores/1
  70. def destroy
  71. do_an_exception
  72. @score.destroy!
  73. redirect_to scores_url, notice: 'Score was successfully destroyed.', status: :see_other
  74. end
  75. private
  76. def do_an_exception
  77. raise 'Unable to destroy this score' * 5
  78. end
  79. # Use callbacks to share common setup or constraints between actions.
  80. def set_score
  81. @score = Score.find(params[:id])
  82. @score.grade
  83. end
  84. # Only allow a list of trusted parameters through.
  85. def score_params
  86. params.require(:score).permit(:name, :grade)
  87. end
  88. end