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.

108 lines
3.5 KiB

10 months ago
6 months ago
10 months ago
6 months ago
10 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
10 months ago
10 months ago
5 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
5 months ago
10 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
  43. # logger.info(Rails.application.routes.url_helpers.root_url)
  44. end
  45. # GET /scores/new
  46. def new
  47. @score = Score.new
  48. flash.now[:notice] = 'Unique name is mandatory!'
  49. flash.now[:alert] = 'Your book was not found'
  50. sleep 0.3
  51. end
  52. # GET /scores/1/edit
  53. def edit; end
  54. # POST /scores
  55. def create
  56. @score = Score.new(score_params)
  57. if @score.save
  58. redirect_to @score, notice: 'Score was successfully created.'
  59. else
  60. render :new, status: :unprocessable_entity
  61. end
  62. end
  63. # PATCH/PUT /scores/1
  64. def update
  65. if @score.update(score_params)
  66. redirect_to @score, notice: 'Score was successfully updated.', status: :see_other
  67. else
  68. render :edit, status: :unprocessable_entity
  69. end
  70. end
  71. # DELETE /scores/1
  72. def destroy
  73. do_an_exception
  74. @score.destroy!
  75. redirect_to scores_url, notice: 'Score was successfully destroyed.', status: :see_other
  76. end
  77. private
  78. def do_an_exception
  79. raise 'Unable to destroy this score. ' * 5
  80. end
  81. # Use callbacks to share common setup or constraints between actions.
  82. def set_score
  83. @score = Score.find(params[:id])
  84. @score.grade
  85. end
  86. # Only allow a list of trusted parameters through.
  87. def score_params
  88. params.require(:score).permit(:name, :grade)
  89. end
  90. end