Class: BeesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/bees_controller.rb

Overview

Handles redirects of spelling bee

Instance Method Summary collapse

Instance Method Details

#editBee

This method sets the bee object to be edited

Parameters:

  • id (Integer)

    the id of bee object to be edited

Returns:



15
16
17
18
19
# File 'app/controllers/bees_controller.rb', line 15

def edit
  @bee = Bee.find(params[:id])
  @valid_words = WordsService.words(@bee.letters)
  @total_score = @valid_words.reduce(0) { |sum, word| sum + (word.length - 3) }
end

#indexArray<Bee>

This method shows the seven games from tomorrow

Returns:



6
7
8
9
# File 'app/controllers/bees_controller.rb', line 6

def index
  BeesService.set_week_bee()
  @bees = Bee.where(play_date: Date.tomorrow..Date.tomorrow + 6).order(:play_date)
end

#playnil

This method is the starting point of the game

Returns:

  • (nil)


40
41
42
43
44
45
46
47
48
# File 'app/controllers/bees_controller.rb', line 40

def play
  session[:sbwords] ||= []
  session[:sbscore] ||= 0
  BeesService.set_day_bee()
  @bee = Bee.find_by(play_date: Date.today)
  @aesthetic = Aesthetic.find_by(game_id: Game.find_by(name: "Spelling Bee").id)

  render "spellingbee"
end

#submit_guessnil

This method is the handler of game logic

Parameters:

  • the (String)

    guessed word

Returns:

  • (nil)


54
55
56
57
58
59
# File 'app/controllers/bees_controller.rb', line 54

def submit_guess
  @bee = Bee.find_by(play_date: Date.today)
  session[:sbwords], session[:sbscore], flash[:sb] = BeesService.guess(params[:sbword], session[:sbwords], session[:sbscore])
  update_stats(session[:sbscore])
  redirect_to bees_play_path
end

#updateBee

This method updates the bee object

Parameters:

  • id (Integer)

    the id of bee object to be updated

  • bee_params (Hash)

    the updates to be made

Returns:



26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/bees_controller.rb', line 26

def update
  @bee = Bee.find(params[:id])
  params[:bee][:ranks] = JSON.parse(params[:bee][:ranks])

  if @bee.update(bee_params)
    redirect_to edit_bee_path(@bee), notice: "Spelling Bee for #{@bee.play_date.strftime("%B %d")} updated successfully!"
  else
    redirect_to edit_bee_path(@bee), alert: "Invalid update"
  end
end