From efc1aad5bca65856889384cd3338b6fe9602d3d0 Mon Sep 17 00:00:00 2001 From: sorgal Date: Wed, 15 Jan 2014 18:01:07 +0400 Subject: [PATCH 01/15] tasks added --- .idea/workspace.xml | 388 ++++++++++-------- ...e_codes.js.coffee => game_tasks.js.coffee} | 0 ...e_hints.js.coffee => task_codes.js.coffee} | 0 app/assets/javascripts/task_hints.js.coffee | 3 + app/assets/javascripts/tasks.js.coffee | 3 + app/assets/javascripts/user_tasks.js.coffee | 3 + ...ame_codes.css.scss => game_tasks.css.scss} | 2 +- app/assets/stylesheets/task_codes.css.scss | 3 + app/assets/stylesheets/task_hints.css.scss | 3 + .../{game_hints.css.scss => tasks.css.scss} | 2 +- app/assets/stylesheets/user_games.css.scss | 2 +- app/assets/stylesheets/user_tasks.css.scss | 3 + app/controllers/code_compares_controller.rb | 18 +- app/controllers/codes_controller.rb | 16 +- ...controller.rb => game_tasks_controller.rb} | 27 +- app/controllers/games_controller.rb | 23 +- app/controllers/hints_controller.rb | 16 +- app/controllers/home_controller.rb | 21 +- app/controllers/task_codes_controller.rb | 38 ++ ...controller.rb => task_hints_controller.rb} | 22 +- app/controllers/tasks_controller.rb | 99 +++++ app/controllers/user_games_controller.rb | 18 +- app/controllers/user_tasks_controller.rb | 40 ++ app/helpers/game_codes_helper.rb | 2 - app/helpers/game_hints_helper.rb | 2 - app/helpers/game_tasks_helper.rb | 2 + app/helpers/task_codes_helper.rb | 2 + app/helpers/task_hints_helper.rb | 2 + app/helpers/tasks_helper.rb | 2 + app/helpers/user_tasks_helper.rb | 2 + app/models/code.rb | 3 +- app/models/code_compare.rb | 2 + app/models/game.rb | 2 - app/models/game_code.rb | 4 - app/models/game_hint.rb | 4 - app/models/game_task.rb | 4 + app/models/hint.rb | 2 +- app/models/task.rb | 7 + app/models/task_code.rb | 4 + app/models/task_hint.rb | 5 + app/models/user.rb | 3 + app/models/user_game.rb | 1 - app/models/user_task.rb | 4 + app/views/codes/_form.html.slim | 2 +- app/views/hints/_form.html.slim | 2 +- app/views/home/game_code_compares.html.slim | 10 - app/views/tasks/_form.html.slim | 13 + app/views/tasks/edit.html.slim | 4 + app/views/tasks/index.html.slim | 25 ++ app/views/tasks/index.json.jbuilder | 4 + app/views/tasks/new.html.slim | 4 + app/views/tasks/show.html.slim | 19 + app/views/tasks/show.json.jbuilder | 1 + app/views/user_games/_form.html.slim | 2 +- app/views/user_games/index.html.slim | 2 + app/views/user_games/show.html.slim | 2 +- config/routes.rb | 47 ++- .../20140113070022_create_game_hints.rb | 10 - ...20140113101225_add_result_to_user_games.rb | 5 - .../20140113170656_create_game_codes.rb | 10 - .../20140114114821_add_state_to_games.rb | 2 +- ...124729_remove_result_from_code_compares.rb | 5 - db/migrate/20140115081012_create_tasks.rb | 10 + .../20140115082513_create_task_codes.rb | 10 + .../20140115082554_create_task_hints.rb | 10 + ...rb => 20140115090623_create_user_games.rb} | 2 +- .../20140115090713_create_user_tasks.rb | 11 + ...b => 20140115091137_create_admin_games.rb} | 0 ...=> 20140115092553_create_code_compares.rb} | 1 - .../20140115100758_create_game_tasks.rb | 10 + db/schema.rb | 44 +- .../code_compares_controller_spec.rb | 32 +- spec/controllers/codes_controller_spec.rb | 20 +- ..._spec.rb => game_tasks_controller_spec.rb} | 22 +- spec/controllers/games_controller_spec.rb | 12 + spec/controllers/hints_controller_spec.rb | 20 +- spec/controllers/home_controller_spec.rb | 16 +- ..._spec.rb => task_codes_controller_spec.rb} | 31 +- .../controllers/task_hints_controller_spec.rb | 57 +++ spec/controllers/tasks_controller_spec.rb | 166 ++++++++ .../controllers/user_games_controller_spec.rb | 8 +- .../controllers/user_tasks_controller_spec.rb | 57 +++ .../{game_codes.rb => game_tasks.rb} | 4 +- spec/factories/task_codes.rb | 8 + .../{game_hints.rb => task_hints.rb} | 4 +- spec/factories/tasks.rb | 8 + spec/factories/user_games.rb | 1 - spec/factories/user_tasks.rb | 9 + ...lper_spec.rb => game_tasks_helper_spec.rb} | 6 +- ...lper_spec.rb => task_codes_helper_spec.rb} | 6 +- spec/helpers/task_hints_helper_spec.rb | 15 + spec/helpers/tasks_helper_spec.rb | 15 + spec/helpers/user_tasks_helper_spec.rb | 15 + .../{game_code_spec.rb => game_task_spec.rb} | 2 +- .../{game_hint_spec.rb => task_code_spec.rb} | 2 +- spec/models/task_hint_spec.rb | 5 + spec/models/task_spec.rb | 5 + spec/models/user_game_spec.rb | 6 +- spec/models/user_task_spec.rb | 5 + spec/routing/game_hints_routing_spec.rb | 21 - ...ing_spec.rb => game_tasks_routing_spec.rb} | 6 +- spec/routing/task_codes_routing_spec.rb | 15 + spec/routing/task_hints_routing_spec.rb | 15 + spec/routing/tasks_routing_spec.rb | 35 ++ spec/routing/user_tasks_routing_spec.rb | 15 + spec/support/controller_macros.rb | 2 - spec/views/tasks/edit.html.slim_spec.rb | 15 + spec/views/tasks/index.html.slim_spec.rb | 17 + spec/views/tasks/new.html.slim_spec.rb | 15 + spec/views/tasks/show.html.slim_spec.rb | 12 + spec/views/user_games/index.html.slim_spec.rb | 2 +- 111 files changed, 1340 insertions(+), 463 deletions(-) rename app/assets/javascripts/{game_codes.js.coffee => game_tasks.js.coffee} (100%) rename app/assets/javascripts/{game_hints.js.coffee => task_codes.js.coffee} (100%) create mode 100644 app/assets/javascripts/task_hints.js.coffee create mode 100644 app/assets/javascripts/tasks.js.coffee create mode 100644 app/assets/javascripts/user_tasks.js.coffee rename app/assets/stylesheets/{game_codes.css.scss => game_tasks.css.scss} (64%) create mode 100644 app/assets/stylesheets/task_codes.css.scss create mode 100644 app/assets/stylesheets/task_hints.css.scss rename app/assets/stylesheets/{game_hints.css.scss => tasks.css.scss} (63%) create mode 100644 app/assets/stylesheets/user_tasks.css.scss rename app/controllers/{game_codes_controller.rb => game_tasks_controller.rb} (55%) create mode 100644 app/controllers/task_codes_controller.rb rename app/controllers/{game_hints_controller.rb => task_hints_controller.rb} (58%) create mode 100644 app/controllers/tasks_controller.rb create mode 100644 app/controllers/user_tasks_controller.rb delete mode 100644 app/helpers/game_codes_helper.rb delete mode 100644 app/helpers/game_hints_helper.rb create mode 100644 app/helpers/game_tasks_helper.rb create mode 100644 app/helpers/task_codes_helper.rb create mode 100644 app/helpers/task_hints_helper.rb create mode 100644 app/helpers/tasks_helper.rb create mode 100644 app/helpers/user_tasks_helper.rb delete mode 100644 app/models/game_code.rb delete mode 100644 app/models/game_hint.rb create mode 100644 app/models/game_task.rb create mode 100644 app/models/task.rb create mode 100644 app/models/task_code.rb create mode 100644 app/models/task_hint.rb create mode 100644 app/models/user_task.rb delete mode 100644 app/views/home/game_code_compares.html.slim create mode 100644 app/views/tasks/_form.html.slim create mode 100644 app/views/tasks/edit.html.slim create mode 100644 app/views/tasks/index.html.slim create mode 100644 app/views/tasks/index.json.jbuilder create mode 100644 app/views/tasks/new.html.slim create mode 100644 app/views/tasks/show.html.slim create mode 100644 app/views/tasks/show.json.jbuilder delete mode 100644 db/migrate/20140113070022_create_game_hints.rb delete mode 100644 db/migrate/20140113101225_add_result_to_user_games.rb delete mode 100644 db/migrate/20140113170656_create_game_codes.rb delete mode 100644 db/migrate/20140114124729_remove_result_from_code_compares.rb create mode 100644 db/migrate/20140115081012_create_tasks.rb create mode 100644 db/migrate/20140115082513_create_task_codes.rb create mode 100644 db/migrate/20140115082554_create_task_hints.rb rename db/migrate/{20140113061527_create_user_games.rb => 20140115090623_create_user_games.rb} (83%) create mode 100644 db/migrate/20140115090713_create_user_tasks.rb rename db/migrate/{20140112202805_create_admin_games.rb => 20140115091137_create_admin_games.rb} (100%) rename db/migrate/{20140114083234_create_code_compares.rb => 20140115092553_create_code_compares.rb} (84%) create mode 100644 db/migrate/20140115100758_create_game_tasks.rb rename spec/controllers/{game_hints_controller_spec.rb => game_tasks_controller_spec.rb} (73%) rename spec/controllers/{game_codes_controller_spec.rb => task_codes_controller_spec.rb} (69%) create mode 100644 spec/controllers/task_hints_controller_spec.rb create mode 100644 spec/controllers/tasks_controller_spec.rb create mode 100644 spec/controllers/user_tasks_controller_spec.rb rename spec/factories/{game_codes.rb => game_tasks.rb} (75%) create mode 100644 spec/factories/task_codes.rb rename spec/factories/{game_hints.rb => task_hints.rb} (75%) create mode 100644 spec/factories/tasks.rb create mode 100644 spec/factories/user_tasks.rb rename spec/helpers/{game_codes_helper_spec.rb => game_tasks_helper_spec.rb} (77%) rename spec/helpers/{game_hints_helper_spec.rb => task_codes_helper_spec.rb} (77%) create mode 100644 spec/helpers/task_hints_helper_spec.rb create mode 100644 spec/helpers/tasks_helper_spec.rb create mode 100644 spec/helpers/user_tasks_helper_spec.rb rename spec/models/{game_code_spec.rb => game_task_spec.rb} (80%) rename spec/models/{game_hint_spec.rb => task_code_spec.rb} (80%) create mode 100644 spec/models/task_hint_spec.rb create mode 100644 spec/models/task_spec.rb create mode 100644 spec/models/user_task_spec.rb delete mode 100644 spec/routing/game_hints_routing_spec.rb rename spec/routing/{game_codes_routing_spec.rb => game_tasks_routing_spec.rb} (50%) create mode 100644 spec/routing/task_codes_routing_spec.rb create mode 100644 spec/routing/task_hints_routing_spec.rb create mode 100644 spec/routing/tasks_routing_spec.rb create mode 100644 spec/routing/user_tasks_routing_spec.rb create mode 100644 spec/views/tasks/edit.html.slim_spec.rb create mode 100644 spec/views/tasks/index.html.slim_spec.rb create mode 100644 spec/views/tasks/new.html.slim_spec.rb create mode 100644 spec/views/tasks/show.html.slim_spec.rb diff --git a/.idea/workspace.xml b/.idea/workspace.xml index bcc6b51..15fdd9c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -23,46 +23,37 @@ - - + + - + - - + + - + - - + + - + - - + + - - - - - - - - - - + @@ -78,57 +69,57 @@ @@ -164,7 +155,6 @@ - @@ -198,6 +188,24 @@ + + @@ -488,6 +522,7 @@ + @@ -498,20 +533,19 @@ - - + - + @@ -522,8 +556,8 @@ - + @@ -544,359 +578,359 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + diff --git a/app/assets/javascripts/game_codes.js.coffee b/app/assets/javascripts/game_tasks.js.coffee similarity index 100% rename from app/assets/javascripts/game_codes.js.coffee rename to app/assets/javascripts/game_tasks.js.coffee diff --git a/app/assets/javascripts/game_hints.js.coffee b/app/assets/javascripts/task_codes.js.coffee similarity index 100% rename from app/assets/javascripts/game_hints.js.coffee rename to app/assets/javascripts/task_codes.js.coffee diff --git a/app/assets/javascripts/task_hints.js.coffee b/app/assets/javascripts/task_hints.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/task_hints.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/javascripts/tasks.js.coffee b/app/assets/javascripts/tasks.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/tasks.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/javascripts/user_tasks.js.coffee b/app/assets/javascripts/user_tasks.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/user_tasks.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/game_codes.css.scss b/app/assets/stylesheets/game_tasks.css.scss similarity index 64% rename from app/assets/stylesheets/game_codes.css.scss rename to app/assets/stylesheets/game_tasks.css.scss index 6497235..e37ba02 100644 --- a/app/assets/stylesheets/game_codes.css.scss +++ b/app/assets/stylesheets/game_tasks.css.scss @@ -1,3 +1,3 @@ -// Place all the styles related to the game_codes controller here. +// Place all the styles related to the game_tasks controller here. // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/task_codes.css.scss b/app/assets/stylesheets/task_codes.css.scss new file mode 100644 index 0000000..4e74da2 --- /dev/null +++ b/app/assets/stylesheets/task_codes.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the task_codes controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/task_hints.css.scss b/app/assets/stylesheets/task_hints.css.scss new file mode 100644 index 0000000..cc2a89d --- /dev/null +++ b/app/assets/stylesheets/task_hints.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the task_hints controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/game_hints.css.scss b/app/assets/stylesheets/tasks.css.scss similarity index 63% rename from app/assets/stylesheets/game_hints.css.scss rename to app/assets/stylesheets/tasks.css.scss index 4198b63..b57862e 100644 --- a/app/assets/stylesheets/game_hints.css.scss +++ b/app/assets/stylesheets/tasks.css.scss @@ -1,3 +1,3 @@ -// Place all the styles related to the GameHints controller here. +// Place all the styles related to the tasks controller here. // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/user_games.css.scss b/app/assets/stylesheets/user_games.css.scss index 55b461e..20bb9f5 100644 --- a/app/assets/stylesheets/user_games.css.scss +++ b/app/assets/stylesheets/user_games.css.scss @@ -1,3 +1,3 @@ -// Place all the styles related to the UserGames controller here. +// Place all the styles related to the user_games controller here. // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/user_tasks.css.scss b/app/assets/stylesheets/user_tasks.css.scss new file mode 100644 index 0000000..8d5d305 --- /dev/null +++ b/app/assets/stylesheets/user_tasks.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the user_tasks controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/code_compares_controller.rb b/app/controllers/code_compares_controller.rb index c85d6bc..1f00918 100644 --- a/app/controllers/code_compares_controller.rb +++ b/app/controllers/code_compares_controller.rb @@ -1,7 +1,7 @@ class CodeComparesController < ApplicationController before_action :set_code_compare, only: [:destroy] skip_before_filter :authorize_admin - def create + def create @code_compare = CodeCompare.new(code_compare_params) respond_to do |format| @@ -24,13 +24,13 @@ def destroy end private - # Use callbacks to share common setup or constraints between actions. - def set_code_compare - @code_compare = CodeCompare.find(params[:id]) - end + # Use callbacks to share common setup or constraints between actions. + def set_code_compare + @code_compare = CodeCompare.find(params[:id]) + end - # Never trust parameters from the scary internet, only allow the white list through. - def code_compare_params - params.require(:code_compare).permit(:user_id, :code_id) - end + # Never trust parameters from the scary internet, only allow the white list through. + def code_compare_params + params.require(:code_compare).permit(:user_id, :code_id) + end end diff --git a/app/controllers/codes_controller.rb b/app/controllers/codes_controller.rb index e7cd73b..345530a 100644 --- a/app/controllers/codes_controller.rb +++ b/app/controllers/codes_controller.rb @@ -1,7 +1,7 @@ class CodesController < ApplicationController before_action :set_code, only: [:show, :edit, :update, :destroy] - before_filter :check_game, only: [:new] - before_filter :check_game_create, only: [:create] + before_filter :check_task, only: [:new] + before_filter :check_task_create, only: [:create] before_filter :no_codes, except: [:new, :create] # GET /codes @@ -28,10 +28,10 @@ def edit # POST /codes.json def create @code = Code.new(code_params) - game = params.require(:code)[:game].to_i + task = params.require(:code)[:task].to_i respond_to do |format| if @code.save - if GameCode.create(game_id: game, code_id: @code.id) + if TaskCode.create(task_id: task, code_id: @code.id) format.html { redirect_to @code, notice: 'Code was successfully created.' } format.json { render action: 'show', status: :created, location: @code } end @@ -79,14 +79,14 @@ def code_params protected - def check_game - unless params.require(:game) + def check_task + unless params.require(:task) redirect_to games_path end end - def check_game_create - unless params.require(:code) + def check_task_create + unless params.require(:code)[:task] redirect_to games_path end end diff --git a/app/controllers/game_codes_controller.rb b/app/controllers/game_tasks_controller.rb similarity index 55% rename from app/controllers/game_codes_controller.rb rename to app/controllers/game_tasks_controller.rb index cbc7999..54d489b 100644 --- a/app/controllers/game_codes_controller.rb +++ b/app/controllers/game_tasks_controller.rb @@ -1,13 +1,13 @@ -class GameCodesController < ApplicationController - before_action :set_game_code, only: [:show, :edit, :update, :destroy] +class GameTasksController < ApplicationController + before_action :set_game_task, only: [:show, :edit, :update, :destroy] - # POST /game_codes - # POST /game_codes.json + # POST /game_tasks + # POST /game_tasks.json def create - @game_code = GameCode.new(game_code_params) + @game_task = GameTask.new(game_task_params) respond_to do |format| - if @game_code.save + if @game_task.save format.html { head :no_content } format.json { head :no_content } else @@ -17,11 +17,10 @@ def create end end - - # DELETE /game_codes/1 - # DELETE /game_codes/1.json + # DELETE /game_tasks/1 + # DELETE /game_tasks/1.json def destroy - @game_code.destroy + @game_task.destroy respond_to do |format| format.html { head :no_content } format.json { head :no_content } @@ -30,12 +29,12 @@ def destroy private # Use callbacks to share common setup or constraints between actions. - def set_game_code - @game_code = GameCode.find(params[:id]) + def set_game_task + @game_task = GameTask.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. - def game_code_params - params.require(:game_code).permit(:game_id, :code_id) + def game_task_params + params.require(:game_task).permit(:game_id, :task_id) end end diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index 8e553dd..c8d03bb 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -1,11 +1,11 @@ class GamesController < ApplicationController - before_action :set_game, only: [:show, :edit, :update, :destroy] + before_action :set_game, only: [:show, :edit, :update, :destroy, :start_game] skip_before_filter :authorize_admin, only: [:index, :show] # GET /games # GET /games.json def index - @games = Game.all + @games = Game.where(state: 0) end # GET /games/1 @@ -73,7 +73,20 @@ def set_game end # Never trust parameters from the scary internet, only allow the white list through. - def game_params - params.require(:game).permit(:title, :start_date, :duration) - end + def game_params + params.require(:game).permit(:title, :start_date, :duration) + end + + public + + def start_game + @game.update(state: 1) + @user_games = UserGame.where(game_id: @game.id) + @game_tasks = GameTask.where(game_id: @game.id).first + @user_games.each do |user_game| + @user_tasks.create(user_id: @user_game.id, task_id: @game_tasks.first.task_id, result: 0) + end + redirect_to games_path + end + end diff --git a/app/controllers/hints_controller.rb b/app/controllers/hints_controller.rb index 5650bf3..928638b 100644 --- a/app/controllers/hints_controller.rb +++ b/app/controllers/hints_controller.rb @@ -1,6 +1,6 @@ class HintsController < ApplicationController - before_filter :check_game, only: [:new] - before_filter :check_game_create, only: [:create] + before_filter :check_task, only: [:new] + before_filter :check_task_create, only: [:create] before_filter :no_hints, except: [:new, :create] before_action :set_hint, only: [:show, :edit, :update, :destroy] skip_before_filter :authorize_admin, only: [:show] @@ -30,10 +30,10 @@ def edit # POST /hints.json def create @hint = Hint.new(hint_params) - game = params.require(:hint)[:game].to_i + task = params.require(:hint)[:task].to_i respond_to do |format| if @hint.save - if GameHint.create(game_id: game, hint_id: @hint.id) + if TaskHint.create(task_id: task, hint_id: @hint.id) format.html { redirect_to @hint, notice: 'Hint was successfully created.' } format.json { render action: 'show', status: :created, location: @hint } end @@ -81,14 +81,14 @@ def hint_params protected - def check_game - unless params.require(:game) + def check_task + unless params.require(:task) redirect_to games_path end end - def check_game_create - unless params.require(:hint) + def check_task_create + unless params.require(:hint)[:task] redirect_to games_path end end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 7c3997a..3ce1d6f 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -6,20 +6,15 @@ def index @games = Game.all end - def game_code_compares - @game = Game.find(params[:game].to_i) - @game_codes = GameCode.where(game_id: @game.id) - end - def create_code_compare - @code = Code.where(params[:try_text]) - if @code.count > 0 - code_compare_params = {} - code_compare_params[:code_compare] = {user_id: current_user.id, game_id: params[:game].to_i} - CodeCompare.create(code_compare_params) - else - redirect_to "home/game_code_compares", notice: "Code does not match" - end + #@code = Code.where(params[:try_text]).joins() + #if @code.count > 0 + # code_compare_params = {} + # code_compare_params[:code_compare] = {user_id: current_user.id, game_id: params[:game].to_i} + # CodeCompare.create(code_compare_params) + #else + # redirect_to "home/game_code_compares", notice: "Code does not match" + #end end end diff --git a/app/controllers/task_codes_controller.rb b/app/controllers/task_codes_controller.rb new file mode 100644 index 0000000..8360edd --- /dev/null +++ b/app/controllers/task_codes_controller.rb @@ -0,0 +1,38 @@ +class TaskCodesController < ApplicationController + before_action :set_task_code, only: [:destroy] + + def create + @task_code = TaskCode.new(task_code_params) + + respond_to do |format| + if @task_code.save + format.html { head :no_content } + format.json { head :no_content } + else + format.html { head :no_content } + format.json { head :no_content } + end + end + end + + # DELETE /task_codes/1 + # DELETE /task_codes/1.json + def destroy + @task_code.destroy + respond_to do |format| + format.html { head :no_content } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_task_code + @task_code = TaskCode.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def task_code_params + params.require(:task_code).permit(:task_id, :code_id) + end +end diff --git a/app/controllers/game_hints_controller.rb b/app/controllers/task_hints_controller.rb similarity index 58% rename from app/controllers/game_hints_controller.rb rename to app/controllers/task_hints_controller.rb index e5722a8..00c7875 100644 --- a/app/controllers/game_hints_controller.rb +++ b/app/controllers/task_hints_controller.rb @@ -1,13 +1,11 @@ -class GameHintsController < ApplicationController - before_action :set_game_hint, only: [:destroy] +class TaskHintsController < ApplicationController + before_action :set_task_hint, only: [:destroy] - # POST /game_hints - # POST /game_hints.json def create - @game_hint = GameHint.new(game_hint_params) + @task_hint = TaskHint.new(task_hint_params) respond_to do |format| - if @game_hint.save + if @task_hint.save format.html { head :no_content } format.json { head :no_content } else @@ -17,8 +15,10 @@ def create end end + # DELETE /task_hints/1 + # DELETE /task_hints/1.json def destroy - @game_hint.destroy + @task_hint.destroy respond_to do |format| format.html { head :no_content } format.json { head :no_content } @@ -27,12 +27,12 @@ def destroy private # Use callbacks to share common setup or constraints between actions. - def set_game_hint - @game_hint = GameHint.find(params[:id]) + def set_task_hint + @task_hint = TaskHint.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. - def game_hint_params - params.require(:game_hint).permit(:game_id, :hint_id) + def task_hint_params + params.require(:task_hint).permit(:task_id, :hint_id) end end diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb new file mode 100644 index 0000000..a5b7d9c --- /dev/null +++ b/app/controllers/tasks_controller.rb @@ -0,0 +1,99 @@ +class TasksController < ApplicationController + before_action :set_task, only: [:show, :edit, :update, :destroy] + before_filter :check_game, only: [:new] + before_filter :check_game_create, only: [:create] + before_filter :no_tasks, except: [:new, :create] + + # GET /tasks + # GET /tasks.json + def index + @tasks = Task.all + end + + # GET /tasks/1 + # GET /tasks/1.json + def show + end + + # GET /tasks/new + def new + @task = Task.new + end + + # GET /tasks/1/edit + def edit + end + + # POST /tasks + # POST /tasks.json + def create + @task = Task.new(task_params) + game = params.require(:task)[:game].to_i + respond_to do |format| + if @task.save + if GameTask.create(game_id: game, task_id: @task.id) + format.html { redirect_to @task, notice: 'Task was successfully created.' } + format.json { render action: 'show', status: :created, location: @code } + end + else + format.html { render action: 'new' } + format.json { render json: @code.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /tasks/1 + # PATCH/PUT /tasks/1.json + def update + respond_to do |format| + if @task.update(task_params) + format.html { redirect_to @task, notice: 'Task was successfully updated.' } + format.json { head :no_content } + else + format.html { render action: 'edit' } + format.json { render json: @task.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /tasks/1 + # DELETE /tasks/1.json + def destroy + @task.destroy + respond_to do |format| + format.html { redirect_to tasks_url } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_task + @task = Task.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def task_params + params.require(:task).permit(:task_text, :points) + end + + protected + + def check_game + unless params.require(:game) + redirect_to games_path + end + end + + def check_game_create + unless params.require(:task)[:game] + redirect_to games_path + end + end + + def no_tasks + unless Task.all.count > 0 + redirect_to games_path + end + end +end diff --git a/app/controllers/user_games_controller.rb b/app/controllers/user_games_controller.rb index 3c97edb..f23f252 100644 --- a/app/controllers/user_games_controller.rb +++ b/app/controllers/user_games_controller.rb @@ -74,15 +74,15 @@ def destroy end private - # Use callbacks to share common setup or constraints between actions. - def set_user_game - @user_game = UserGame.find(params[:id]) - end + # Use callbacks to share common setup or constraints between actions. + def set_user_game + @user_game = UserGame.find(params[:id]) + end - # Never trust parameters from the scary internet, only allow the white list through. - def user_game_params - params.require(:user_game).permit(:user_id, :game_id, :result) - end + # Never trust parameters from the scary internet, only allow the white list through. + def user_game_params + params.require(:user_game).permit(:user_id, :game_id, :result) + end protected @@ -91,4 +91,4 @@ def check_game_create redirect_to root_path end end -end +end \ No newline at end of file diff --git a/app/controllers/user_tasks_controller.rb b/app/controllers/user_tasks_controller.rb new file mode 100644 index 0000000..d85708d --- /dev/null +++ b/app/controllers/user_tasks_controller.rb @@ -0,0 +1,40 @@ +class UserTasksController < ApplicationController + before_action :set_user_task, only: [:destroy] + skip_before_filter :authorize_admin + before_filter :authenticate_user! + + def create + @user_task = UserTask.new(user_task_params) + + respond_to do |format| + if @user_task.save + format.html { head :no_content } + format.json { head :no_content } + else + format.html { head :no_content } + format.json { head :no_content } + end + end + end + + # DELETE /user_tasks/1 + # DELETE /user_tasks/1.json + def destroy + @user_task.destroy + respond_to do |format| + format.html { head :no_content } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_user_task + @user_task = UserTask.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def user_task_params + params.require(:user_task).permit(:user_id, :task_id, :result) + end +end diff --git a/app/helpers/game_codes_helper.rb b/app/helpers/game_codes_helper.rb deleted file mode 100644 index fa04e65..0000000 --- a/app/helpers/game_codes_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module GameCodesHelper -end diff --git a/app/helpers/game_hints_helper.rb b/app/helpers/game_hints_helper.rb deleted file mode 100644 index 0779689..0000000 --- a/app/helpers/game_hints_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module GameHintsHelper -end diff --git a/app/helpers/game_tasks_helper.rb b/app/helpers/game_tasks_helper.rb new file mode 100644 index 0000000..552b0a2 --- /dev/null +++ b/app/helpers/game_tasks_helper.rb @@ -0,0 +1,2 @@ +module GameTasksHelper +end diff --git a/app/helpers/task_codes_helper.rb b/app/helpers/task_codes_helper.rb new file mode 100644 index 0000000..2c31b21 --- /dev/null +++ b/app/helpers/task_codes_helper.rb @@ -0,0 +1,2 @@ +module TaskCodesHelper +end diff --git a/app/helpers/task_hints_helper.rb b/app/helpers/task_hints_helper.rb new file mode 100644 index 0000000..1f9d1f7 --- /dev/null +++ b/app/helpers/task_hints_helper.rb @@ -0,0 +1,2 @@ +module TaskHintsHelper +end diff --git a/app/helpers/tasks_helper.rb b/app/helpers/tasks_helper.rb new file mode 100644 index 0000000..ce894d0 --- /dev/null +++ b/app/helpers/tasks_helper.rb @@ -0,0 +1,2 @@ +module TasksHelper +end diff --git a/app/helpers/user_tasks_helper.rb b/app/helpers/user_tasks_helper.rb new file mode 100644 index 0000000..596de7c --- /dev/null +++ b/app/helpers/user_tasks_helper.rb @@ -0,0 +1,2 @@ +module UserTasksHelper +end diff --git a/app/models/code.rb b/app/models/code.rb index e834eeb..5ded048 100644 --- a/app/models/code.rb +++ b/app/models/code.rb @@ -1,4 +1,5 @@ class Code < ActiveRecord::Base - has_one :game_code, dependent: :destroy + has_one :task_code, dependent: :destroy + has_many :code_compares, dependent: :destroy validate :code_string, presence: true end diff --git a/app/models/code_compare.rb b/app/models/code_compare.rb index e90aa48..30f4b70 100644 --- a/app/models/code_compare.rb +++ b/app/models/code_compare.rb @@ -1,2 +1,4 @@ class CodeCompare < ActiveRecord::Base + belongs_to :user + belongs_to :code end diff --git a/app/models/game.rb b/app/models/game.rb index 67065eb..c1cc3c3 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -2,8 +2,6 @@ class Game < ActiveRecord::Base has_one :admin_game, dependent: :destroy has_many :user_games, dependent: :destroy - has_many :game_hints, dependent: :destroy - has_many :game_codes, dependent: :destroy validate :title, :start_date, :duration, presence: true diff --git a/app/models/game_code.rb b/app/models/game_code.rb deleted file mode 100644 index 589db33..0000000 --- a/app/models/game_code.rb +++ /dev/null @@ -1,4 +0,0 @@ -class GameCode < ActiveRecord::Base - belongs_to :game - belongs_to :code -end diff --git a/app/models/game_hint.rb b/app/models/game_hint.rb deleted file mode 100644 index 4f4aea6..0000000 --- a/app/models/game_hint.rb +++ /dev/null @@ -1,4 +0,0 @@ -class GameHint < ActiveRecord::Base - belongs_to :game - belongs_to :hint -end diff --git a/app/models/game_task.rb b/app/models/game_task.rb new file mode 100644 index 0000000..9c03994 --- /dev/null +++ b/app/models/game_task.rb @@ -0,0 +1,4 @@ +class GameTask < ActiveRecord::Base + belongs_to :game + belongs_to :task +end diff --git a/app/models/hint.rb b/app/models/hint.rb index 6e77c9b..79bbf6f 100644 --- a/app/models/hint.rb +++ b/app/models/hint.rb @@ -1,5 +1,5 @@ class Hint < ActiveRecord::Base - has_one :game_hint, dependent: :destroy + has_one :task_hint, dependent: :destroy validate :hint_text, :queue_number, presence: true end diff --git a/app/models/task.rb b/app/models/task.rb new file mode 100644 index 0000000..fd7eddb --- /dev/null +++ b/app/models/task.rb @@ -0,0 +1,7 @@ +class Task < ActiveRecord::Base + has_many :task_hints, dependent: :destroy + has_many :task_codes, dependent: :destroy + has_many :user_tasks, dependent: :destroy + + validate :task_text, :points, presence: true +end diff --git a/app/models/task_code.rb b/app/models/task_code.rb new file mode 100644 index 0000000..df6a839 --- /dev/null +++ b/app/models/task_code.rb @@ -0,0 +1,4 @@ +class TaskCode < ActiveRecord::Base + belongs_to :task + belongs_to :code +end diff --git a/app/models/task_hint.rb b/app/models/task_hint.rb new file mode 100644 index 0000000..52e7c0d --- /dev/null +++ b/app/models/task_hint.rb @@ -0,0 +1,5 @@ +class TaskHint < ActiveRecord::Base + belongs_to :task + + belongs_to :hint +end diff --git a/app/models/user.rb b/app/models/user.rb index 27a78a2..55da2f0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,6 +2,9 @@ class User < ActiveRecord::Base # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable has_many :user_games, dependent: :destroy + has_many :code_compares, dependent: :destroy + has_many :code_compares, dependent: :destroy + has_many :user_tasks, dependent: :destroy devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable end diff --git a/app/models/user_game.rb b/app/models/user_game.rb index 4ce4cda..4ff8f59 100644 --- a/app/models/user_game.rb +++ b/app/models/user_game.rb @@ -3,5 +3,4 @@ class UserGame < ActiveRecord::Base belongs_to :game validate :user_id, :game_id, :result, presence: true - end diff --git a/app/models/user_task.rb b/app/models/user_task.rb new file mode 100644 index 0000000..6d45865 --- /dev/null +++ b/app/models/user_task.rb @@ -0,0 +1,4 @@ +class UserTask < ActiveRecord::Base + belongs_to :user + belongs_to :task +end diff --git a/app/views/codes/_form.html.slim b/app/views/codes/_form.html.slim index 8457f0a..8c849e5 100644 --- a/app/views/codes/_form.html.slim +++ b/app/views/codes/_form.html.slim @@ -4,7 +4,7 @@ .controls = f.text_field :code_string, :class => 'text_field' .controls - = f.hidden_field :game, value: @game + = f.hidden_field :task, value: @game .form-actions = f.submit nil, :class => 'btn btn-primary' ' diff --git a/app/views/hints/_form.html.slim b/app/views/hints/_form.html.slim index 3cc8f97..5ef2398 100644 --- a/app/views/hints/_form.html.slim +++ b/app/views/hints/_form.html.slim @@ -8,7 +8,7 @@ .controls = f.text_field :queue_number, :class => 'text_field' .controls - = f.hidden_field :game, value: @game + = f.hidden_field :task, value: @game .form-actions = f.submit nil, :class => 'btn btn-primary' = link_to t('.cancel', :default => t("helpers.links.cancel")), hints_path, :class => 'btn' diff --git a/app/views/home/game_code_compares.html.slim b/app/views/home/game_code_compares.html.slim deleted file mode 100644 index fec76fd..0000000 --- a/app/views/home/game_code_compares.html.slim +++ /dev/null @@ -1,10 +0,0 @@ -- if @game.state >= 0 - table class="table table-striped" - tbody - - @game_codes.each do |game_code| - tr - td= render partial: form -- else - div id="main" - p - span class="redbox" Game was not started \ No newline at end of file diff --git a/app/views/tasks/_form.html.slim b/app/views/tasks/_form.html.slim new file mode 100644 index 0000000..6af4282 --- /dev/null +++ b/app/views/tasks/_form.html.slim @@ -0,0 +1,13 @@ += form_for @task, :html => { :class => "form-horizontal" } do |f| + .control-group + = f.label :task_text, :class => 'control-label' + .controls + = f.text_field :task_text, :class => 'text_field' + .control-group + = f.label :points, :class => 'control-label' + .controls + = f.text_field :points, :class => 'text_field' + .form-actions + = f.submit nil, :class => 'btn btn-primary' + ' + = link_to t('.cancel', :default => t("helpers.links.cancel")), tasks_path, :class => 'btn' diff --git a/app/views/tasks/edit.html.slim b/app/views/tasks/edit.html.slim new file mode 100644 index 0000000..b186334 --- /dev/null +++ b/app/views/tasks/edit.html.slim @@ -0,0 +1,4 @@ +- model_class = Task +div class="page-header" + h1=t '.title', :default => [:'helpers.titles.edit', 'Edit %{model}'], :model => model_class.model_name.human.titleize += render :partial => "form" diff --git a/app/views/tasks/index.html.slim b/app/views/tasks/index.html.slim new file mode 100644 index 0000000..399f488 --- /dev/null +++ b/app/views/tasks/index.html.slim @@ -0,0 +1,25 @@ +- model_class = Task +div class="page-header" + h1=t '.title', :default => model_class.model_name.human.pluralize.titleize +table class="table table-striped" + thead + tr + th= model_class.human_attribute_name(:id) + th= model_class.human_attribute_name(:task_text) + th= model_class.human_attribute_name(:points) + th= model_class.human_attribute_name(:created_at) + th=t '.actions', :default => t("helpers.actions") + tbody + - @tasks.each do |task| + tr + td= link_to task.id, task_path(task) + td= task.task_text + td= task.points + td=l task.created_at + td + = link_to t('.edit', :default => t("helpers.links.edit")), edit_task_path(task), :class => 'btn btn-mini' + ' + = link_to t('.destroy', :default => t("helpers.links.destroy")), task_path(task), :method => :delete, :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-mini btn-danger' + += link_to t('.new', :default => t("helpers.links.new")), new_task_path, :class => 'btn btn-primary' + diff --git a/app/views/tasks/index.json.jbuilder b/app/views/tasks/index.json.jbuilder new file mode 100644 index 0000000..5008e50 --- /dev/null +++ b/app/views/tasks/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@tasks) do |task| + json.extract! task, :id + json.url task_url(task, format: :json) +end diff --git a/app/views/tasks/new.html.slim b/app/views/tasks/new.html.slim new file mode 100644 index 0000000..0a0930b --- /dev/null +++ b/app/views/tasks/new.html.slim @@ -0,0 +1,4 @@ +- model_class = Task +div class="page-header" + h1=t '.title', :default => [:'helpers.titles.new', 'New %{model}'], :model => model_class.model_name.human.titleize += render :partial => "form" diff --git a/app/views/tasks/show.html.slim b/app/views/tasks/show.html.slim new file mode 100644 index 0000000..a8e56d0 --- /dev/null +++ b/app/views/tasks/show.html.slim @@ -0,0 +1,19 @@ +- model_class = Task +div class="page-header" + h1=t '.title', :default => model_class.model_name.human.titleize + +p + strong= model_class.human_attribute_name(:task_text) + ':' + br + = @task.task_text +p + strong= model_class.human_attribute_name(:points) + ':' + br + = @task.points + +.form-actions + = link_to t('.back', :default => t("helpers.links.back")), tasks_path, :class => 'btn' + ' + = link_to t('.edit', :default => t("helpers.links.edit")), edit_task_path(@task), :class => 'btn' + ' + = link_to t('.destroy', :default => t("helpers.links.destroy")), task_path(@task), :method => "delete", :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-danger' diff --git a/app/views/tasks/show.json.jbuilder b/app/views/tasks/show.json.jbuilder new file mode 100644 index 0000000..f03fde3 --- /dev/null +++ b/app/views/tasks/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @task, :id, :created_at, :updated_at diff --git a/app/views/user_games/_form.html.slim b/app/views/user_games/_form.html.slim index 6acd66a..3c49db0 100644 --- a/app/views/user_games/_form.html.slim +++ b/app/views/user_games/_form.html.slim @@ -14,4 +14,4 @@ .form-actions = f.submit nil, :class => 'btn btn-primary' ' - = link_to t('.cancel', :default => t("helpers.links.cancel")), user_games_path, :class => 'btn' + = link_to t('.cancel', :default => t("helpers.links.cancel")), user_games_path, :class => 'btn' \ No newline at end of file diff --git a/app/views/user_games/index.html.slim b/app/views/user_games/index.html.slim index 87554e4..bbf8251 100644 --- a/app/views/user_games/index.html.slim +++ b/app/views/user_games/index.html.slim @@ -19,3 +19,5 @@ table class="table table-striped" td=l game.created_at //td = link_to t('.destroy', :default => t("helpers.links.destroy")), user_game_path({id: game[:user_game]}), :method => :delete, :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-mini btn-danger' + + diff --git a/app/views/user_games/show.html.slim b/app/views/user_games/show.html.slim index c3932be..2b7b127 100644 --- a/app/views/user_games/show.html.slim +++ b/app/views/user_games/show.html.slim @@ -20,4 +20,4 @@ p ' = link_to t('.edit', :default => t("helpers.links.edit")), edit_user_game_path(@user_game), :class => 'btn' ' - = link_to t('.destroy', :default => t("helpers.links.destroy")), user_game_path(@user_game), :method => "delete", :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-danger' + = link_to t('.destroy', :default => t("helpers.links.destroy")), user_game_path(@user_game), :method => "delete", :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-danger' \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index a71daaa..7645542 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,37 +1,62 @@ Nightzone::Application.routes.draw do + controller :game_tasks do + post 'game_tasks/', :to => 'game_tasks#create' + delete 'game_tasks/:id', :to => 'game_tasks#destroy' + end + controller :code_compares do post 'code_compares/', :to => 'code_compares#create' delete 'code_compares/:id', :to => 'code_compares#destroy' end - controller :game_codes do - post 'game_codes/', :to => 'game_codes#create' - delete 'game_codes/:id', :to => 'game_codes#destroy' + controller :user_tasks do + post 'user_tasks/', :to => 'user_tasks#create' + delete 'user_tasks/:id', :to => 'user_tasks#destroy' end - resources :codes + resources :user_games - controller :game_hints do - post 'game_hints/', :to => 'game_hints#create' - delete 'game_hints/:id', :to => 'game_hints#destroy' + controller :task_hints do + post 'task_hints/', :to => 'task_hints#create' + delete 'task_hints/:id', :to => 'task_hints#destroy' end - resources :user_games + controller :task_codes do + post 'task_codes/', :to => 'task_codes#create' + delete 'task_codes/:id', :to => 'task_codes#destroy' + end - resources :hints + resources :tasks + + controller :code_compares do + post 'code_compares/', :to => 'code_compares#create' + delete 'code_compares/:id', :to => 'code_compares#destroy' + end + + controller :game_codes do + post 'game_codes/', :to => 'game_codes#create' + delete 'game_codes/:id', :to => 'game_codes#destroy' + end controller :admin_games do post 'admin_games/', :to => 'admin_games#create' delete 'admin_games/:id', :to => 'admin_games#destroy' end + resources :codes + + resources :hints + resources :games devise_for :users get "home/index" - get "home/game_code_compares" - post "home/create_code_compare" + controller :games do + get "start_game/:id" => "games#start_game" + end + #get "home/game_tasks_compares" + #post "home/create_code_compare" devise_for :admin_users, ActiveAdmin::Devise.config ActiveAdmin.routes(self) diff --git a/db/migrate/20140113070022_create_game_hints.rb b/db/migrate/20140113070022_create_game_hints.rb deleted file mode 100644 index 7846c97..0000000 --- a/db/migrate/20140113070022_create_game_hints.rb +++ /dev/null @@ -1,10 +0,0 @@ -class CreateGameHints < ActiveRecord::Migration - def change - create_table :game_hints do |t| - t.integer :game_id - t.integer :hint_id - - t.timestamps - end - end -end diff --git a/db/migrate/20140113101225_add_result_to_user_games.rb b/db/migrate/20140113101225_add_result_to_user_games.rb deleted file mode 100644 index d8d39db..0000000 --- a/db/migrate/20140113101225_add_result_to_user_games.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddResultToUserGames < ActiveRecord::Migration - def change - add_column :user_games, :result, :integer, default: 0 - end -end diff --git a/db/migrate/20140113170656_create_game_codes.rb b/db/migrate/20140113170656_create_game_codes.rb deleted file mode 100644 index ffd9e74..0000000 --- a/db/migrate/20140113170656_create_game_codes.rb +++ /dev/null @@ -1,10 +0,0 @@ -class CreateGameCodes < ActiveRecord::Migration - def change - create_table :game_codes do |t| - t.integer :game_id - t.integer :code_id - - t.timestamps - end - end -end diff --git a/db/migrate/20140114114821_add_state_to_games.rb b/db/migrate/20140114114821_add_state_to_games.rb index db5c972..fb6402c 100644 --- a/db/migrate/20140114114821_add_state_to_games.rb +++ b/db/migrate/20140114114821_add_state_to_games.rb @@ -1,5 +1,5 @@ class AddStateToGames < ActiveRecord::Migration def change - add_column :games, :state, :integer, default: -1 + add_column :games, :state, :integer, default: 0 end end diff --git a/db/migrate/20140114124729_remove_result_from_code_compares.rb b/db/migrate/20140114124729_remove_result_from_code_compares.rb deleted file mode 100644 index b9dd83c..0000000 --- a/db/migrate/20140114124729_remove_result_from_code_compares.rb +++ /dev/null @@ -1,5 +0,0 @@ -class RemoveResultFromCodeCompares < ActiveRecord::Migration - def change - remove_column :code_compares, :result - end -end diff --git a/db/migrate/20140115081012_create_tasks.rb b/db/migrate/20140115081012_create_tasks.rb new file mode 100644 index 0000000..e65b6c9 --- /dev/null +++ b/db/migrate/20140115081012_create_tasks.rb @@ -0,0 +1,10 @@ +class CreateTasks < ActiveRecord::Migration + def change + create_table :tasks do |t| + t.text :task_text + t.integer :points + + t.timestamps + end + end +end diff --git a/db/migrate/20140115082513_create_task_codes.rb b/db/migrate/20140115082513_create_task_codes.rb new file mode 100644 index 0000000..dcf4fbc --- /dev/null +++ b/db/migrate/20140115082513_create_task_codes.rb @@ -0,0 +1,10 @@ +class CreateTaskCodes < ActiveRecord::Migration + def change + create_table :task_codes do |t| + t.integer :task_id + t.integer :code_id + + t.timestamps + end + end +end diff --git a/db/migrate/20140115082554_create_task_hints.rb b/db/migrate/20140115082554_create_task_hints.rb new file mode 100644 index 0000000..b479f37 --- /dev/null +++ b/db/migrate/20140115082554_create_task_hints.rb @@ -0,0 +1,10 @@ +class CreateTaskHints < ActiveRecord::Migration + def change + create_table :task_hints do |t| + t.integer :task_id + t.integer :hint_id + + t.timestamps + end + end +end diff --git a/db/migrate/20140113061527_create_user_games.rb b/db/migrate/20140115090623_create_user_games.rb similarity index 83% rename from db/migrate/20140113061527_create_user_games.rb rename to db/migrate/20140115090623_create_user_games.rb index fdceae3..4a9413f 100644 --- a/db/migrate/20140113061527_create_user_games.rb +++ b/db/migrate/20140115090623_create_user_games.rb @@ -3,7 +3,7 @@ def change create_table :user_games do |t| t.integer :user_id t.integer :game_id - + t.integer :result, default: 0 t.timestamps end end diff --git a/db/migrate/20140115090713_create_user_tasks.rb b/db/migrate/20140115090713_create_user_tasks.rb new file mode 100644 index 0000000..6ebdd23 --- /dev/null +++ b/db/migrate/20140115090713_create_user_tasks.rb @@ -0,0 +1,11 @@ +class CreateUserTasks < ActiveRecord::Migration + def change + create_table :user_tasks do |t| + t.integer :user_id + t.integer :task_id + t.integer :result, default: 0 + + t.timestamps + end + end +end diff --git a/db/migrate/20140112202805_create_admin_games.rb b/db/migrate/20140115091137_create_admin_games.rb similarity index 100% rename from db/migrate/20140112202805_create_admin_games.rb rename to db/migrate/20140115091137_create_admin_games.rb diff --git a/db/migrate/20140114083234_create_code_compares.rb b/db/migrate/20140115092553_create_code_compares.rb similarity index 84% rename from db/migrate/20140114083234_create_code_compares.rb rename to db/migrate/20140115092553_create_code_compares.rb index 316637d..3ab30d9 100644 --- a/db/migrate/20140114083234_create_code_compares.rb +++ b/db/migrate/20140115092553_create_code_compares.rb @@ -3,7 +3,6 @@ def change create_table :code_compares do |t| t.integer :user_id t.integer :code_id - t.integer :result, default: 0 t.timestamps end diff --git a/db/migrate/20140115100758_create_game_tasks.rb b/db/migrate/20140115100758_create_game_tasks.rb new file mode 100644 index 0000000..3272884 --- /dev/null +++ b/db/migrate/20140115100758_create_game_tasks.rb @@ -0,0 +1,10 @@ +class CreateGameTasks < ActiveRecord::Migration + def change + create_table :game_tasks do |t| + t.integer :game_id + t.integer :task_id + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 1e8943c..0e0336f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140114124729) do +ActiveRecord::Schema.define(version: 20140115100758) do create_table "active_admin_comments", force: true do |t| t.string "namespace" @@ -66,16 +66,9 @@ t.datetime "updated_at" end - create_table "game_codes", force: true do |t| + create_table "game_tasks", force: true do |t| t.integer "game_id" - t.integer "code_id" - t.datetime "created_at" - t.datetime "updated_at" - end - - create_table "game_hints", force: true do |t| - t.integer "game_id" - t.integer "hint_id" + t.integer "task_id" t.datetime "created_at" t.datetime "updated_at" end @@ -86,7 +79,7 @@ t.integer "duration" t.datetime "created_at" t.datetime "updated_at" - t.integer "state", default: -1 + t.integer "state", default: 0 end create_table "hints", force: true do |t| @@ -96,12 +89,41 @@ t.integer "queue_number" end + create_table "task_codes", force: true do |t| + t.integer "task_id" + t.integer "code_id" + t.datetime "created_at" + t.datetime "updated_at" + end + + create_table "task_hints", force: true do |t| + t.integer "task_id" + t.integer "hint_id" + t.datetime "created_at" + t.datetime "updated_at" + end + + create_table "tasks", force: true do |t| + t.text "task_text" + t.integer "points" + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "user_games", force: true do |t| t.integer "user_id" t.integer "game_id" + t.integer "result", default: 0 t.datetime "created_at" t.datetime "updated_at" + end + + create_table "user_tasks", force: true do |t| + t.integer "user_id" + t.integer "task_id" t.integer "result", default: 0 + t.datetime "created_at" + t.datetime "updated_at" end create_table "users", force: true do |t| diff --git a/spec/controllers/code_compares_controller_spec.rb b/spec/controllers/code_compares_controller_spec.rb index c070fab..9999395 100644 --- a/spec/controllers/code_compares_controller_spec.rb +++ b/spec/controllers/code_compares_controller_spec.rb @@ -5,51 +5,51 @@ # was generated by Rails when you ran the scaffold generator. # # It assumes that the implementation code is generated by the rails scaffold -# generator. If you are using any extension libraries to generate different +# generator. If you are using any extension libraries to generate different # controller code, this generated spec may or may not pass. # -# It only uses APIs available in rails and/or rspec-rails. There are a number +# It only uses APIs available in rails and/or rspec-rails. There are a number # of tools you can use to make these specs even more expressive, but we're # sticking to rails and rspec-rails APIs to keep things simple and stable. # # Compared to earlier versions of this generator, there is very limited use of -# stubs and message expectations in this spec. Stubs are only used when there +# stubs and message expectations in this spec. Stubs are only used when there # is no simpler way to get a handle on the object needed for the example. # Message expectations are only used when there is no simpler way to specify # that an instance is receiving a specific message. -describe CodeComparesController do - login_user +describe AdminGamesController do + login_admin # This should return the minimal set of attributes required to create a valid - # CodeCompare. As you add validations to CodeCompare, be sure to + # AdminGame. As you add validations to AdminGame, be sure to # adjust the attributes here as well. - let(:valid_attributes) { { "user_id" => "1" } } + let(:valid_attributes) { { "admin_id" => "1" } } # This should return the minimal set of values that should be in the session # in order to pass any filters (e.g. authentication) defined in - # CodeComparesController. Be sure to keep this updated too. + # AdminGamesController. Be sure to keep this updated too. let(:valid_session) { {} } before do - @code = FactoryGirl.create(:code) - @code_compare = FactoryGirl.create(:code_compare, user_id: @user_id, code_id: @code.to_param) + @game = FactoryGirl.create(:game) + @admin_game = FactoryGirl.create(:admin_game, admin_id: @admin_id, game_id: @game.to_param) end describe "POST create" do describe "with valid params" do - it "creates a new CodeCompare" do + it "creates a new AdminGame" do expect { - post :create, {:code_compare => @code_compare.attributes}, valid_session - }.to change(CodeCompare, :count).by(1) + post :create, {:admin_game => @admin_game.attributes}, valid_session + }.to change(AdminGame, :count).by(1) end end end describe "DELETE destroy" do - it "destroys the requested code_compare" do + it "destroys the requested admin_game" do expect { - delete :destroy, {:id => @code_compare.to_param}, valid_session - }.to change(CodeCompare, :count).by(-1) + delete :destroy, {:id => @admin_game.to_param}, valid_session + }.to change(AdminGame, :count).by(-1) end end diff --git a/spec/controllers/codes_controller_spec.rb b/spec/controllers/codes_controller_spec.rb index bc7731c..8321045 100644 --- a/spec/controllers/codes_controller_spec.rb +++ b/spec/controllers/codes_controller_spec.rb @@ -30,7 +30,7 @@ # CodesController. Be sure to keep this updated too. let(:valid_session) { {} } before do - @game = FactoryGirl.create(:game) + @task = FactoryGirl.create(:task) @code = FactoryGirl.create(:code) @invalid_attributes = FactoryGirl.build(:code, code_string: "ывампывипа").attributes end @@ -51,7 +51,7 @@ describe "GET new" do it "assigns a new code as @code" do - get :new, {game: @game.to_param}, valid_session + get :new, {task: @task.to_param}, valid_session expect(assigns(:code)).to be_a_new(Code) end end @@ -64,21 +64,27 @@ end describe "POST create" do + before(:each) do + @code_post = @code.attributes + @code_post[:task] = @task.to_param + @code_post_invalid = @invalid_attributes + @code_post_invalid[:task] = @task.to_param + end describe "with valid params" do it "creates a new Code" do expect { - post :create, {:code => @code.attributes}, valid_session + post :create, {:code => @code_post}, valid_session }.to change(Code, :count).by(1) end it "assigns a newly created code as @code" do - post :create, {:code => @code.attributes}, valid_session + post :create, {:code => @code_post}, valid_session expect(assigns(:code)).to be_a(Code) expect(assigns(:code)).to be_persisted end it "redirects to the created code" do - post :create, {:code => @code.attributes}, valid_session + post :create, {:code => @code_post}, valid_session expect(response).to redirect_to(Code.last) end end @@ -87,14 +93,14 @@ it "assigns a newly created but unsaved code as @code" do # Trigger the behavior that occurs when invalid params are submitted Code.any_instance.stub(:save).and_return(false) - post :create, {:code => @invalid_attributes}, valid_session + post :create, {:code => @code_post_invalid}, valid_session expect(assigns(:code)).to be_a_new(Code) end it "re-renders the 'new' template" do # Trigger the behavior that occurs when invalid params are submitted Code.any_instance.stub(:save).and_return(false) - post :create, {:code => @invalid_attributes}, valid_session + post :create, {:code => @code_post_invalid}, valid_session expect(response).to render_template("new") end end diff --git a/spec/controllers/game_hints_controller_spec.rb b/spec/controllers/game_tasks_controller_spec.rb similarity index 73% rename from spec/controllers/game_hints_controller_spec.rb rename to spec/controllers/game_tasks_controller_spec.rb index 22e2b99..3f0c5c7 100644 --- a/spec/controllers/game_hints_controller_spec.rb +++ b/spec/controllers/game_tasks_controller_spec.rb @@ -18,39 +18,39 @@ # Message expectations are only used when there is no simpler way to specify # that an instance is receiving a specific message. -describe GameHintsController do +describe GameTasksController do login_admin # This should return the minimal set of attributes required to create a valid - # GameHint. As you add validations to GameHint, be sure to + # GameTask. As you add validations to GameTask, be sure to # adjust the attributes here as well. let(:valid_attributes) { { "game_id" => "1" } } # This should return the minimal set of values that should be in the session # in order to pass any filters (e.g. authentication) defined in - # GameHintsController. Be sure to keep this updated too. + # GameTasksController. Be sure to keep this updated too. let(:valid_session) { {} } before do @game = FactoryGirl.create(:game) - @hint = FactoryGirl.create(:hint) - @game_hint = FactoryGirl.create(:game_hint, game_id: @game.to_param, hint_id: @hint.to_param) + @task = FactoryGirl.create(:task) + @game_task = FactoryGirl.create(:game_task, game_id: @game.to_param, task_id: @task.to_param) end describe "POST create" do describe "with valid params" do - it "creates a new GameHint" do + it "creates a new GameTask" do expect { - post :create, {:game_hint => @game_hint.attributes}, valid_session - }.to change(GameHint, :count).by(1) + post :create, {:game_task => @game_task.attributes}, valid_session + }.to change(GameTask, :count).by(1) end end end describe "DELETE destroy" do - it "destroys the requested game_hint" do + it "destroys the requested game_task" do expect { - delete :destroy, {:id => @game_hint.to_param}, valid_session - }.to change(GameHint, :count).by(-1) + delete :destroy, {:id => @game_task.to_param}, valid_session + }.to change(GameTask, :count).by(-1) end end diff --git a/spec/controllers/games_controller_spec.rb b/spec/controllers/games_controller_spec.rb index d5abbf8..a3a5d87 100644 --- a/spec/controllers/games_controller_spec.rb +++ b/spec/controllers/games_controller_spec.rb @@ -32,6 +32,10 @@ before do @game = FactoryGirl.create(:game) + @task = FactoryGirl.create(:task) + @user = FactoryGirl.create(:user) + @game_task = FactoryGirl.create(:game_task) + @user_game = FactoryGirl.create(:user_game) @invalid_attributes = FactoryGirl.build(:game, title: "впирапеи", start_date: "01.01.2014 00:00:00".to_datetime, duration: 11).attributes end @@ -155,4 +159,12 @@ end end + describe "Start game" do + it "exuctes action " do + expect { + get "start_game", {id: @game.to_param}, valid_session + }.to change(UserTask, :count).by(1) + end + end + end diff --git a/spec/controllers/hints_controller_spec.rb b/spec/controllers/hints_controller_spec.rb index f8b9d12..c0b2e6a 100644 --- a/spec/controllers/hints_controller_spec.rb +++ b/spec/controllers/hints_controller_spec.rb @@ -32,7 +32,7 @@ before do queue_number = rand(1...2) - @game = FactoryGirl.create(:game) + @task = FactoryGirl.create(:task) @hint = FactoryGirl.create(:hint, queue_number: queue_number) @invalid_attributes = FactoryGirl.build(:hint, hint_text: "акпыук").attributes end @@ -53,7 +53,7 @@ describe "GET new" do it "assigns a new hint as @hint" do - get :new, {game: @game.to_param}, valid_session + get :new, {task: @task.to_param}, valid_session expect(assigns(:hint)).to be_a_new(Hint) end end @@ -66,21 +66,27 @@ end describe "POST create" do + before(:each) do + @hint_post = @hint.attributes + @hint_post[:task] = @task.to_param + @hint_post_invalid = @invalid_attributes + @hint_post_invalid[:task] = @task.to_param + end describe "with valid params" do it "creates a new Hint" do expect { - post :create, {:hint => @hint.attributes}, valid_session + post :create, {:hint => @hint_post}, valid_session }.to change(Hint, :count).by(1) end it "assigns a newly created hint as @hint" do - post :create, {:hint => @hint.attributes}, valid_session + post :create, {:hint => @hint_post}, valid_session expect(assigns(:hint)).to be_a(Hint) expect(assigns(:hint)).to be_persisted end it "redirects to the created hint" do - post :create, {:hint => @hint.attributes}, valid_session + post :create, {:hint => @hint_post}, valid_session expect(response).to redirect_to(Hint.last) end end @@ -89,14 +95,14 @@ it "assigns a newly created but unsaved hint as @hint" do # Trigger the behavior that occurs when invalid params are submitted Hint.any_instance.stub(:save).and_return(false) - post :create, {:hint => @invalid_attributes}, valid_session + post :create, {:hint => @hint_post_invalid}, valid_session expect(assigns(:hint)).to be_a_new(Hint) end it "re-renders the 'new' template" do # Trigger the behavior that occurs when invalid params are submitted Hint.any_instance.stub(:save).and_return(false) - post :create, {:hint => @invalid_attributes}, valid_session + post :create, {:hint => @hint_post_invalid}, valid_session expect(response).to render_template("new") end end diff --git a/spec/controllers/home_controller_spec.rb b/spec/controllers/home_controller_spec.rb index 1b58657..2025240 100644 --- a/spec/controllers/home_controller_spec.rb +++ b/spec/controllers/home_controller_spec.rb @@ -8,6 +8,7 @@ before do @game = FactoryGirl.create(:game) @code = FactoryGirl.create(:code) + # @game_code = FactoryGirl.create(:game_code, game_id: @game.to_param, code_id: @code.to_param) end describe "GET 'index'" do @@ -17,19 +18,4 @@ end end - describe "GET 'game_code_compares'" do - it "returns http success" do - get 'game_code_compares', {game: @game.to_param}, valid_session - expect(response).to be_success - end - end - - describe "GET 'create_code_compare'" do - it "returns http success" do - expect { - post "create_code_compare", {game: @game.to_param, try_text: @code.attributes[:code_text]}, valid_session - }.to change(CodeCompare, :count).by(1) - end - end - end diff --git a/spec/controllers/game_codes_controller_spec.rb b/spec/controllers/task_codes_controller_spec.rb similarity index 69% rename from spec/controllers/game_codes_controller_spec.rb rename to spec/controllers/task_codes_controller_spec.rb index fab2441..5108800 100644 --- a/spec/controllers/game_codes_controller_spec.rb +++ b/spec/controllers/task_codes_controller_spec.rb @@ -18,39 +18,40 @@ # Message expectations are only used when there is no simpler way to specify # that an instance is receiving a specific message. -describe GameCodesController do +describe TaskCodesController do login_admin # This should return the minimal set of attributes required to create a valid - # GameCode. As you add validations to GameCode, be sure to + # TaskCode. As you add validations to TaskCode, be sure to # adjust the attributes here as well. - let(:valid_attributes) { { "create" => "MyString" } } + let(:valid_attributes) { { "task_id" => "1" } } # This should return the minimal set of values that should be in the session # in order to pass any filters (e.g. authentication) defined in - # GameCodesController. Be sure to keep this updated too. + # TaskCodesController. Be sure to keep this updated too. let(:valid_session) { {} } + before do - @game = FactoryGirl.create(:game) - @code= FactoryGirl.create(:code) - @game_code = FactoryGirl.create(:game_code, game_id: @game.to_param, code_id: @code.to_param) + @task = FactoryGirl.create(:task) + @code = FactoryGirl.create(:code) + @task_code = FactoryGirl.create(:task_code, task_id: @task.to_param, code_id: @code.to_param) end - describe "POST create" do + describe "POST create" do describe "with valid params" do - it "creates a new GameCode" do + it "creates a new TaskCode" do expect { - post :create, {:game_code => @game_code.attributes}, valid_session - }.to change(GameCode, :count).by(1) + post :create, {:task_code => @task_code.attributes}, valid_session + }.to change(TaskCode, :count).by(1) end end - end + end describe "DELETE destroy" do - it "destroys the requested game_code" do + it "destroys the requested task_code" do expect { - delete :destroy, {:id => @game_code.to_param}, valid_session - }.to change(GameCode, :count).by(-1) + delete :destroy, {:id => @task_code.to_param}, valid_session + }.to change(TaskCode, :count).by(-1) end end diff --git a/spec/controllers/task_hints_controller_spec.rb b/spec/controllers/task_hints_controller_spec.rb new file mode 100644 index 0000000..b9aa971 --- /dev/null +++ b/spec/controllers/task_hints_controller_spec.rb @@ -0,0 +1,57 @@ +require 'spec_helper' + +# This spec was generated by rspec-rails when you ran the scaffold generator. +# It demonstrates how one might use RSpec to specify the controller code that +# was generated by Rails when you ran the scaffold generator. +# +# It assumes that the implementation code is generated by the rails scaffold +# generator. If you are using any extension libraries to generate different +# controller code, this generated spec may or may not pass. +# +# It only uses APIs available in rails and/or rspec-rails. There are a number +# of tools you can use to make these specs even more expressive, but we're +# sticking to rails and rspec-rails APIs to keep things simple and stable. +# +# Compared to earlier versions of this generator, there is very limited use of +# stubs and message expectations in this spec. Stubs are only used when there +# is no simpler way to get a handle on the object needed for the example. +# Message expectations are only used when there is no simpler way to specify +# that an instance is receiving a specific message. + +describe TaskHintsController do + login_admin + # This should return the minimal set of attributes required to create a valid + # TaskHint. As you add validations to TaskHint, be sure to + # adjust the attributes here as well. + let(:valid_attributes) { { "task_id" => "1" } } + + # This should return the minimal set of values that should be in the session + # in order to pass any filters (e.g. authentication) defined in + # TaskHintsController. Be sure to keep this updated too. + let(:valid_session) { {} } + + before do + @hint = FactoryGirl.create(:hint) + @task = FactoryGirl.create(:task) + @task_hint = FactoryGirl.create(:task_hint, task_id: @game.to_param, hint_id: @hint.to_param) + end + + describe "POST create" do + describe "with valid params" do + it "creates a new TaskHint" do + expect { + post :create, {:task_hint => @task.attributes}, valid_session + }.to change(TaskHint, :count).by(1) + end + end + end + + describe "DELETE destroy" do + it "destroys the requested task_hint" do + expect { + delete :destroy, {:id => @task_hint.to_param}, valid_session + }.to change(TaskHint, :count).by(-1) + end + end + +end diff --git a/spec/controllers/tasks_controller_spec.rb b/spec/controllers/tasks_controller_spec.rb new file mode 100644 index 0000000..18678c5 --- /dev/null +++ b/spec/controllers/tasks_controller_spec.rb @@ -0,0 +1,166 @@ +require 'spec_helper' + +# This spec was generated by rspec-rails when you ran the scaffold generator. +# It demonstrates how one might use RSpec to specify the controller code that +# was generated by Rails when you ran the scaffold generator. +# +# It assumes that the implementation code is generated by the rails scaffold +# generator. If you are using any extension libraries to generate different +# controller code, this generated spec may or may not pass. +# +# It only uses APIs available in rails and/or rspec-rails. There are a number +# of tools you can use to make these specs even more expressive, but we're +# sticking to rails and rspec-rails APIs to keep things simple and stable. +# +# Compared to earlier versions of this generator, there is very limited use of +# stubs and message expectations in this spec. Stubs are only used when there +# is no simpler way to get a handle on the object needed for the example. +# Message expectations are only used when there is no simpler way to specify +# that an instance is receiving a specific message. + +describe TasksController do + login_admin + # This should return the minimal set of attributes required to create a valid + # Task. As you add validations to Task, be sure to + # adjust the attributes here as well. + let(:valid_attributes) { { } } + + # This should return the minimal set of values that should be in the session + # in order to pass any filters (e.g. authentication) defined in + # TasksController. Be sure to keep this updated too. + let(:valid_session) { {} } + + before do + @game = FactoryGirl.create(:game) + @task = FactoryGirl.create(:task) + #puts Task.first.to_yaml + @invalid_attributes = FactoryGirl.build(:task, task_text: "впирапеи").attributes + end + + describe "GET index" do + it "assigns all tasks as @tasks" do + get :index, {}, valid_session + expect(assigns(:tasks)).to eq([@task]) + end + end + + describe "GET show" do + it "assigns the requested task as @task" do + get :show, {:id => @task.to_param}, valid_session + expect(assigns(:task)).to eq(@task) + end + end + + describe "GET new" do + it "assigns a new task as @task" do + get :new, {game: @game.to_param}, valid_session + expect(assigns(:task)).to be_a_new(Task) + end + end + + describe "GET edit" do + it "assigns the requested task as @task" do + get :edit, {:id => @task.to_param}, valid_session + expect(assigns(:task)).to eq(@task) + end + end + + describe "POST create" do + before(:each) do + @task_post = @task.attributes + @task_post[:game] = @game.to_param + @task_post_invalid = @invalid_attributes + @task_post_invalid[:game] = @game.to_param + end + describe "with valid params" do + it "creates a new Task" do + expect { + post :create, {:task => @task_post}, valid_session + }.to change(Task, :count).by(1) + end + + it "assigns a newly created task as @task" do + post :create, {:task => @task_post}, valid_session + expect(assigns(:task)).to be_a(Task) + expect(assigns(:task)).to be_persisted + end + + it "redirects to the created task" do + post :create, {:task =>@task_post}, valid_session + expect(response).to redirect_to(Task.last) + end + end + + describe "with invalid params" do + it "assigns a newly created but unsaved task as @task" do + # Trigger the behavior that occurs when invalid params are submitted + Task.any_instance.stub(:save).and_return(false) + post :create, {:task => @task_post_invalid}, valid_session + expect(assigns(:task)).to be_a_new(Task) + end + + it "re-renders the 'new' template" do + # Trigger the behavior that occurs when invalid params are submitted + Task.any_instance.stub(:save).and_return(false) + post :create, {:task => @task_post_invalid}, valid_session + expect(response).to render_template("new") + end + end + end + + describe "PUT update" do + describe "with valid params" do + before (:each) do + @valid_attributes = FactoryGirl.build(:task, task_text: "asfvubasdfchnlasidfciojnk").attributes + end + it "updates the requested task" do + # Assuming there are no other tasks in the database, this + # specifies that the Task created on the previous line + # receives the :update_attributes message with whatever params are + # submitted in the request. + expect_any_instance_of(Task).to receive(:update).with({ "task_text" => "arghbstghset" }) + put :update, {:id => @task.to_param, :task => { "task_text" => "arghbstghset" }}, valid_session + end + + it "assigns the requested task as @task" do + put :update, {:id => @task.to_param, :task => @valid_attributes}, valid_session + expect(assigns(:task)).to eq(@task) + end + + it "redirects to the task" do + put :update, {:id => @task.to_param, :task => @valid_attributes}, valid_session + expect(response).to redirect_to(@task) + end + end + + describe "with invalid params" do + it "assigns the task as @task" do + # Trigger the behavior that occurs when invalid params are submitted + Task.any_instance.stub(:save).and_return(false) + put :update, {:id => @task.to_param, :task => @invalid_attributes}, valid_session + expect(assigns(:task)).to eq(@task) + end + + it "re-renders the 'edit' template" do + # Trigger the behavior that occurs when invalid params are submitted + Task.any_instance.stub(:save).and_return(false) + put :update, {:id => @task.to_param, :task => @invalid_attributes}, valid_session + expect(response).to render_template("edit") + end + end + end + + describe "DELETE destroy" do + it "destroys the requested task" do + expect { + delete :destroy, {:id => @task.to_param}, valid_session + }.to change(Task, :count).by(-1) + end + + it "redirects to the tasks list" do + delete :destroy, {:id => @task.to_param}, valid_session + expect(response).to redirect_to(tasks_url) + end + end + +end diff --git a/spec/controllers/user_games_controller_spec.rb b/spec/controllers/user_games_controller_spec.rb index 4ab416b..5082e10 100644 --- a/spec/controllers/user_games_controller_spec.rb +++ b/spec/controllers/user_games_controller_spec.rb @@ -5,15 +5,15 @@ # was generated by Rails when you ran the scaffold generator. # # It assumes that the implementation code is generated by the rails scaffold -# generator. If you are using any extension libraries to generate different +# generator. If you are using any extension libraries to generate different # controller code, this generated spec may or may not pass. # -# It only uses APIs available in rails and/or rspec-rails. There are a number +# It only uses APIs available in rails and/or rspec-rails. There are a number # of tools you can use to make these specs even more expressive, but we're # sticking to rails and rspec-rails APIs to keep things simple and stable. # # Compared to earlier versions of this generator, there is very limited use of -# stubs and message expectations in this spec. Stubs are only used when there +# stubs and message expectations in this spec. Stubs are only used when there # is no simpler way to get a handle on the object needed for the example. # Message expectations are only used when there is no simpler way to specify # that an instance is receiving a specific message. @@ -106,7 +106,7 @@ describe "PUT update" do describe "with valid params" do before (:each) do - @valid_attributes = FactoryGirl.build(:user_game, user_id: @user_id).attributes + @valid_attributes = FactoryGirl.build(:user_game, user_id: @user_id.to_param).attributes end it "updates the requested user_game" do # Assuming there are no other user_games in the database, this diff --git a/spec/controllers/user_tasks_controller_spec.rb b/spec/controllers/user_tasks_controller_spec.rb new file mode 100644 index 0000000..cd32571 --- /dev/null +++ b/spec/controllers/user_tasks_controller_spec.rb @@ -0,0 +1,57 @@ +require 'spec_helper' + +# This spec was generated by rspec-rails when you ran the scaffold generator. +# It demonstrates how one might use RSpec to specify the controller code that +# was generated by Rails when you ran the scaffold generator. +# +# It assumes that the implementation code is generated by the rails scaffold +# generator. If you are using any extension libraries to generate different +# controller code, this generated spec may or may not pass. +# +# It only uses APIs available in rails and/or rspec-rails. There are a number +# of tools you can use to make these specs even more expressive, but we're +# sticking to rails and rspec-rails APIs to keep things simple and stable. +# +# Compared to earlier versions of this generator, there is very limited use of +# stubs and message expectations in this spec. Stubs are only used when there +# is no simpler way to get a handle on the object needed for the example. +# Message expectations are only used when there is no simpler way to specify +# that an instance is receiving a specific message. + +describe UserTasksController do + login_user + # This should return the minimal set of attributes required to create a valid + # UserTask. As you add validations to UserTask, be sure to + # adjust the attributes here as well. + let(:valid_attributes) { { "user_id" => "1" } } + + # This should return the minimal set of values that should be in the session + # in order to pass any filters (e.g. authentication) defined in + # UserTasksController. Be sure to keep this updated too. + let(:valid_session) { {} } + + before do + result = rand(-1..1) + @task = FactoryGirl.create(:task) + @user_task = FactoryGirl.create(:user_task, user_id: @user_id, task_id: @task.to_param, result: result) + end + + describe "POST create" do + describe "with valid params" do + it "creates a new UserTask" do + expect { + post :create, {:user_task => @user_task.attributes}, valid_session + }.to change(UserTask, :count).by(1) + end + end + end + + describe "DELETE destroy" do + it "destroys the requested user_task" do + expect { + delete :destroy, {:id => @user_task.to_param}, valid_session + }.to change(UserTask, :count).by(-1) + end + end + +end diff --git a/spec/factories/game_codes.rb b/spec/factories/game_tasks.rb similarity index 75% rename from spec/factories/game_codes.rb rename to spec/factories/game_tasks.rb index 950cb13..82f7a8f 100644 --- a/spec/factories/game_codes.rb +++ b/spec/factories/game_tasks.rb @@ -1,8 +1,8 @@ # Read about factories at https://github.com/thoughtbot/factory_girl FactoryGirl.define do - factory :game_code do + factory :game_task do game_id 1 - code_id 1 + task_id 1 end end diff --git a/spec/factories/task_codes.rb b/spec/factories/task_codes.rb new file mode 100644 index 0000000..2a86be8 --- /dev/null +++ b/spec/factories/task_codes.rb @@ -0,0 +1,8 @@ +# Read about factories at https://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :task_code do + task_id 1 + code_id 1 + end +end diff --git a/spec/factories/game_hints.rb b/spec/factories/task_hints.rb similarity index 75% rename from spec/factories/game_hints.rb rename to spec/factories/task_hints.rb index 0311333..eec1f39 100644 --- a/spec/factories/game_hints.rb +++ b/spec/factories/task_hints.rb @@ -1,8 +1,8 @@ # Read about factories at https://github.com/thoughtbot/factory_girl FactoryGirl.define do - factory :game_hint do - game_id 1 + factory :task_hint do + task_id 1 hint_id 1 end end diff --git a/spec/factories/tasks.rb b/spec/factories/tasks.rb new file mode 100644 index 0000000..cb61a12 --- /dev/null +++ b/spec/factories/tasks.rb @@ -0,0 +1,8 @@ +# Read about factories at https://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :task do + task_text "fgvjnsmgsjkanrfgasrfg" + points 10 + end +end diff --git a/spec/factories/user_games.rb b/spec/factories/user_games.rb index a5b9300..5f71020 100644 --- a/spec/factories/user_games.rb +++ b/spec/factories/user_games.rb @@ -4,6 +4,5 @@ factory :user_game do user_id 1 game_id 1 - result 0 end end diff --git a/spec/factories/user_tasks.rb b/spec/factories/user_tasks.rb new file mode 100644 index 0000000..921b326 --- /dev/null +++ b/spec/factories/user_tasks.rb @@ -0,0 +1,9 @@ +# Read about factories at https://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :user_task do + user_id 1 + task_id 1 + result 1 + end +end diff --git a/spec/helpers/game_codes_helper_spec.rb b/spec/helpers/game_tasks_helper_spec.rb similarity index 77% rename from spec/helpers/game_codes_helper_spec.rb rename to spec/helpers/game_tasks_helper_spec.rb index f4cec26..b70a9c6 100644 --- a/spec/helpers/game_codes_helper_spec.rb +++ b/spec/helpers/game_tasks_helper_spec.rb @@ -1,15 +1,15 @@ require 'spec_helper' # Specs in this file have access to a helper object that includes -# the GameCodesHelper. For example: +# the GameTasksHelper. For example: # -# describe GameCodesHelper do +# describe GameTasksHelper do # describe "string concat" do # it "concats two strings with spaces" do # expect(helper.concat_strings("this","that")).to eq("this that") # end # end # end -describe GameCodesHelper do +describe GameTasksHelper do pending "add some examples to (or delete) #{__FILE__}" end diff --git a/spec/helpers/game_hints_helper_spec.rb b/spec/helpers/task_codes_helper_spec.rb similarity index 77% rename from spec/helpers/game_hints_helper_spec.rb rename to spec/helpers/task_codes_helper_spec.rb index 43d31a3..b5bd269 100644 --- a/spec/helpers/game_hints_helper_spec.rb +++ b/spec/helpers/task_codes_helper_spec.rb @@ -1,15 +1,15 @@ require 'spec_helper' # Specs in this file have access to a helper object that includes -# the GameHintsHelper. For example: +# the TaskCodesHelper. For example: # -# describe GameHintsHelper do +# describe TaskCodesHelper do # describe "string concat" do # it "concats two strings with spaces" do # expect(helper.concat_strings("this","that")).to eq("this that") # end # end # end -describe GameHintsHelper do +describe TaskCodesHelper do pending "add some examples to (or delete) #{__FILE__}" end diff --git a/spec/helpers/task_hints_helper_spec.rb b/spec/helpers/task_hints_helper_spec.rb new file mode 100644 index 0000000..73c21fc --- /dev/null +++ b/spec/helpers/task_hints_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the TaskHintsHelper. For example: +# +# describe TaskHintsHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +describe TaskHintsHelper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/helpers/tasks_helper_spec.rb b/spec/helpers/tasks_helper_spec.rb new file mode 100644 index 0000000..c459188 --- /dev/null +++ b/spec/helpers/tasks_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the TasksHelper. For example: +# +# describe TasksHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +describe TasksHelper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/helpers/user_tasks_helper_spec.rb b/spec/helpers/user_tasks_helper_spec.rb new file mode 100644 index 0000000..50132d8 --- /dev/null +++ b/spec/helpers/user_tasks_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the UserTasksHelper. For example: +# +# describe UserTasksHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +describe UserTasksHelper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/game_code_spec.rb b/spec/models/game_task_spec.rb similarity index 80% rename from spec/models/game_code_spec.rb rename to spec/models/game_task_spec.rb index 21305ed..ba06088 100644 --- a/spec/models/game_code_spec.rb +++ b/spec/models/game_task_spec.rb @@ -1,5 +1,5 @@ require 'spec_helper' -describe GameCode do +describe GameTask do pending "add some examples to (or delete) #{__FILE__}" end diff --git a/spec/models/game_hint_spec.rb b/spec/models/task_code_spec.rb similarity index 80% rename from spec/models/game_hint_spec.rb rename to spec/models/task_code_spec.rb index 7d1e3a6..ef856c4 100644 --- a/spec/models/game_hint_spec.rb +++ b/spec/models/task_code_spec.rb @@ -1,5 +1,5 @@ require 'spec_helper' -describe GameHint do +describe TaskCode do pending "add some examples to (or delete) #{__FILE__}" end diff --git a/spec/models/task_hint_spec.rb b/spec/models/task_hint_spec.rb new file mode 100644 index 0000000..9b09aa1 --- /dev/null +++ b/spec/models/task_hint_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe TaskHint do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/task_spec.rb b/spec/models/task_spec.rb new file mode 100644 index 0000000..296efd2 --- /dev/null +++ b/spec/models/task_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe Task do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/user_game_spec.rb b/spec/models/user_game_spec.rb index 444a62d..9e972c4 100644 --- a/spec/models/user_game_spec.rb +++ b/spec/models/user_game_spec.rb @@ -1,9 +1,5 @@ require 'spec_helper' describe UserGame do - it "user games validation checking" do - user_game_1 = FactoryGirl.create(:user_game) - - user_game_1.should be_valid - end + pending "add some examples to (or delete) #{__FILE__}" end diff --git a/spec/models/user_task_spec.rb b/spec/models/user_task_spec.rb new file mode 100644 index 0000000..aed2c9c --- /dev/null +++ b/spec/models/user_task_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe UserTask do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/routing/game_hints_routing_spec.rb b/spec/routing/game_hints_routing_spec.rb deleted file mode 100644 index 2513cf0..0000000 --- a/spec/routing/game_hints_routing_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -require "spec_helper" - -describe GameHintsController do - - before do - @game = FactoryGirl.create(:game) - @hint = FactoryGirl.create(:hint) - @game_hint = FactoryGirl.create(:game_hint, game_id: @game.to_param, hint_id: @hint.to_param) - end - describe "routing" do - - it "routes to #create" do - expect(:post => "/game_hints").to route_to("game_hints#create") - end - - it "routes to #destroy" do - expect(:delete => "/game_hints/#{@game_hint.to_param}").to route_to("game_hints#destroy", :id => "#{@game_hint.to_param}") - end - - end -end diff --git a/spec/routing/game_codes_routing_spec.rb b/spec/routing/game_tasks_routing_spec.rb similarity index 50% rename from spec/routing/game_codes_routing_spec.rb rename to spec/routing/game_tasks_routing_spec.rb index 15124ec..387e5e5 100644 --- a/spec/routing/game_codes_routing_spec.rb +++ b/spec/routing/game_tasks_routing_spec.rb @@ -1,14 +1,14 @@ require "spec_helper" -describe GameCodesController do +describe GameTasksController do describe "routing" do it "routes to #create" do - expect(:post => "/game_codes").to route_to("game_codes#create") + expect(:post => "/game_tasks").to route_to("game_tasks#create") end it "routes to #destroy" do - expect(:delete => "/game_codes/1").to route_to("game_codes#destroy", :id => "1") + expect(:delete => "/game_tasks/1").to route_to("game_tasks#destroy", :id => "1") end end diff --git a/spec/routing/task_codes_routing_spec.rb b/spec/routing/task_codes_routing_spec.rb new file mode 100644 index 0000000..2b7462b --- /dev/null +++ b/spec/routing/task_codes_routing_spec.rb @@ -0,0 +1,15 @@ +require "spec_helper" + +describe TaskCodesController do + describe "routing" do + + it "routes to #create" do + expect(:post => "/task_codes").to route_to("task_codes#create") + end + + it "routes to #destroy" do + expect(:delete => "/task_codes/1").to route_to("task_codes#destroy", :id => "1") + end + + end +end diff --git a/spec/routing/task_hints_routing_spec.rb b/spec/routing/task_hints_routing_spec.rb new file mode 100644 index 0000000..2ed04ea --- /dev/null +++ b/spec/routing/task_hints_routing_spec.rb @@ -0,0 +1,15 @@ +require "spec_helper" + +describe TaskHintsController do + describe "routing" do + + it "routes to #create" do + expect(:post => "/task_hints").to route_to("task_hints#create") + end + + it "routes to #destroy" do + expect(:delete => "/task_hints/1").to route_to("task_hints#destroy", :id => "1") + end + + end +end diff --git a/spec/routing/tasks_routing_spec.rb b/spec/routing/tasks_routing_spec.rb new file mode 100644 index 0000000..808d05c --- /dev/null +++ b/spec/routing/tasks_routing_spec.rb @@ -0,0 +1,35 @@ +require "spec_helper" + +describe TasksController do + describe "routing" do + + it "routes to #index" do + expect(:get => "/tasks").to route_to("tasks#index") + end + + it "routes to #new" do + expect(:get => "/tasks/new").to route_to("tasks#new") + end + + it "routes to #show" do + expect(:get => "/tasks/1").to route_to("tasks#show", :id => "1") + end + + it "routes to #edit" do + expect(:get => "/tasks/1/edit").to route_to("tasks#edit", :id => "1") + end + + it "routes to #create" do + expect(:post => "/tasks").to route_to("tasks#create") + end + + it "routes to #update" do + expect(:put => "/tasks/1").to route_to("tasks#update", :id => "1") + end + + it "routes to #destroy" do + expect(:delete => "/tasks/1").to route_to("tasks#destroy", :id => "1") + end + + end +end diff --git a/spec/routing/user_tasks_routing_spec.rb b/spec/routing/user_tasks_routing_spec.rb new file mode 100644 index 0000000..9e014e8 --- /dev/null +++ b/spec/routing/user_tasks_routing_spec.rb @@ -0,0 +1,15 @@ +require "spec_helper" + +describe UserTasksController do + describe "routing" do + + it "routes to #create" do + expect(:post => "/user_tasks").to route_to("user_tasks#create") + end + + it "routes to #destroy" do + expect(:delete => "/user_tasks/1").to route_to("user_tasks#destroy", :id => "1") + end + + end +end diff --git a/spec/support/controller_macros.rb b/spec/support/controller_macros.rb index ff059c5..8ee25ca 100644 --- a/spec/support/controller_macros.rb +++ b/spec/support/controller_macros.rb @@ -18,8 +18,6 @@ def login_user end after(:each) do User.find(@user_id).destroy - - end end end \ No newline at end of file diff --git a/spec/views/tasks/edit.html.slim_spec.rb b/spec/views/tasks/edit.html.slim_spec.rb new file mode 100644 index 0000000..77d6935 --- /dev/null +++ b/spec/views/tasks/edit.html.slim_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +describe "tasks/edit" do + before(:each) do + @task = assign(:task, stub_model(Task)) + end + + it "renders the edit task form" do + render + + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "form[action=?][method=?]", task_path(@task), "post" do + end + end +end diff --git a/spec/views/tasks/index.html.slim_spec.rb b/spec/views/tasks/index.html.slim_spec.rb new file mode 100644 index 0000000..53e529a --- /dev/null +++ b/spec/views/tasks/index.html.slim_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' + +describe "tasks/index" do + before do + task1 = FactoryGirl.create(:task) + task2 = FactoryGirl.create(:task, :task_text => "fzvbsdfgvsdfgvsfd") + assign(:tasks, [ + task1, task2 + #stub_model Game, :id => game2.to_param + ]) + end + + it "renders a list of tasks" do + render + # Run the generator again with the --webrat flag if you want to use webrat matchers + end +end diff --git a/spec/views/tasks/new.html.slim_spec.rb b/spec/views/tasks/new.html.slim_spec.rb new file mode 100644 index 0000000..3b82589 --- /dev/null +++ b/spec/views/tasks/new.html.slim_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +describe "tasks/new" do + before(:each) do + assign(:task, stub_model(Task).as_new_record) + end + + it "renders new task form" do + render + + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "form[action=?][method=?]", tasks_path, "post" do + end + end +end diff --git a/spec/views/tasks/show.html.slim_spec.rb b/spec/views/tasks/show.html.slim_spec.rb new file mode 100644 index 0000000..d55f204 --- /dev/null +++ b/spec/views/tasks/show.html.slim_spec.rb @@ -0,0 +1,12 @@ +require 'spec_helper' + +describe "tasks/show" do + before(:each) do + @task = assign(:task, stub_model(Task)) + end + + it "renders attributes in

" do + render + # Run the generator again with the --webrat flag if you want to use webrat matchers + end +end diff --git a/spec/views/user_games/index.html.slim_spec.rb b/spec/views/user_games/index.html.slim_spec.rb index ec6d6b2..c0da63d 100644 --- a/spec/views/user_games/index.html.slim_spec.rb +++ b/spec/views/user_games/index.html.slim_spec.rb @@ -5,7 +5,7 @@ game1 = FactoryGirl.create(:game) game2 = FactoryGirl.create(:game, title: "dsgsdfgsdfgv") assign(:games, [ - game1, game2 + game1, game2 ]) end From ad436b0b2412c266d14cbcf27ae266922cdf6ae4 Mon Sep 17 00:00:00 2001 From: sorgal Date: Thu, 16 Jan 2014 08:56:37 +0400 Subject: [PATCH 02/15] games functionality without time functions, normal views, integration tests and with two non-working tests --- .idea/.rakeTasks | 2 +- .idea/workspace.xml | 359 +++++++++--------- app/assets/javascripts/user_hints.js.coffee | 3 + app/assets/stylesheets/user_hints.css.scss | 3 + app/controllers/code_compares_controller.rb | 65 +++- app/controllers/games_controller.rb | 45 ++- app/controllers/hints_controller.rb | 16 +- app/controllers/home_controller.rb | 14 +- app/controllers/tasks_controller.rb | 26 +- app/controllers/user_games_controller.rb | 20 +- app/controllers/user_hints_controller.rb | 42 ++ app/helpers/user_hints_helper.rb | 2 + app/models/game.rb | 3 +- app/models/hint.rb | 2 +- app/models/task.rb | 1 + app/models/user.rb | 1 + app/models/user_hint.rb | 4 + .../_code_compare_form.html.slim} | 4 +- app/views/games/_form.html.slim | 4 - app/views/games/index.html.slim | 3 - app/views/games/show.html.slim | 44 ++- app/views/hints/new.html.slim | 13 +- app/views/home/index.html.slim | 14 +- app/views/user_games/index.html.slim | 8 +- config/routes.rb | 11 + db/migrate/20140112202751_create_games.rb | 1 - .../20140115090713_create_user_tasks.rb | 2 +- .../20140115183545_create_user_hints.rb | 10 + .../20140115190550_add_state_to_user_games.rb | 5 + db/schema.rb | 13 +- .../code_compares_controller_spec.rb | 54 ++- spec/controllers/games_controller_spec.rb | 20 +- spec/controllers/hints_controller_spec.rb | 15 +- spec/controllers/tasks_controller_spec.rb | 23 ++ .../controllers/user_games_controller_spec.rb | 10 +- .../controllers/user_hints_controller_spec.rb | 56 +++ spec/factories/games.rb | 1 - spec/factories/user_hints.rb | 8 + spec/factories/user_tasks.rb | 2 +- spec/helpers/user_hints_helper_spec.rb | 15 + spec/models/user_hint_spec.rb | 5 + spec/routing/user_hints_routing_spec.rb | 15 + 42 files changed, 661 insertions(+), 303 deletions(-) create mode 100644 app/assets/javascripts/user_hints.js.coffee create mode 100644 app/assets/stylesheets/user_hints.css.scss create mode 100644 app/controllers/user_hints_controller.rb create mode 100644 app/helpers/user_hints_helper.rb create mode 100644 app/models/user_hint.rb rename app/views/{home/_form.html.slim => games/_code_compare_form.html.slim} (58%) create mode 100644 db/migrate/20140115183545_create_user_hints.rb create mode 100644 db/migrate/20140115190550_add_state_to_user_games.rb create mode 100644 spec/controllers/user_hints_controller_spec.rb create mode 100644 spec/factories/user_hints.rb create mode 100644 spec/helpers/user_hints_helper_spec.rb create mode 100644 spec/models/user_hint_spec.rb create mode 100644 spec/routing/user_hints_routing_spec.rb diff --git a/.idea/.rakeTasks b/.idea/.rakeTasks index 84c84c5..6047d4f 100644 --- a/.idea/.rakeTasks +++ b/.idea/.rakeTasks @@ -4,4 +4,4 @@ You are allowed to: 1. Remove rake task 2. Add existing rake tasks To add existing rake tasks automatically delete this file and reload the project. ---> +--> diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 15fdd9c..af93d95 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -26,34 +26,34 @@ - + - - + + - + - - + + - + - - + + - + @@ -69,31 +69,7 @@ @@ -202,7 +202,7 @@ + + + + + @@ -516,16 +519,15 @@ - + - - + @@ -534,6 +536,7 @@ + @@ -544,8 +547,8 @@ - + @@ -555,10 +558,10 @@ - + + - @@ -578,359 +581,339 @@ - + - + - + - + - + - + - + - - - + - + - + - + - + - + - - - + - + - + - + - - - + - + - + - + - + - + - + - + - - - + - + - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + diff --git a/app/assets/javascripts/user_hints.js.coffee b/app/assets/javascripts/user_hints.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/user_hints.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/user_hints.css.scss b/app/assets/stylesheets/user_hints.css.scss new file mode 100644 index 0000000..8f2be89 --- /dev/null +++ b/app/assets/stylesheets/user_hints.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the user_hints controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/code_compares_controller.rb b/app/controllers/code_compares_controller.rb index 1f00918..86dc2bc 100644 --- a/app/controllers/code_compares_controller.rb +++ b/app/controllers/code_compares_controller.rb @@ -1,17 +1,57 @@ class CodeComparesController < ApplicationController before_action :set_code_compare, only: [:destroy] skip_before_filter :authorize_admin + before_filter :check_code_compare_creation, only: [:create] + before_filter :authenticate_user! + def create - @code_compare = CodeCompare.new(code_compare_params) + #Не совсем понял как получить пароль конкретного юзера из девиза + @user_task = UserTask.where(user_id: current_user.id).last + @task_codes = TaskCode.where(task_id: @user_task.task_id) + notice = "" + @task_game = GameTask.where(task_id: @user_task.task_id).first + @task_codes.each do |task_code| + @code = Code.where(id: task_code.code_id, code_string: params[:try_text]) + if @code.count > 0 + new_code_compare = {user_id: current_user.id, code_id: @code.first.id} + @code_compare = CodeCompare.new(new_code_compare) + #respond_to do |format| + if @code_compare.save + notice = "Code was matched" + if (CodeCompare.where(user_id: current_user.id, code_id: task_code.code_id).count == + @task_codes.count) + @user_hints = UserHint.where(user_id: current_user.id).count + @task = Task.find(task_code.task_id) + points_received = @task.points - @user_hints + @user_task.update(result: points_received) + @new_game_task = GameTask.where("`game_id` = ? AND `task_id` <> ?", @task_game.game_id, task_code.task_id) + if @new_game_task.count > 0 + UserTask.create(user_id: current_user.id, task_id: @new_game_task.first.task_id) + notice = "Code was matched. New Task was started" + else + @user_tasks = UserTask.where(user_id: current_user.id) + points = 0 + @user_tasks.each do |user_task| + points += user_task.result + end + @user_game = UserGame.where(user_id: current_user.id, game_id: @task_game.game_id).first + @user_game.update(result: points, state: -1) + notice = "Code was matched. Game was ended" + end + end + end + #end + break + end + end + + if notice == "" + notice = "Code was not matched" + end respond_to do |format| - if @code_compare.save - format.html { head :no_content } - format.json { head :no_content } - else - format.html { head :no_content } - format.json { head :no_content } - end + format.html { redirect_to game_path(id: @task_game.game_id), notice: notice } + format.json { head :no_content } end end @@ -33,4 +73,13 @@ def set_code_compare def code_compare_params params.require(:code_compare).permit(:user_id, :code_id) end + + protected + def check_code_compare_creation + unless params[:try_text] && params[:task] + @game_task = GameTask.where(task_id: params[:task].to_i).first + redirect_to game_path(id: @game_task.game_id) + end + end + end diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index c8d03bb..48ad355 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -1,16 +1,27 @@ class GamesController < ApplicationController - before_action :set_game, only: [:show, :edit, :update, :destroy, :start_game] + before_action :set_game, only: [:show, :edit, :update, :destroy, :start_game, :check_game_status, :finish_game] skip_before_filter :authorize_admin, only: [:index, :show] - + before_filter :check_game_status, only: [:show] # GET /games # GET /games.json def index - @games = Game.where(state: 0) + @games = Game.all end # GET /games/1 # GET /games/1.json def show + @status = "" + if @game.state == 0 + @status = "Wait" + elsif @game.state > 0 + @status = "Started" + @user_task = UserTask.where(user_id: current_user.id, game_id: @game.id).last + @task = Task.find(@user_task.task_id).first + @task_codes_count = TaskCode.where(task_id: @user_task.task_id).count + elsif @game.state < 0 + @status = "Finished" + end end # GET /games/new @@ -74,19 +85,41 @@ def set_game # Never trust parameters from the scary internet, only allow the white list through. def game_params - params.require(:game).permit(:title, :start_date, :duration) + params.require(:game).permit(:title, :start_date) end public - + #Заглушки для отложенных задач def start_game @game.update(state: 1) @user_games = UserGame.where(game_id: @game.id) @game_tasks = GameTask.where(game_id: @game.id).first @user_games.each do |user_game| - @user_tasks.create(user_id: @user_game.id, task_id: @game_tasks.first.task_id, result: 0) + UserTask.create(user_id: user_game.id, task_id: @game_tasks.task_id, result: 0) + user_game.update(state: 1) + end + redirect_to games_path + end + + def finish_game + @game.update(state: -1) + @user_games = UserGame.where(game_id: @game.id) + @user_games.each do |user_game| + points = 0 + UserTask.where(user_id: user_game.user_id).each do |user_task| + points += user_task.result + end + user_game.update(state: -1, result: points) end redirect_to games_path end + protected + + def check_game_status + if user_signed_in? && @game.status <= 0 + redirect_to root_path + end + end + end diff --git a/app/controllers/hints_controller.rb b/app/controllers/hints_controller.rb index 928638b..f5bf62e 100644 --- a/app/controllers/hints_controller.rb +++ b/app/controllers/hints_controller.rb @@ -18,8 +18,12 @@ def show # GET /hints/new def new + task = params[:task].to_i + @cant_add = false + if TaskHint.where(task_id: task).count == 2 + @cant_add = true + end @hint = Hint.new - @game = params[:game] end # GET /hints/1/edit @@ -29,8 +33,8 @@ def edit # POST /hints # POST /hints.json def create - @hint = Hint.new(hint_params) task = params.require(:hint)[:task].to_i + @hint = Hint.new(hint_params) respond_to do |format| if @hint.save if TaskHint.create(task_id: task, hint_id: @hint.id) @@ -91,6 +95,14 @@ def check_task_create unless params.require(:hint)[:task] redirect_to games_path end + task = params.require(:hint)[:task].to_i + @task_hints = TaskHint.where(task_id: task) + if @task_hints.count > 0 + @hint = Hint.find(@task_hints.first.hint_id) + if @hint.queue_number == params.require(:hint)[:queue_number].to_i + redirect_to new_hint_path, notice: "Parameter queue_number must be equal to " + (3 - params.require(:hint)[:queue_number].to_i).to_s + end + end end def no_hints diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 3ce1d6f..7193e8f 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -3,18 +3,6 @@ class HomeController < ApplicationController before_filter :authenticate_user!, only: [:game_code_compares, :create_code_compare] def index - @games = Game.all + @games = Game.where(state: 0) end - - def create_code_compare - #@code = Code.where(params[:try_text]).joins() - #if @code.count > 0 - # code_compare_params = {} - # code_compare_params[:code_compare] = {user_id: current_user.id, game_id: params[:game].to_i} - # CodeCompare.create(code_compare_params) - #else - # redirect_to "home/game_code_compares", notice: "Code does not match" - #end - end - end diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index a5b7d9c..b146676 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -1,9 +1,8 @@ class TasksController < ApplicationController - before_action :set_task, only: [:show, :edit, :update, :destroy] + before_action :set_task, only: [:show, :edit, :update, :destroy, :raise_hint] before_filter :check_game, only: [:new] before_filter :check_game_create, only: [:create] before_filter :no_tasks, except: [:new, :create] - # GET /tasks # GET /tasks.json def index @@ -96,4 +95,27 @@ def no_tasks redirect_to games_path end end + + public + #еще заглушка + def raise_hint + TaskHint.where(task_id: @task.id).each do |task_hint| + @hint = Hint.find(task_hint.hint_id) + if UserHint.where(hint_id: @hint.id).count > 0 + next + else + @user_tasks = UserTask.where(task_id: task_hint.task_id) + @user_tasks.each do |user_task| + if user_task.result < 0 + UserHint.create(user_id: user_task.user_id, hint_id: @hint.id) + + end + end + break + end + end + + redirect_to tasks_path + end + end diff --git a/app/controllers/user_games_controller.rb b/app/controllers/user_games_controller.rb index f23f252..3a5a8e2 100644 --- a/app/controllers/user_games_controller.rb +++ b/app/controllers/user_games_controller.rb @@ -3,10 +3,12 @@ class UserGamesController < ApplicationController skip_before_filter :authorize_admin before_filter :authenticate_user! before_filter :check_game_create, only: [:create] + before_filter :user_games_count, except: [:create] # GET /user_games # GET /user_games.json def index - @user_games = UserGame.where(user_id: current_user.id) + #@user_games = UserGame.where(""user_id: current_user.id, "`state` >= 0""") + @user_games = UserGame.where("`user_id` = ? AND `state` >= ?", current_user.id, 0) @games = [] @user_games.each do |user_game| @games << Game.find(user_game.game_id) @@ -30,15 +32,14 @@ def edit # POST /user_games # POST /user_games.json def create - new_user_game = user_game_params + new_user_game = {} + new_user_game[:game_id] = params[:game_id].to_i new_user_game[:user_id] = current_user.id @user_game = UserGame.new(new_user_game) - #@user_game = UserGame.new({game_id: params[:game].to_i, user_id: current_user.id}) - respond_to do |format| if @user_game.save - format.html { redirect_to user_games_url, notice: 'User game was successfully created.' } + format.html { redirect_to user_games_url, notice: 'User game was join in game with success.' } format.json { render action: 'index', status: :created, location: @user_game } else format.html { redirect_to root_path } @@ -87,8 +88,15 @@ def user_game_params protected def check_game_create - unless params.require(:user_game) + unless params.require(:game_id) + redirect_to root_path + end + end + + def user_games_count + unless UserGame.count > 0 redirect_to root_path end end + end \ No newline at end of file diff --git a/app/controllers/user_hints_controller.rb b/app/controllers/user_hints_controller.rb new file mode 100644 index 0000000..6e9a047 --- /dev/null +++ b/app/controllers/user_hints_controller.rb @@ -0,0 +1,42 @@ +class UserHintsController < ApplicationController + before_action :set_user_hint, only: [:destroy] + before_filter :authenticate_user! + skip_before_filter :authorize_admin + + # POST /user_hints + # POST /user_hints.json + def create + @user_hint = UserHint.new(user_hint_params) + + respond_to do |format| + if @user_hint.save + format.html { head :no_content } + format.json { head :no_content } + else + format.html { head :no_content } + format.json { head :no_content } + end + end + end + + # DELETE /user_hints/1 + # DELETE /user_hints/1.json + def destroy + @user_hint.destroy + respond_to do |format| + format.html { head :no_content } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_user_hint + @user_hint = UserHint.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def user_hint_params + params.require(:user_hint).permit(:user_id, :hint_id) + end +end diff --git a/app/helpers/user_hints_helper.rb b/app/helpers/user_hints_helper.rb new file mode 100644 index 0000000..2211fa8 --- /dev/null +++ b/app/helpers/user_hints_helper.rb @@ -0,0 +1,2 @@ +module UserHintsHelper +end diff --git a/app/models/game.rb b/app/models/game.rb index c1cc3c3..07da62d 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -2,7 +2,8 @@ class Game < ActiveRecord::Base has_one :admin_game, dependent: :destroy has_many :user_games, dependent: :destroy + has_many :game_tasks, dependent: :destroy - validate :title, :start_date, :duration, presence: true + validate :title, :start_date, presence: true end diff --git a/app/models/hint.rb b/app/models/hint.rb index 79bbf6f..2e4b3bf 100644 --- a/app/models/hint.rb +++ b/app/models/hint.rb @@ -1,5 +1,5 @@ class Hint < ActiveRecord::Base has_one :task_hint, dependent: :destroy - + has_many :user_hints, dependent: :destroy validate :hint_text, :queue_number, presence: true end diff --git a/app/models/task.rb b/app/models/task.rb index fd7eddb..0d689f1 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -2,6 +2,7 @@ class Task < ActiveRecord::Base has_many :task_hints, dependent: :destroy has_many :task_codes, dependent: :destroy has_many :user_tasks, dependent: :destroy + has_one :game_task, dependent: :destroy validate :task_text, :points, presence: true end diff --git a/app/models/user.rb b/app/models/user.rb index 55da2f0..18b384d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,6 +5,7 @@ class User < ActiveRecord::Base has_many :code_compares, dependent: :destroy has_many :code_compares, dependent: :destroy has_many :user_tasks, dependent: :destroy + has_many :user_hints, dependent: :destroy devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable end diff --git a/app/models/user_hint.rb b/app/models/user_hint.rb new file mode 100644 index 0000000..b304cc1 --- /dev/null +++ b/app/models/user_hint.rb @@ -0,0 +1,4 @@ +class UserHint < ActiveRecord::Base + belongs_to :user + belongs_to :hint +end diff --git a/app/views/home/_form.html.slim b/app/views/games/_code_compare_form.html.slim similarity index 58% rename from app/views/home/_form.html.slim rename to app/views/games/_code_compare_form.html.slim index 15ff142..01e0533 100644 --- a/app/views/home/_form.html.slim +++ b/app/views/games/_code_compare_form.html.slim @@ -1,7 +1,7 @@ -= form_tag "home/create_code_compare", method: "post" do |f| += form_tag "code_compares/create", method: "post" do |f| .control-group = f.label "try_text", :class => 'control-label' .controls = f.text_field "try_text", :class => 'text_field' .controls - = f.hidden_field_tag "game", :value => game.id \ No newline at end of file + = f.hidden_field_tag "task", :value => task.id \ No newline at end of file diff --git a/app/views/games/_form.html.slim b/app/views/games/_form.html.slim index aa73253..f2e8c72 100644 --- a/app/views/games/_form.html.slim +++ b/app/views/games/_form.html.slim @@ -7,10 +7,6 @@ = f.label :start_date, :class => 'control-label' .controls = f.text_field :start_date, :class => 'text_field' - .control-group - = f.label :duration, :class => 'control-label' - .controls - = f.text_field :duration, :class => 'text_field' .form-actions = f.submit nil, :class => 'btn btn-primary' ' diff --git a/app/views/games/index.html.slim b/app/views/games/index.html.slim index 036bb96..a7253b8 100644 --- a/app/views/games/index.html.slim +++ b/app/views/games/index.html.slim @@ -7,7 +7,6 @@ table class="table table-striped" th= model_class.human_attribute_name(:id) th= model_class.human_attribute_name(:title) th= model_class.human_attribute_name(:start_date) - th= model_class.human_attribute_name(:duration) th= model_class.human_attribute_name(:created_at) th=t '.actions', :default => t("helpers.actions") tbody @@ -16,8 +15,6 @@ table class="table table-striped" td= link_to game.id, game_path(game) td= game.title td= game.start_date - td= game.duration - td=l game.created_at td = link_to t('.edit', :default => t("helpers.links.edit")), edit_game_path(game), :class => 'btn btn-mini' ' diff --git a/app/views/games/show.html.slim b/app/views/games/show.html.slim index f23d5e4..3ee62a0 100644 --- a/app/views/games/show.html.slim +++ b/app/views/games/show.html.slim @@ -1,25 +1,29 @@ - model_class = Game -div class="page-header" - h1=t '.title', :default => model_class.model_name.human.titleize -p - strong= model_class.human_attribute_name(:title) + ':' - br - = @game.title -p - strong= model_class.human_attribute_name(:start_date) + ':' - br - = @game.start_date -p - strong= model_class.human_attribute_name(:duration) + ':' - br - = @game.duration + div class="page-header" + h1=t '.title', :default => model_class.model_name.human.titleize -.form-actions - = link_to t('.back', :default => t("helpers.links.back")), games_path, :class => 'btn' + p + strong= model_class.human_attribute_name(:title) + ':' + br + = @game.title + -unless current_user? + p + strong= model_class.human_attribute_name(:start_date) + ':' + br + = @game.start_date + p + strong= model_class.human_attribute_name(:start_date) + ':' + br + = @status + .form-actions + = link_to t('.back', :default => t("helpers.links.back")), games_path, :class => 'btn' - = link_to t('.back', :default => t("helpers.links.back")), new_hint_path(game: @game.id), :class => 'btn' + = link_to t('.back', :default => t("helpers.links.back")), new_hint_path(game: @game.id), :class => 'btn' - = link_to t('.edit', :default => t("helpers.links.edit")), edit_game_path(@game), :class => 'btn' - ' - = link_to t('.destroy', :default => t("helpers.links.destroy")), game_path(@game), :method => "delete", :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-danger' + = link_to t('.edit', :default => t("helpers.links.edit")), edit_game_path(@game), :class => 'btn' + ' + = link_to t('.destroy', :default => t("helpers.links.destroy")), game_path(@game), :method => "delete", :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-danger' + -else + -@task_codes_count.each do |num| + -render partial: code_compare_form \ No newline at end of file diff --git a/app/views/hints/new.html.slim b/app/views/hints/new.html.slim index 8ac3066..2926f4d 100644 --- a/app/views/hints/new.html.slim +++ b/app/views/hints/new.html.slim @@ -1,4 +1,11 @@ - model_class = Hint -div class="page-header" - h1=t '.title', :default => [:'helpers.titles.new', 'New %{model}'], :model => model_class.model_name.human.titleize -= render :partial => "form" + div class="page-header" + -unless @cant_add + -if notice + h1 = notice + h1=t '.title', :default => [:'helpers.titles.new', 'New %{model}'], :model => model_class.model_name.human.titleize + = render :partial => "form" + -else + h1 You can't add more hints for this task + + diff --git a/app/views/home/index.html.slim b/app/views/home/index.html.slim index 23a8d94..7edbf87 100644 --- a/app/views/home/index.html.slim +++ b/app/views/home/index.html.slim @@ -10,14 +10,12 @@ th=t '.actions', :default => t("helpers.actions") tbody - @games.each do |game| - tr - //td= link_to game.id, game_path(game) - td= game.title - td= game.start_date - td= game.duration - td=l game.created_at - td - = link_to "Subscribe", {controller: "user_games", action: "create", user_game: {game_id: game.id}}, :class => 'btn btn-mini' + - if game.state == 0 + tr + td= game.title + td= game.start_date + td + = link_to "Join", {controller: "user_games", action: "create", user_game: {game_id: game.id}}, :class => 'btn btn-mini' - else div id="main" diff --git a/app/views/user_games/index.html.slim b/app/views/user_games/index.html.slim index bbf8251..b3a2b72 100644 --- a/app/views/user_games/index.html.slim +++ b/app/views/user_games/index.html.slim @@ -13,10 +13,12 @@ table class="table table-striped" tbody - @games.each do |game| tr - td= game.title + -if game.state >0 + td + = link_to game.title, game_path({id: game.id}), :method => :get, :class => 'btn btn-mini' + -else + td= game.title td= game.start_date - td= game.duration - td=l game.created_at //td = link_to t('.destroy', :default => t("helpers.links.destroy")), user_game_path({id: game[:user_game]}), :method => :delete, :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-mini btn-danger' diff --git a/config/routes.rb b/config/routes.rb index 7645542..07660f3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,10 @@ Nightzone::Application.routes.draw do + controller :user_hints do + post 'user_hints/', :to => 'user_hints#create' + delete 'user_hints/:id', :to => 'user_hints#destroy' + end + controller :game_tasks do post 'game_tasks/', :to => 'game_tasks#create' delete 'game_tasks/:id', :to => 'game_tasks#destroy' @@ -54,7 +59,13 @@ get "home/index" controller :games do get "start_game/:id" => "games#start_game" + get "finish_game/:id" => "games#finish_game" end + + controller :tasks do + get "raise_hint/:id" => "tasks#raise_hint" + end + #get "home/game_tasks_compares" #post "home/create_code_compare" devise_for :admin_users, ActiveAdmin::Devise.config diff --git a/db/migrate/20140112202751_create_games.rb b/db/migrate/20140112202751_create_games.rb index 685e4e6..5d8ed96 100644 --- a/db/migrate/20140112202751_create_games.rb +++ b/db/migrate/20140112202751_create_games.rb @@ -3,7 +3,6 @@ def change create_table :games do |t| t.string :title t.datetime :start_date - t.integer :duration t.timestamps end diff --git a/db/migrate/20140115090713_create_user_tasks.rb b/db/migrate/20140115090713_create_user_tasks.rb index 6ebdd23..381311a 100644 --- a/db/migrate/20140115090713_create_user_tasks.rb +++ b/db/migrate/20140115090713_create_user_tasks.rb @@ -3,7 +3,7 @@ def change create_table :user_tasks do |t| t.integer :user_id t.integer :task_id - t.integer :result, default: 0 + t.integer :result, default: -1 t.timestamps end diff --git a/db/migrate/20140115183545_create_user_hints.rb b/db/migrate/20140115183545_create_user_hints.rb new file mode 100644 index 0000000..73a7385 --- /dev/null +++ b/db/migrate/20140115183545_create_user_hints.rb @@ -0,0 +1,10 @@ +class CreateUserHints < ActiveRecord::Migration + def change + create_table :user_hints do |t| + t.integer :user_id + t.integer :hint_id + + t.timestamps + end + end +end diff --git a/db/migrate/20140115190550_add_state_to_user_games.rb b/db/migrate/20140115190550_add_state_to_user_games.rb new file mode 100644 index 0000000..354af4c --- /dev/null +++ b/db/migrate/20140115190550_add_state_to_user_games.rb @@ -0,0 +1,5 @@ +class AddStateToUserGames < ActiveRecord::Migration + def change + add_column :user_games, :state, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index 0e0336f..84ffca4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140115100758) do +ActiveRecord::Schema.define(version: 20140115190550) do create_table "active_admin_comments", force: true do |t| t.string "namespace" @@ -76,7 +76,6 @@ create_table "games", force: true do |t| t.string "title" t.datetime "start_date" - t.integer "duration" t.datetime "created_at" t.datetime "updated_at" t.integer "state", default: 0 @@ -116,12 +115,20 @@ t.integer "result", default: 0 t.datetime "created_at" t.datetime "updated_at" + t.integer "state", default: 0 + end + + create_table "user_hints", force: true do |t| + t.integer "user_id" + t.integer "hint_id" + t.datetime "created_at" + t.datetime "updated_at" end create_table "user_tasks", force: true do |t| t.integer "user_id" t.integer "task_id" - t.integer "result", default: 0 + t.integer "result", default: -1 t.datetime "created_at" t.datetime "updated_at" end diff --git a/spec/controllers/code_compares_controller_spec.rb b/spec/controllers/code_compares_controller_spec.rb index 9999395..3ea9f88 100644 --- a/spec/controllers/code_compares_controller_spec.rb +++ b/spec/controllers/code_compares_controller_spec.rb @@ -18,39 +18,67 @@ # Message expectations are only used when there is no simpler way to specify # that an instance is receiving a specific message. -describe AdminGamesController do - login_admin +describe CodeComparesController do + login_user # This should return the minimal set of attributes required to create a valid - # AdminGame. As you add validations to AdminGame, be sure to + # CodeCompare. As you add validations to CodeCompare, be sure to # adjust the attributes here as well. - let(:valid_attributes) { { "admin_id" => "1" } } + let(:valid_attributes) { { "user_id" => "1" } } # This should return the minimal set of values that should be in the session # in order to pass any filters (e.g. authentication) defined in - # AdminGamesController. Be sure to keep this updated too. + # CodeComparesController. Be sure to keep this updated too. let(:valid_session) { {} } before do + @code = FactoryGirl.create(:code) + @task = FactoryGirl.create(:task) @game = FactoryGirl.create(:game) - @admin_game = FactoryGirl.create(:admin_game, admin_id: @admin_id, game_id: @game.to_param) + @user_task = FactoryGirl.create(:user_task, user_id: @user_id, task_id: @task.to_param) + @task_code = FactoryGirl.create(:task_code, task_id: @task.to_param, code_id: @code.to_param) + @task_game = FactoryGirl.create(:game_task, game_id: @game.to_param, task_id: @task.to_param) + @user_game = FactoryGirl.create(:user_game, user_id: @user_id, game_id: @game.to_param) end describe "POST create" do describe "with valid params" do - it "creates a new AdminGame" do + it "creates a new CodeCompare" do expect { - post :create, {:admin_game => @admin_game.attributes}, valid_session - }.to change(AdminGame, :count).by(1) + post :create, {:try_text => @code.attributes["code_string"], task: @task.to_param}, valid_session + }.to change(CodeCompare, :count).by(1) end + + it "creates a new CodeCompare and change task" do + @new_task = FactoryGirl.create(:task, task_text: "sdgbhdrhndrbhzsgfr") + @new_code = FactoryGirl.create(:code, code_string: "dfbdfbndfgnhdfgdfh") + @new_user_task = FactoryGirl.build(:user_task, user_id: @user_id, task_id: @new_task.to_param) + @new_task_code = FactoryGirl.create(:task_code, task_id: @new_task.to_param, code_id: @new_code.to_param) + @new_task_game = FactoryGirl.create(:game_task, game_id: @game.to_param, task_id: @new_task.to_param) + post :create, {:try_text => @code.attributes["code_string"], task: @task.to_param}, valid_session + #puts UserTask.count + expect(UserTask.last.task_id).to eq(@new_task.to_param.to_i) + end + + it "creates a new CodeCompare and end the game of current user" do + post :create, {:try_text => @code.attributes["code_string"], task: @task.to_param}, valid_session + expect(UserGame.find(@user_game.to_param.to_i).state).to eq(-1) + end + + it "creates a new CodeCompare with code mismatched result" do + post :create, {:try_text => @code.attributes["code_string"] + "sefbgdfgbnfg", task: @task.to_param}, valid_session + expect(response).to redirect_to(game_path(id: @game.to_param)) + end + end end describe "DELETE destroy" do - it "destroys the requested admin_game" do + it "destroys the requested code_compare" do + @code_compare = FactoryGirl.create(:code_compare, user_id: @user_id, code_id: @code.to_param) expect { - delete :destroy, {:id => @admin_game.to_param}, valid_session - }.to change(AdminGame, :count).by(-1) + delete :destroy, {:id => @code_compare.to_param}, valid_session + }.to change(CodeCompare, :count).by(-1) end end -end +end \ No newline at end of file diff --git a/spec/controllers/games_controller_spec.rb b/spec/controllers/games_controller_spec.rb index a3a5d87..e994b78 100644 --- a/spec/controllers/games_controller_spec.rb +++ b/spec/controllers/games_controller_spec.rb @@ -33,13 +33,14 @@ before do @game = FactoryGirl.create(:game) @task = FactoryGirl.create(:task) - @user = FactoryGirl.create(:user) - @game_task = FactoryGirl.create(:game_task) - @user_game = FactoryGirl.create(:user_game) - @invalid_attributes = FactoryGirl.build(:game, title: "впирапеи", start_date: "01.01.2014 00:00:00".to_datetime, duration: 11).attributes + #@user = FactoryGirl.create(:user) + @game_task = FactoryGirl.create(:game_task, game_id: @game.to_param, task_id: @task.to_param) + @user_game = FactoryGirl.create(:user_game, user_id: @user_id, game_id: @game.to_param) + @invalid_attributes = FactoryGirl.build(:game, title: "впирапеи", start_date: "01.01.2014 00:00:00".to_datetime).attributes end describe "GET index" do + login_user it "assigns all games as @games" do get :index, {}, valid_session expect(assigns(:games)).to eq([@game]) @@ -54,6 +55,7 @@ end describe "GET new" do + login_user it "assigns a new game as @game" do get :new, {}, valid_session expect(assigns(:game)).to be_a_new(Game) @@ -160,11 +162,19 @@ end describe "Start game" do - it "exuctes action " do + it "execute action " do expect { get "start_game", {id: @game.to_param}, valid_session }.to change(UserTask, :count).by(1) end end + describe "Finish game" do + it "execute action " do + get "finish_game", {id: @game.to_param}, valid_session + expect(Game.find(@game.to_param.to_i).state).to eq(-1) + expect(UserGame.find(@user_game.to_param.to_i).state).to eq(-1) + end + end + end diff --git a/spec/controllers/hints_controller_spec.rb b/spec/controllers/hints_controller_spec.rb index c0b2e6a..1f87ba1 100644 --- a/spec/controllers/hints_controller_spec.rb +++ b/spec/controllers/hints_controller_spec.rb @@ -34,7 +34,9 @@ queue_number = rand(1...2) @task = FactoryGirl.create(:task) @hint = FactoryGirl.create(:hint, queue_number: queue_number) - @invalid_attributes = FactoryGirl.build(:hint, hint_text: "акпыук").attributes + @new_hint = FactoryGirl.build(:hint, queue_number: 3 - queue_number) + @task_hint = FactoryGirl.create(:task_hint, task_id: @task.to_param, hint_id: @hint.to_param) + @invalid_attributes = FactoryGirl.build(:hint, hint_text: "акпыук", queue_number: 3 - queue_number).attributes end describe "GET index" do @@ -49,6 +51,7 @@ get :show, {:id => @hint.to_param}, valid_session expect(assigns(:hint)).to eq(@hint) end + end describe "GET new" do @@ -56,6 +59,14 @@ get :new, {task: @task.to_param}, valid_session expect(assigns(:hint)).to be_a_new(Hint) end + + it "check task hints count" do + @new_hint = FactoryGirl.create(:hint, hint_text: "gfbdfsbgsdfgbdfg") + @new_task_hint = FactoryGirl.create(:task_hint, task_id: @task.to_param, hint_id: @new_hint.to_param) + get :show, {:id => @hint.to_param}, valid_session + expect(assigns(:hint)).to eq(@hint) + end + end describe "GET edit" do @@ -67,7 +78,7 @@ describe "POST create" do before(:each) do - @hint_post = @hint.attributes + @hint_post = @new_hint.attributes @hint_post[:task] = @task.to_param @hint_post_invalid = @invalid_attributes @hint_post_invalid[:task] = @task.to_param diff --git a/spec/controllers/tasks_controller_spec.rb b/spec/controllers/tasks_controller_spec.rb index 18678c5..e58fb9a 100644 --- a/spec/controllers/tasks_controller_spec.rb +++ b/spec/controllers/tasks_controller_spec.rb @@ -33,6 +33,12 @@ before do @game = FactoryGirl.create(:game) @task = FactoryGirl.create(:task) + @hint = FactoryGirl.create(:hint) + @user = FactoryGirl.create(:user) + @user_task = FactoryGirl.create(:user_task, user_id: @user.to_param, task_id: @task.to_param) + @task_hint = FactoryGirl.create(:task_hint, task_id: @task.to_param, hint_id: @hint.to_param) + @hint2 = FactoryGirl.create(:hint, hint_text: " wvgrawrfswf", queue_number: 2) + @task_hint2 = FactoryGirl.create(:task_hint, task_id: @task.to_param, hint_id: @hint2.to_param) #puts Task.first.to_yaml @invalid_attributes = FactoryGirl.build(:task, task_text: "впирапеи").attributes end @@ -163,4 +169,21 @@ end end + describe "Raise hint" do + it "execute action " do + expect { + get "raise_hint", {id: @task.to_param}, valid_session + }.to change(UserHint, :count).by(1) + end + + it "insert second hint" do + @user_hint = FactoryGirl.create(:user_hint, user_id: @user.to_param, hint_id: @hint.to_param) + expect { + get "raise_hint", {id: @task.to_param}, valid_session + }.to change(UserHint, :count).by(1) + expect(UserHint.last.hint_id).to eq(@hint2.to_param.to_i) + end + + end + end diff --git a/spec/controllers/user_games_controller_spec.rb b/spec/controllers/user_games_controller_spec.rb index 5082e10..c3c7008 100644 --- a/spec/controllers/user_games_controller_spec.rb +++ b/spec/controllers/user_games_controller_spec.rb @@ -70,18 +70,18 @@ describe "with valid params" do it "creates a new UserGame" do expect { - post :create, {:user_game => @user_game.attributes}, valid_session + post :create, {game_id: @user_game.attributes["game_id"]}, valid_session }.to change(UserGame, :count).by(1) end it "assigns a newly created user_game as @user_game" do - post :create, {:user_game => @user_game.attributes}, valid_session + post :create, {game_id: @user_game.attributes["game_id"]}, valid_session expect(assigns(:user_game)).to be_a(UserGame) expect(assigns(:user_game)).to be_persisted end it "redirects to the created users games list" do - post :create, {:user_game => @user_game.attributes}, valid_session + post :create, {game_id: @user_game.attributes["game_id"]}, valid_session expect(response).to redirect_to(user_games_url) end end @@ -90,14 +90,14 @@ it "assigns a newly created but unsaved user_game as @user_game" do # Trigger the behavior that occurs when invalid params are submitted UserGame.any_instance.stub(:save).and_return(false) - post :create, {:user_game => @invalid_attributes}, valid_session + post :create, {game_id: "123"}, valid_session expect(assigns(:user_game)).to be_a_new(UserGame) end it "redirect the root template" do # Trigger the behavior that occurs when invalid params are submitted UserGame.any_instance.stub(:save).and_return(false) - post :create, {:user_game => @invalid_attributes}, valid_session + post :create, {game_id: "123"}, valid_session expect(response).to redirect_to(root_path) end end diff --git a/spec/controllers/user_hints_controller_spec.rb b/spec/controllers/user_hints_controller_spec.rb new file mode 100644 index 0000000..ffc0bcf --- /dev/null +++ b/spec/controllers/user_hints_controller_spec.rb @@ -0,0 +1,56 @@ +require 'spec_helper' + +# This spec was generated by rspec-rails when you ran the scaffold generator. +# It demonstrates how one might use RSpec to specify the controller code that +# was generated by Rails when you ran the scaffold generator. +# +# It assumes that the implementation code is generated by the rails scaffold +# generator. If you are using any extension libraries to generate different +# controller code, this generated spec may or may not pass. +# +# It only uses APIs available in rails and/or rspec-rails. There are a number +# of tools you can use to make these specs even more expressive, but we're +# sticking to rails and rspec-rails APIs to keep things simple and stable. +# +# Compared to earlier versions of this generator, there is very limited use of +# stubs and message expectations in this spec. Stubs are only used when there +# is no simpler way to get a handle on the object needed for the example. +# Message expectations are only used when there is no simpler way to specify +# that an instance is receiving a specific message. + +describe UserHintsController do + login_user + # This should return the minimal set of attributes required to create a valid + # UserHint. As you add validations to UserHint, be sure to + # adjust the attributes here as well. + let(:valid_attributes) { { "user_id" => "1" } } + + # This should return the minimal set of values that should be in the session + # in order to pass any filters (e.g. authentication) defined in + # UserHintsController. Be sure to keep this updated too. + let(:valid_session) { {} } + + before do + @hint = FactoryGirl.create(:hint) + @user_hint = FactoryGirl.create(:user_hint, user_id: @user_id, hint_id: @hint.to_param) + end + + describe "POST create" do + describe "with valid params" do + it "creates a new UserHint" do + expect { + post :create, {:user_hint => @user_hint.attributes}, valid_session + }.to change(UserHint, :count).by(1) + end + end + end + + describe "DELETE destroy" do + it "destroys the requested user_hint" do + expect { + delete :destroy, {:id => @user_hint.to_param}, valid_session + }.to change(UserHint, :count).by(-1) + end + end + +end diff --git a/spec/factories/games.rb b/spec/factories/games.rb index 2b60216..3771d14 100644 --- a/spec/factories/games.rb +++ b/spec/factories/games.rb @@ -4,6 +4,5 @@ factory :game do title "new_game" start_date "01.01.2014 00:00:00".to_datetime - duration 30 end end diff --git a/spec/factories/user_hints.rb b/spec/factories/user_hints.rb new file mode 100644 index 0000000..d21cac1 --- /dev/null +++ b/spec/factories/user_hints.rb @@ -0,0 +1,8 @@ +# Read about factories at https://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :user_hint do + user_id 1 + hint_id 1 + end +end diff --git a/spec/factories/user_tasks.rb b/spec/factories/user_tasks.rb index 921b326..86ddf0d 100644 --- a/spec/factories/user_tasks.rb +++ b/spec/factories/user_tasks.rb @@ -4,6 +4,6 @@ factory :user_task do user_id 1 task_id 1 - result 1 + result -1 end end diff --git a/spec/helpers/user_hints_helper_spec.rb b/spec/helpers/user_hints_helper_spec.rb new file mode 100644 index 0000000..87ba482 --- /dev/null +++ b/spec/helpers/user_hints_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the UserHintsHelper. For example: +# +# describe UserHintsHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +describe UserHintsHelper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/user_hint_spec.rb b/spec/models/user_hint_spec.rb new file mode 100644 index 0000000..64b6d6c --- /dev/null +++ b/spec/models/user_hint_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe UserHint do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/routing/user_hints_routing_spec.rb b/spec/routing/user_hints_routing_spec.rb new file mode 100644 index 0000000..f643ddc --- /dev/null +++ b/spec/routing/user_hints_routing_spec.rb @@ -0,0 +1,15 @@ +require "spec_helper" + +describe UserHintsController do + describe "routing" do + + it "routes to #create" do + expect(:post => "/user_hints").to route_to("user_hints#create") + end + + it "routes to #destroy" do + expect(:delete => "/user_hints/1").to route_to("user_hints#destroy", :id => "1") + end + + end +end From b012798e2bf63efe80f9d42f4fe72f6f838c17fb Mon Sep 17 00:00:00 2001 From: sorgal Date: Thu, 16 Jan 2014 18:06:37 +0400 Subject: [PATCH 03/15] change code compare create function --- .idea/nightzone.iml | 9 + .idea/workspace.xml | 374 ++++++++---------- Gemfile | 3 +- Gemfile.lock | 26 ++ app/controllers/code_compares_controller.rb | 88 +++-- app/controllers/games_controller.rb | 1 + app/controllers/hints_controller.rb | 2 +- app/realtime/realtime_chat_controller.rb | 0 app/views/games/index.html.slim | 6 + app/views/games/show.html.slim | 38 +- app/views/hints/new.html.slim | 4 +- app/views/home/index.html.slim | 8 +- app/views/tasks/index.html.slim | 6 +- app/views/tasks/show.html.slim | 4 + .../code_compares_controller_spec.rb | 7 + spec/spec_helper.rb | 1 + spec/views/hints/new.html.slim_spec.rb | 1 + 17 files changed, 307 insertions(+), 271 deletions(-) create mode 100644 app/realtime/realtime_chat_controller.rb diff --git a/.idea/nightzone.iml b/.idea/nightzone.iml index 0255447..e664211 100644 --- a/.idea/nightzone.iml +++ b/.idea/nightzone.iml @@ -142,21 +142,29 @@ + + + + + + + + @@ -216,6 +224,7 @@ + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index af93d95..533913e 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -23,37 +23,28 @@ - - + + - + - - + + - + - - + + - - - - - - - - - - + @@ -69,18 +60,10 @@ @@ -156,6 +147,7 @@ + @@ -188,46 +180,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - @@ -274,15 +222,11 @@ - @@ -328,6 +271,10 @@ + + + + @@ -510,6 +457,23 @@ + + @@ -519,7 +483,7 @@ - + @@ -527,7 +491,7 @@ - + @@ -535,12 +499,12 @@ - + - + @@ -559,9 +523,9 @@ - - + + @@ -581,339 +545,327 @@ - + - + - + - - - + - + - - - + - + - + - + - - - + - + - + - - - - - - + - + - + - + - + - - - + - + - + - + - - - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - + - + - + + + + + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + diff --git a/Gemfile b/Gemfile index a45a9c7..8cb4751 100644 --- a/Gemfile +++ b/Gemfile @@ -67,4 +67,5 @@ gem 'selenium-webdriver' gem 'activeadmin', github: 'gregbell/active_admin' -gem 'poltergeist' \ No newline at end of file +gem 'poltergeist' +gem 'faye-rails' diff --git a/Gemfile.lock b/Gemfile.lock index 6b2ca2d..2b56367 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -69,6 +69,7 @@ GEM coffee-script-source execjs coffee-script-source (1.6.3) + cookiejar (0.3.0) cucumber (1.3.10) builder (>= 2.1.2) diff-lcs (>= 1.1.3) @@ -88,16 +89,38 @@ GEM thread_safe (~> 0.1) warden (~> 1.2.3) diff-lcs (1.2.5) + em-http-request (1.1.2) + addressable (>= 2.3.4) + cookiejar + em-socksify (>= 0.3) + eventmachine (>= 1.0.3) + http_parser.rb (>= 0.6.0) + em-socksify (0.3.0) + eventmachine (>= 1.0.0.beta.4) email_spec (1.5.0) launchy (~> 2.1) mail (~> 2.2) erubis (2.7.0) + eventmachine (1.0.3) execjs (2.0.2) factory_girl (4.3.0) activesupport (>= 3.0.0) factory_girl_rails (4.3.0) factory_girl (~> 4.3.0) railties (>= 3.0.0) + faye (0.8.9) + cookiejar (>= 0.3.0) + em-http-request (>= 0.3.0) + eventmachine (>= 0.12.0) + faye-websocket (>= 0.4.0) + rack (>= 1.0.0) + yajl-ruby (>= 1.0.0) + faye-rails (1.0.10) + eventmachine (~> 1.0.0) + faye (~> 0.8.9) + faye-websocket (0.7.2) + eventmachine (>= 0.12.0) + websocket-driver (>= 0.3.1) ffi (1.9.3) formtastic (2.3.0.rc2) actionpack (>= 3.0) @@ -107,6 +130,7 @@ GEM actionpack (>= 3.2, < 5) activesupport (>= 3.2, < 5) hike (1.2.3) + http_parser.rb (0.6.0) i18n (0.6.9) inherited_resources (1.4.1) has_scope (~> 0.6.0.rc) @@ -250,6 +274,7 @@ GEM websocket-driver (0.3.2) xpath (2.0.0) nokogiri (~> 1.3) + yajl-ruby (1.2.0) PLATFORMS ruby @@ -263,6 +288,7 @@ DEPENDENCIES devise email_spec factory_girl_rails (~> 4.0) + faye-rails jbuilder (~> 1.2) jquery-rails mysql2 diff --git a/app/controllers/code_compares_controller.rb b/app/controllers/code_compares_controller.rb index 86dc2bc..2cb365b 100644 --- a/app/controllers/code_compares_controller.rb +++ b/app/controllers/code_compares_controller.rb @@ -5,48 +5,33 @@ class CodeComparesController < ApplicationController before_filter :authenticate_user! def create - #Не совсем понял как получить пароль конкретного юзера из девиза @user_task = UserTask.where(user_id: current_user.id).last - @task_codes = TaskCode.where(task_id: @user_task.task_id) - notice = "" @task_game = GameTask.where(task_id: @user_task.task_id).first - @task_codes.each do |task_code| - @code = Code.where(id: task_code.code_id, code_string: params[:try_text]) - if @code.count > 0 - new_code_compare = {user_id: current_user.id, code_id: @code.first.id} - @code_compare = CodeCompare.new(new_code_compare) - #respond_to do |format| + notice = "" + unless pass_matching(params[:try_text]) + @task_codes = TaskCode.where(task_id: @user_task.task_id) + @task_codes.each do |task_code| + @code = Code.where(id: task_code.code_id, code_string: params[:try_text]) + if @code.count > 0 + new_code_compare = {user_id: current_user.id, code_id: @code.first.id} + @code_compare = CodeCompare.new(new_code_compare) if @code_compare.save - notice = "Code was matched" + notice += "Code was matched" if (CodeCompare.where(user_id: current_user.id, code_id: task_code.code_id).count == @task_codes.count) - @user_hints = UserHint.where(user_id: current_user.id).count - @task = Task.find(task_code.task_id) - points_received = @task.points - @user_hints - @user_task.update(result: points_received) - @new_game_task = GameTask.where("`game_id` = ? AND `task_id` <> ?", @task_game.game_id, task_code.task_id) - if @new_game_task.count > 0 - UserTask.create(user_id: current_user.id, task_id: @new_game_task.first.task_id) - notice = "Code was matched. New Task was started" - else - @user_tasks = UserTask.where(user_id: current_user.id) - points = 0 - @user_tasks.each do |user_task| - points += user_task.result - end - @user_game = UserGame.where(user_id: current_user.id, game_id: @task_game.game_id).first - @user_game.update(result: points, state: -1) - notice = "Code was matched. Game was ended" - end + notice += ". " + change_user_task_and_user_game(task_code.task_id, -1, @task_game.game_id) end end - #end - break + break + end end + else + notice += "Pass was matched" + notice += ". " + change_user_task_and_user_game(@user_task.task_id, 0, @task_game.game_id) end if notice == "" - notice = "Code was not matched" + notice += "Code or pass was not matched" end respond_to do |format| @@ -74,6 +59,7 @@ def code_compare_params params.require(:code_compare).permit(:user_id, :code_id) end + protected def check_code_compare_creation unless params[:try_text] && params[:task] @@ -82,4 +68,42 @@ def check_code_compare_creation end end -end + def pass_matching(text) + if User.find(current_user.id).valid_password?(text) + return true + else + return false + end + end + + def change_user_task_and_user_game(task_id, points, game_id) + notice = "" + @user_task = UserTask.where(user_id: current_user.id, task_id: task_id).last + @user_hints = UserHint.where(user_id: current_user.id).count + @task = Task.find(task_id) + received_points = 0 + if (points < 0) + received_points = @task.points - @user_hints + else + received_points = points + end + @user_task.update(result: received_points) + puts 1 + @new_game_task = GameTask.where("`game_id` = ? AND `task_id` <> ?", game_id, task_id) + if @new_game_task.count > 0 + UserTask.create(user_id: current_user.id, task_id: @new_game_task.first.task_id) + notice += "New Task was started" + else + @user_tasks = UserTask.where(user_id: current_user.id) + points = 0 + @user_tasks.each do |user_task| + points += user_task.result + end + @user_game = UserGame.where(user_id: current_user.id, game_id: game_id).first + @user_game.update(result: points, state: -1) + notice += "Game was ended" + end + return notice + end + +end \ No newline at end of file diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index 48ad355..008c045 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -1,6 +1,7 @@ class GamesController < ApplicationController before_action :set_game, only: [:show, :edit, :update, :destroy, :start_game, :check_game_status, :finish_game] skip_before_filter :authorize_admin, only: [:index, :show] + before_filter :authenticate_user!, only: [:show] before_filter :check_game_status, only: [:show] # GET /games # GET /games.json diff --git a/app/controllers/hints_controller.rb b/app/controllers/hints_controller.rb index f5bf62e..03ffc4f 100644 --- a/app/controllers/hints_controller.rb +++ b/app/controllers/hints_controller.rb @@ -3,7 +3,7 @@ class HintsController < ApplicationController before_filter :check_task_create, only: [:create] before_filter :no_hints, except: [:new, :create] before_action :set_hint, only: [:show, :edit, :update, :destroy] - skip_before_filter :authorize_admin, only: [:show] + #skip_before_filter :authorize_admin, only: [:show] # GET /hints # GET /hints.json diff --git a/app/realtime/realtime_chat_controller.rb b/app/realtime/realtime_chat_controller.rb new file mode 100644 index 0000000..e69de29 diff --git a/app/views/games/index.html.slim b/app/views/games/index.html.slim index a7253b8..12f46b3 100644 --- a/app/views/games/index.html.slim +++ b/app/views/games/index.html.slim @@ -17,6 +17,12 @@ table class="table table-striped" td= game.start_date td = link_to t('.edit', :default => t("helpers.links.edit")), edit_game_path(game), :class => 'btn btn-mini' + + = link_to "New_task", new_task_path(game: game.id), :class => 'btn' + + = link_to "Start game", {controller: "games", action: "start_game", id: game.id}, :class => 'btn' + + = link_to "Finish_game", {controller: "games", action: "finish_game", id: game.id}, :class => 'btn' ' = link_to t('.destroy', :default => t("helpers.links.destroy")), game_path(game), :method => :delete, :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-mini btn-danger' diff --git a/app/views/games/show.html.slim b/app/views/games/show.html.slim index 3ee62a0..a4f5cbc 100644 --- a/app/views/games/show.html.slim +++ b/app/views/games/show.html.slim @@ -1,29 +1,29 @@ - model_class = Game - - div class="page-header" - h1=t '.title', :default => model_class.model_name.human.titleize - - p - strong= model_class.human_attribute_name(:title) + ':' - br +div class="page-header" + h1=t '.title', :default => model_class.model_name.human.titleize + p + strong= model_class.human_attribute_name(:title) + ':' + br = @game.title - -unless current_user? - p - strong= model_class.human_attribute_name(:start_date) + ':' - br +-unless user_signed_in? + p + strong= model_class.human_attribute_name(:start_date) + ':' + br = @game.start_date - p - strong= model_class.human_attribute_name(:start_date) + ':' - br - = @status + br + = @status .form-actions = link_to t('.back', :default => t("helpers.links.back")), games_path, :class => 'btn' - = link_to t('.back', :default => t("helpers.links.back")), new_hint_path(game: @game.id), :class => 'btn' + = link_to "New_task", new_task_path(game: @game.id), :class => 'btn' + + = link_to "Start game", {controller: "games", action: "start_game", id: @game.id}, :class => 'btn' + + = link_to "Finish_game", {controller: "games", action: "finish_game", id: @game.id}, :class => 'btn' = link_to t('.edit', :default => t("helpers.links.edit")), edit_game_path(@game), :class => 'btn' ' = link_to t('.destroy', :default => t("helpers.links.destroy")), game_path(@game), :method => "delete", :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-danger' - -else - -@task_codes_count.each do |num| - -render partial: code_compare_form \ No newline at end of file +-else + -@task_codes_count.each do |num| + -render partial: code_compare_form \ No newline at end of file diff --git a/app/views/hints/new.html.slim b/app/views/hints/new.html.slim index 2926f4d..fb0262d 100644 --- a/app/views/hints/new.html.slim +++ b/app/views/hints/new.html.slim @@ -1,5 +1,5 @@ -- model_class = Hint - div class="page-header" +-model_class = Hint +div class="page-header" -unless @cant_add -if notice h1 = notice diff --git a/app/views/home/index.html.slim b/app/views/home/index.html.slim index 7edbf87..a11c37d 100644 --- a/app/views/home/index.html.slim +++ b/app/views/home/index.html.slim @@ -10,10 +10,10 @@ th=t '.actions', :default => t("helpers.actions") tbody - @games.each do |game| - - if game.state == 0 - tr - td= game.title - td= game.start_date + tr + td= game.title + td= game.start_date + - if game.state == 0 td = link_to "Join", {controller: "user_games", action: "create", user_game: {game_id: game.id}}, :class => 'btn btn-mini' diff --git a/app/views/tasks/index.html.slim b/app/views/tasks/index.html.slim index 399f488..5f06089 100644 --- a/app/views/tasks/index.html.slim +++ b/app/views/tasks/index.html.slim @@ -18,8 +18,12 @@ table class="table table-striped" td=l task.created_at td = link_to t('.edit', :default => t("helpers.links.edit")), edit_task_path(task), :class => 'btn btn-mini' + + = link_to "Add code", new_code_path(task: task.id), :class => 'btn' + + = link_to "Add hint", new_hint_path(task: task.id), :class => 'btn' ' = link_to t('.destroy', :default => t("helpers.links.destroy")), task_path(task), :method => :delete, :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-mini btn-danger' -= link_to t('.new', :default => t("helpers.links.new")), new_task_path, :class => 'btn btn-primary' + = link_to t('.new', :default => t("helpers.links.new")), new_task_path, :class => 'btn btn-primary' diff --git a/app/views/tasks/show.html.slim b/app/views/tasks/show.html.slim index a8e56d0..77b8b9f 100644 --- a/app/views/tasks/show.html.slim +++ b/app/views/tasks/show.html.slim @@ -13,6 +13,10 @@ p .form-actions = link_to t('.back', :default => t("helpers.links.back")), tasks_path, :class => 'btn' + + = link_to "Add code", new_code_path(task: @task.id), :class => 'btn' + + = link_to "Add hint", new_hint_path(task: @task.id), :class => 'btn' ' = link_to t('.edit', :default => t("helpers.links.edit")), edit_task_path(@task), :class => 'btn' ' diff --git a/spec/controllers/code_compares_controller_spec.rb b/spec/controllers/code_compares_controller_spec.rb index 3ea9f88..b094112 100644 --- a/spec/controllers/code_compares_controller_spec.rb +++ b/spec/controllers/code_compares_controller_spec.rb @@ -69,6 +69,13 @@ expect(response).to redirect_to(game_path(id: @game.to_param)) end + it "send valid pass with complete of user game" do + @user_data = FactoryGirl.build(:user).attributes + post :create, {:try_text => @user_data["password"], task: @task.to_param}, valid_session + puts 1 + expect(UserGame.find(@user_game.to_param.to_i).state).to eq(-1) + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index eda9a2d..1a46010 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -49,6 +49,7 @@ config.order = "random" config.include Devise::TestHelpers, :type => :controller + config.include Devise::TestHelpers, :type => :view config.extend ControllerMacros, :type => :controller config.include(EmailSpec::Helpers) diff --git a/spec/views/hints/new.html.slim_spec.rb b/spec/views/hints/new.html.slim_spec.rb index 62dc348..0f7d6b2 100644 --- a/spec/views/hints/new.html.slim_spec.rb +++ b/spec/views/hints/new.html.slim_spec.rb @@ -6,6 +6,7 @@ end it "renders new hint form" do + puts 1 render # Run the generator again with the --webrat flag if you want to use webrat matchers From 988c1ba9dc0146d5d7313293c22620b9dac666c8 Mon Sep 17 00:00:00 2001 From: sorgal Date: Fri, 17 Jan 2014 18:00:35 +0400 Subject: [PATCH 04/15] correction of code compare cretion action and tests --- .idea/workspace.xml | 370 +++++++++++------- Gemfile | 2 +- Gemfile.lock | 2 +- app/controllers/code_compares_controller.rb | 110 ++---- app/models/game.rb | 68 ++++ app/models/task.rb | 6 + app/models/user.rb | 4 + app/models/user_game.rb | 5 + app/models/user_task.rb | 4 + ...ller.rb => realtime_message_controller.rb} | 0 db/schema.rb | 7 +- .../code_compares_controller_spec.rb | 18 +- spec/models/game_spec.rb | 58 ++- 13 files changed, 428 insertions(+), 226 deletions(-) rename app/realtime/{realtime_chat_controller.rb => realtime_message_controller.rb} (100%) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 533913e..9f40fdc 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -23,28 +23,46 @@ - - + + - + - - + + - + - + + + + + + + + + + - + + + + + + + + + + @@ -60,12 +78,6 @@ @@ -180,6 +198,42 @@ @@ -545,327 +631,311 @@ - - - - - - - - + - + - + - + - + - + - + - + - + - - - + - + - - - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + - + - + - + - - - + - + - - - + - + - + - + - - - + - + - - - + - + - + - + - - - + - + - - - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + diff --git a/Gemfile b/Gemfile index 8cb4751..7bc0582 100644 --- a/Gemfile +++ b/Gemfile @@ -68,4 +68,4 @@ gem 'selenium-webdriver' gem 'activeadmin', github: 'gregbell/active_admin' gem 'poltergeist' -gem 'faye-rails' +gem 'faye-rails', '~> 1.0.6' diff --git a/Gemfile.lock b/Gemfile.lock index 2b56367..5eb0095 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -288,7 +288,7 @@ DEPENDENCIES devise email_spec factory_girl_rails (~> 4.0) - faye-rails + faye-rails (~> 1.0.6) jbuilder (~> 1.2) jquery-rails mysql2 diff --git a/app/controllers/code_compares_controller.rb b/app/controllers/code_compares_controller.rb index 2cb365b..6c557e5 100644 --- a/app/controllers/code_compares_controller.rb +++ b/app/controllers/code_compares_controller.rb @@ -5,37 +5,14 @@ class CodeComparesController < ApplicationController before_filter :authenticate_user! def create - @user_task = UserTask.where(user_id: current_user.id).last - @task_game = GameTask.where(task_id: @user_task.task_id).first - notice = "" - unless pass_matching(params[:try_text]) - @task_codes = TaskCode.where(task_id: @user_task.task_id) - @task_codes.each do |task_code| - @code = Code.where(id: task_code.code_id, code_string: params[:try_text]) - if @code.count > 0 - new_code_compare = {user_id: current_user.id, code_id: @code.first.id} - @code_compare = CodeCompare.new(new_code_compare) - if @code_compare.save - notice += "Code was matched" - if (CodeCompare.where(user_id: current_user.id, code_id: task_code.code_id).count == - @task_codes.count) - notice += ". " + change_user_task_and_user_game(task_code.task_id, -1, @task_game.game_id) - end - end - break - end - end - else - notice += "Pass was matched" - notice += ". " + change_user_task_and_user_game(@user_task.task_id, 0, @task_game.game_id) - end + @task = current_user.tasks.last + @game = @task.game - if notice == "" - notice += "Code or pass was not matched" - end + notice = "" + notice = @game.process(current_user, @task, params[:try_text]) respond_to do |format| - format.html { redirect_to game_path(id: @task_game.game_id), notice: notice } + format.html { redirect_to game_path(id: @game.id), notice: notice } format.json { head :no_content } end end @@ -61,49 +38,48 @@ def code_compare_params protected - def check_code_compare_creation - unless params[:try_text] && params[:task] - @game_task = GameTask.where(task_id: params[:task].to_i).first - redirect_to game_path(id: @game_task.game_id) - end + + def check_code_compare_creation + unless params[:try_text] && params[:task] + @game = @task.game + redirect_to game_path(id: @game.game) end + end - def pass_matching(text) - if User.find(current_user.id).valid_password?(text) - return true - else - return false - end + def pass_matching(text) + if User.find(current_user.id).valid_password?(text) + return true + else + return false end + end - def change_user_task_and_user_game(task_id, points, game_id) - notice = "" - @user_task = UserTask.where(user_id: current_user.id, task_id: task_id).last - @user_hints = UserHint.where(user_id: current_user.id).count - @task = Task.find(task_id) - received_points = 0 - if (points < 0) - received_points = @task.points - @user_hints - else - received_points = points - end - @user_task.update(result: received_points) - puts 1 - @new_game_task = GameTask.where("`game_id` = ? AND `task_id` <> ?", game_id, task_id) - if @new_game_task.count > 0 - UserTask.create(user_id: current_user.id, task_id: @new_game_task.first.task_id) - notice += "New Task was started" - else - @user_tasks = UserTask.where(user_id: current_user.id) - points = 0 - @user_tasks.each do |user_task| - points += user_task.result - end - @user_game = UserGame.where(user_id: current_user.id, game_id: game_id).first - @user_game.update(result: points, state: -1) - notice += "Game was ended" - end - return notice + def next_task(points) + if has_more_tasks? + assign_next_task(points) + "Next task assigned" + else + end_game + "Game completed" end + end + + def has_more_tasks? + untaken_tasks.any? + end + + def assign_next_task(points) + current_user.user_tasks.last.update_attribute(:result, points) + current_user.user_tasks.create(task: untaken_tasks.first) + end + + def end_game + current_user.user_games.current.first.update_attribute(:state, UserGame::COMPLETED) + end + + def untaken_tasks + completed_ids = @game.tasks.for_user(current_user.id).pluck(:id) + @untaken_tasks ||= @game.tasks.where('`tasks`.`id` NOT IN (?)', completed_ids.join(',') ) + end end \ No newline at end of file diff --git a/app/models/game.rb b/app/models/game.rb index 07da62d..3cdf8ee 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -3,7 +3,75 @@ class Game < ActiveRecord::Base has_one :admin_game, dependent: :destroy has_many :user_games, dependent: :destroy has_many :game_tasks, dependent: :destroy + has_many :tasks, through: :game_tasks validate :title, :start_date, presence: true + def process(user, task, try_text) + @user = user + @task = task + if pass_matching(try_text) + notice = "Pass was matched. #{next_task(0)}" + else + if matched_code = @task.codes.where(code_string: try_text).first + new_code_compare = @user.code_compares.build(code: matched_code) + if new_code_compare.save + if all_codes_input? + notice = "Code was matched. #{next_task(@task.points - @user.hints.count)}" + end + end + end + end + + if notice == "" + notice += "Code or pass was not matched" + end + @notice = notice + end + + def all_codes_input? + if @user.code_compares.count == @task.codes.count + return true + else + return false + end + end + + def pass_matching(text) + if User.find(@user.id).valid_password?(text) + return true + else + return false + end + end + + def next_task(points) + if has_more_tasks? + assign_next_task(points) + "Next task assigned" + else + end_game + "Game completed" + end + end + + def has_more_tasks? + untaken_tasks.any? + end + + def assign_next_task(points) + puts @user.user_tasks + @user.user_tasks.last.update_attribute(:result, points) + @user.user_tasks.create(task: untaken_tasks.first) + end + + def end_game + @user.user_games.first.update_attribute(:state, UserGame::COMPLETED) + end + + def untaken_tasks + completed_ids = self.tasks.for_user(@user.id).pluck(:id) + @untaken_tasks ||= self.tasks.where('`tasks`.`id` NOT IN (?)', completed_ids.join(',') ) + end + end diff --git a/app/models/task.rb b/app/models/task.rb index 0d689f1..9995967 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -1,8 +1,14 @@ class Task < ActiveRecord::Base has_many :task_hints, dependent: :destroy + has_many :hints, through: :task_hints has_many :task_codes, dependent: :destroy + has_many :codes, through: :task_codes has_many :user_tasks, dependent: :destroy has_one :game_task, dependent: :destroy + has_one :game, through: :game_task validate :task_text, :points, presence: true + + scope :for_user, -> id {joins(:user_tasks).where(user_tasks: {user_id: id})} + end diff --git a/app/models/user.rb b/app/models/user.rb index 18b384d..e24c5d5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,10 +2,14 @@ class User < ActiveRecord::Base # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable has_many :user_games, dependent: :destroy + has_many :games, through: :user_games has_many :code_compares, dependent: :destroy has_many :code_compares, dependent: :destroy has_many :user_tasks, dependent: :destroy + has_many :tasks, through: :user_tasks has_many :user_hints, dependent: :destroy + has_many :hints, through: :user_hints + devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable end diff --git a/app/models/user_game.rb b/app/models/user_game.rb index 4ff8f59..d460448 100644 --- a/app/models/user_game.rb +++ b/app/models/user_game.rb @@ -2,5 +2,10 @@ class UserGame < ActiveRecord::Base belongs_to :user belongs_to :game + INTACT = 0 + CURRENT = 1 + COMPLETED = -1 + validate :user_id, :game_id, :result, presence: true + scope :current, -> { where(state: CURRENT) } end diff --git a/app/models/user_task.rb b/app/models/user_task.rb index 6d45865..add9f60 100644 --- a/app/models/user_task.rb +++ b/app/models/user_task.rb @@ -1,4 +1,8 @@ class UserTask < ActiveRecord::Base belongs_to :user belongs_to :task + + scope :completed, -> { where('state < 0') } + scope :incomplete, -> { where('state >= 0') } + scope :for_user, -> id {where(user_id: id)} end diff --git a/app/realtime/realtime_chat_controller.rb b/app/realtime/realtime_message_controller.rb similarity index 100% rename from app/realtime/realtime_chat_controller.rb rename to app/realtime/realtime_message_controller.rb diff --git a/db/schema.rb b/db/schema.rb index 84ffca4..c7069b1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140115190550) do +ActiveRecord::Schema.define(version: 20140117062532) do create_table "active_admin_comments", force: true do |t| t.string "namespace" @@ -88,6 +88,11 @@ t.integer "queue_number" end + create_table "messages", force: true do |t| + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "task_codes", force: true do |t| t.integer "task_id" t.integer "code_id" diff --git a/spec/controllers/code_compares_controller_spec.rb b/spec/controllers/code_compares_controller_spec.rb index b094112..89adaed 100644 --- a/spec/controllers/code_compares_controller_spec.rb +++ b/spec/controllers/code_compares_controller_spec.rb @@ -37,7 +37,7 @@ @user_task = FactoryGirl.create(:user_task, user_id: @user_id, task_id: @task.to_param) @task_code = FactoryGirl.create(:task_code, task_id: @task.to_param, code_id: @code.to_param) @task_game = FactoryGirl.create(:game_task, game_id: @game.to_param, task_id: @task.to_param) - @user_game = FactoryGirl.create(:user_game, user_id: @user_id, game_id: @game.to_param) + @user_game = FactoryGirl.create(:user_game, user_id: @user_id, game_id: @game.to_param, state: 1) end describe "POST create" do @@ -66,16 +66,26 @@ it "creates a new CodeCompare with code mismatched result" do post :create, {:try_text => @code.attributes["code_string"] + "sefbgdfgbnfg", task: @task.to_param}, valid_session - expect(response).to redirect_to(game_path(id: @game.to_param)) + expect(response).to redirect_to(game_path(@game)) end it "send valid pass with complete of user game" do @user_data = FactoryGirl.build(:user).attributes - post :create, {:try_text => @user_data["password"], task: @task.to_param}, valid_session - puts 1 + post :create, {:try_text => "1234567890", task: @task.to_param}, valid_session expect(UserGame.find(@user_game.to_param.to_i).state).to eq(-1) end + it "creates a new CodeCompare and change task" do + @new_task = FactoryGirl.create(:task, task_text: "sdgbhdrhndrbhzsgfr") + @new_code = FactoryGirl.create(:code, code_string: "dfbdfbndfgnhdfgdfh") + @new_user_task = FactoryGirl.build(:user_task, user_id: @user_id, task_id: @new_task.to_param) + @new_task_code = FactoryGirl.create(:task_code, task_id: @new_task.to_param, code_id: @new_code.to_param) + @new_task_game = FactoryGirl.create(:game_task, game_id: @game.to_param, task_id: @new_task.to_param) + post :create, {:try_text => "1234567890", task: @task.to_param}, valid_session + #puts UserTask.count + expect(UserTask.last.task_id).to eq(@new_task.to_param.to_i) + end + end end diff --git a/spec/models/game_spec.rb b/spec/models/game_spec.rb index c273778..f549d0a 100644 --- a/spec/models/game_spec.rb +++ b/spec/models/game_spec.rb @@ -1,9 +1,63 @@ require 'spec_helper' describe Game do + + before do + @user = FactoryGirl.create(:user) + @game = FactoryGirl.create(:game) + @code = FactoryGirl.create(:code) + @task = FactoryGirl.create(:task) + @user_task = FactoryGirl.create(:user_task, user_id: @user.to_param, task_id: @task.to_param) + @task_code = FactoryGirl.create(:task_code, task_id: @task.to_param, code_id: @code.to_param) + @task_game = FactoryGirl.create(:game_task, game_id: @game.to_param, task_id: @task.to_param) + @user_game = FactoryGirl.create(:user_game, user_id: @user.to_param, game_id: @game.to_param, state: 1) + end + it "games validation checking" do - game_1 = FactoryGirl.create(:game) + @game.should be_valid + end + + it "code compare processing" do + count = CodeCompare.count + expect(@game.process(@user, @task, @code.code_string) == "Code was matched. Game completed") + expect(CodeCompare.count).to eq(count + 1) + end + + it "code compare processing and change" do + @new_task = FactoryGirl.create(:task, task_text: "sdgbhdrhndrbhzsgfr") + @new_code = FactoryGirl.create(:code, code_string: "dfbdfbndfgnhdfgdfh") + @new_user_task = FactoryGirl.build(:user_task, user_id: @user.to_param, task_id: @new_task.to_param) + @new_task_code = FactoryGirl.create(:task_code, task_id: @new_task.to_param, code_id: @new_code.to_param) + @new_task_game = FactoryGirl.create(:game_task, game_id: @game.to_param, task_id: @new_task.to_param) + expect(@game.process(@user, @task, @code.attributes["code_string"]) == "Code was matched. Next task assigned") + expect(UserTask.last.task_id).to eq(@new_task.to_param.to_i) + end - game_1.should be_valid + it "creates a new CodeCompare and end the game of current user" do + expect(@game.process(@user, @task, @code.code_string) == "Code was matched. Game completed") + expect(UserGame.find(@user_game.to_param.to_i).state).to eq(-1) end + + it "creates a new CodeCompare with code mismatched result" do + expect(@game.process(@user, @task, @code.attributes["code_string"] + "egbrhnjtyhjnawfgbh") == "Code or pass was not matched") + expect(UserTask.last.task_id).to eq(@task.to_param.to_i) + expect(UserGame.find(@user_game.to_param.to_i).state).to eq(1) + end + + it "send valid pass with complete of user game" do + expect(@game.process(@user, @task, "1234567890") == "Code was matched. Game completed") + expect(UserGame.find(@user_game.to_param.to_i).state).to eq(-1) + end + + it "creates a new CodeCompare and change task" do + @new_task = FactoryGirl.create(:task, task_text: "sdgbhdrhndrbhzsgfr") + @new_code = FactoryGirl.create(:code, code_string: "dfbdfbndfgnhdfgdfh") + @new_user_task = FactoryGirl.build(:user_task, user_id: @user_id, task_id: @new_task.to_param) + @new_task_code = FactoryGirl.create(:task_code, task_id: @new_task.to_param, code_id: @new_code.to_param) + @new_task_game = FactoryGirl.create(:game_task, game_id: @game.to_param, task_id: @new_task.to_param) + expect(@game.process(@user, @task, "1234567890") == "Code was matched. Next task assigned") + #puts UserTask.count + expect(UserTask.last.task_id).to eq(@new_task.to_param.to_i) + end + end From a2d7a016a4e01ee0b7e46bbef648831cd6e459ff Mon Sep 17 00:00:00 2001 From: sorgal Date: Mon, 20 Jan 2014 10:11:59 +0400 Subject: [PATCH 05/15] changes berfore switch branch --- .idea/workspace.xml | 174 +++++++------------- app/controllers/code_compares_controller.rb | 38 ----- 2 files changed, 58 insertions(+), 154 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 9f40fdc..44869fb 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -23,46 +23,10 @@ - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -125,10 +89,10 @@ @@ -164,7 +128,6 @@ - @@ -350,6 +313,7 @@ + @@ -585,12 +549,12 @@ - + - + @@ -609,9 +573,9 @@ - - + + @@ -631,16 +595,6 @@ - - - - - - - - - - @@ -673,9 +627,7 @@ - - - + @@ -688,13 +640,6 @@ - - - - - - - @@ -727,9 +672,7 @@ - - - + @@ -749,9 +692,7 @@ - - - + @@ -766,9 +707,7 @@ - - - + @@ -781,13 +720,6 @@ - - - - - - - @@ -797,30 +729,22 @@ - - - + - - - + - - - + - - - + @@ -851,37 +775,27 @@ - - - + - - - + - - - + - - - + - - - + @@ -898,13 +812,6 @@ - - - - - - - @@ -933,9 +840,44 @@ + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/controllers/code_compares_controller.rb b/app/controllers/code_compares_controller.rb index 6c557e5..e2443b8 100644 --- a/app/controllers/code_compares_controller.rb +++ b/app/controllers/code_compares_controller.rb @@ -7,8 +7,6 @@ class CodeComparesController < ApplicationController def create @task = current_user.tasks.last @game = @task.game - - notice = "" notice = @game.process(current_user, @task, params[:try_text]) respond_to do |format| @@ -46,40 +44,4 @@ def check_code_compare_creation end end - def pass_matching(text) - if User.find(current_user.id).valid_password?(text) - return true - else - return false - end - end - - def next_task(points) - if has_more_tasks? - assign_next_task(points) - "Next task assigned" - else - end_game - "Game completed" - end - end - - def has_more_tasks? - untaken_tasks.any? - end - - def assign_next_task(points) - current_user.user_tasks.last.update_attribute(:result, points) - current_user.user_tasks.create(task: untaken_tasks.first) - end - - def end_game - current_user.user_games.current.first.update_attribute(:state, UserGame::COMPLETED) - end - - def untaken_tasks - completed_ids = @game.tasks.for_user(current_user.id).pluck(:id) - @untaken_tasks ||= @game.tasks.where('`tasks`.`id` NOT IN (?)', completed_ids.join(',') ) - end - end \ No newline at end of file From 8a646664c763124468165153cd008ad103c8e3cf Mon Sep 17 00:00:00 2001 From: sorgal Date: Mon, 20 Jan 2014 10:16:35 +0400 Subject: [PATCH 06/15] games changing before merge --- .idea/workspace.xml | 260 ++++++++------------------------------------ 1 file changed, 43 insertions(+), 217 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index bcc6b51..87df27b 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -22,53 +22,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -164,8 +118,8 @@ - + @@ -198,24 +152,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -483,14 +361,14 @@ - + - + @@ -498,12 +376,12 @@ - + - + @@ -522,9 +400,9 @@ - - + + @@ -544,46 +422,29 @@ - - - - - - - - - - + - - - + - - - + - - - + - - - + @@ -665,44 +526,32 @@ - - - + - - - + - - - + - - - + - - - + - - - + @@ -714,9 +563,7 @@ - - - + @@ -735,30 +582,22 @@ - - - + - - - + - - - + - - - + @@ -784,9 +623,7 @@ - - - + @@ -798,16 +635,12 @@ - - - + - - - + @@ -819,51 +652,37 @@ - - - + - - - + - - - + - - - + - - - + - - - + - - - + @@ -882,7 +701,7 @@ - + @@ -894,9 +713,16 @@ + + + + + + + - + From bd432efb7126f096d1c4cbcb191f644052a2bef8 Mon Sep 17 00:00:00 2001 From: sorgal Date: Mon, 20 Jan 2014 18:09:53 +0400 Subject: [PATCH 07/15] move business logic from controllers to models --- .idea/workspace.xml | 886 ++++++++++++++++++ app/controllers/games_controller.rb | 18 +- app/controllers/hints_controller.rb | 13 +- app/controllers/tasks_controller.rb | 17 +- app/models/admin_user.rb | 1 + app/models/game.rb | 21 +- app/models/hint.rb | 4 + app/models/task.rb | 9 + .../20140120081734_add_raised_to_hint.rb | 5 + ...in_id_from_admin_games_to_admin_user_id.rb | 5 + db/schema.rb | 5 +- .../admin_games_controller_spec.rb | 35 +- .../code_compares_controller_spec.rb | 101 +- spec/controllers/games_controller_spec.rb | 57 +- spec/controllers/hints_controller_spec.rb | 18 +- spec/controllers/tasks_controller_spec.rb | 29 +- spec/factories/hints.rb | 1 + spec/models/game_spec.rb | 116 ++- spec/models/task_spec.rb | 57 +- spec/spec_helper.rb | 9 +- spec/support/controller_macros.rb | 11 +- 21 files changed, 1218 insertions(+), 200 deletions(-) create mode 100644 .idea/workspace.xml create mode 100644 db/migrate/20140120081734_add_raised_to_hint.rb create mode 100644 db/migrate/20140120140458_rename_column_admin_id_from_admin_games_to_admin_user_id.rb diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..86f16ec --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,886 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1389592251443 + 1389592251443 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index 008c045..ca0ccd5 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -92,26 +92,12 @@ def game_params public #Заглушки для отложенных задач def start_game - @game.update(state: 1) - @user_games = UserGame.where(game_id: @game.id) - @game_tasks = GameTask.where(game_id: @game.id).first - @user_games.each do |user_game| - UserTask.create(user_id: user_game.id, task_id: @game_tasks.task_id, result: 0) - user_game.update(state: 1) - end + @game.start_game redirect_to games_path end def finish_game - @game.update(state: -1) - @user_games = UserGame.where(game_id: @game.id) - @user_games.each do |user_game| - points = 0 - UserTask.where(user_id: user_game.user_id).each do |user_task| - points += user_task.result - end - user_game.update(state: -1, result: points) - end + @game.finish_game redirect_to games_path end diff --git a/app/controllers/hints_controller.rb b/app/controllers/hints_controller.rb index 03ffc4f..f26b7e4 100644 --- a/app/controllers/hints_controller.rb +++ b/app/controllers/hints_controller.rb @@ -96,11 +96,14 @@ def check_task_create redirect_to games_path end task = params.require(:hint)[:task].to_i - @task_hints = TaskHint.where(task_id: task) - if @task_hints.count > 0 - @hint = Hint.find(@task_hints.first.hint_id) - if @hint.queue_number == params.require(:hint)[:queue_number].to_i - redirect_to new_hint_path, notice: "Parameter queue_number must be equal to " + (3 - params.require(:hint)[:queue_number].to_i).to_s + @task = Task.find(task) + if @task.hints.count > 0 + if (@task.hints.count == 1) + if @task.hints.first.queue_number == params.require(:hint)[:queue_number].to_i + redirect_to new_hint_path, notice: "Parameter queue_number must be equal to " + (3 - params.require(:hint)[:queue_number].to_i).to_s + end + elsif @task.hints.count >= 2 + redirect_to new_hint_path, notice: "Only two hints can be assigned with one task" end end end diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index b146676..d9ca32f 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -99,22 +99,7 @@ def no_tasks public #еще заглушка def raise_hint - TaskHint.where(task_id: @task.id).each do |task_hint| - @hint = Hint.find(task_hint.hint_id) - if UserHint.where(hint_id: @hint.id).count > 0 - next - else - @user_tasks = UserTask.where(task_id: task_hint.task_id) - @user_tasks.each do |user_task| - if user_task.result < 0 - UserHint.create(user_id: user_task.user_id, hint_id: @hint.id) - - end - end - break - end - end - + @task.raise_hint redirect_to tasks_path end diff --git a/app/models/admin_user.rb b/app/models/admin_user.rb index 72ae4c4..1f38b33 100644 --- a/app/models/admin_user.rb +++ b/app/models/admin_user.rb @@ -2,6 +2,7 @@ class AdminUser < ActiveRecord::Base # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable has_many :admin_games, dependent: :destroy + has_many :games, through: :admin_games devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable end diff --git a/app/models/game.rb b/app/models/game.rb index 3cdf8ee..2d8f9ff 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -60,18 +60,35 @@ def has_more_tasks? end def assign_next_task(points) - puts @user.user_tasks @user.user_tasks.last.update_attribute(:result, points) @user.user_tasks.create(task: untaken_tasks.first) end def end_game - @user.user_games.first.update_attribute(:state, UserGame::COMPLETED) + @user.user_games.where(state: UserGame::CURRENT).first.update_attribute(:state, UserGame::COMPLETED) end def untaken_tasks completed_ids = self.tasks.for_user(@user.id).pluck(:id) @untaken_tasks ||= self.tasks.where('`tasks`.`id` NOT IN (?)', completed_ids.join(',') ) end + + def start_game + self.update(state: UserGame::CURRENT) + #Для каждого user_game генерится user_task. Я не придумал как убрать цикл + self.user_games.update_all(state: UserGame::CURRENT) + self.user_games.each do |user_game| + UserTask.create(user_id: user_game.id, task_id: self.tasks.first.id, result: 0) + end + end + + def finish_game + self.update(state: UserGame::COMPLETED) + #Здесь я тоже не придумал как убрать цикл + self.user_games.each do |user_game| + points = UserTask.where(user_id: user_game.user_id).pluck(:result).inject{|sum, num| sum += num} + user_game.update(state: UserGame::COMPLETED, result: points) + end + end end diff --git a/app/models/hint.rb b/app/models/hint.rb index 2e4b3bf..8d99c80 100644 --- a/app/models/hint.rb +++ b/app/models/hint.rb @@ -2,4 +2,8 @@ class Hint < ActiveRecord::Base has_one :task_hint, dependent: :destroy has_many :user_hints, dependent: :destroy validate :hint_text, :queue_number, presence: true + + RAISED = 1 + NOT = 0 + end diff --git a/app/models/task.rb b/app/models/task.rb index 9995967..2ebb867 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -11,4 +11,13 @@ class Task < ActiveRecord::Base scope :for_user, -> id {joins(:user_tasks).where(user_tasks: {user_id: id})} + def raise_hint + @hint = self.hints.where(raised: Hint::NOT).first + if @hint + self.user_tasks.each do |user_task| + UserHint.create(user_id: user_task.user_id, hint_id: @hint.id) + end + end + end + end diff --git a/db/migrate/20140120081734_add_raised_to_hint.rb b/db/migrate/20140120081734_add_raised_to_hint.rb new file mode 100644 index 0000000..95ec5ea --- /dev/null +++ b/db/migrate/20140120081734_add_raised_to_hint.rb @@ -0,0 +1,5 @@ +class AddRaisedToHint < ActiveRecord::Migration + def change + add_column :hints, :raised, :integer, default: 0 + end +end diff --git a/db/migrate/20140120140458_rename_column_admin_id_from_admin_games_to_admin_user_id.rb b/db/migrate/20140120140458_rename_column_admin_id_from_admin_games_to_admin_user_id.rb new file mode 100644 index 0000000..bd1514c --- /dev/null +++ b/db/migrate/20140120140458_rename_column_admin_id_from_admin_games_to_admin_user_id.rb @@ -0,0 +1,5 @@ +class RenameColumnAdminIdFromAdminGamesToAdminUserId < ActiveRecord::Migration + def change + rename_column :admin_games, :admin_id, :admin_user_id + end +end diff --git a/db/schema.rb b/db/schema.rb index c7069b1..4ddfbdf 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140117062532) do +ActiveRecord::Schema.define(version: 20140120140458) do create_table "active_admin_comments", force: true do |t| t.string "namespace" @@ -29,7 +29,7 @@ add_index "active_admin_comments", ["resource_type", "resource_id"], name: "index_active_admin_comments_on_resource_type_and_resource_id", using: :btree create_table "admin_games", force: true do |t| - t.integer "admin_id" + t.integer "admin_user_id" t.integer "game_id" t.datetime "created_at" t.datetime "updated_at" @@ -86,6 +86,7 @@ t.datetime "created_at" t.datetime "updated_at" t.integer "queue_number" + t.integer "raised", default: 0 end create_table "messages", force: true do |t| diff --git a/spec/controllers/admin_games_controller_spec.rb b/spec/controllers/admin_games_controller_spec.rb index 0a4e9e0..c05c473 100644 --- a/spec/controllers/admin_games_controller_spec.rb +++ b/spec/controllers/admin_games_controller_spec.rb @@ -1,45 +1,22 @@ require 'spec_helper' -# This spec was generated by rspec-rails when you ran the scaffold generator. -# It demonstrates how one might use RSpec to specify the controller code that -# was generated by Rails when you ran the scaffold generator. -# -# It assumes that the implementation code is generated by the rails scaffold -# generator. If you are using any extension libraries to generate different -# controller code, this generated spec may or may not pass. -# -# It only uses APIs available in rails and/or rspec-rails. There are a number -# of tools you can use to make these specs even more expressive, but we're -# sticking to rails and rspec-rails APIs to keep things simple and stable. -# -# Compared to earlier versions of this generator, there is very limited use of -# stubs and message expectations in this spec. Stubs are only used when there -# is no simpler way to get a handle on the object needed for the example. -# Message expectations are only used when there is no simpler way to specify -# that an instance is receiving a specific message. describe AdminGamesController do login_admin - # This should return the minimal set of attributes required to create a valid - # AdminGame. As you add validations to AdminGame, be sure to - # adjust the attributes here as well. + let(:valid_attributes) { { "admin_id" => "1" } } - # This should return the minimal set of values that should be in the session - # in order to pass any filters (e.g. authentication) defined in - # AdminGamesController. Be sure to keep this updated too. let(:valid_session) { {} } - before do - @game = FactoryGirl.create(:game) - @admin_game = FactoryGirl.create(:admin_game, admin_id: @admin_id, game_id: @game.to_param) - end + let(:game) {create :game} + + let!(:admin_game) {create :admin_game, game: game, admin: @admin} describe "POST create" do describe "with valid params" do it "creates a new AdminGame" do expect { - post :create, {:admin_game => @admin_game.attributes}, valid_session + post :create, {admin_game: admin_game}, valid_session }.to change(AdminGame, :count).by(1) end end @@ -48,7 +25,7 @@ describe "DELETE destroy" do it "destroys the requested admin_game" do expect { - delete :destroy, {:id => @admin_game.to_param}, valid_session + delete :destroy, {id: admin_game.id}, valid_session }.to change(AdminGame, :count).by(-1) end end diff --git a/spec/controllers/code_compares_controller_spec.rb b/spec/controllers/code_compares_controller_spec.rb index 89adaed..7f5da3b 100644 --- a/spec/controllers/code_compares_controller_spec.rb +++ b/spec/controllers/code_compares_controller_spec.rb @@ -1,99 +1,72 @@ require 'spec_helper' -# This spec was generated by rspec-rails when you ran the scaffold generator. -# It demonstrates how one might use RSpec to specify the controller code that -# was generated by Rails when you ran the scaffold generator. -# -# It assumes that the implementation code is generated by the rails scaffold -# generator. If you are using any extension libraries to generate different -# controller code, this generated spec may or may not pass. -# -# It only uses APIs available in rails and/or rspec-rails. There are a number -# of tools you can use to make these specs even more expressive, but we're -# sticking to rails and rspec-rails APIs to keep things simple and stable. -# -# Compared to earlier versions of this generator, there is very limited use of -# stubs and message expectations in this spec. Stubs are only used when there -# is no simpler way to get a handle on the object needed for the example. -# Message expectations are only used when there is no simpler way to specify -# that an instance is receiving a specific message. - describe CodeComparesController do login_user - # This should return the minimal set of attributes required to create a valid - # CodeCompare. As you add validations to CodeCompare, be sure to - # adjust the attributes here as well. + let(:valid_attributes) { { "user_id" => "1" } } - # This should return the minimal set of values that should be in the session - # in order to pass any filters (e.g. authentication) defined in - # CodeComparesController. Be sure to keep this updated too. let(:valid_session) { {} } - before do - @code = FactoryGirl.create(:code) - @task = FactoryGirl.create(:task) - @game = FactoryGirl.create(:game) - @user_task = FactoryGirl.create(:user_task, user_id: @user_id, task_id: @task.to_param) - @task_code = FactoryGirl.create(:task_code, task_id: @task.to_param, code_id: @code.to_param) - @task_game = FactoryGirl.create(:game_task, game_id: @game.to_param, task_id: @task.to_param) - @user_game = FactoryGirl.create(:user_game, user_id: @user_id, game_id: @game.to_param, state: 1) - end + let(:task) { create :task } + let(:code) { create :code } + + let(:game) { create :game } + + let!(:task_code) { create :task_code, task: task, code: code } + let!(:user_task) { create :user_task, task: task, user: @user } + let!(:task_game) { create :game_task, game: game, task: task } + let!(:user_game) { create :user_game, game: game, user: @user, state: UserGame::CURRENT} describe "POST create" do describe "with valid params" do it "creates a new CodeCompare" do expect { - post :create, {:try_text => @code.attributes["code_string"], task: @task.to_param}, valid_session + post :create, {try_text: code.code_string, task: task}, valid_session }.to change(CodeCompare, :count).by(1) end - it "creates a new CodeCompare and change task" do - @new_task = FactoryGirl.create(:task, task_text: "sdgbhdrhndrbhzsgfr") - @new_code = FactoryGirl.create(:code, code_string: "dfbdfbndfgnhdfgdfh") - @new_user_task = FactoryGirl.build(:user_task, user_id: @user_id, task_id: @new_task.to_param) - @new_task_code = FactoryGirl.create(:task_code, task_id: @new_task.to_param, code_id: @new_code.to_param) - @new_task_game = FactoryGirl.create(:game_task, game_id: @game.to_param, task_id: @new_task.to_param) - post :create, {:try_text => @code.attributes["code_string"], task: @task.to_param}, valid_session - #puts UserTask.count - expect(UserTask.last.task_id).to eq(@new_task.to_param.to_i) - end - it "creates a new CodeCompare and end the game of current user" do - post :create, {:try_text => @code.attributes["code_string"], task: @task.to_param}, valid_session - expect(UserGame.find(@user_game.to_param.to_i).state).to eq(-1) + post :create, {try_text: code.code_string, task: task}, valid_session + expect(UserGame.find(user_game.id).state).to eq(-1) end it "creates a new CodeCompare with code mismatched result" do - post :create, {:try_text => @code.attributes["code_string"] + "sefbgdfgbnfg", task: @task.to_param}, valid_session - expect(response).to redirect_to(game_path(@game)) + post :create, {try_text: code.code_string + "sefbgdfgbnfg", task: task}, valid_session + expect(response).to redirect_to(game_path(game)) end - it "send valid pass with complete of user game" do - @user_data = FactoryGirl.build(:user).attributes - post :create, {:try_text => "1234567890", task: @task.to_param}, valid_session - expect(UserGame.find(@user_game.to_param.to_i).state).to eq(-1) + it "send valid pass with complete user game" do + post :create, {try_text: "1234567890", task: task}, valid_session + expect(UserGame.find(user_game.id).state).to eq(-1) end - it "creates a new CodeCompare and change task" do - @new_task = FactoryGirl.create(:task, task_text: "sdgbhdrhndrbhzsgfr") - @new_code = FactoryGirl.create(:code, code_string: "dfbdfbndfgnhdfgdfh") - @new_user_task = FactoryGirl.build(:user_task, user_id: @user_id, task_id: @new_task.to_param) - @new_task_code = FactoryGirl.create(:task_code, task_id: @new_task.to_param, code_id: @new_code.to_param) - @new_task_game = FactoryGirl.create(:game_task, game_id: @game.to_param, task_id: @new_task.to_param) - post :create, {:try_text => "1234567890", task: @task.to_param}, valid_session - #puts UserTask.count - expect(UserTask.last.task_id).to eq(@new_task.to_param.to_i) + describe "change_task" do + let(:new_task) { create :task, task_text: "sdgbhdrhndrbhzsgfr" } + let(:new_code) { create :code, code_string: "dfbdfbndfgnhdfgdfh" } + + let!(:new_task_code) { create :task_code, task: new_task, code: new_code } + let!(:new_user_task) { build :user_task, user: @user, task: new_task } + let!(:new_task_game) { create :game_task, game: game, task: new_task } + + it "creates a new CodeCompare and change task" do + post :create, {try_text: code.code_string, task: task}, valid_session + expect(UserTask.last.task_id).to eq(new_task.id) + end + + it "check pass matched and change task" do + post :create, {try_text: "1234567890", task: task}, valid_session + expect(UserTask.last.task_id).to eq(new_task.id) + end end end end describe "DELETE destroy" do + let!(:code_compare) {create :code_compare, code: code, user: @user} it "destroys the requested code_compare" do - @code_compare = FactoryGirl.create(:code_compare, user_id: @user_id, code_id: @code.to_param) expect { - delete :destroy, {:id => @code_compare.to_param}, valid_session + delete :destroy, {id: code_compare.id}, valid_session }.to change(CodeCompare, :count).by(-1) end end diff --git a/spec/controllers/games_controller_spec.rb b/spec/controllers/games_controller_spec.rb index e994b78..8166a9a 100644 --- a/spec/controllers/games_controller_spec.rb +++ b/spec/controllers/games_controller_spec.rb @@ -33,9 +33,7 @@ before do @game = FactoryGirl.create(:game) @task = FactoryGirl.create(:task) - #@user = FactoryGirl.create(:user) @game_task = FactoryGirl.create(:game_task, game_id: @game.to_param, task_id: @task.to_param) - @user_game = FactoryGirl.create(:user_game, user_id: @user_id, game_id: @game.to_param) @invalid_attributes = FactoryGirl.build(:game, title: "впирапеи", start_date: "01.01.2014 00:00:00".to_datetime).attributes end @@ -161,20 +159,53 @@ end end - describe "Start game" do - it "execute action " do - expect { + describe "Start and finish game" do + before(:each) do + @user_id = FactoryGirl.create(:user).to_param + @user_game = FactoryGirl.create(:user_game, user_id: @user_id, game_id: @game.to_param) + end + describe "Start game" do + it "execute action " do + expect { + get "start_game", {id: @game.to_param}, valid_session + }.to change(UserTask, :count).by(1) + end + + it "check game state equal to CURRENT" do get "start_game", {id: @game.to_param}, valid_session - }.to change(UserTask, :count).by(1) + expect(Game.find(@game.to_param.to_i).state).to equal(UserGame::CURRENT) + end + + it "check user_game state equal to CURRENT" do + get "start_game", {id: @game.to_param}, valid_session + expect(UserGame.where(user_id: @user_id, game_id:@game.to_param.to_i).first.state).to equal(UserGame::CURRENT) + end + end - end - describe "Finish game" do - it "execute action " do - get "finish_game", {id: @game.to_param}, valid_session - expect(Game.find(@game.to_param.to_i).state).to eq(-1) - expect(UserGame.find(@user_game.to_param.to_i).state).to eq(-1) + describe "Finish game" do + + before(:each) do + @task1 = FactoryGirl.create(:task) + @user_task = FactoryGirl.create(:user_task, task_id: @task.to_param, user_id: @user_id, result: 3) + @user_task1 = FactoryGirl.create(:user_task, task_id: @task1.to_param, user_id: @user_id, result: 3) + end + + it "check game status as equal to COMPLETED" do + get "finish_game", {id: @game.to_param}, valid_session + expect(Game.find(@game.to_param.to_i).state).to eq(UserGame::COMPLETED) + end + + it "check user_game status as equal to COMPLETED" do + get "finish_game", {id: @game.to_param}, valid_session + expect(UserGame.where(user_id: @user_id, game_id:@game.to_param.to_i).first.state).to eq(UserGame::COMPLETED) + end + + it "check user_game points count as equal to user_tasks results sum " do + get "finish_game", {id: @game.to_param}, valid_session + expect(UserGame.where(user_id: @user_id, game_id:@game.to_param.to_i).first.result).to eq(@user_task.result + @user_task1.result) + end + end end - end diff --git a/spec/controllers/hints_controller_spec.rb b/spec/controllers/hints_controller_spec.rb index 1f87ba1..26e9f25 100644 --- a/spec/controllers/hints_controller_spec.rb +++ b/spec/controllers/hints_controller_spec.rb @@ -31,12 +31,12 @@ let(:valid_session) { {} } before do - queue_number = rand(1...2) + @queue_number = rand(1...2) @task = FactoryGirl.create(:task) - @hint = FactoryGirl.create(:hint, queue_number: queue_number) - @new_hint = FactoryGirl.build(:hint, queue_number: 3 - queue_number) + @hint = FactoryGirl.create(:hint, queue_number: @queue_number) + @new_hint = FactoryGirl.build(:hint, queue_number: 3 - @queue_number) @task_hint = FactoryGirl.create(:task_hint, task_id: @task.to_param, hint_id: @hint.to_param) - @invalid_attributes = FactoryGirl.build(:hint, hint_text: "акпыук", queue_number: 3 - queue_number).attributes + @invalid_attributes = FactoryGirl.build(:hint, hint_text: "акпыук", queue_number: 3 - @queue_number).attributes end describe "GET index" do @@ -100,6 +100,16 @@ post :create, {:hint => @hint_post}, valid_session expect(response).to redirect_to(Hint.last) end + + it "creates a third Hint" do + @new_hint = FactoryGirl.create(:hint, queue_number: 3 - @queue_number) + @task_hint = FactoryGirl.create(:task_hint, task_id: @task.to_param, hint_id: @new_hint.to_param) + expect { + post :create, {:hint => @hint_post}, valid_session + }.to change(Hint, :count).by(0) + end + + end describe "with invalid params" do diff --git a/spec/controllers/tasks_controller_spec.rb b/spec/controllers/tasks_controller_spec.rb index e58fb9a..833eb60 100644 --- a/spec/controllers/tasks_controller_spec.rb +++ b/spec/controllers/tasks_controller_spec.rb @@ -176,14 +176,37 @@ }.to change(UserHint, :count).by(1) end - it "insert second hint" do - @user_hint = FactoryGirl.create(:user_hint, user_id: @user.to_param, hint_id: @hint.to_param) + it "raises first hint" do expect { get "raise_hint", {id: @task.to_param}, valid_session }.to change(UserHint, :count).by(1) - expect(UserHint.last.hint_id).to eq(@hint2.to_param.to_i) + expect(UserHint.last.hint_id).to eq(@hint.to_param.to_i) + expect(UserHint.count).to eq(1) end + describe "hints count > 1" do + before(:each) do + Hint.find(@hint.to_param.to_i).update_attribute(:raised, Hint::RAISED) + @user_hint = FactoryGirl.create(:user_hint, user_id: @user.to_param, hint_id: @hint.to_param) + end + it "raises second hint" do + expect { + get "raise_hint", {id: @task.to_param}, valid_session + }.to change(UserHint, :count).by(1) + expect(UserHint.last.hint_id).to eq(@hint2.to_param.to_i) + expect(UserHint.count).to eq(2) + end + + it "doesn't raise third hint" do + Hint.find(@hint2.to_param.to_i).update_attribute(:raised, Hint::RAISED) + @user_hint2 = FactoryGirl.create(:user_hint, user_id: @user.to_param, hint_id: @hint2.to_param) + expect { + get "raise_hint", {id: @task.to_param}, valid_session + }.to change(UserHint, :count).by(0) + expect(UserHint.last.hint_id).to eq(@hint2.to_param.to_i) + expect(UserHint.count).to eq(2) + end + end end end diff --git a/spec/factories/hints.rb b/spec/factories/hints.rb index 44f6def..7a23e6e 100644 --- a/spec/factories/hints.rb +++ b/spec/factories/hints.rb @@ -4,5 +4,6 @@ factory :hint do hint_text "hinttext: fgshgbsgbnhwaf wsegbsFsfhbnsfasfhnefargZGrzZGVFasdfgnadfgzsddhzdfzsef" queue_number 1 + raised 0 end end diff --git a/spec/models/game_spec.rb b/spec/models/game_spec.rb index f549d0a..7f39972 100644 --- a/spec/models/game_spec.rb +++ b/spec/models/game_spec.rb @@ -7,7 +7,7 @@ @game = FactoryGirl.create(:game) @code = FactoryGirl.create(:code) @task = FactoryGirl.create(:task) - @user_task = FactoryGirl.create(:user_task, user_id: @user.to_param, task_id: @task.to_param) + @user_task = FactoryGirl.create(:user_task, user_id: @user.to_param, task_id: @task.to_param, result: 3) @task_code = FactoryGirl.create(:task_code, task_id: @task.to_param, code_id: @code.to_param) @task_game = FactoryGirl.create(:game_task, game_id: @game.to_param, task_id: @task.to_param) @user_game = FactoryGirl.create(:user_game, user_id: @user.to_param, game_id: @game.to_param, state: 1) @@ -17,47 +17,89 @@ @game.should be_valid end - it "code compare processing" do - count = CodeCompare.count - expect(@game.process(@user, @task, @code.code_string) == "Code was matched. Game completed") - expect(CodeCompare.count).to eq(count + 1) - end + describe "create code compare testing" do - it "code compare processing and change" do - @new_task = FactoryGirl.create(:task, task_text: "sdgbhdrhndrbhzsgfr") - @new_code = FactoryGirl.create(:code, code_string: "dfbdfbndfgnhdfgdfh") - @new_user_task = FactoryGirl.build(:user_task, user_id: @user.to_param, task_id: @new_task.to_param) - @new_task_code = FactoryGirl.create(:task_code, task_id: @new_task.to_param, code_id: @new_code.to_param) - @new_task_game = FactoryGirl.create(:game_task, game_id: @game.to_param, task_id: @new_task.to_param) - expect(@game.process(@user, @task, @code.attributes["code_string"]) == "Code was matched. Next task assigned") - expect(UserTask.last.task_id).to eq(@new_task.to_param.to_i) - end + it "code compare processing" do + count = CodeCompare.count + expect(@game.process(@user, @task, @code.code_string) == "Code was matched. Game completed") + expect(CodeCompare.count).to eq(count + 1) + end - it "creates a new CodeCompare and end the game of current user" do - expect(@game.process(@user, @task, @code.code_string) == "Code was matched. Game completed") - expect(UserGame.find(@user_game.to_param.to_i).state).to eq(-1) - end + it "creates a new CodeCompare and end the game of current user" do + expect(@game.process(@user, @task, @code.code_string) == "Code was matched. Game completed") + expect(UserGame.find(@user_game.to_param.to_i).state).to eq(-1) + end - it "creates a new CodeCompare with code mismatched result" do - expect(@game.process(@user, @task, @code.attributes["code_string"] + "egbrhnjtyhjnawfgbh") == "Code or pass was not matched") - expect(UserTask.last.task_id).to eq(@task.to_param.to_i) - expect(UserGame.find(@user_game.to_param.to_i).state).to eq(1) - end + it "creates a new CodeCompare with code mismatched result" do + expect(@game.process(@user, @task, @code.attributes["code_string"] + "egbrhnjtyhjnawfgbh") == "Code or pass was not matched") + expect(UserTask.last.task_id).to eq(@task.to_param.to_i) + expect(UserGame.find(@user_game.to_param.to_i).state).to eq(1) + end - it "send valid pass with complete of user game" do - expect(@game.process(@user, @task, "1234567890") == "Code was matched. Game completed") - expect(UserGame.find(@user_game.to_param.to_i).state).to eq(-1) + it "send valid pass with complete of user game" do + expect(@game.process(@user, @task, "1234567890") == "Code was matched. Game completed") + expect(UserGame.find(@user_game.to_param.to_i).state).to eq(-1) + end + describe "assign next task" do + before(:each) do + @new_task = FactoryGirl.create(:task, task_text: "sdgbhdrhndrbhzsgfr") + @new_code = FactoryGirl.create(:code, code_string: "dfbdfbndfgnhdfgdfh") + @new_user_task = FactoryGirl.build(:user_task, user_id: @user.to_param, task_id: @new_task.to_param) + @new_task_code = FactoryGirl.create(:task_code, task_id: @new_task.to_param, code_id: @new_code.to_param) + @new_task_game = FactoryGirl.create(:game_task, game_id: @game.to_param, task_id: @new_task.to_param) + end + it "creates a new CodeCompare and change task" do + expect(@game.process(@user, @task, "1234567890") == "Code was matched. Next task assigned") + expect(UserTask.last.task_id).to eq(@new_task.to_param.to_i) + end + it "code compare processing and change" do + expect(@game.process(@user, @task, @code.attributes["code_string"]) == "Code was matched. Next task assigned") + expect(UserTask.last.task_id).to eq(@new_task.to_param.to_i) + end + end end + describe "Start and finish game" do + describe "Start game" do + it "execute action " do + expect { + @game.start_game + }.to change(UserTask, :count).by(1) + end - it "creates a new CodeCompare and change task" do - @new_task = FactoryGirl.create(:task, task_text: "sdgbhdrhndrbhzsgfr") - @new_code = FactoryGirl.create(:code, code_string: "dfbdfbndfgnhdfgdfh") - @new_user_task = FactoryGirl.build(:user_task, user_id: @user_id, task_id: @new_task.to_param) - @new_task_code = FactoryGirl.create(:task_code, task_id: @new_task.to_param, code_id: @new_code.to_param) - @new_task_game = FactoryGirl.create(:game_task, game_id: @game.to_param, task_id: @new_task.to_param) - expect(@game.process(@user, @task, "1234567890") == "Code was matched. Next task assigned") - #puts UserTask.count - expect(UserTask.last.task_id).to eq(@new_task.to_param.to_i) - end + it "check game state equal to CURRENT" do + @game.start_game + expect(Game.find(@game.to_param.to_i).state).to equal(UserGame::CURRENT) + end + + it "check user_game state equal to CURRENT" do + @game.start_game + expect(UserGame.where(user_id: @user.to_param, game_id:@game.to_param.to_i).first.state).to equal(UserGame::CURRENT) + end + + end + describe "Finish game" do + + before(:each) do + @task1 = FactoryGirl.create(:task) + @user_task1 = FactoryGirl.create(:user_task, task_id: @task1.to_param, user_id: @user.to_param, result: 3) + end + + it "check game status as equal to COMPLETED" do + @game.finish_game + expect(Game.find(@game.to_param.to_i).state).to eq(UserGame::COMPLETED) + end + + it "check user_game status as equal to COMPLETED" do + @game.finish_game + expect(UserGame.where(user_id: @user.to_param, game_id:@game.to_param.to_i).first.state).to eq(UserGame::COMPLETED) + end + + it "check user_game points count as equal to user_tasks results sum " do + @game.finish_game + expect(UserGame.where(user_id: @user.to_param, game_id:@game.to_param.to_i).first.result).to eq(@user_task.result + @user_task1.result) + end + + end + end end diff --git a/spec/models/task_spec.rb b/spec/models/task_spec.rb index 296efd2..deb6f3d 100644 --- a/spec/models/task_spec.rb +++ b/spec/models/task_spec.rb @@ -1,5 +1,60 @@ require 'spec_helper' describe Task do - pending "add some examples to (or delete) #{__FILE__}" + + before do + @game = FactoryGirl.create(:game) + @task = FactoryGirl.create(:task) + @hint = FactoryGirl.create(:hint) + @user = FactoryGirl.create(:user) + @user_task = FactoryGirl.create(:user_task, user_id: @user.to_param, task_id: @task.to_param) + @task_hint = FactoryGirl.create(:task_hint, task_id: @task.to_param, hint_id: @hint.to_param) + @hint2 = FactoryGirl.create(:hint, hint_text: " wvgrawrfswf", queue_number: 2) + @task_hint2 = FactoryGirl.create(:task_hint, task_id: @task.to_param, hint_id: @hint2.to_param) + end + + it "games validation checking" do + @task.should be_valid + end + + describe "raise hint" do + it "execute action " do + expect { + @task.raise_hint + }.to change(UserHint, :count).by(1) + end + + it "raises first hint" do + expect { + @task.raise_hint + }.to change(UserHint, :count).by(1) + expect(UserHint.last.hint_id).to eq(@hint.to_param.to_i) + expect(UserHint.count).to eq(1) + end + + describe "hints count > 1" do + before(:each) do + Hint.find(@hint.to_param.to_i).update_attribute(:raised, Hint::RAISED) + @user_hint = FactoryGirl.create(:user_hint, user_id: @user.to_param, hint_id: @hint.to_param) + end + it "raises second hint" do + expect { + @task.raise_hint + }.to change(UserHint, :count).by(1) + expect(UserHint.last.hint_id).to eq(@hint2.to_param.to_i) + expect(UserHint.count).to eq(2) + end + + it "doesn't raise third hint" do + Hint.find(@hint2.to_param.to_i).update_attribute(:raised, Hint::RAISED) + @user_hint2 = FactoryGirl.create(:user_hint, user_id: @user.to_param, hint_id: @hint2.to_param) + expect { + @task.raise_hint + }.to change(UserHint, :count).by(0) + expect(UserHint.last.hint_id).to eq(@hint2.to_param.to_i) + expect(UserHint.count).to eq(2) + end + end + end + end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1a46010..a925dd9 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -48,10 +48,13 @@ # --seed 1234 config.order = "random" - config.include Devise::TestHelpers, :type => :controller - config.include Devise::TestHelpers, :type => :view - config.extend ControllerMacros, :type => :controller + config.include Devise::TestHelpers, type: :controller + config.include Devise::TestHelpers, type: :view + config.extend ControllerMacros, type: :controller config.include(EmailSpec::Helpers) config.include(EmailSpec::Matchers) + + config.include FactoryGirl::Syntax::Methods + end diff --git a/spec/support/controller_macros.rb b/spec/support/controller_macros.rb index 8ee25ca..23f1171 100644 --- a/spec/support/controller_macros.rb +++ b/spec/support/controller_macros.rb @@ -2,9 +2,12 @@ module ControllerMacros def login_admin before(:each) do @request.env["devise.mapping"] = Devise.mappings[:admin] - @admin = FactoryGirl.create(:admin_user, email: "admin#{rand(1000)}@example.com}") # Using factory girl as an example + @admin = FactoryGirl.create(:admin_user, email: "admin#{rand(1000)}@example.com") sign_in @admin - @admin_id = @admin.to_param + @admin_id = @admin.id + end + after(:each) do + AdminUser.find(@admin.id).destroy end end @@ -12,12 +15,10 @@ def login_user before(:each) do @request.env["devise.mapping"] = Devise.mappings[:user] @user = FactoryGirl.create(:user) - #@user.confirm! sign_in @user - @user_id = @user.to_param end after(:each) do - User.find(@user_id).destroy + User.find(@user).destroy end end end \ No newline at end of file From d73038d2577ba22dd6236003694fdbb2fc2969c8 Mon Sep 17 00:00:00 2001 From: sorgal Date: Tue, 21 Jan 2014 17:49:56 +0400 Subject: [PATCH 08/15] specs cleaning --- .idea/workspace.xml | 459 ++++++++---------- app/controllers/admin_games_controller.rb | 2 +- app/controllers/games_controller.rb | 9 +- app/controllers/hints_controller.rb | 6 +- app/models/game_task.rb | 2 +- .../admin_games_controller_spec.rb | 4 +- spec/controllers/codes_controller_spec.rb | 104 ++-- .../controllers/game_tasks_controller_spec.rb | 39 +- spec/controllers/games_controller_spec.rb | 142 +++--- spec/controllers/hints_controller_spec.rb | 140 ++---- .../controllers/task_codes_controller_spec.rb | 38 +- .../controllers/task_hints_controller_spec.rb | 40 +- spec/controllers/tasks_controller_spec.rb | 128 ++--- .../controllers/user_games_controller_spec.rb | 91 ++-- .../controllers/user_hints_controller_spec.rb | 36 +- .../controllers/user_tasks_controller_spec.rb | 37 +- spec/factories/admin_games.rb | 2 +- spec/helpers/admin_games_helper_spec.rb | 15 - spec/helpers/code_compares_helper_spec.rb | 15 - spec/helpers/codes_helper_spec.rb | 15 - spec/helpers/game_tasks_helper_spec.rb | 15 - spec/helpers/games_helper_spec.rb | 15 - spec/helpers/hints_helper_spec.rb | 15 - spec/helpers/home_helper_spec.rb | 15 - spec/helpers/task_codes_helper_spec.rb | 15 - spec/helpers/task_hints_helper_spec.rb | 15 - spec/helpers/tasks_helper_spec.rb | 15 - spec/helpers/user_games_helper_spec.rb | 15 - spec/helpers/user_hints_helper_spec.rb | 15 - spec/helpers/user_tasks_helper_spec.rb | 15 - spec/models/admin_game_spec.rb | 5 - spec/models/admin_user_spec.rb | 5 - spec/models/code_compare_spec.rb | 5 - spec/models/code_spec.rb | 4 +- spec/models/game_spec.rb | 96 ++-- spec/models/game_task_spec.rb | 5 - spec/models/hint_spec.rb | 6 +- spec/models/task_code_spec.rb | 5 - spec/models/task_hint_spec.rb | 5 - spec/models/task_spec.rb | 59 ++- spec/models/user_game_spec.rb | 5 - spec/models/user_hint_spec.rb | 5 - spec/models/user_task_spec.rb | 5 - spec/routing/admin_games_routing_spec.rb | 9 +- spec/routing/code_compares_routing_spec.rb | 9 +- spec/routing/codes_routing_spec.rb | 9 +- spec/routing/game_tasks_routing_spec.rb | 9 +- spec/routing/games_routing_spec.rb | 11 +- spec/routing/hints_routing_spec.rb | 15 +- spec/routing/task_codes_routing_spec.rb | 9 +- spec/routing/task_hints_routing_spec.rb | 9 +- spec/routing/tasks_routing_spec.rb | 11 +- spec/routing/user_games_routing_spec.rb | 15 +- spec/routing/user_hints_routing_spec.rb | 9 +- spec/routing/user_tasks_routing_spec.rb | 9 +- spec/support/controller_macros.rb | 10 +- spec/views/codes/edit.html.slim_spec.rb | 15 - spec/views/codes/index.html.slim_spec.rb | 18 - spec/views/codes/new.html.slim_spec.rb | 15 - spec/views/codes/show.html.slim_spec.rb | 12 - spec/views/games/edit.html.slim_spec.rb | 15 - spec/views/games/index.html.slim_spec.rb | 17 - spec/views/games/new.html.slim_spec.rb | 15 - spec/views/games/show.html.slim_spec.rb | 12 - spec/views/hints/edit.html.slim_spec.rb | 15 - spec/views/hints/index.html.slim_spec.rb | 17 - spec/views/hints/new.html.slim_spec.rb | 16 - spec/views/hints/show.html.slim_spec.rb | 12 - spec/views/home/index.html.slim_spec.rb | 5 - spec/views/tasks/edit.html.slim_spec.rb | 15 - spec/views/tasks/index.html.slim_spec.rb | 17 - spec/views/tasks/new.html.slim_spec.rb | 15 - spec/views/tasks/show.html.slim_spec.rb | 12 - spec/views/user_games/edit.html.slim_spec.rb | 20 - spec/views/user_games/index.html.slim_spec.rb | 18 - spec/views/user_games/new.html.slim_spec.rb | 20 - spec/views/user_games/show.html.slim_spec.rb | 17 - 77 files changed, 679 insertions(+), 1457 deletions(-) delete mode 100644 spec/helpers/admin_games_helper_spec.rb delete mode 100644 spec/helpers/code_compares_helper_spec.rb delete mode 100644 spec/helpers/codes_helper_spec.rb delete mode 100644 spec/helpers/game_tasks_helper_spec.rb delete mode 100644 spec/helpers/games_helper_spec.rb delete mode 100644 spec/helpers/hints_helper_spec.rb delete mode 100644 spec/helpers/home_helper_spec.rb delete mode 100644 spec/helpers/task_codes_helper_spec.rb delete mode 100644 spec/helpers/task_hints_helper_spec.rb delete mode 100644 spec/helpers/tasks_helper_spec.rb delete mode 100644 spec/helpers/user_games_helper_spec.rb delete mode 100644 spec/helpers/user_hints_helper_spec.rb delete mode 100644 spec/helpers/user_tasks_helper_spec.rb delete mode 100644 spec/models/admin_game_spec.rb delete mode 100644 spec/models/admin_user_spec.rb delete mode 100644 spec/models/code_compare_spec.rb delete mode 100644 spec/models/game_task_spec.rb delete mode 100644 spec/models/task_code_spec.rb delete mode 100644 spec/models/task_hint_spec.rb delete mode 100644 spec/models/user_game_spec.rb delete mode 100644 spec/models/user_hint_spec.rb delete mode 100644 spec/models/user_task_spec.rb delete mode 100644 spec/views/codes/edit.html.slim_spec.rb delete mode 100644 spec/views/codes/index.html.slim_spec.rb delete mode 100644 spec/views/codes/new.html.slim_spec.rb delete mode 100644 spec/views/codes/show.html.slim_spec.rb delete mode 100644 spec/views/games/edit.html.slim_spec.rb delete mode 100644 spec/views/games/index.html.slim_spec.rb delete mode 100644 spec/views/games/new.html.slim_spec.rb delete mode 100644 spec/views/games/show.html.slim_spec.rb delete mode 100644 spec/views/hints/edit.html.slim_spec.rb delete mode 100644 spec/views/hints/index.html.slim_spec.rb delete mode 100644 spec/views/hints/new.html.slim_spec.rb delete mode 100644 spec/views/hints/show.html.slim_spec.rb delete mode 100644 spec/views/home/index.html.slim_spec.rb delete mode 100644 spec/views/tasks/edit.html.slim_spec.rb delete mode 100644 spec/views/tasks/index.html.slim_spec.rb delete mode 100644 spec/views/tasks/new.html.slim_spec.rb delete mode 100644 spec/views/tasks/show.html.slim_spec.rb delete mode 100644 spec/views/user_games/edit.html.slim_spec.rb delete mode 100644 spec/views/user_games/index.html.slim_spec.rb delete mode 100644 spec/views/user_games/new.html.slim_spec.rb delete mode 100644 spec/views/user_games/show.html.slim_spec.rb diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 86f16ec..32beae5 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -23,46 +23,37 @@ - - + + - + - - + + - + - - + + - + - - - - - - - - - - + - + @@ -78,31 +69,6 @@ @@ -164,6 +155,8 @@ + + @@ -214,88 +207,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -544,12 +449,12 @@ - + - + @@ -568,9 +473,9 @@ - - + + @@ -590,293 +495,353 @@ - + - + - + - + - + - + - + - + + + - + - + - + - + + + - + - + - + - + - + - + - + - + + + - + - + + + - + - + + + - + - + + + - + - + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + - + - + + + - + - + + + - + - + + + - + - + - + - + - + - + + + - + - + - + - + + + - + - + + + - + - + + + - + - + + + - + - + - + - + + + - + - + + + - + - + + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + diff --git a/app/controllers/admin_games_controller.rb b/app/controllers/admin_games_controller.rb index 5d6c71b..b4ffc2f 100644 --- a/app/controllers/admin_games_controller.rb +++ b/app/controllers/admin_games_controller.rb @@ -35,6 +35,6 @@ def set_admin_game # Never trust parameters from the scary internet, only allow the white list through. def admin_game_params - params.require(:admin_game).permit(:admin_id, :game_id) + params.require(:admin_game).permit(:admin_user_id, :game_id) end end diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index ca0ccd5..c52ad6d 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -17,9 +17,8 @@ def show @status = "Wait" elsif @game.state > 0 @status = "Started" - @user_task = UserTask.where(user_id: current_user.id, game_id: @game.id).last - @task = Task.find(@user_task.task_id).first - @task_codes_count = TaskCode.where(task_id: @user_task.task_id).count + @task = current_user.tasks.last + @task_codes_count = current_user.tasks.codes.where(task_id: @user_task.task_id).count elsif @game.state < 0 @status = "Finished" end @@ -43,7 +42,7 @@ def create respond_to do |format| if @game.save - if AdminGame.create(admin_id: current_admin_user.id, game_id: @game.id) + if AdminGame.create(admin_user_id: current_admin_user.id, game_id: @game.id) format.html { redirect_to @game, notice: 'Game was successfully created.' } format.json { render action: 'show', status: :created, location: @game } end @@ -104,7 +103,7 @@ def finish_game protected def check_game_status - if user_signed_in? && @game.status <= 0 + if user_signed_in? && @game.state <= 0 redirect_to root_path end end diff --git a/app/controllers/hints_controller.rb b/app/controllers/hints_controller.rb index f26b7e4..aabc419 100644 --- a/app/controllers/hints_controller.rb +++ b/app/controllers/hints_controller.rb @@ -3,7 +3,6 @@ class HintsController < ApplicationController before_filter :check_task_create, only: [:create] before_filter :no_hints, except: [:new, :create] before_action :set_hint, only: [:show, :edit, :update, :destroy] - #skip_before_filter :authorize_admin, only: [:show] # GET /hints # GET /hints.json @@ -89,11 +88,12 @@ def check_task unless params.require(:task) redirect_to games_path end + end def check_task_create unless params.require(:hint)[:task] - redirect_to games_path + redirect_to tasks_path end task = params.require(:hint)[:task].to_i @task = Task.find(task) @@ -103,7 +103,7 @@ def check_task_create redirect_to new_hint_path, notice: "Parameter queue_number must be equal to " + (3 - params.require(:hint)[:queue_number].to_i).to_s end elsif @task.hints.count >= 2 - redirect_to new_hint_path, notice: "Only two hints can be assigned with one task" + redirect_to tasks_path, notice: "Only two hints can be assigned with one task" end end end diff --git a/app/models/game_task.rb b/app/models/game_task.rb index 9c03994..e42de19 100644 --- a/app/models/game_task.rb +++ b/app/models/game_task.rb @@ -1,4 +1,4 @@ class GameTask < ActiveRecord::Base belongs_to :game belongs_to :task -end +end \ No newline at end of file diff --git a/spec/controllers/admin_games_controller_spec.rb b/spec/controllers/admin_games_controller_spec.rb index c05c473..fcc0fe6 100644 --- a/spec/controllers/admin_games_controller_spec.rb +++ b/spec/controllers/admin_games_controller_spec.rb @@ -10,13 +10,13 @@ let(:game) {create :game} - let!(:admin_game) {create :admin_game, game: game, admin: @admin} + let!(:admin_game) {create :admin_game, game: game, admin_user: @admin} describe "POST create" do describe "with valid params" do it "creates a new AdminGame" do expect { - post :create, {admin_game: admin_game}, valid_session + post :create, {admin_game: admin_game.attributes}, valid_session }.to change(AdminGame, :count).by(1) end end diff --git a/spec/controllers/codes_controller_spec.rb b/spec/controllers/codes_controller_spec.rb index 8321045..7b3bbf1 100644 --- a/spec/controllers/codes_controller_spec.rb +++ b/spec/controllers/codes_controller_spec.rb @@ -1,106 +1,82 @@ require 'spec_helper' -# This spec was generated by rspec-rails when you ran the scaffold generator. -# It demonstrates how one might use RSpec to specify the controller code that -# was generated by Rails when you ran the scaffold generator. -# -# It assumes that the implementation code is generated by the rails scaffold -# generator. If you are using any extension libraries to generate different -# controller code, this generated spec may or may not pass. -# -# It only uses APIs available in rails and/or rspec-rails. There are a number -# of tools you can use to make these specs even more expressive, but we're -# sticking to rails and rspec-rails APIs to keep things simple and stable. -# -# Compared to earlier versions of this generator, there is very limited use of -# stubs and message expectations in this spec. Stubs are only used when there -# is no simpler way to get a handle on the object needed for the example. -# Message expectations are only used when there is no simpler way to specify -# that an instance is receiving a specific message. - describe CodesController do login_admin - # This should return the minimal set of attributes required to create a valid - # Code. As you add validations to Code, be sure to - # adjust the attributes here as well. - let(:valid_attributes) { { } } - - # This should return the minimal set of values that should be in the session - # in order to pass any filters (e.g. authentication) defined in - # CodesController. Be sure to keep this updated too. + let(:valid_session) { {} } - before do - @task = FactoryGirl.create(:task) - @code = FactoryGirl.create(:code) - @invalid_attributes = FactoryGirl.build(:code, code_string: "ывампывипа").attributes - end + + let(:task) {create :task} + + let!(:code) {create :code} + + let(:invalid_attributes) {build :code, code_string: "ывампывипа"} + + let!(:valid_attributes) {build :code, code_string: "eghrfhfhfh"} describe "GET index" do it "assigns all codes as @codes" do get :index, {}, valid_session - expect(assigns(:codes)).to eq([@code]) + expect(assigns(:codes)).to eq([code]) end end describe "GET show" do it "assigns the requested code as @code" do - get :show, {:id => @code.to_param}, valid_session - expect(assigns(:code)).to eq(@code) + get :show, {id: code.id}, valid_session + expect(assigns(:code)).to eq(code) end end describe "GET new" do it "assigns a new code as @code" do - get :new, {task: @task.to_param}, valid_session + get :new, {task: task.id}, valid_session expect(assigns(:code)).to be_a_new(Code) end end describe "GET edit" do it "assigns the requested code as @code" do - get :edit, {:id => @code.to_param}, valid_session - expect(assigns(:code)).to eq(@code) + get :edit, {id: code.id}, valid_session + expect(assigns(:code)).to eq(code) end end describe "POST create" do before(:each) do - @code_post = @code.attributes - @code_post[:task] = @task.to_param - @code_post_invalid = @invalid_attributes - @code_post_invalid[:task] = @task.to_param + @code_post = code.attributes + @code_post[:task] = task.id + @code_post_invalid = invalid_attributes.attributes + @code_post_invalid[:task] = task.id end describe "with valid params" do it "creates a new Code" do expect { - post :create, {:code => @code_post}, valid_session + post :create, {code: @code_post}, valid_session }.to change(Code, :count).by(1) end it "assigns a newly created code as @code" do - post :create, {:code => @code_post}, valid_session + post :create, {code: @code_post}, valid_session expect(assigns(:code)).to be_a(Code) expect(assigns(:code)).to be_persisted end it "redirects to the created code" do - post :create, {:code => @code_post}, valid_session + post :create, {code: @code_post}, valid_session expect(response).to redirect_to(Code.last) end end describe "with invalid params" do it "assigns a newly created but unsaved code as @code" do - # Trigger the behavior that occurs when invalid params are submitted Code.any_instance.stub(:save).and_return(false) - post :create, {:code => @code_post_invalid}, valid_session + post :create, {code: @code_post_invalid}, valid_session expect(assigns(:code)).to be_a_new(Code) end it "re-renders the 'new' template" do - # Trigger the behavior that occurs when invalid params are submitted Code.any_instance.stub(:save).and_return(false) - post :create, {:code => @code_post_invalid}, valid_session + post :create, {code: @code_post_invalid}, valid_session expect(response).to render_template("new") end end @@ -108,40 +84,36 @@ describe "PUT update" do describe "with valid params" do - before (:each) do - @valid_attributes = FactoryGirl.build(:code, code_string: "dzfvbzjfgnarmkgnhbmargtgbhsergnerf;").attributes - end + #before (:each) do + + #end it "updates the requested code" do - # Assuming there are no other codes in the database, this - # specifies that the Code created on the previous line - # receives the :update_attributes message with whatever params are - # submitted in the request. - expect_any_instance_of(Code).to receive(:update).with({ "code_string" => "sdfgsdgfdf" }) - put :update, {:id => @code.to_param, :code => { "code_string" => "sdfgsdgfdf" }}, valid_session + expect_any_instance_of(Code).to receive(:update).with({ "code_string" => valid_attributes.code_string }) + put :update, {id: code.id, code: { "code_string" => valid_attributes.code_string }}, valid_session end it "assigns the requested code as @code" do - put :update, {:id => @code.to_param, :code => @valid_attributes}, valid_session - expect(assigns(:code)).to eq(@code) + put :update, {id: code.id, code: valid_attributes.attributes}, valid_session + expect(assigns(:code)).to eq(code) end it "redirects to the code" do - put :update, {:id => @code.to_param, :code => @valid_attributes}, valid_session - expect(response).to redirect_to(@code) + put :update, {id: code.id, code: valid_attributes.attributes}, valid_session + expect(response).to redirect_to(code) end end describe "with invalid params" do it "assigns the code as @code" do Code.any_instance.stub(:save).and_return(false) - put :update, {:id => @code.to_param, :code => @invalid_attributes}, valid_session - expect(assigns(:code)).to eq(@code) + put :update, {id: code.id, code: invalid_attributes.attributes}, valid_session + expect(assigns(:code)).to eq(code) end it "re-renders the 'edit' template" do Code.any_instance.stub(:save).and_return(false) - put :update, {:id => @code.to_param, :code => @invalid_attributes}, valid_session + put :update, {id: code.id, code: invalid_attributes.attributes}, valid_session expect(response).to render_template("edit") end end @@ -150,12 +122,12 @@ describe "DELETE destroy" do it "destroys the requested code" do expect { - delete :destroy, {:id => @code.to_param}, valid_session + delete :destroy, {id: code.id}, valid_session }.to change(Code, :count).by(-1) end it "redirects to the codes list" do - delete :destroy, {:id => @code.to_param}, valid_session + delete :destroy, {id: code.id}, valid_session expect(response).to redirect_to(codes_url) end end diff --git a/spec/controllers/game_tasks_controller_spec.rb b/spec/controllers/game_tasks_controller_spec.rb index 3f0c5c7..cd90c6c 100644 --- a/spec/controllers/game_tasks_controller_spec.rb +++ b/spec/controllers/game_tasks_controller_spec.rb @@ -1,46 +1,23 @@ require 'spec_helper' -# This spec was generated by rspec-rails when you ran the scaffold generator. -# It demonstrates how one might use RSpec to specify the controller code that -# was generated by Rails when you ran the scaffold generator. -# -# It assumes that the implementation code is generated by the rails scaffold -# generator. If you are using any extension libraries to generate different -# controller code, this generated spec may or may not pass. -# -# It only uses APIs available in rails and/or rspec-rails. There are a number -# of tools you can use to make these specs even more expressive, but we're -# sticking to rails and rspec-rails APIs to keep things simple and stable. -# -# Compared to earlier versions of this generator, there is very limited use of -# stubs and message expectations in this spec. Stubs are only used when there -# is no simpler way to get a handle on the object needed for the example. -# Message expectations are only used when there is no simpler way to specify -# that an instance is receiving a specific message. - describe GameTasksController do login_admin - # This should return the minimal set of attributes required to create a valid - # GameTask. As you add validations to GameTask, be sure to - # adjust the attributes here as well. + let(:valid_attributes) { { "game_id" => "1" } } - # This should return the minimal set of values that should be in the session - # in order to pass any filters (e.g. authentication) defined in - # GameTasksController. Be sure to keep this updated too. let(:valid_session) { {} } - before do - @game = FactoryGirl.create(:game) - @task = FactoryGirl.create(:task) - @game_task = FactoryGirl.create(:game_task, game_id: @game.to_param, task_id: @task.to_param) - end + let(:game) {create :game} + + let(:task) {create :task} + + let!(:game_task) {create :game_task, game: game, task: task} describe "POST create" do describe "with valid params" do it "creates a new GameTask" do expect { - post :create, {:game_task => @game_task.attributes}, valid_session + post :create, {game_task: game_task.attributes}, valid_session }.to change(GameTask, :count).by(1) end end @@ -49,7 +26,7 @@ describe "DELETE destroy" do it "destroys the requested game_task" do expect { - delete :destroy, {:id => @game_task.to_param}, valid_session + delete :destroy, {id: game_task.id}, valid_session }.to change(GameTask, :count).by(-1) end end diff --git a/spec/controllers/games_controller_spec.rb b/spec/controllers/games_controller_spec.rb index 8166a9a..9102041 100644 --- a/spec/controllers/games_controller_spec.rb +++ b/spec/controllers/games_controller_spec.rb @@ -1,59 +1,37 @@ require 'spec_helper' -# This spec was generated by rspec-rails when you ran the scaffold generator. -# It demonstrates how one might use RSpec to specify the controller code that -# was generated by Rails when you ran the scaffold generator. -# -# It assumes that the implementation code is generated by the rails scaffold -# generator. If you are using any extension libraries to generate different -# controller code, this generated spec may or may not pass. -# -# It only uses APIs available in rails and/or rspec-rails. There are a number -# of tools you can use to make these specs even more expressive, but we're -# sticking to rails and rspec-rails APIs to keep things simple and stable. -# -# Compared to earlier versions of this generator, there is very limited use of -# stubs and message expectations in this spec. Stubs are only used when there -# is no simpler way to get a handle on the object needed for the example. -# Message expectations are only used when there is no simpler way to specify -# that an instance is receiving a specific message. - describe GamesController do login_admin - # This should return the minimal set of attributes required to create a valid - # Game. As you add validations to Game, be sure to - # adjust the attributes here as well. - let(:valid_attributes) { { } } - - # This should return the minimal set of values that should be in the session - # in order to pass any filters (e.g. authentication) defined in - # GamesController. Be sure to keep this updated too. + let(:valid_session) { {} } - before do - @game = FactoryGirl.create(:game) - @task = FactoryGirl.create(:task) - @game_task = FactoryGirl.create(:game_task, game_id: @game.to_param, task_id: @task.to_param) - @invalid_attributes = FactoryGirl.build(:game, title: "впирапеи", start_date: "01.01.2014 00:00:00".to_datetime).attributes - end + let!(:game) {create :game} + + let!(:task) {create :task} + + let!(:game_task) {create :game_task, game: game, task: task} + + let(:invalid_attributes) {build :game, title: "впирапеи"} + + let(:valid_attributes) {build :game, title: "title_1"} describe "GET index" do login_user it "assigns all games as @games" do get :index, {}, valid_session - expect(assigns(:games)).to eq([@game]) + expect(assigns(:games)).to eq([game]) end end describe "GET show" do + login_user it "assigns the requested game as @game" do - get :show, {:id => @game.to_param}, valid_session - expect(assigns(:game)).to eq(@game) + get :show, {id: game.id}, valid_session + expect(assigns(:game)).to eq(game) end end describe "GET new" do - login_user it "assigns a new game as @game" do get :new, {}, valid_session expect(assigns(:game)).to be_a_new(Game) @@ -62,43 +40,45 @@ describe "GET edit" do it "assigns the requested game as @game" do - get :edit, {:id => @game.to_param}, valid_session - expect(assigns(:game)).to eq(@game) + get :edit, {id: game.id}, valid_session + expect(assigns(:game)).to eq(game) end end describe "POST create" do + before(:each) do + @valid_attributes = valid_attributes.attributes + @invalid_attributes = invalid_attributes + end describe "with valid params" do it "creates a new Game" do expect { - post :create, {:game => @game.attributes}, valid_session + post :create, {game: valid_attributes.attributes}, valid_session }.to change(Game, :count).by(1) end it "assigns a newly created game as @game" do - post :create, {:game => @game.attributes}, valid_session + post :create, {game: valid_attributes.attributes}, valid_session expect(assigns(:game)).to be_a(Game) expect(assigns(:game)).to be_persisted end it "redirects to the created game" do - post :create, {:game => @game.attributes}, valid_session + post :create, {game: valid_attributes.attributes}, valid_session expect(response).to redirect_to(Game.last) end end describe "with invalid params" do it "assigns a newly created but unsaved game as @game" do - # Trigger the behavior that occurs when invalid params are submitted Game.any_instance.stub(:save).and_return(false) - post :create, {:game => @invalid_attributes}, valid_session + post :create, {game: invalid_attributes.attributes}, valid_session expect(assigns(:game)).to be_a_new(Game) end it "re-renders the 'new' template" do - # Trigger the behavior that occurs when invalid params are submitted Game.any_instance.stub(:save).and_return(false) - post :create, {:game => @invalid_attributes}, valid_session + post :create, {game: invalid_attributes.attributes}, valid_session expect(response).to render_template("new") end end @@ -106,41 +86,32 @@ describe "PUT update" do describe "with valid params" do - before (:each) do - @valid_attributes = FactoryGirl.build(:game, title: "title_1").attributes - end it "updates the requested game" do - # Assuming there are no other games in the database, this - # specifies that the Game created on the previous line - # receives the :update_attributes message with whatever params are - # submitted in the request. - expect_any_instance_of(Game).to receive(:update).with({ "title" => "1234567890" }) - put :update, {:id => @game.to_param, :game => { "title" => "1234567890" }}, valid_session + expect_any_instance_of(Game).to receive(:update).with({ "title" => valid_attributes.title }) + put :update, {id: game.id, game: { "title" => valid_attributes.title }}, valid_session end it "assigns the requested game as @game" do - put :update, {:id => @game.to_param, :game => @valid_attributes}, valid_session - expect(assigns(:game)).to eq(@game) + put :update, {id: game.id, game: { title: valid_attributes.title }}, valid_session + expect(assigns(:game)).to eq(game) end it "redirects to the game" do - put :update, {:id => @game.to_param, :game => @valid_attributes}, valid_session - expect(response).to redirect_to(@game) + put :update, {id: game.id, game: { title: valid_attributes.title }}, valid_session + expect(response).to redirect_to(game) end end describe "with invalid params" do it "assigns the game as @game" do - # Trigger the behavior that occurs when invalid params are submitted Game.any_instance.stub(:save).and_return(false) - put :update, {:id => @game.to_param, :game => @invalid_attributes}, valid_session - expect(assigns(:game)).to eq(@game) + put :update, {id: game.id, game: { title: invalid_attributes.title }}, valid_session + expect(assigns(:game)).to eq(game) end it "re-renders the 'edit' template" do - # Trigger the behavior that occurs when invalid params are submitted Game.any_instance.stub(:save).and_return(false) - put :update, {:id => @game.to_param, :game => @invalid_attributes}, valid_session + put :update, {id: game.id, game: { title: invalid_attributes.title }}, valid_session expect(response).to render_template("edit") end end @@ -149,61 +120,62 @@ describe "DELETE destroy" do it "destroys the requested game" do expect { - delete :destroy, {:id => @game.to_param}, valid_session + delete :destroy, {id: game.id}, valid_session }.to change(Game, :count).by(-1) end it "redirects to the games list" do - delete :destroy, {:id => @game.to_param}, valid_session + delete :destroy, {id: game.id}, valid_session expect(response).to redirect_to(games_url) end end describe "Start and finish game" do - before(:each) do - @user_id = FactoryGirl.create(:user).to_param - @user_game = FactoryGirl.create(:user_game, user_id: @user_id, game_id: @game.to_param) - end + + let!(:user) {create :user} + + let!(:user_game) {create :user_game, game: game, user: user} + describe "Start game" do it "execute action " do expect { - get "start_game", {id: @game.to_param}, valid_session + get "start_game", {id: game.id}, valid_session }.to change(UserTask, :count).by(1) end it "check game state equal to CURRENT" do - get "start_game", {id: @game.to_param}, valid_session - expect(Game.find(@game.to_param.to_i).state).to equal(UserGame::CURRENT) + get "start_game", {id: game.id}, valid_session + expect(Game.find(game.id).state).to equal(UserGame::CURRENT) end it "check user_game state equal to CURRENT" do - get "start_game", {id: @game.to_param}, valid_session - expect(UserGame.where(user_id: @user_id, game_id:@game.to_param.to_i).first.state).to equal(UserGame::CURRENT) + get "start_game", {id: game.id}, valid_session + expect(UserGame.where(user_id: user.id, game_id: game.id).first.state).to equal(UserGame::CURRENT) end end describe "Finish game" do - before(:each) do - @task1 = FactoryGirl.create(:task) - @user_task = FactoryGirl.create(:user_task, task_id: @task.to_param, user_id: @user_id, result: 3) - @user_task1 = FactoryGirl.create(:user_task, task_id: @task1.to_param, user_id: @user_id, result: 3) - end + let(:task1) {create :task} + + let!(:user_task) {create :user_task, user: user, task: task, result: 3} + + let!(:user_task1) {create :user_task, user: user, task: task1, result: 3} it "check game status as equal to COMPLETED" do - get "finish_game", {id: @game.to_param}, valid_session - expect(Game.find(@game.to_param.to_i).state).to eq(UserGame::COMPLETED) + get "finish_game", {id: game.id}, valid_session + expect(Game.find(game.id).state).to eq(UserGame::COMPLETED) end it "check user_game status as equal to COMPLETED" do - get "finish_game", {id: @game.to_param}, valid_session - expect(UserGame.where(user_id: @user_id, game_id:@game.to_param.to_i).first.state).to eq(UserGame::COMPLETED) + get "finish_game", {id: game.id}, valid_session + expect(UserGame.where(user_id: user.id, game_id: game.id).first.state).to eq(UserGame::COMPLETED) end it "check user_game points count as equal to user_tasks results sum " do - get "finish_game", {id: @game.to_param}, valid_session - expect(UserGame.where(user_id: @user_id, game_id:@game.to_param.to_i).first.result).to eq(@user_task.result + @user_task1.result) + get "finish_game", {id: game.id}, valid_session + expect(UserGame.where(user_id: user.id, game_id: game.id).first.result).to eq(user_task.result + user_task1.result) end end diff --git a/spec/controllers/hints_controller_spec.rb b/spec/controllers/hints_controller_spec.rb index 26e9f25..a8daa40 100644 --- a/spec/controllers/hints_controller_spec.rb +++ b/spec/controllers/hints_controller_spec.rb @@ -1,129 +1,100 @@ require 'spec_helper' -# This spec was generated by rspec-rails when you ran the scaffold generator. -# It demonstrates how one might use RSpec to specify the controller code that -# was generated by Rails when you ran the scaffold generator. -# -# It assumes that the implementation code is generated by the rails scaffold -# generator. If you are using any extension libraries to generate different -# controller code, this generated spec may or may not pass. -# -# It only uses APIs available in rails and/or rspec-rails. There are a number -# of tools you can use to make these specs even more expressive, but we're -# sticking to rails and rspec-rails APIs to keep things simple and stable. -# -# Compared to earlier versions of this generator, there is very limited use of -# stubs and message expectations in this spec. Stubs are only used when there -# is no simpler way to get a handle on the object needed for the example. -# Message expectations are only used when there is no simpler way to specify -# that an instance is receiving a specific message. - describe HintsController do login_admin - # This should return the minimal set of attributes required to create a valid - # Hint. As you add validations to Hint, be sure to - # adjust the attributes here as well. - let(:valid_attributes) { { } } - - # This should return the minimal set of values that should be in the session - # in order to pass any filters (e.g. authentication) defined in - # HintsController. Be sure to keep this updated too. + let(:valid_session) { {} } - before do - @queue_number = rand(1...2) - @task = FactoryGirl.create(:task) - @hint = FactoryGirl.create(:hint, queue_number: @queue_number) - @new_hint = FactoryGirl.build(:hint, queue_number: 3 - @queue_number) - @task_hint = FactoryGirl.create(:task_hint, task_id: @task.to_param, hint_id: @hint.to_param) - @invalid_attributes = FactoryGirl.build(:hint, hint_text: "акпыук", queue_number: 3 - @queue_number).attributes - end + let(:task) {create :task} + + let(:hint) {create :hint, queue_number: rand(1...2)} + + let!(:task_hint) {create :task_hint, task: task, hint: hint} + + let(:invalid_attributes) {build :hint, hint_text: "акпыук", queue_number: 3 - hint.queue_number} + + let(:valid_attributes) {build :hint, hint_text: "dzfvbzjfgnarmkgnhbmargtgbhsergnerf;"} describe "GET index" do it "assigns all hints as @hints" do get :index, {}, valid_session - expect(assigns(:hints)).to eq([@hint]) + expect(assigns(:hints)).to eq([hint]) end end describe "GET show" do it "assigns the requested hint as @hint" do - get :show, {:id => @hint.to_param}, valid_session - expect(assigns(:hint)).to eq(@hint) + get :show, {id: hint.id}, valid_session + expect(assigns(:hint)).to eq(hint) end end describe "GET new" do it "assigns a new hint as @hint" do - get :new, {task: @task.to_param}, valid_session + get :new, {task: task.id}, valid_session expect(assigns(:hint)).to be_a_new(Hint) end - - it "check task hints count" do - @new_hint = FactoryGirl.create(:hint, hint_text: "gfbdfsbgsdfgbdfg") - @new_task_hint = FactoryGirl.create(:task_hint, task_id: @task.to_param, hint_id: @new_hint.to_param) - get :show, {:id => @hint.to_param}, valid_session - expect(assigns(:hint)).to eq(@hint) - end - end describe "GET edit" do it "assigns the requested hint as @hint" do - get :edit, {:id => @hint.to_param}, valid_session - expect(assigns(:hint)).to eq(@hint) + get :edit, {id: hint.id}, valid_session + expect(assigns(:hint)).to eq(hint) end end describe "POST create" do + + let(:new_hint) {create :hint, queue_number: 3 - hint.queue_number} + + describe "third task hint" do + + let!(:new_task_hint) {create :task_hint, task: task, hint: new_hint} + + it "creates a third Hint" do + expect { + post :create, {:hint => @hint_post}, valid_session + }.to change(Hint, :count).by(0) + end + + end + before(:each) do - @hint_post = @new_hint.attributes - @hint_post[:task] = @task.to_param - @hint_post_invalid = @invalid_attributes - @hint_post_invalid[:task] = @task.to_param + @hint_post = new_hint.attributes + @hint_post[:task] = task.id + @hint_post_invalid = invalid_attributes.attributes + @hint_post_invalid[:task] = task.id end describe "with valid params" do it "creates a new Hint" do expect { - post :create, {:hint => @hint_post}, valid_session + post :create, {hint: @hint_post}, valid_session }.to change(Hint, :count).by(1) end it "assigns a newly created hint as @hint" do - post :create, {:hint => @hint_post}, valid_session + post :create, {hint: @hint_post}, valid_session expect(assigns(:hint)).to be_a(Hint) expect(assigns(:hint)).to be_persisted end it "redirects to the created hint" do - post :create, {:hint => @hint_post}, valid_session + post :create, {hint: @hint_post}, valid_session expect(response).to redirect_to(Hint.last) end - - it "creates a third Hint" do - @new_hint = FactoryGirl.create(:hint, queue_number: 3 - @queue_number) - @task_hint = FactoryGirl.create(:task_hint, task_id: @task.to_param, hint_id: @new_hint.to_param) - expect { - post :create, {:hint => @hint_post}, valid_session - }.to change(Hint, :count).by(0) - end - - end describe "with invalid params" do it "assigns a newly created but unsaved hint as @hint" do - # Trigger the behavior that occurs when invalid params are submitted Hint.any_instance.stub(:save).and_return(false) - post :create, {:hint => @hint_post_invalid}, valid_session + post :create, {hint: @hint_post_invalid}, valid_session expect(assigns(:hint)).to be_a_new(Hint) end it "re-renders the 'new' template" do - # Trigger the behavior that occurs when invalid params are submitted Hint.any_instance.stub(:save).and_return(false) - post :create, {:hint => @hint_post_invalid}, valid_session + post :create, {hint: @hint_post_invalid}, valid_session expect(response).to render_template("new") end end @@ -131,43 +102,32 @@ describe "PUT update" do describe "with valid params" do - before (:each) do - @valid_attributes = FactoryGirl.build(:hint, hint_text: "dzfvbzjfgnarmkgnhbmargtgbhsergnerf;").attributes - end it "updates the requested hint" do - # Assuming there are no other hints in the database, this - # specifies that the Hint created on the previous line - # receives the :update_attributes message with whatever params are - # submitted in the request. expect_any_instance_of(Hint).to receive(:update).with({ "hint_text" => "asfgasdfiasfauhguhawfjawurfh" }) - put :update, {:id => @hint.to_param, :hint => { "hint_text" => "asfgasdfiasfauhguhawfjawurfh" }}, valid_session + put :update, {id: hint.id, hint: { "hint_text" => "asfgasdfiasfauhguhawfjawurfh" }}, valid_session end it "assigns the requested hint as @hint" do - hint = Hint.create! valid_attributes - put :update, {:id => @hint.to_param, :hint => @valid_attributes}, valid_session - expect(assigns(:hint)).to eq(@hint) + put :update, {id: hint.id, hint: valid_attributes.attributes}, valid_session + expect(assigns(:hint)).to eq(hint) end it "redirects to the hint" do - put :update, {:id => @hint.to_param, :hint => @valid_attributes}, valid_session - expect(response).to redirect_to(@hint) + put :update, {id: hint.id, hint: valid_attributes.attributes}, valid_session + expect(response).to redirect_to(hint) end end describe "with invalid params" do it "assigns the hint as @hint" do - # Trigger the behavior that occurs when invalid params are submitted Hint.any_instance.stub(:save).and_return(false) - put :update, {:id => @hint.to_param, :hint => @invalid_attributes}, valid_session - expect(assigns(:hint)).to eq(@hint) + put :update, {id: hint.id, hint: invalid_attributes.attributes}, valid_session + expect(assigns(:hint)).to eq(hint) end it "re-renders the 'edit' template" do - hint = Hint.create! valid_attributes - # Trigger the behavior that occurs when invalid params are submitted Hint.any_instance.stub(:save).and_return(false) - put :update, {:id => @hint.to_param, :hint => @invalid_attributes}, valid_session + put :update, {id: hint.id, hint: invalid_attributes.attributes}, valid_session expect(response).to render_template("edit") end end @@ -176,12 +136,12 @@ describe "DELETE destroy" do it "destroys the requested hint" do expect { - delete :destroy, {:id => @hint.to_param}, valid_session + delete :destroy, {id: hint.id}, valid_session }.to change(Hint, :count).by(-1) end it "redirects to the hints list" do - delete :destroy, {:id => @hint.to_param}, valid_session + delete :destroy, {id: hint.id}, valid_session expect(response).to redirect_to(hints_url) end end diff --git a/spec/controllers/task_codes_controller_spec.rb b/spec/controllers/task_codes_controller_spec.rb index 5108800..94ff466 100644 --- a/spec/controllers/task_codes_controller_spec.rb +++ b/spec/controllers/task_codes_controller_spec.rb @@ -1,47 +1,23 @@ require 'spec_helper' -# This spec was generated by rspec-rails when you ran the scaffold generator. -# It demonstrates how one might use RSpec to specify the controller code that -# was generated by Rails when you ran the scaffold generator. -# -# It assumes that the implementation code is generated by the rails scaffold -# generator. If you are using any extension libraries to generate different -# controller code, this generated spec may or may not pass. -# -# It only uses APIs available in rails and/or rspec-rails. There are a number -# of tools you can use to make these specs even more expressive, but we're -# sticking to rails and rspec-rails APIs to keep things simple and stable. -# -# Compared to earlier versions of this generator, there is very limited use of -# stubs and message expectations in this spec. Stubs are only used when there -# is no simpler way to get a handle on the object needed for the example. -# Message expectations are only used when there is no simpler way to specify -# that an instance is receiving a specific message. - describe TaskCodesController do login_admin - # This should return the minimal set of attributes required to create a valid - # TaskCode. As you add validations to TaskCode, be sure to - # adjust the attributes here as well. + let(:valid_attributes) { { "task_id" => "1" } } - # This should return the minimal set of values that should be in the session - # in order to pass any filters (e.g. authentication) defined in - # TaskCodesController. Be sure to keep this updated too. let(:valid_session) { {} } + let(:task) {create :task} - before do - @task = FactoryGirl.create(:task) - @code = FactoryGirl.create(:code) - @task_code = FactoryGirl.create(:task_code, task_id: @task.to_param, code_id: @code.to_param) - end + let(:code) {create :code} + + let!(:task_code) {create :task_code, task: task, code: code} describe "POST create" do describe "with valid params" do it "creates a new TaskCode" do expect { - post :create, {:task_code => @task_code.attributes}, valid_session + post :create, {:task_code => task_code.attributes}, valid_session }.to change(TaskCode, :count).by(1) end end @@ -50,7 +26,7 @@ describe "DELETE destroy" do it "destroys the requested task_code" do expect { - delete :destroy, {:id => @task_code.to_param}, valid_session + delete :destroy, {id: task_code.id}, valid_session }.to change(TaskCode, :count).by(-1) end end diff --git a/spec/controllers/task_hints_controller_spec.rb b/spec/controllers/task_hints_controller_spec.rb index b9aa971..77cdbee 100644 --- a/spec/controllers/task_hints_controller_spec.rb +++ b/spec/controllers/task_hints_controller_spec.rb @@ -1,46 +1,22 @@ require 'spec_helper' -# This spec was generated by rspec-rails when you ran the scaffold generator. -# It demonstrates how one might use RSpec to specify the controller code that -# was generated by Rails when you ran the scaffold generator. -# -# It assumes that the implementation code is generated by the rails scaffold -# generator. If you are using any extension libraries to generate different -# controller code, this generated spec may or may not pass. -# -# It only uses APIs available in rails and/or rspec-rails. There are a number -# of tools you can use to make these specs even more expressive, but we're -# sticking to rails and rspec-rails APIs to keep things simple and stable. -# -# Compared to earlier versions of this generator, there is very limited use of -# stubs and message expectations in this spec. Stubs are only used when there -# is no simpler way to get a handle on the object needed for the example. -# Message expectations are only used when there is no simpler way to specify -# that an instance is receiving a specific message. - describe TaskHintsController do login_admin - # This should return the minimal set of attributes required to create a valid - # TaskHint. As you add validations to TaskHint, be sure to - # adjust the attributes here as well. - let(:valid_attributes) { { "task_id" => "1" } } - # This should return the minimal set of values that should be in the session - # in order to pass any filters (e.g. authentication) defined in - # TaskHintsController. Be sure to keep this updated too. + let(:valid_attributes) { { "task_id" => "1" } } let(:valid_session) { {} } - before do - @hint = FactoryGirl.create(:hint) - @task = FactoryGirl.create(:task) - @task_hint = FactoryGirl.create(:task_hint, task_id: @game.to_param, hint_id: @hint.to_param) - end + let(:task) {create :task} + + let(:hint) {create :hint} + + let!(:task_hint) {create :task_hint, task: task, hint: hint} describe "POST create" do describe "with valid params" do it "creates a new TaskHint" do expect { - post :create, {:task_hint => @task.attributes}, valid_session + post :create, {task_hint: task_hint.attributes}, valid_session }.to change(TaskHint, :count).by(1) end end @@ -49,7 +25,7 @@ describe "DELETE destroy" do it "destroys the requested task_hint" do expect { - delete :destroy, {:id => @task_hint.to_param}, valid_session + delete :destroy, {id: task_hint.id}, valid_session }.to change(TaskHint, :count).by(-1) end end diff --git a/spec/controllers/tasks_controller_spec.rb b/spec/controllers/tasks_controller_spec.rb index 833eb60..7a07d20 100644 --- a/spec/controllers/tasks_controller_spec.rb +++ b/spec/controllers/tasks_controller_spec.rb @@ -30,85 +30,90 @@ # TasksController. Be sure to keep this updated too. let(:valid_session) { {} } - before do - @game = FactoryGirl.create(:game) - @task = FactoryGirl.create(:task) - @hint = FactoryGirl.create(:hint) - @user = FactoryGirl.create(:user) - @user_task = FactoryGirl.create(:user_task, user_id: @user.to_param, task_id: @task.to_param) - @task_hint = FactoryGirl.create(:task_hint, task_id: @task.to_param, hint_id: @hint.to_param) - @hint2 = FactoryGirl.create(:hint, hint_text: " wvgrawrfswf", queue_number: 2) - @task_hint2 = FactoryGirl.create(:task_hint, task_id: @task.to_param, hint_id: @hint2.to_param) - #puts Task.first.to_yaml - @invalid_attributes = FactoryGirl.build(:task, task_text: "впирапеи").attributes - end + let(:game) {create :game} + + let(:task) {create :task} + + let(:hint) {create :hint} + + let!(:user) {create :user} + + let!(:user_task) {create :user_task, user: user, task: task} + + let!(:task_hint) {create :task_hint, task: task, hint: hint} + + let(:hint2) {create :hint, hint_text: " wvgrawrfswf", queue_number: 2} + + let!(:task_hint2) {create :task_hint, task: task, hint: hint2} + + let(:invalid_attributes) {build :task, task_text: "впирапеи"} + + let(:valid_attributes) {build :task, task_text: "zhvnasjfnj"} describe "GET index" do it "assigns all tasks as @tasks" do get :index, {}, valid_session - expect(assigns(:tasks)).to eq([@task]) + expect(assigns(:tasks)).to eq([task]) end end describe "GET show" do it "assigns the requested task as @task" do - get :show, {:id => @task.to_param}, valid_session - expect(assigns(:task)).to eq(@task) + get :show, {id: task.id}, valid_session + expect(assigns(:task)).to eq(task) end end describe "GET new" do it "assigns a new task as @task" do - get :new, {game: @game.to_param}, valid_session + get :new, {game: game.id}, valid_session expect(assigns(:task)).to be_a_new(Task) end end describe "GET edit" do it "assigns the requested task as @task" do - get :edit, {:id => @task.to_param}, valid_session - expect(assigns(:task)).to eq(@task) + get :edit, {id:task.id}, valid_session + expect(assigns(:task)).to eq(task) end end describe "POST create" do before(:each) do - @task_post = @task.attributes - @task_post[:game] = @game.to_param - @task_post_invalid = @invalid_attributes - @task_post_invalid[:game] = @game.to_param + @task_post = task.attributes + @task_post[:game] = game.id + @task_post_invalid = invalid_attributes.attributes + @task_post_invalid[:game] = game.id end describe "with valid params" do it "creates a new Task" do expect { - post :create, {:task => @task_post}, valid_session + post :create, {task: @task_post}, valid_session }.to change(Task, :count).by(1) end it "assigns a newly created task as @task" do - post :create, {:task => @task_post}, valid_session + post :create, {task: @task_post}, valid_session expect(assigns(:task)).to be_a(Task) expect(assigns(:task)).to be_persisted end it "redirects to the created task" do - post :create, {:task =>@task_post}, valid_session + post :create, {task: @task_post}, valid_session expect(response).to redirect_to(Task.last) end end describe "with invalid params" do it "assigns a newly created but unsaved task as @task" do - # Trigger the behavior that occurs when invalid params are submitted Task.any_instance.stub(:save).and_return(false) - post :create, {:task => @task_post_invalid}, valid_session + post :create, {task: @task_post_invalid}, valid_session expect(assigns(:task)).to be_a_new(Task) end it "re-renders the 'new' template" do - # Trigger the behavior that occurs when invalid params are submitted Task.any_instance.stub(:save).and_return(false) - post :create, {:task => @task_post_invalid}, valid_session + post :create, {task: @task_post_invalid}, valid_session expect(response).to render_template("new") end end @@ -116,41 +121,33 @@ describe "PUT update" do describe "with valid params" do - before (:each) do - @valid_attributes = FactoryGirl.build(:task, task_text: "asfvubasdfchnlasidfciojnk").attributes - end + it "updates the requested task" do - # Assuming there are no other tasks in the database, this - # specifies that the Task created on the previous line - # receives the :update_attributes message with whatever params are - # submitted in the request. expect_any_instance_of(Task).to receive(:update).with({ "task_text" => "arghbstghset" }) - put :update, {:id => @task.to_param, :task => { "task_text" => "arghbstghset" }}, valid_session + put :update, {id: task.id, :task => { "task_text" => "arghbstghset" }}, valid_session end it "assigns the requested task as @task" do - put :update, {:id => @task.to_param, :task => @valid_attributes}, valid_session - expect(assigns(:task)).to eq(@task) + put :update, {id: task.id, task: valid_attributes.attributes}, valid_session + expect(assigns(:task)).to eq(task) end it "redirects to the task" do - put :update, {:id => @task.to_param, :task => @valid_attributes}, valid_session - expect(response).to redirect_to(@task) + put :update, {id: task.id, task: valid_attributes.attributes}, valid_session + expect(response).to redirect_to(task) end end describe "with invalid params" do it "assigns the task as @task" do - # Trigger the behavior that occurs when invalid params are submitted Task.any_instance.stub(:save).and_return(false) - put :update, {:id => @task.to_param, :task => @invalid_attributes}, valid_session - expect(assigns(:task)).to eq(@task) + put :update, {id: task.id, task: invalid_attributes.attributes}, valid_session + expect(assigns(:task)).to eq(task) end it "re-renders the 'edit' template" do - # Trigger the behavior that occurs when invalid params are submitted Task.any_instance.stub(:save).and_return(false) - put :update, {:id => @task.to_param, :task => @invalid_attributes}, valid_session + put :update, {id: task.id, task: invalid_attributes.attributes}, valid_session expect(response).to render_template("edit") end end @@ -159,12 +156,12 @@ describe "DELETE destroy" do it "destroys the requested task" do expect { - delete :destroy, {:id => @task.to_param}, valid_session + delete :destroy, {id: task.id}, valid_session }.to change(Task, :count).by(-1) end it "redirects to the tasks list" do - delete :destroy, {:id => @task.to_param}, valid_session + delete :destroy, {id: task.id}, valid_session expect(response).to redirect_to(tasks_url) end end @@ -172,39 +169,44 @@ describe "Raise hint" do it "execute action " do expect { - get "raise_hint", {id: @task.to_param}, valid_session + get "raise_hint", {id: task.id}, valid_session }.to change(UserHint, :count).by(1) end it "raises first hint" do expect { - get "raise_hint", {id: @task.to_param}, valid_session + get "raise_hint", {id: task.id}, valid_session }.to change(UserHint, :count).by(1) - expect(UserHint.last.hint_id).to eq(@hint.to_param.to_i) + expect(UserHint.last.hint_id).to eq(hint.id) expect(UserHint.count).to eq(1) end describe "hints count > 1" do + + let!(:user_hint) {create :user_hint, user: user, hint: hint} + before(:each) do - Hint.find(@hint.to_param.to_i).update_attribute(:raised, Hint::RAISED) - @user_hint = FactoryGirl.create(:user_hint, user_id: @user.to_param, hint_id: @hint.to_param) + Hint.find(hint.id).update_attribute(:raised, Hint::RAISED) end + it "raises second hint" do expect { - get "raise_hint", {id: @task.to_param}, valid_session + get "raise_hint", {id: task.id}, valid_session }.to change(UserHint, :count).by(1) - expect(UserHint.last.hint_id).to eq(@hint2.to_param.to_i) + expect(UserHint.last.hint_id).to eq(hint2.id) expect(UserHint.count).to eq(2) end - it "doesn't raise third hint" do - Hint.find(@hint2.to_param.to_i).update_attribute(:raised, Hint::RAISED) - @user_hint2 = FactoryGirl.create(:user_hint, user_id: @user.to_param, hint_id: @hint2.to_param) - expect { - get "raise_hint", {id: @task.to_param}, valid_session - }.to change(UserHint, :count).by(0) - expect(UserHint.last.hint_id).to eq(@hint2.to_param.to_i) - expect(UserHint.count).to eq(2) + describe "third hint" do + let!(:user_hint2) {create :user_hint, user: user, hint: hint2} + it "doesn't raise third hint" do + Hint.find(hint2.id).update_attribute(:raised, Hint::RAISED) + expect { + get "raise_hint", {id: task.id}, valid_session + }.to change(UserHint, :count).by(0) + expect(UserHint.last.hint_id).to eq(hint2.id) + expect(UserHint.count).to eq(2) + end end end end diff --git a/spec/controllers/user_games_controller_spec.rb b/spec/controllers/user_games_controller_spec.rb index c3c7008..2914e03 100644 --- a/spec/controllers/user_games_controller_spec.rb +++ b/spec/controllers/user_games_controller_spec.rb @@ -1,54 +1,30 @@ require 'spec_helper' -# This spec was generated by rspec-rails when you ran the scaffold generator. -# It demonstrates how one might use RSpec to specify the controller code that -# was generated by Rails when you ran the scaffold generator. -# -# It assumes that the implementation code is generated by the rails scaffold -# generator. If you are using any extension libraries to generate different -# controller code, this generated spec may or may not pass. -# -# It only uses APIs available in rails and/or rspec-rails. There are a number -# of tools you can use to make these specs even more expressive, but we're -# sticking to rails and rspec-rails APIs to keep things simple and stable. -# -# Compared to earlier versions of this generator, there is very limited use of -# stubs and message expectations in this spec. Stubs are only used when there -# is no simpler way to get a handle on the object needed for the example. -# Message expectations are only used when there is no simpler way to specify -# that an instance is receiving a specific message. - describe UserGamesController do login_user render_views - # This should return the minimal set of attributes required to create a valid - # UserGame. As you add validations to UserGame, be sure to - # adjust the attributes here as well. - let(:valid_attributes) { { "user_id" => "1" } } - - # This should return the minimal set of values that should be in the session - # in order to pass any filters (e.g. authentication) defined in - # UserGamesController. Be sure to keep this updated too. + let(:valid_session) { {} } - before do - @game = FactoryGirl.create(:game) - result = rand(-1..1) - @user_game = FactoryGirl.create(:user_game, user_id: @user_id, game_id: @game.to_param, result: result) - @invalid_attributes = FactoryGirl.build(:user_game, user_id: @user_id, game_id: @game.to_param, result: "aerg").attributes - end + let(:game) {create :game} + + let!(:user_game) {create :user_game, user: @user, game: game, result: 0} + + let(:invalid_attributes) {build :user_game, user: @user, game: game, result: "aerg"} + + let(:valid_attributes) {build :user_game, user: @user, game: game, result: 10} describe "GET index" do it "assigns all user_games as @user_games" do get :index, {}, valid_session - expect(assigns(:user_games)).to eq([@user_game]) + expect(assigns(:user_games)).to eq([user_game]) end end describe "GET show" do it "assigns the requested user_game as @user_game" do - get :show, {:id => @user_game.to_param}, valid_session - expect(assigns(:user_game)).to eq(@user_game) + get :show, {id: user_game.id}, valid_session + expect(assigns(:user_game)).to eq(user_game) end end @@ -61,8 +37,8 @@ describe "GET edit" do it "assigns the requested user_game as @user_game" do - get :edit, {:id => @user_game.to_param}, valid_session - expect(assigns(:user_game)).to eq(@user_game) + get :edit, {id: user_game.id}, valid_session + expect(assigns(:user_game)).to eq(user_game) end end @@ -70,34 +46,33 @@ describe "with valid params" do it "creates a new UserGame" do expect { - post :create, {game_id: @user_game.attributes["game_id"]}, valid_session + post :create, {game_id: game.id}, valid_session }.to change(UserGame, :count).by(1) end it "assigns a newly created user_game as @user_game" do - post :create, {game_id: @user_game.attributes["game_id"]}, valid_session + post :create, {game_id: game.id}, valid_session expect(assigns(:user_game)).to be_a(UserGame) expect(assigns(:user_game)).to be_persisted end it "redirects to the created users games list" do - post :create, {game_id: @user_game.attributes["game_id"]}, valid_session + post :create, {game_id: game.id}, valid_session expect(response).to redirect_to(user_games_url) end end describe "with invalid params" do it "assigns a newly created but unsaved user_game as @user_game" do - # Trigger the behavior that occurs when invalid params are submitted UserGame.any_instance.stub(:save).and_return(false) - post :create, {game_id: "123"}, valid_session + post :create, {game_id: game.id}, valid_session expect(assigns(:user_game)).to be_a_new(UserGame) end it "redirect the root template" do # Trigger the behavior that occurs when invalid params are submitted UserGame.any_instance.stub(:save).and_return(false) - post :create, {game_id: "123"}, valid_session + post :create, {game_id: game.id}, valid_session expect(response).to redirect_to(root_path) end end @@ -105,41 +80,33 @@ describe "PUT update" do describe "with valid params" do - before (:each) do - @valid_attributes = FactoryGirl.build(:user_game, user_id: @user_id.to_param).attributes - end it "updates the requested user_game" do - # Assuming there are no other user_games in the database, this - # specifies that the UserGame created on the previous line - # receives the :update_attributes message with whatever params are - # submitted in the request. - expect_any_instance_of(UserGame).to receive(:update).with({ "user_id" => @user.to_param }) - put :update, {:id => @user_game.to_param, :user_game => { "user_id" => @user.to_param }}, valid_session + expect_any_instance_of(UserGame).to receive(:update).with({ "user_id" => "#{@user.id}" }) + put :update, {id: user_game.id, user_game: { "user_id" => @user.id }}, valid_session end it "assigns the requested user_game as @user_game" do - put :update, {:id => @user_game.to_param, :user_game => @valid_attributes}, valid_session - expect(assigns(:user_game)).to eq(@user_game) + put :update, {id: user_game.id, user_game: valid_attributes.attributes}, valid_session + expect(assigns(:user_game)).to eq(user_game) end it "redirects to the user_game" do - put :update, {:id => @user_game.to_param, :user_game => @valid_attributes}, valid_session - expect(response).to redirect_to(@user_game) + put :update, {id: user_game.id, user_game: valid_attributes.attributes}, valid_session + expect(response).to redirect_to(user_game) end end describe "with invalid params" do it "assigns the user_game as @user_game" do - # Trigger the behavior that occurs when invalid params are submitted UserGame.any_instance.stub(:save).and_return(false) - put :update, {:id => @user_game.to_param, :user_game => @invalid_attributes}, valid_session - expect(assigns(:user_game)).to eq(@user_game) + put :update, {id: user_game.id, user_game: invalid_attributes.attributes}, valid_session + expect(assigns(:user_game)).to eq(user_game) end it "re-renders the 'edit' template" do # Trigger the behavior that occurs when invalid params are submitted UserGame.any_instance.stub(:save).and_return(false) - put :update, {:id => @user_game.to_param, :user_game => @invalid_attributes}, valid_session + put :update, {id: user_game.id, user_game: invalid_attributes.attributes}, valid_session expect(response).to render_template("edit") end end @@ -148,12 +115,12 @@ describe "DELETE destroy" do it "destroys the requested user_game" do expect { - delete :destroy, {:id => @user_game.to_param}, valid_session + delete :destroy, {id: user_game.id}, valid_session }.to change(UserGame, :count).by(-1) end it "redirects to the user_games list" do - delete :destroy, {:id => @user_game.to_param}, valid_session + delete :destroy, {id: user_game.id}, valid_session expect(response).to redirect_to(user_games_url) end end diff --git a/spec/controllers/user_hints_controller_spec.rb b/spec/controllers/user_hints_controller_spec.rb index ffc0bcf..fa28ce1 100644 --- a/spec/controllers/user_hints_controller_spec.rb +++ b/spec/controllers/user_hints_controller_spec.rb @@ -1,45 +1,21 @@ require 'spec_helper' -# This spec was generated by rspec-rails when you ran the scaffold generator. -# It demonstrates how one might use RSpec to specify the controller code that -# was generated by Rails when you ran the scaffold generator. -# -# It assumes that the implementation code is generated by the rails scaffold -# generator. If you are using any extension libraries to generate different -# controller code, this generated spec may or may not pass. -# -# It only uses APIs available in rails and/or rspec-rails. There are a number -# of tools you can use to make these specs even more expressive, but we're -# sticking to rails and rspec-rails APIs to keep things simple and stable. -# -# Compared to earlier versions of this generator, there is very limited use of -# stubs and message expectations in this spec. Stubs are only used when there -# is no simpler way to get a handle on the object needed for the example. -# Message expectations are only used when there is no simpler way to specify -# that an instance is receiving a specific message. - describe UserHintsController do login_user - # This should return the minimal set of attributes required to create a valid - # UserHint. As you add validations to UserHint, be sure to - # adjust the attributes here as well. + let(:valid_attributes) { { "user_id" => "1" } } - # This should return the minimal set of values that should be in the session - # in order to pass any filters (e.g. authentication) defined in - # UserHintsController. Be sure to keep this updated too. let(:valid_session) { {} } - before do - @hint = FactoryGirl.create(:hint) - @user_hint = FactoryGirl.create(:user_hint, user_id: @user_id, hint_id: @hint.to_param) - end + let(:hint) {create :hint} + + let!(:user_hint) {create :user_hint, user: @user, hint: hint} describe "POST create" do describe "with valid params" do it "creates a new UserHint" do expect { - post :create, {:user_hint => @user_hint.attributes}, valid_session + post :create, {user_hint: user_hint.attributes}, valid_session }.to change(UserHint, :count).by(1) end end @@ -48,7 +24,7 @@ describe "DELETE destroy" do it "destroys the requested user_hint" do expect { - delete :destroy, {:id => @user_hint.to_param}, valid_session + delete :destroy, {id: user_hint.id}, valid_session }.to change(UserHint, :count).by(-1) end end diff --git a/spec/controllers/user_tasks_controller_spec.rb b/spec/controllers/user_tasks_controller_spec.rb index cd32571..60e08ca 100644 --- a/spec/controllers/user_tasks_controller_spec.rb +++ b/spec/controllers/user_tasks_controller_spec.rb @@ -1,46 +1,21 @@ require 'spec_helper' -# This spec was generated by rspec-rails when you ran the scaffold generator. -# It demonstrates how one might use RSpec to specify the controller code that -# was generated by Rails when you ran the scaffold generator. -# -# It assumes that the implementation code is generated by the rails scaffold -# generator. If you are using any extension libraries to generate different -# controller code, this generated spec may or may not pass. -# -# It only uses APIs available in rails and/or rspec-rails. There are a number -# of tools you can use to make these specs even more expressive, but we're -# sticking to rails and rspec-rails APIs to keep things simple and stable. -# -# Compared to earlier versions of this generator, there is very limited use of -# stubs and message expectations in this spec. Stubs are only used when there -# is no simpler way to get a handle on the object needed for the example. -# Message expectations are only used when there is no simpler way to specify -# that an instance is receiving a specific message. - describe UserTasksController do login_user - # This should return the minimal set of attributes required to create a valid - # UserTask. As you add validations to UserTask, be sure to - # adjust the attributes here as well. + let(:valid_attributes) { { "user_id" => "1" } } - # This should return the minimal set of values that should be in the session - # in order to pass any filters (e.g. authentication) defined in - # UserTasksController. Be sure to keep this updated too. let(:valid_session) { {} } - before do - result = rand(-1..1) - @task = FactoryGirl.create(:task) - @user_task = FactoryGirl.create(:user_task, user_id: @user_id, task_id: @task.to_param, result: result) - end + let(:task) {create :task} + + let!(:user_task) {create :user_task, user: @user, task: task, result: rand(0...3)} describe "POST create" do describe "with valid params" do it "creates a new UserTask" do expect { - post :create, {:user_task => @user_task.attributes}, valid_session + post :create, {user_task: user_task.attributes}, valid_session }.to change(UserTask, :count).by(1) end end @@ -49,7 +24,7 @@ describe "DELETE destroy" do it "destroys the requested user_task" do expect { - delete :destroy, {:id => @user_task.to_param}, valid_session + delete :destroy, {id: user_task.id}, valid_session }.to change(UserTask, :count).by(-1) end end diff --git a/spec/factories/admin_games.rb b/spec/factories/admin_games.rb index 5e57080..8a57dce 100644 --- a/spec/factories/admin_games.rb +++ b/spec/factories/admin_games.rb @@ -2,7 +2,7 @@ FactoryGirl.define do factory :admin_game do - admin_id 1 + admin_user_id 1 game_id 1 end end diff --git a/spec/helpers/admin_games_helper_spec.rb b/spec/helpers/admin_games_helper_spec.rb deleted file mode 100644 index daca0c5..0000000 --- a/spec/helpers/admin_games_helper_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -# Specs in this file have access to a helper object that includes -# the AdminGamesHelper. For example: -# -# describe AdminGamesHelper do -# describe "string concat" do -# it "concats two strings with spaces" do -# expect(helper.concat_strings("this","that")).to eq("this that") -# end -# end -# end -describe AdminGamesHelper do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/helpers/code_compares_helper_spec.rb b/spec/helpers/code_compares_helper_spec.rb deleted file mode 100644 index 7cfab59..0000000 --- a/spec/helpers/code_compares_helper_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -# Specs in this file have access to a helper object that includes -# the CodeComparesHelper. For example: -# -# describe CodeComparesHelper do -# describe "string concat" do -# it "concats two strings with spaces" do -# expect(helper.concat_strings("this","that")).to eq("this that") -# end -# end -# end -describe CodeComparesHelper do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/helpers/codes_helper_spec.rb b/spec/helpers/codes_helper_spec.rb deleted file mode 100644 index d8f4f96..0000000 --- a/spec/helpers/codes_helper_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -# Specs in this file have access to a helper object that includes -# the CodesHelper. For example: -# -# describe CodesHelper do -# describe "string concat" do -# it "concats two strings with spaces" do -# expect(helper.concat_strings("this","that")).to eq("this that") -# end -# end -# end -describe CodesHelper do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/helpers/game_tasks_helper_spec.rb b/spec/helpers/game_tasks_helper_spec.rb deleted file mode 100644 index b70a9c6..0000000 --- a/spec/helpers/game_tasks_helper_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -# Specs in this file have access to a helper object that includes -# the GameTasksHelper. For example: -# -# describe GameTasksHelper do -# describe "string concat" do -# it "concats two strings with spaces" do -# expect(helper.concat_strings("this","that")).to eq("this that") -# end -# end -# end -describe GameTasksHelper do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/helpers/games_helper_spec.rb b/spec/helpers/games_helper_spec.rb deleted file mode 100644 index 026e01b..0000000 --- a/spec/helpers/games_helper_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -# Specs in this file have access to a helper object that includes -# the GamesHelper. For example: -# -# describe GamesHelper do -# describe "string concat" do -# it "concats two strings with spaces" do -# expect(helper.concat_strings("this","that")).to eq("this that") -# end -# end -# end -describe GamesHelper do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/helpers/hints_helper_spec.rb b/spec/helpers/hints_helper_spec.rb deleted file mode 100644 index 903f975..0000000 --- a/spec/helpers/hints_helper_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -# Specs in this file have access to a helper object that includes -# the HintsHelper. For example: -# -# describe HintsHelper do -# describe "string concat" do -# it "concats two strings with spaces" do -# expect(helper.concat_strings("this","that")).to eq("this that") -# end -# end -# end -describe HintsHelper do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/helpers/home_helper_spec.rb b/spec/helpers/home_helper_spec.rb deleted file mode 100644 index f529425..0000000 --- a/spec/helpers/home_helper_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -# Specs in this file have access to a helper object that includes -# the HomeHelper. For example: -# -# describe HomeHelper do -# describe "string concat" do -# it "concats two strings with spaces" do -# expect(helper.concat_strings("this","that")).to eq("this that") -# end -# end -# end -describe HomeHelper do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/helpers/task_codes_helper_spec.rb b/spec/helpers/task_codes_helper_spec.rb deleted file mode 100644 index b5bd269..0000000 --- a/spec/helpers/task_codes_helper_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -# Specs in this file have access to a helper object that includes -# the TaskCodesHelper. For example: -# -# describe TaskCodesHelper do -# describe "string concat" do -# it "concats two strings with spaces" do -# expect(helper.concat_strings("this","that")).to eq("this that") -# end -# end -# end -describe TaskCodesHelper do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/helpers/task_hints_helper_spec.rb b/spec/helpers/task_hints_helper_spec.rb deleted file mode 100644 index 73c21fc..0000000 --- a/spec/helpers/task_hints_helper_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -# Specs in this file have access to a helper object that includes -# the TaskHintsHelper. For example: -# -# describe TaskHintsHelper do -# describe "string concat" do -# it "concats two strings with spaces" do -# expect(helper.concat_strings("this","that")).to eq("this that") -# end -# end -# end -describe TaskHintsHelper do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/helpers/tasks_helper_spec.rb b/spec/helpers/tasks_helper_spec.rb deleted file mode 100644 index c459188..0000000 --- a/spec/helpers/tasks_helper_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -# Specs in this file have access to a helper object that includes -# the TasksHelper. For example: -# -# describe TasksHelper do -# describe "string concat" do -# it "concats two strings with spaces" do -# expect(helper.concat_strings("this","that")).to eq("this that") -# end -# end -# end -describe TasksHelper do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/helpers/user_games_helper_spec.rb b/spec/helpers/user_games_helper_spec.rb deleted file mode 100644 index 2069d0d..0000000 --- a/spec/helpers/user_games_helper_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -# Specs in this file have access to a helper object that includes -# the UserGamesHelper. For example: -# -# describe UserGamesHelper do -# describe "string concat" do -# it "concats two strings with spaces" do -# expect(helper.concat_strings("this","that")).to eq("this that") -# end -# end -# end -describe UserGamesHelper do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/helpers/user_hints_helper_spec.rb b/spec/helpers/user_hints_helper_spec.rb deleted file mode 100644 index 87ba482..0000000 --- a/spec/helpers/user_hints_helper_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -# Specs in this file have access to a helper object that includes -# the UserHintsHelper. For example: -# -# describe UserHintsHelper do -# describe "string concat" do -# it "concats two strings with spaces" do -# expect(helper.concat_strings("this","that")).to eq("this that") -# end -# end -# end -describe UserHintsHelper do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/helpers/user_tasks_helper_spec.rb b/spec/helpers/user_tasks_helper_spec.rb deleted file mode 100644 index 50132d8..0000000 --- a/spec/helpers/user_tasks_helper_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -# Specs in this file have access to a helper object that includes -# the UserTasksHelper. For example: -# -# describe UserTasksHelper do -# describe "string concat" do -# it "concats two strings with spaces" do -# expect(helper.concat_strings("this","that")).to eq("this that") -# end -# end -# end -describe UserTasksHelper do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/models/admin_game_spec.rb b/spec/models/admin_game_spec.rb deleted file mode 100644 index 7869230..0000000 --- a/spec/models/admin_game_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'spec_helper' - -describe AdminGame do - -end diff --git a/spec/models/admin_user_spec.rb b/spec/models/admin_user_spec.rb deleted file mode 100644 index 3d15302..0000000 --- a/spec/models/admin_user_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'spec_helper' - -describe AdminUser do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/models/code_compare_spec.rb b/spec/models/code_compare_spec.rb deleted file mode 100644 index 6621939..0000000 --- a/spec/models/code_compare_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'spec_helper' - -describe CodeCompare do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/models/code_spec.rb b/spec/models/code_spec.rb index 9c3cf3f..f34e6af 100644 --- a/spec/models/code_spec.rb +++ b/spec/models/code_spec.rb @@ -1,9 +1,9 @@ require 'spec_helper' describe Code do + let(:code) {create :code} it "codes validation checking" do - code_1 = FactoryGirl.create(:code) - code_1.should be_valid + code.should be_valid end end diff --git a/spec/models/game_spec.rb b/spec/models/game_spec.rb index 7f39972..6a833d9 100644 --- a/spec/models/game_spec.rb +++ b/spec/models/game_spec.rb @@ -2,59 +2,63 @@ describe Game do - before do - @user = FactoryGirl.create(:user) - @game = FactoryGirl.create(:game) - @code = FactoryGirl.create(:code) - @task = FactoryGirl.create(:task) - @user_task = FactoryGirl.create(:user_task, user_id: @user.to_param, task_id: @task.to_param, result: 3) - @task_code = FactoryGirl.create(:task_code, task_id: @task.to_param, code_id: @code.to_param) - @task_game = FactoryGirl.create(:game_task, game_id: @game.to_param, task_id: @task.to_param) - @user_game = FactoryGirl.create(:user_game, user_id: @user.to_param, game_id: @game.to_param, state: 1) - end + let(:user) {create :user} + + let(:game) {create :game} + + let(:code) {create :code} + + let(:task) {create :task} + + let!(:user_task) {create :user_task, user: user, task: task, result: 3} + + let!(:task_code) {create :task_code, task: task, code: code} + + let!(:task_game) {create :game_task, game: game, task: task} + + let!(:user_game) {create :user_game, user: user, game: game, state: 1} it "games validation checking" do - @game.should be_valid + game.should be_valid end describe "create code compare testing" do it "code compare processing" do count = CodeCompare.count - expect(@game.process(@user, @task, @code.code_string) == "Code was matched. Game completed") + expect(game.process(user, task, code.code_string) == "Code was matched. Game completed") expect(CodeCompare.count).to eq(count + 1) end it "creates a new CodeCompare and end the game of current user" do - expect(@game.process(@user, @task, @code.code_string) == "Code was matched. Game completed") - expect(UserGame.find(@user_game.to_param.to_i).state).to eq(-1) + expect(game.process(user, task, code.code_string) == "Code was matched. Game completed") + expect(UserGame.find(user_game.id).state).to eq(-1) end it "creates a new CodeCompare with code mismatched result" do - expect(@game.process(@user, @task, @code.attributes["code_string"] + "egbrhnjtyhjnawfgbh") == "Code or pass was not matched") - expect(UserTask.last.task_id).to eq(@task.to_param.to_i) - expect(UserGame.find(@user_game.to_param.to_i).state).to eq(1) + expect(game.process(user, task, code.code_string + "egbrhnjtyhjnawfgbh") == "Code or pass was not matched") + expect(UserTask.last.task_id).to eq(task.id) + expect(UserGame.find(user_game.id).state).to eq(1) end it "send valid pass with complete of user game" do - expect(@game.process(@user, @task, "1234567890") == "Code was matched. Game completed") - expect(UserGame.find(@user_game.to_param.to_i).state).to eq(-1) + expect(game.process(user, task, "1234567890") == "Code was matched. Game completed") + expect(UserGame.find(user_game.id).state).to eq(-1) end describe "assign next task" do - before(:each) do - @new_task = FactoryGirl.create(:task, task_text: "sdgbhdrhndrbhzsgfr") - @new_code = FactoryGirl.create(:code, code_string: "dfbdfbndfgnhdfgdfh") - @new_user_task = FactoryGirl.build(:user_task, user_id: @user.to_param, task_id: @new_task.to_param) - @new_task_code = FactoryGirl.create(:task_code, task_id: @new_task.to_param, code_id: @new_code.to_param) - @new_task_game = FactoryGirl.create(:game_task, game_id: @game.to_param, task_id: @new_task.to_param) - end + let!(:new_task) {create :task, task_text: "sdgbhdrhndrbhzsgfr"} + let!(:new_code) {create :code, code_string: "dfbdfbndfgnhdfgdfh"} + let(:new_user_task) {build :user_task, user: user, task: new_task} + let!(:new_task_code) {create :task_code, task: new_task, code: new_code} + let!(:new_task_game) {create :game_task, game: game, task: new_task} + it "creates a new CodeCompare and change task" do - expect(@game.process(@user, @task, "1234567890") == "Code was matched. Next task assigned") - expect(UserTask.last.task_id).to eq(@new_task.to_param.to_i) + expect(game.process(user, task, "1234567890") == "Code was matched. Next task assigned") + expect(UserTask.last.task_id).to eq(new_task.id) end - it "code compare processing and change" do - expect(@game.process(@user, @task, @code.attributes["code_string"]) == "Code was matched. Next task assigned") - expect(UserTask.last.task_id).to eq(@new_task.to_param.to_i) + it "code compare processing and change task" do + expect(game.process(user, task, code.code_string) == "Code was matched. Next task assigned") + expect(UserTask.last.task_id).to eq(new_task.id) end end end @@ -62,42 +66,42 @@ describe "Start game" do it "execute action " do expect { - @game.start_game + game.start_game }.to change(UserTask, :count).by(1) end it "check game state equal to CURRENT" do - @game.start_game - expect(Game.find(@game.to_param.to_i).state).to equal(UserGame::CURRENT) + game.start_game + expect(Game.find(game.id).state).to equal(UserGame::CURRENT) end it "check user_game state equal to CURRENT" do - @game.start_game - expect(UserGame.where(user_id: @user.to_param, game_id:@game.to_param.to_i).first.state).to equal(UserGame::CURRENT) + game.start_game + expect(UserGame.where(user_id: user.id, game_id: game.id).first.state).to equal(UserGame::CURRENT) end end describe "Finish game" do - before(:each) do - @task1 = FactoryGirl.create(:task) - @user_task1 = FactoryGirl.create(:user_task, task_id: @task1.to_param, user_id: @user.to_param, result: 3) - end + let(:task1) {create :task} + let!(:user_task1) {create :user_task, user: user, task: task1, result: 3} + #user_task1 = FactoryGirl.create(:user_task, task_id: @task1.to_param, user_id: @user.to_param, result: 3) + it "check game status as equal to COMPLETED" do - @game.finish_game - expect(Game.find(@game.to_param.to_i).state).to eq(UserGame::COMPLETED) + game.finish_game + expect(Game.find(game.id).state).to eq(UserGame::COMPLETED) end it "check user_game status as equal to COMPLETED" do - @game.finish_game - expect(UserGame.where(user_id: @user.to_param, game_id:@game.to_param.to_i).first.state).to eq(UserGame::COMPLETED) + game.finish_game + expect(UserGame.where(user_id: user.id, game_id: game.id).first.state).to eq(UserGame::COMPLETED) end it "check user_game points count as equal to user_tasks results sum " do - @game.finish_game - expect(UserGame.where(user_id: @user.to_param, game_id:@game.to_param.to_i).first.result).to eq(@user_task.result + @user_task1.result) + game.finish_game + expect(UserGame.where(user_id: user.id, game_id: game.id).first.result).to eq(user_task.result + user_task1.result) end end diff --git a/spec/models/game_task_spec.rb b/spec/models/game_task_spec.rb deleted file mode 100644 index ba06088..0000000 --- a/spec/models/game_task_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'spec_helper' - -describe GameTask do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/models/hint_spec.rb b/spec/models/hint_spec.rb index 1313641..66651e9 100644 --- a/spec/models/hint_spec.rb +++ b/spec/models/hint_spec.rb @@ -1,9 +1,11 @@ require 'spec_helper' describe Hint do + + let(:hint) {create :hint} + it "hints validation checking" do - hint_1 = FactoryGirl.create(:hint) - hint_1.should be_valid + hint.should be_valid end end diff --git a/spec/models/task_code_spec.rb b/spec/models/task_code_spec.rb deleted file mode 100644 index ef856c4..0000000 --- a/spec/models/task_code_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'spec_helper' - -describe TaskCode do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/models/task_hint_spec.rb b/spec/models/task_hint_spec.rb deleted file mode 100644 index 9b09aa1..0000000 --- a/spec/models/task_hint_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'spec_helper' - -describe TaskHint do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/models/task_spec.rb b/spec/models/task_spec.rb index deb6f3d..c8a7bb0 100644 --- a/spec/models/task_spec.rb +++ b/spec/models/task_spec.rb @@ -2,57 +2,64 @@ describe Task do - before do - @game = FactoryGirl.create(:game) - @task = FactoryGirl.create(:task) - @hint = FactoryGirl.create(:hint) - @user = FactoryGirl.create(:user) - @user_task = FactoryGirl.create(:user_task, user_id: @user.to_param, task_id: @task.to_param) - @task_hint = FactoryGirl.create(:task_hint, task_id: @task.to_param, hint_id: @hint.to_param) - @hint2 = FactoryGirl.create(:hint, hint_text: " wvgrawrfswf", queue_number: 2) - @task_hint2 = FactoryGirl.create(:task_hint, task_id: @task.to_param, hint_id: @hint2.to_param) - end + let(:game) {create :game} + + let(:task) {create :task} + + let(:hint) {create :hint} + + let(:user) {create :user} + + let!(:user_task) {create :user_task, user: user, task: task} + + let!(:task_hint) {create :task_hint, task: task, hint: hint} + + let(:hint2) {create :hint, hint_text: " wvgrawrfswf", queue_number: 2} + + let!(:task_hint2) {create :task_hint, task: task, hint: hint2} it "games validation checking" do - @task.should be_valid + task.should be_valid end describe "raise hint" do it "execute action " do expect { - @task.raise_hint + task.raise_hint }.to change(UserHint, :count).by(1) end it "raises first hint" do expect { - @task.raise_hint + task.raise_hint }.to change(UserHint, :count).by(1) - expect(UserHint.last.hint_id).to eq(@hint.to_param.to_i) + expect(UserHint.last.hint_id).to eq(hint.id) expect(UserHint.count).to eq(1) end describe "hints count > 1" do + let!(:user_hint) {create :user_hint, user: user, hint: hint} before(:each) do - Hint.find(@hint.to_param.to_i).update_attribute(:raised, Hint::RAISED) - @user_hint = FactoryGirl.create(:user_hint, user_id: @user.to_param, hint_id: @hint.to_param) + Hint.find(hint.id).update_attribute(:raised, Hint::RAISED) end it "raises second hint" do expect { - @task.raise_hint + task.raise_hint }.to change(UserHint, :count).by(1) - expect(UserHint.last.hint_id).to eq(@hint2.to_param.to_i) + expect(UserHint.last.hint_id).to eq(hint2.id) expect(UserHint.count).to eq(2) end - it "doesn't raise third hint" do - Hint.find(@hint2.to_param.to_i).update_attribute(:raised, Hint::RAISED) - @user_hint2 = FactoryGirl.create(:user_hint, user_id: @user.to_param, hint_id: @hint2.to_param) - expect { - @task.raise_hint - }.to change(UserHint, :count).by(0) - expect(UserHint.last.hint_id).to eq(@hint2.to_param.to_i) - expect(UserHint.count).to eq(2) + describe "third hint" do + let!(:user_hint2) {create :user_hint, user: user, hint: hint2} + it "doesn't raise third hint" do + Hint.find(hint2.id).update_attribute(:raised, Hint::RAISED) + expect { + task.raise_hint + }.to change(UserHint, :count).by(0) + expect(UserHint.last.hint_id).to eq(hint2.id) + expect(UserHint.count).to eq(2) + end end end end diff --git a/spec/models/user_game_spec.rb b/spec/models/user_game_spec.rb deleted file mode 100644 index 9e972c4..0000000 --- a/spec/models/user_game_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'spec_helper' - -describe UserGame do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/models/user_hint_spec.rb b/spec/models/user_hint_spec.rb deleted file mode 100644 index 64b6d6c..0000000 --- a/spec/models/user_hint_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'spec_helper' - -describe UserHint do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/models/user_task_spec.rb b/spec/models/user_task_spec.rb deleted file mode 100644 index aed2c9c..0000000 --- a/spec/models/user_task_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'spec_helper' - -describe UserTask do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/routing/admin_games_routing_spec.rb b/spec/routing/admin_games_routing_spec.rb index ba36dea..9062246 100644 --- a/spec/routing/admin_games_routing_spec.rb +++ b/spec/routing/admin_games_routing_spec.rb @@ -1,17 +1,16 @@ require "spec_helper" describe AdminGamesController do - before do - @game = FactoryGirl.create(:game) - @admin_game = FactoryGirl.create(:admin_game, admin_id: 1, game_id: @game.to_param) - end + let(:admin) {create :admin_user, email: "admin#{rand(1000)}@example.com"} + let(:game) {create :game} + let!(:admin_game) {create :admin_game, admin_user: admin, game: game} describe "routing" do it "routes to #create" do expect(:post => "/admin_games").to route_to("admin_games#create") end it "routes to #destroy" do - expect(:delete => "/admin_games/#{@admin_game.to_param}").to route_to("admin_games#destroy", :id => "#{@admin_game.to_param}") + expect(:delete => "/admin_games/#{admin_game.id}").to route_to("admin_games#destroy", :id => "#{admin_game.id}") end end diff --git a/spec/routing/code_compares_routing_spec.rb b/spec/routing/code_compares_routing_spec.rb index a560b04..0ca2a53 100644 --- a/spec/routing/code_compares_routing_spec.rb +++ b/spec/routing/code_compares_routing_spec.rb @@ -1,6 +1,13 @@ require "spec_helper" describe CodeComparesController do + + let(:user) {create :user} + + let(:code) {create :code} + + let!(:code_compare) {create :code_compare, user: user, code: code} + describe "routing" do it "routes to #create" do @@ -8,7 +15,7 @@ end it "routes to #destroy" do - expect(:delete => "/code_compares/1").to route_to("code_compares#destroy", :id => "1") + expect(:delete => "/code_compares/#{code_compare.id}").to route_to("code_compares#destroy", :id => "#{code_compare.id}") end end diff --git a/spec/routing/codes_routing_spec.rb b/spec/routing/codes_routing_spec.rb index 0d51ea6..6a3fcc9 100644 --- a/spec/routing/codes_routing_spec.rb +++ b/spec/routing/codes_routing_spec.rb @@ -1,6 +1,7 @@ require "spec_helper" describe CodesController do + let!(:code) {create :code} describe "routing" do it "routes to #index" do @@ -12,11 +13,11 @@ end it "routes to #show" do - expect(:get => "/codes/1").to route_to("codes#show", :id => "1") + expect(:get => "/codes/#{code.id}").to route_to("codes#show", :id => "#{code.id}") end it "routes to #edit" do - expect(:get => "/codes/1/edit").to route_to("codes#edit", :id => "1") + expect(:get => "/codes/#{code.id}/edit").to route_to("codes#edit", :id => "#{code.id}") end it "routes to #create" do @@ -24,11 +25,11 @@ end it "routes to #update" do - expect(:put => "/codes/1").to route_to("codes#update", :id => "1") + expect(:put => "/codes/#{code.id}").to route_to("codes#update", :id => "#{code.id}") end it "routes to #destroy" do - expect(:delete => "/codes/1").to route_to("codes#destroy", :id => "1") + expect(:delete => "/codes/#{code.id}").to route_to("codes#destroy", :id => "#{code.id}") end end diff --git a/spec/routing/game_tasks_routing_spec.rb b/spec/routing/game_tasks_routing_spec.rb index 387e5e5..7d61436 100644 --- a/spec/routing/game_tasks_routing_spec.rb +++ b/spec/routing/game_tasks_routing_spec.rb @@ -1,6 +1,13 @@ require "spec_helper" describe GameTasksController do + + let(:game) {create :game} + + let(:task) {create :task} + + let!(:game_task) {create :game_task, game: game, task: task} + describe "routing" do it "routes to #create" do @@ -8,7 +15,7 @@ end it "routes to #destroy" do - expect(:delete => "/game_tasks/1").to route_to("game_tasks#destroy", :id => "1") + expect(:delete => "/game_tasks/#{game_task.id}").to route_to("game_tasks#destroy", :id => "#{game_task.id}") end end diff --git a/spec/routing/games_routing_spec.rb b/spec/routing/games_routing_spec.rb index cd074c0..0e04726 100644 --- a/spec/routing/games_routing_spec.rb +++ b/spec/routing/games_routing_spec.rb @@ -1,6 +1,9 @@ require "spec_helper" describe GamesController do + + let!(:game) {create :game} + describe "routing" do it "routes to #index" do @@ -12,11 +15,11 @@ end it "routes to #show" do - expect(:get => "/games/1").to route_to("games#show", :id => "1") + expect(:get => "/games/#{game.id}").to route_to("games#show", :id => "#{game.id}") end it "routes to #edit" do - expect(:get => "/games/1/edit").to route_to("games#edit", :id => "1") + expect(:get => "/games/#{game.id}/edit").to route_to("games#edit", :id => "#{game.id}") end it "routes to #create" do @@ -24,11 +27,11 @@ end it "routes to #update" do - expect(:put => "/games/1").to route_to("games#update", :id => "1") + expect(:put => "/games/#{game.id}").to route_to("games#update", :id => "#{game.id}") end it "routes to #destroy" do - expect(:delete => "/games/1").to route_to("games#destroy", :id => "1") + expect(:delete => "/games/#{game.id}").to route_to("games#destroy", :id => "#{game.id}") end end diff --git a/spec/routing/hints_routing_spec.rb b/spec/routing/hints_routing_spec.rb index 3d6dbdf..2802626 100644 --- a/spec/routing/hints_routing_spec.rb +++ b/spec/routing/hints_routing_spec.rb @@ -1,12 +1,7 @@ require "spec_helper" describe HintsController do - before do - queue_number = rand(1...2) - @game = FactoryGirl.create(:game) - @hint = FactoryGirl.create(:hint, queue_number: queue_number) - @invalid_attributes = FactoryGirl.build(:hint, hint_text: "").attributes - end + let!(:hint) {create :hint} describe "routing" do it "routes to #index" do @@ -18,11 +13,11 @@ end it "routes to #show" do - expect(:get => "/hints/1").to route_to("hints#show", :id => "1") + expect(:get => "/hints/#{hint.id}").to route_to("hints#show", :id => "#{hint.id}") end it "routes to #edit" do - expect(:get => "/hints/1/edit").to route_to("hints#edit", :id => "1") + expect(:get => "/hints/#{hint.id}/edit").to route_to("hints#edit", :id => "#{hint.id}") end it "routes to #create" do @@ -30,11 +25,11 @@ end it "routes to #update" do - expect(:put => "/hints/1").to route_to("hints#update", :id => "1") + expect(:put => "/hints/#{hint.id}").to route_to("hints#update", :id => "#{hint.id}") end it "routes to #destroy" do - expect(:delete => "/hints/1").to route_to("hints#destroy", :id => "1") + expect(:delete => "/hints/#{hint.id}").to route_to("hints#destroy", :id => "#{hint.id}") end end diff --git a/spec/routing/task_codes_routing_spec.rb b/spec/routing/task_codes_routing_spec.rb index 2b7462b..a02f47e 100644 --- a/spec/routing/task_codes_routing_spec.rb +++ b/spec/routing/task_codes_routing_spec.rb @@ -1,6 +1,13 @@ require "spec_helper" describe TaskCodesController do + + let(:task) {create :task} + + let(:code) {create :code} + + let!(:task_code) {create :task_code, task: task, code: code} + describe "routing" do it "routes to #create" do @@ -8,7 +15,7 @@ end it "routes to #destroy" do - expect(:delete => "/task_codes/1").to route_to("task_codes#destroy", :id => "1") + expect(:delete => "/task_codes/#{task_code.id}").to route_to("task_codes#destroy", :id => "#{task_code.id}") end end diff --git a/spec/routing/task_hints_routing_spec.rb b/spec/routing/task_hints_routing_spec.rb index 2ed04ea..85f1c58 100644 --- a/spec/routing/task_hints_routing_spec.rb +++ b/spec/routing/task_hints_routing_spec.rb @@ -1,6 +1,13 @@ require "spec_helper" describe TaskHintsController do + + let(:task) {create :task} + + let(:hint) {create :hint} + + let!(:task_hint) {create :task_hint, task: task, hint: hint} + describe "routing" do it "routes to #create" do @@ -8,7 +15,7 @@ end it "routes to #destroy" do - expect(:delete => "/task_hints/1").to route_to("task_hints#destroy", :id => "1") + expect(:delete => "/task_hints/#{task_hint.id}").to route_to("task_hints#destroy", :id => "#{task_hint.id}") end end diff --git a/spec/routing/tasks_routing_spec.rb b/spec/routing/tasks_routing_spec.rb index 808d05c..55329cb 100644 --- a/spec/routing/tasks_routing_spec.rb +++ b/spec/routing/tasks_routing_spec.rb @@ -1,6 +1,9 @@ require "spec_helper" describe TasksController do + + let!(:task) {create :task} + describe "routing" do it "routes to #index" do @@ -12,11 +15,11 @@ end it "routes to #show" do - expect(:get => "/tasks/1").to route_to("tasks#show", :id => "1") + expect(:get => "/tasks/#{task.id}").to route_to("tasks#show", :id => "#{task.id}") end it "routes to #edit" do - expect(:get => "/tasks/1/edit").to route_to("tasks#edit", :id => "1") + expect(:get => "/tasks/#{task.id}/edit").to route_to("tasks#edit", :id => "#{task.id}") end it "routes to #create" do @@ -24,11 +27,11 @@ end it "routes to #update" do - expect(:put => "/tasks/1").to route_to("tasks#update", :id => "1") + expect(:put => "/tasks/#{task.id}").to route_to("tasks#update", :id => "#{task.id}") end it "routes to #destroy" do - expect(:delete => "/tasks/1").to route_to("tasks#destroy", :id => "1") + expect(:delete => "/tasks/#{task.id}").to route_to("tasks#destroy", :id => "#{task.id}") end end diff --git a/spec/routing/user_games_routing_spec.rb b/spec/routing/user_games_routing_spec.rb index d271811..a15275e 100644 --- a/spec/routing/user_games_routing_spec.rb +++ b/spec/routing/user_games_routing_spec.rb @@ -1,6 +1,13 @@ require "spec_helper" describe UserGamesController do + + let(:user) {create :user} + + let(:game) {create :game} + + let!(:user_game) {create :user_game, user: user, game: game} + describe "routing" do it "routes to #index" do @@ -12,11 +19,11 @@ end it "routes to #show" do - expect(:get => "/user_games/1").to route_to("user_games#show", :id => "1") + expect(:get => "/user_games/#{user_game.id}").to route_to("user_games#show", :id => "#{user_game.id}") end it "routes to #edit" do - expect(:get => "/user_games/1/edit").to route_to("user_games#edit", :id => "1") + expect(:get => "/user_games/#{user_game.id}/edit").to route_to("user_games#edit", :id => "#{user_game.id}") end it "routes to #create" do @@ -24,11 +31,11 @@ end it "routes to #update" do - expect(:put => "/user_games/1").to route_to("user_games#update", :id => "1") + expect(:put => "/user_games/#{user_game.id}").to route_to("user_games#update", :id => "#{user_game.id}") end it "routes to #destroy" do - expect(:delete => "/user_games/1").to route_to("user_games#destroy", :id => "1") + expect(:delete => "/user_games/#{user_game.id}").to route_to("user_games#destroy", :id => "#{user_game.id}") end end diff --git a/spec/routing/user_hints_routing_spec.rb b/spec/routing/user_hints_routing_spec.rb index f643ddc..8b3b74e 100644 --- a/spec/routing/user_hints_routing_spec.rb +++ b/spec/routing/user_hints_routing_spec.rb @@ -1,6 +1,13 @@ require "spec_helper" describe UserHintsController do + + let(:user) {create :user} + + let(:hint) {create :hint} + + let!(:user_hint) {create :user_hint, user: user, hint: hint} + describe "routing" do it "routes to #create" do @@ -8,7 +15,7 @@ end it "routes to #destroy" do - expect(:delete => "/user_hints/1").to route_to("user_hints#destroy", :id => "1") + expect(:delete => "/user_hints/#{user_hint.id}").to route_to("user_hints#destroy", :id => "#{user_hint.id}") end end diff --git a/spec/routing/user_tasks_routing_spec.rb b/spec/routing/user_tasks_routing_spec.rb index 9e014e8..013931a 100644 --- a/spec/routing/user_tasks_routing_spec.rb +++ b/spec/routing/user_tasks_routing_spec.rb @@ -1,6 +1,13 @@ require "spec_helper" describe UserTasksController do + + let(:user) {create :user} + + let(:task) {create :task} + + let!(:user_task) {create :user_task, user: user, task: task} + describe "routing" do it "routes to #create" do @@ -8,7 +15,7 @@ end it "routes to #destroy" do - expect(:delete => "/user_tasks/1").to route_to("user_tasks#destroy", :id => "1") + expect(:delete => "/user_tasks/#{user_task.id}").to route_to("user_tasks#destroy", :id => "#{user_task.id}") end end diff --git a/spec/support/controller_macros.rb b/spec/support/controller_macros.rb index 23f1171..d0d6788 100644 --- a/spec/support/controller_macros.rb +++ b/spec/support/controller_macros.rb @@ -1,10 +1,10 @@ module ControllerMacros def login_admin + let!(:admin) {create :admin_user, email: "admin#{rand(1000)}@example.com"} before(:each) do @request.env["devise.mapping"] = Devise.mappings[:admin] - @admin = FactoryGirl.create(:admin_user, email: "admin#{rand(1000)}@example.com") + @admin = admin sign_in @admin - @admin_id = @admin.id end after(:each) do AdminUser.find(@admin.id).destroy @@ -12,13 +12,13 @@ def login_admin end def login_user + let!(:user) {create :user} before(:each) do - @request.env["devise.mapping"] = Devise.mappings[:user] - @user = FactoryGirl.create(:user) + @user = user sign_in @user end after(:each) do - User.find(@user).destroy + #User.find(@user).destroy end end end \ No newline at end of file diff --git a/spec/views/codes/edit.html.slim_spec.rb b/spec/views/codes/edit.html.slim_spec.rb deleted file mode 100644 index 99f10e4..0000000 --- a/spec/views/codes/edit.html.slim_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -describe "codes/edit" do - before(:each) do - @code = assign(:code, stub_model(Code)) - end - - it "renders the edit code form" do - render - - # Run the generator again with the --webrat flag if you want to use webrat matchers - assert_select "form[action=?][method=?]", code_path(@code), "post" do - end - end -end diff --git a/spec/views/codes/index.html.slim_spec.rb b/spec/views/codes/index.html.slim_spec.rb deleted file mode 100644 index 2e2f75e..0000000 --- a/spec/views/codes/index.html.slim_spec.rb +++ /dev/null @@ -1,18 +0,0 @@ -require 'spec_helper' - -describe "codes/index" do - before do - code1 = FactoryGirl.create(:code) - code2 = FactoryGirl.create(:code, :code_string => "game_test_title") - assign(:codes, [ - code1, code2 - #stub_model Game, :id => game2.to_param - ]) - end - - - it "renders a list of codes" do - render - # Run the generator again with the --webrat flag if you want to use webrat matchers - end -end diff --git a/spec/views/codes/new.html.slim_spec.rb b/spec/views/codes/new.html.slim_spec.rb deleted file mode 100644 index 8dbded1..0000000 --- a/spec/views/codes/new.html.slim_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -describe "codes/new" do - before(:each) do - assign(:code, stub_model(Code).as_new_record) - end - - it "renders new code form" do - render - - # Run the generator again with the --webrat flag if you want to use webrat matchers - assert_select "form[action=?][method=?]", codes_path, "post" do - end - end -end diff --git a/spec/views/codes/show.html.slim_spec.rb b/spec/views/codes/show.html.slim_spec.rb deleted file mode 100644 index 90afc7b..0000000 --- a/spec/views/codes/show.html.slim_spec.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'spec_helper' - -describe "codes/show" do - before(:each) do - @code = assign(:code, stub_model(Code)) - end - - it "renders attributes in

" do - render - # Run the generator again with the --webrat flag if you want to use webrat matchers - end -end diff --git a/spec/views/games/edit.html.slim_spec.rb b/spec/views/games/edit.html.slim_spec.rb deleted file mode 100644 index 430be80..0000000 --- a/spec/views/games/edit.html.slim_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -describe "games/edit" do - before(:each) do - @game = assign(:game, stub_model(Game)) - end - - it "renders the edit game form" do - render - - # Run the generator again with the --webrat flag if you want to use webrat matchers - assert_select "form[action=?][method=?]", game_path(@game), "post" do - end - end -end diff --git a/spec/views/games/index.html.slim_spec.rb b/spec/views/games/index.html.slim_spec.rb deleted file mode 100644 index 8bc4591..0000000 --- a/spec/views/games/index.html.slim_spec.rb +++ /dev/null @@ -1,17 +0,0 @@ -require 'spec_helper' - -describe "games/index" do - before do - game1 = FactoryGirl.create(:game) - game2 = FactoryGirl.create(:game, :title => "game_test_title") - assign(:games, [ - game1, game2 - #stub_model Game, :id => game2.to_param - ]) - end - - it "renders a list of games" do - render - # Run the generator again with the --webrat flag if you want to use webrat matchers - end -end diff --git a/spec/views/games/new.html.slim_spec.rb b/spec/views/games/new.html.slim_spec.rb deleted file mode 100644 index 5b20b01..0000000 --- a/spec/views/games/new.html.slim_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -describe "games/new" do - before(:each) do - assign(:game, stub_model(Game).as_new_record) - end - - it "renders new game form" do - render - - # Run the generator again with the --webrat flag if you want to use webrat matchers - assert_select "form[action=?][method=?]", games_path, "post" do - end - end -end diff --git a/spec/views/games/show.html.slim_spec.rb b/spec/views/games/show.html.slim_spec.rb deleted file mode 100644 index a14322b..0000000 --- a/spec/views/games/show.html.slim_spec.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'spec_helper' - -describe "games/show" do - before(:each) do - @game = assign(:game, stub_model(Game)) - end - - it "renders attributes in

" do - render - # Run the generator again with the --webrat flag if you want to use webrat matchers - end -end diff --git a/spec/views/hints/edit.html.slim_spec.rb b/spec/views/hints/edit.html.slim_spec.rb deleted file mode 100644 index b4b2f95..0000000 --- a/spec/views/hints/edit.html.slim_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -describe "hints/edit" do - before(:each) do - @hint = assign(:hint, stub_model(Hint)) - end - - it "renders the edit hint form" do - render - - # Run the generator again with the --webrat flag if you want to use webrat matchers - assert_select "form[action=?][method=?]", hint_path(@hint), "post" do - end - end -end diff --git a/spec/views/hints/index.html.slim_spec.rb b/spec/views/hints/index.html.slim_spec.rb deleted file mode 100644 index 9da6012..0000000 --- a/spec/views/hints/index.html.slim_spec.rb +++ /dev/null @@ -1,17 +0,0 @@ -require 'spec_helper' - -describe "hints/index" do - before do - hint1 = FactoryGirl.create(:hint) - hint2 = FactoryGirl.create(:hint, :hint_text => "game_hint_text") - assign(:hints, [ - hint1, hint2 - #stub_model Game, :id => game2.to_param - ]) - end - - it "renders a list of hints" do - render - # Run the generator again with the --webrat flag if you want to use webrat matchers - end -end diff --git a/spec/views/hints/new.html.slim_spec.rb b/spec/views/hints/new.html.slim_spec.rb deleted file mode 100644 index 0f7d6b2..0000000 --- a/spec/views/hints/new.html.slim_spec.rb +++ /dev/null @@ -1,16 +0,0 @@ -require 'spec_helper' - -describe "hints/new" do - before(:each) do - assign(:hint, stub_model(Hint).as_new_record) - end - - it "renders new hint form" do - puts 1 - render - - # Run the generator again with the --webrat flag if you want to use webrat matchers - assert_select "form[action=?][method=?]", hints_path, "post" do - end - end -end diff --git a/spec/views/hints/show.html.slim_spec.rb b/spec/views/hints/show.html.slim_spec.rb deleted file mode 100644 index 0f26c39..0000000 --- a/spec/views/hints/show.html.slim_spec.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'spec_helper' - -describe "hints/show" do - before(:each) do - @hint = assign(:hint, stub_model(Hint)) - end - - it "renders attributes in

" do - render - # Run the generator again with the --webrat flag if you want to use webrat matchers - end -end diff --git a/spec/views/home/index.html.slim_spec.rb b/spec/views/home/index.html.slim_spec.rb deleted file mode 100644 index 6a21b7a..0000000 --- a/spec/views/home/index.html.slim_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'spec_helper' - -describe "home/index.html.slim" do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/views/tasks/edit.html.slim_spec.rb b/spec/views/tasks/edit.html.slim_spec.rb deleted file mode 100644 index 77d6935..0000000 --- a/spec/views/tasks/edit.html.slim_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -describe "tasks/edit" do - before(:each) do - @task = assign(:task, stub_model(Task)) - end - - it "renders the edit task form" do - render - - # Run the generator again with the --webrat flag if you want to use webrat matchers - assert_select "form[action=?][method=?]", task_path(@task), "post" do - end - end -end diff --git a/spec/views/tasks/index.html.slim_spec.rb b/spec/views/tasks/index.html.slim_spec.rb deleted file mode 100644 index 53e529a..0000000 --- a/spec/views/tasks/index.html.slim_spec.rb +++ /dev/null @@ -1,17 +0,0 @@ -require 'spec_helper' - -describe "tasks/index" do - before do - task1 = FactoryGirl.create(:task) - task2 = FactoryGirl.create(:task, :task_text => "fzvbsdfgvsdfgvsfd") - assign(:tasks, [ - task1, task2 - #stub_model Game, :id => game2.to_param - ]) - end - - it "renders a list of tasks" do - render - # Run the generator again with the --webrat flag if you want to use webrat matchers - end -end diff --git a/spec/views/tasks/new.html.slim_spec.rb b/spec/views/tasks/new.html.slim_spec.rb deleted file mode 100644 index 3b82589..0000000 --- a/spec/views/tasks/new.html.slim_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -describe "tasks/new" do - before(:each) do - assign(:task, stub_model(Task).as_new_record) - end - - it "renders new task form" do - render - - # Run the generator again with the --webrat flag if you want to use webrat matchers - assert_select "form[action=?][method=?]", tasks_path, "post" do - end - end -end diff --git a/spec/views/tasks/show.html.slim_spec.rb b/spec/views/tasks/show.html.slim_spec.rb deleted file mode 100644 index d55f204..0000000 --- a/spec/views/tasks/show.html.slim_spec.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'spec_helper' - -describe "tasks/show" do - before(:each) do - @task = assign(:task, stub_model(Task)) - end - - it "renders attributes in

" do - render - # Run the generator again with the --webrat flag if you want to use webrat matchers - end -end diff --git a/spec/views/user_games/edit.html.slim_spec.rb b/spec/views/user_games/edit.html.slim_spec.rb deleted file mode 100644 index 1ddb539..0000000 --- a/spec/views/user_games/edit.html.slim_spec.rb +++ /dev/null @@ -1,20 +0,0 @@ -require 'spec_helper' - -describe "user_games/edit" do - before(:each) do - @user_game = assign(:user_game, stub_model(UserGame, - :user_id => 1, - :game_id => 1 - )) - end - - it "renders the edit user_game form" do - render - - # Run the generator again with the --webrat flag if you want to use webrat matchers - assert_select "form[action=?][method=?]", user_game_path(@user_game), "post" do - assert_select "input#user_game_user_id[name=?]", "user_game[user_id]" - assert_select "input#user_game_game_id[name=?]", "user_game[game_id]" - end - end -end diff --git a/spec/views/user_games/index.html.slim_spec.rb b/spec/views/user_games/index.html.slim_spec.rb deleted file mode 100644 index c0da63d..0000000 --- a/spec/views/user_games/index.html.slim_spec.rb +++ /dev/null @@ -1,18 +0,0 @@ -require 'spec_helper' - -describe "user_games/index" do - before(:each) do - game1 = FactoryGirl.create(:game) - game2 = FactoryGirl.create(:game, title: "dsgsdfgsdfgv") - assign(:games, [ - game1, game2 - ]) - end - - it "renders a list of user_games" do - render - # Run the generator again with the --webrat flag if you want to use webrat matchers - assert_select "tr>td", :text => "new_game", :count => 1 - assert_select "tr>td", :text => "new_game", :count => 1 - end -end diff --git a/spec/views/user_games/new.html.slim_spec.rb b/spec/views/user_games/new.html.slim_spec.rb deleted file mode 100644 index ecfdbd9..0000000 --- a/spec/views/user_games/new.html.slim_spec.rb +++ /dev/null @@ -1,20 +0,0 @@ -require 'spec_helper' - -describe "user_games/new" do - before(:each) do - assign(:user_game, stub_model(UserGame, - :user_id => 1, - :game_id => 1 - ).as_new_record) - end - - it "renders new user_game form" do - render - - # Run the generator again with the --webrat flag if you want to use webrat matchers - assert_select "form[action=?][method=?]", user_games_path, "post" do - assert_select "input#user_game_user_id[name=?]", "user_game[user_id]" - assert_select "input#user_game_game_id[name=?]", "user_game[game_id]" - end - end -end diff --git a/spec/views/user_games/show.html.slim_spec.rb b/spec/views/user_games/show.html.slim_spec.rb deleted file mode 100644 index b12a41b..0000000 --- a/spec/views/user_games/show.html.slim_spec.rb +++ /dev/null @@ -1,17 +0,0 @@ -require 'spec_helper' - -describe "user_games/show" do - before(:each) do - @user_game = assign(:user_game, stub_model(UserGame, - :user_id => 1, - :game_id => 2 - )) - end - - it "renders attributes in

" do - render - # Run the generator again with the --webrat flag if you want to use webrat matchers - expect(rendered).to match(/1/) - expect(rendered).to match(/2/) - end -end From 6cf83f4ed4cb448013785537ab5f1a881157cdb4 Mon Sep 17 00:00:00 2001 From: sorgal Date: Tue, 21 Jan 2014 23:19:18 +0400 Subject: [PATCH 09/15] 123 --- .idea/workspace.xml | 102 +++++++++++++++++--------------------------- 1 file changed, 38 insertions(+), 64 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 44869fb..c4683c4 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -23,10 +23,10 @@ - - + + - + @@ -71,7 +71,6 @@ @@ -128,7 +128,6 @@ - @@ -313,6 +312,7 @@ + @@ -533,7 +533,7 @@ - + @@ -541,7 +541,7 @@ - + @@ -573,9 +573,9 @@ - - + + @@ -595,6 +595,13 @@ + + + + + + + @@ -695,11 +702,6 @@ - - - - - @@ -722,9 +724,7 @@ - - - + @@ -749,23 +749,17 @@ - - - + - - - + - - - + @@ -800,84 +794,64 @@ - - - + - - - + - - - + - - - + - - - + - - - + - - - + - - - + - + - - - + - + - - - + - + - + - + - + From b94893f26f3415752cb4e1bb5359a79d9698414c Mon Sep 17 00:00:00 2001 From: sorgal Date: Wed, 22 Jan 2014 17:59:38 +0400 Subject: [PATCH 10/15] create app prototype --- .idea/workspace.xml | 397 ++++++++++-------- app/controllers/codes_controller.rb | 1 + app/controllers/games_controller.rb | 23 +- app/controllers/hints_controller.rb | 6 +- app/controllers/home_controller.rb | 2 +- app/controllers/tasks_controller.rb | 1 + app/controllers/user_games_controller.rb | 1 - app/models/game.rb | 32 +- app/views/codes/_form.html.slim | 2 +- app/views/games/_code_compare_form.html.slim | 10 +- app/views/games/show.html.slim | 16 +- app/views/hints/_form.html.slim | 2 +- app/views/home/index.html.slim | 2 +- app/views/tasks/_form.html.slim | 1 + .../code_compares_controller_spec.rb | 12 + spec/controllers/games_controller_spec.rb | 256 ++++++----- spec/models/game_spec.rb | 26 ++ 17 files changed, 469 insertions(+), 321 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 32beae5..ad694f0 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -23,37 +23,28 @@ - - + + - + - - + + - + - - + + - - - - - - - - - - + @@ -69,30 +60,15 @@ @@ -155,8 +146,6 @@ - - @@ -185,7 +174,25 @@ + + + + + + + + + @@ -199,11 +206,15 @@ + + @@ -441,7 +512,7 @@ - + @@ -449,12 +520,12 @@ - + - + @@ -473,9 +544,9 @@ - - + + @@ -495,353 +566,319 @@ - + - - - + - + - - - + - + - + - + - - - + - + - + - + - - - + - + - + - + - - - + - + - - - + - + - + - + - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + diff --git a/app/controllers/codes_controller.rb b/app/controllers/codes_controller.rb index 345530a..e4f9ca3 100644 --- a/app/controllers/codes_controller.rb +++ b/app/controllers/codes_controller.rb @@ -17,6 +17,7 @@ def show # GET /codes/new def new + @task = params[:task].to_i @code = Code.new end diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index c52ad6d..58805af 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -1,8 +1,9 @@ class GamesController < ApplicationController - before_action :set_game, only: [:show, :edit, :update, :destroy, :start_game, :check_game_status, :finish_game] + before_action :set_game, only: [:show, :edit, :update, :destroy, :start_game, :check_game_status, :finish_game, :check_task] skip_before_filter :authorize_admin, only: [:index, :show] - before_filter :authenticate_user!, only: [:show] + before_filter :authenticate_admin_or_user, only: [:show] before_filter :check_game_status, only: [:show] + before_filter :check_tasks, only: [:start_game] # GET /games # GET /games.json def index @@ -17,8 +18,10 @@ def show @status = "Wait" elsif @game.state > 0 @status = "Started" - @task = current_user.tasks.last - @task_codes_count = current_user.tasks.codes.where(task_id: @user_task.task_id).count + @task = current_user.tasks.first + @arr = @game.get_task_codes_count(@task, current_user) + @code_compares_count = @arr[0] + @task_codes_count = @arr[1] elsif @game.state < 0 @status = "Finished" end @@ -108,4 +111,16 @@ def check_game_status end end + def authenticate_admin_or_user + unless user_signed_in? || current_admin_user + redirect_to root_path + end + end + + def check_tasks + if @game.tasks.count == 0 + redirect_to game_path(@game.id), notice: "You can't start game without tasks" + end + end + end diff --git a/app/controllers/hints_controller.rb b/app/controllers/hints_controller.rb index aabc419..9893b70 100644 --- a/app/controllers/hints_controller.rb +++ b/app/controllers/hints_controller.rb @@ -17,9 +17,9 @@ def show # GET /hints/new def new - task = params[:task].to_i + @task = params[:task].to_i @cant_add = false - if TaskHint.where(task_id: task).count == 2 + if TaskHint.where(task_id: @task).count == 2 @cant_add = true end @hint = Hint.new @@ -100,7 +100,7 @@ def check_task_create if @task.hints.count > 0 if (@task.hints.count == 1) if @task.hints.first.queue_number == params.require(:hint)[:queue_number].to_i - redirect_to new_hint_path, notice: "Parameter queue_number must be equal to " + (3 - params.require(:hint)[:queue_number].to_i).to_s + redirect_to new_hint_path(task: @task.id), notice: "Parameter Queue number must be equal to " + (3 - params.require(:hint)[:queue_number].to_i).to_s end elsif @task.hints.count >= 2 redirect_to tasks_path, notice: "Only two hints can be assigned with one task" diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 7193e8f..cf99f3e 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -3,6 +3,6 @@ class HomeController < ApplicationController before_filter :authenticate_user!, only: [:game_code_compares, :create_code_compare] def index - @games = Game.where(state: 0) + @games = Game.where("`state` >= ?", 0) end end diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index d9ca32f..6cec3ab 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -16,6 +16,7 @@ def show # GET /tasks/new def new + @game = params[:game].to_i @task = Task.new end diff --git a/app/controllers/user_games_controller.rb b/app/controllers/user_games_controller.rb index 3a5a8e2..4c2d4fa 100644 --- a/app/controllers/user_games_controller.rb +++ b/app/controllers/user_games_controller.rb @@ -7,7 +7,6 @@ class UserGamesController < ApplicationController # GET /user_games # GET /user_games.json def index - #@user_games = UserGame.where(""user_id: current_user.id, "`state` >= 0""") @user_games = UserGame.where("`user_id` = ? AND `state` >= ?", current_user.id, 0) @games = [] @user_games.each do |user_game| diff --git a/app/models/game.rb b/app/models/game.rb index 2d8f9ff..8535051 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -14,17 +14,21 @@ def process(user, task, try_text) notice = "Pass was matched. #{next_task(0)}" else if matched_code = @task.codes.where(code_string: try_text).first - new_code_compare = @user.code_compares.build(code: matched_code) - if new_code_compare.save - if all_codes_input? - notice = "Code was matched. #{next_task(@task.points - @user.hints.count)}" + if check_already_inserted_code(matched_code.id) + notice = "Compare for this code created early" + else + new_code_compare = @user.code_compares.build(code: matched_code) + if new_code_compare.save + if all_codes_input? + notice = "Code was matched. #{next_task(@task.points - @user.hints.count)}" + end end end end end - if notice == "" - notice += "Code or pass was not matched" + unless notice + notice = "Code or pass was not matched" end @notice = notice end @@ -90,5 +94,19 @@ def finish_game user_game.update(state: UserGame::COMPLETED, result: points) end end - + + def check_already_inserted_code(code) + if @user.code_compares.where(code_id: code).first + return true + else + return false + end + end + + def get_task_codes_count(task, user) + task_codes = task.codes.pluck(:id) + code_compares = user.code_compares.where('`code_compares`.`code_id` IN (?)', task_codes).count + return [code_compares, task_codes.count - code_compares] + end + end diff --git a/app/views/codes/_form.html.slim b/app/views/codes/_form.html.slim index 8c849e5..a0d549e 100644 --- a/app/views/codes/_form.html.slim +++ b/app/views/codes/_form.html.slim @@ -4,7 +4,7 @@ .controls = f.text_field :code_string, :class => 'text_field' .controls - = f.hidden_field :task, value: @game + = f.hidden_field :task, value: @task .form-actions = f.submit nil, :class => 'btn btn-primary' ' diff --git a/app/views/games/_code_compare_form.html.slim b/app/views/games/_code_compare_form.html.slim index 01e0533..1f41304 100644 --- a/app/views/games/_code_compare_form.html.slim +++ b/app/views/games/_code_compare_form.html.slim @@ -1,7 +1,9 @@ -= form_tag "code_compares/create", method: "post" do |f| += form_tag "/code_compares/", method: "post" .control-group - = f.label "try_text", :class => 'control-label' + = label_tag "try_text", 'Try text', :class => 'control-label' .controls - = f.text_field "try_text", :class => 'text_field' + = text_field_tag "try_text", nil, :class => 'text_field' .controls - = f.hidden_field_tag "task", :value => task.id \ No newline at end of file + = hidden_field_tag "task", :value => @task.id + .contrils + = submit_tag "Try" \ No newline at end of file diff --git a/app/views/games/show.html.slim b/app/views/games/show.html.slim index a4f5cbc..1365ff5 100644 --- a/app/views/games/show.html.slim +++ b/app/views/games/show.html.slim @@ -5,7 +5,7 @@ div class="page-header" strong= model_class.human_attribute_name(:title) + ':' br = @game.title --unless user_signed_in? +-if current_admin_user p strong= model_class.human_attribute_name(:start_date) + ':' br @@ -24,6 +24,14 @@ div class="page-header" = link_to t('.edit', :default => t("helpers.links.edit")), edit_game_path(@game), :class => 'btn' ' = link_to t('.destroy', :default => t("helpers.links.destroy")), game_path(@game), :method => "delete", :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-danger' --else - -@task_codes_count.each do |num| - -render partial: code_compare_form \ No newline at end of file +-elsif user_signed_in? + -if notice + = notice + -if @code_compares_count > 0 + = "Already inserted codes #{@code_compares_count}" + -i = 1 + -while i <= @task_codes_count + = render partial: "code_compare_form" + -i += 1 + + diff --git a/app/views/hints/_form.html.slim b/app/views/hints/_form.html.slim index 5ef2398..e83b1d7 100644 --- a/app/views/hints/_form.html.slim +++ b/app/views/hints/_form.html.slim @@ -8,7 +8,7 @@ .controls = f.text_field :queue_number, :class => 'text_field' .controls - = f.hidden_field :task, value: @game + = f.hidden_field :task, value: @task .form-actions = f.submit nil, :class => 'btn btn-primary' = link_to t('.cancel', :default => t("helpers.links.cancel")), hints_path, :class => 'btn' diff --git a/app/views/home/index.html.slim b/app/views/home/index.html.slim index a11c37d..21100e8 100644 --- a/app/views/home/index.html.slim +++ b/app/views/home/index.html.slim @@ -15,7 +15,7 @@ td= game.start_date - if game.state == 0 td - = link_to "Join", {controller: "user_games", action: "create", user_game: {game_id: game.id}}, :class => 'btn btn-mini' + = button_to "Join", {controller: "user_games", action: "create", game_id: game.id}, :class => 'btn btn-mini' - else div id="main" diff --git a/app/views/tasks/_form.html.slim b/app/views/tasks/_form.html.slim index 6af4282..61d7e31 100644 --- a/app/views/tasks/_form.html.slim +++ b/app/views/tasks/_form.html.slim @@ -7,6 +7,7 @@ = f.label :points, :class => 'control-label' .controls = f.text_field :points, :class => 'text_field' + = f.hidden_field :game, value: @game .form-actions = f.submit nil, :class => 'btn btn-primary' ' diff --git a/spec/controllers/code_compares_controller_spec.rb b/spec/controllers/code_compares_controller_spec.rb index 7f5da3b..92127e7 100644 --- a/spec/controllers/code_compares_controller_spec.rb +++ b/spec/controllers/code_compares_controller_spec.rb @@ -40,6 +40,18 @@ expect(UserGame.find(user_game.id).state).to eq(-1) end + describe "code already inderted" do + + let!(:code_compare) {create :code_compare, code: code, user: user} + + it "insert already inserted code" do + expect { + post :create, {try_text: code.code_string, task: task}, valid_session + }.to change(CodeCompare, :count).by(0) + end + + end + describe "change_task" do let(:new_task) { create :task, task_text: "sdgbhdrhndrbhzsgfr" } let(:new_code) { create :code, code_string: "dfbdfbndfgnhdfgdfh" } diff --git a/spec/controllers/games_controller_spec.rb b/spec/controllers/games_controller_spec.rb index 9102041..cbd104a 100644 --- a/spec/controllers/games_controller_spec.rb +++ b/spec/controllers/games_controller_spec.rb @@ -1,7 +1,6 @@ require 'spec_helper' describe GamesController do - login_admin let(:valid_session) { {} } @@ -15,169 +14,198 @@ let(:valid_attributes) {build :game, title: "title_1"} - describe "GET index" do - login_user - it "assigns all games as @games" do - get :index, {}, valid_session - expect(assigns(:games)).to eq([game]) + describe "without sign in" do + describe "GET show" do + it "assigns the requested game as @game" do + get :show, {id: game.id}, valid_session + expect(response).to redirect_to(root_path) + end end end - describe "GET show" do - login_user - it "assigns the requested game as @game" do - get :show, {id: game.id}, valid_session - expect(assigns(:game)).to eq(game) - end - end + describe "with sign in" do + login_admin - describe "GET new" do - it "assigns a new game as @game" do - get :new, {}, valid_session - expect(assigns(:game)).to be_a_new(Game) + describe "GET index" do + login_user + it "assigns all games as @games" do + get :index, {}, valid_session + expect(assigns(:games)).to eq([game]) + end end - end - describe "GET edit" do - it "assigns the requested game as @game" do - get :edit, {id: game.id}, valid_session - expect(assigns(:game)).to eq(game) - end - end + describe "GET show" do + login_user - describe "POST create" do - before(:each) do - @valid_attributes = valid_attributes.attributes - @invalid_attributes = invalid_attributes - end - describe "with valid params" do - it "creates a new Game" do - expect { - post :create, {game: valid_attributes.attributes}, valid_session - }.to change(Game, :count).by(1) - end + let(:code) {create :code} + + let!(:task_code) {create :task_code, task: task, code: code} + + let!(:code_compare) {create :code_compare, code: code, user: @user} - it "assigns a newly created game as @game" do - post :create, {game: valid_attributes.attributes}, valid_session - expect(assigns(:game)).to be_a(Game) - expect(assigns(:game)).to be_persisted + let!(:user_task) {create :user_task, task: task, user: @user} + + it "assigns the requested game as @game" do + get :show, {id: game.id}, valid_session + expect(assigns(:game)).to eq(game) end - it "redirects to the created game" do - post :create, {game: valid_attributes.attributes}, valid_session - expect(response).to redirect_to(Game.last) + it "returns array with code_compare forms count" do + Game.find(game.id).update(state: UserGame::CURRENT) + get :show, {id: game.id}, valid_session + expect(assigns(:arr) == [CodeCompare.count, TaskCode.count - CodeCompare.count]) end + end - describe "with invalid params" do - it "assigns a newly created but unsaved game as @game" do - Game.any_instance.stub(:save).and_return(false) - post :create, {game: invalid_attributes.attributes}, valid_session + describe "GET new" do + it "assigns a new game as @game" do + get :new, {}, valid_session expect(assigns(:game)).to be_a_new(Game) end + end - it "re-renders the 'new' template" do - Game.any_instance.stub(:save).and_return(false) - post :create, {game: invalid_attributes.attributes}, valid_session - expect(response).to render_template("new") + describe "GET edit" do + it "assigns the requested game as @game" do + get :edit, {id: game.id}, valid_session + expect(assigns(:game)).to eq(game) end end - end - describe "PUT update" do - describe "with valid params" do - it "updates the requested game" do - expect_any_instance_of(Game).to receive(:update).with({ "title" => valid_attributes.title }) - put :update, {id: game.id, game: { "title" => valid_attributes.title }}, valid_session + describe "POST create" do + before(:each) do + @valid_attributes = valid_attributes.attributes + @invalid_attributes = invalid_attributes end + describe "with valid params" do + it "creates a new Game" do + expect { + post :create, {game: valid_attributes.attributes}, valid_session + }.to change(Game, :count).by(1) + end - it "assigns the requested game as @game" do - put :update, {id: game.id, game: { title: valid_attributes.title }}, valid_session - expect(assigns(:game)).to eq(game) + it "assigns a newly created game as @game" do + post :create, {game: valid_attributes.attributes}, valid_session + expect(assigns(:game)).to be_a(Game) + expect(assigns(:game)).to be_persisted + end + + it "redirects to the created game" do + post :create, {game: valid_attributes.attributes}, valid_session + expect(response).to redirect_to(Game.last) + end end - it "redirects to the game" do - put :update, {id: game.id, game: { title: valid_attributes.title }}, valid_session - expect(response).to redirect_to(game) + describe "with invalid params" do + it "assigns a newly created but unsaved game as @game" do + Game.any_instance.stub(:save).and_return(false) + post :create, {game: invalid_attributes.attributes}, valid_session + expect(assigns(:game)).to be_a_new(Game) + end + + it "re-renders the 'new' template" do + Game.any_instance.stub(:save).and_return(false) + post :create, {game: invalid_attributes.attributes}, valid_session + expect(response).to render_template("new") + end end end - describe "with invalid params" do - it "assigns the game as @game" do - Game.any_instance.stub(:save).and_return(false) - put :update, {id: game.id, game: { title: invalid_attributes.title }}, valid_session - expect(assigns(:game)).to eq(game) + describe "PUT update" do + describe "with valid params" do + it "updates the requested game" do + expect_any_instance_of(Game).to receive(:update).with({ "title" => valid_attributes.title }) + put :update, {id: game.id, game: { "title" => valid_attributes.title }}, valid_session + end + + it "assigns the requested game as @game" do + put :update, {id: game.id, game: { title: valid_attributes.title }}, valid_session + expect(assigns(:game)).to eq(game) + end + + it "redirects to the game" do + put :update, {id: game.id, game: { title: valid_attributes.title }}, valid_session + expect(response).to redirect_to(game) + end + end + + describe "with invalid params" do + it "assigns the game as @game" do + Game.any_instance.stub(:save).and_return(false) + put :update, {id: game.id, game: { title: invalid_attributes.title }}, valid_session + expect(assigns(:game)).to eq(game) + end + + it "re-renders the 'edit' template" do + Game.any_instance.stub(:save).and_return(false) + put :update, {id: game.id, game: { title: invalid_attributes.title }}, valid_session + expect(response).to render_template("edit") + end end + end - it "re-renders the 'edit' template" do - Game.any_instance.stub(:save).and_return(false) - put :update, {id: game.id, game: { title: invalid_attributes.title }}, valid_session - expect(response).to render_template("edit") + describe "DELETE destroy" do + it "destroys the requested game" do + expect { + delete :destroy, {id: game.id}, valid_session + }.to change(Game, :count).by(-1) end - end - end - describe "DELETE destroy" do - it "destroys the requested game" do - expect { + it "redirects to the games list" do delete :destroy, {id: game.id}, valid_session - }.to change(Game, :count).by(-1) + expect(response).to redirect_to(games_url) + end end - it "redirects to the games list" do - delete :destroy, {id: game.id}, valid_session - expect(response).to redirect_to(games_url) - end - end + describe "Start and finish game" do - describe "Start and finish game" do + let!(:user) {create :user} - let!(:user) {create :user} + let!(:user_game) {create :user_game, game: game, user: user} - let!(:user_game) {create :user_game, game: game, user: user} + describe "Start game" do + it "execute action " do + expect { + get "start_game", {id: game.id}, valid_session + }.to change(UserTask, :count).by(1) + end - describe "Start game" do - it "execute action " do - expect { + it "check game state equal to CURRENT" do get "start_game", {id: game.id}, valid_session - }.to change(UserTask, :count).by(1) - end + expect(Game.find(game.id).state).to equal(UserGame::CURRENT) + end - it "check game state equal to CURRENT" do - get "start_game", {id: game.id}, valid_session - expect(Game.find(game.id).state).to equal(UserGame::CURRENT) - end + it "check user_game state equal to CURRENT" do + get "start_game", {id: game.id}, valid_session + expect(UserGame.where(user_id: user.id, game_id: game.id).first.state).to equal(UserGame::CURRENT) + end - it "check user_game state equal to CURRENT" do - get "start_game", {id: game.id}, valid_session - expect(UserGame.where(user_id: user.id, game_id: game.id).first.state).to equal(UserGame::CURRENT) end - end + describe "Finish game" do - describe "Finish game" do + let(:task1) {create :task} - let(:task1) {create :task} + let!(:user_task) {create :user_task, user: user, task: task, result: 3} - let!(:user_task) {create :user_task, user: user, task: task, result: 3} + let!(:user_task1) {create :user_task, user: user, task: task1, result: 3} - let!(:user_task1) {create :user_task, user: user, task: task1, result: 3} + it "check game status as equal to COMPLETED" do + get "finish_game", {id: game.id}, valid_session + expect(Game.find(game.id).state).to eq(UserGame::COMPLETED) + end - it "check game status as equal to COMPLETED" do - get "finish_game", {id: game.id}, valid_session - expect(Game.find(game.id).state).to eq(UserGame::COMPLETED) - end + it "check user_game status as equal to COMPLETED" do + get "finish_game", {id: game.id}, valid_session + expect(UserGame.where(user_id: user.id, game_id: game.id).first.state).to eq(UserGame::COMPLETED) + end - it "check user_game status as equal to COMPLETED" do - get "finish_game", {id: game.id}, valid_session - expect(UserGame.where(user_id: user.id, game_id: game.id).first.state).to eq(UserGame::COMPLETED) - end + it "check user_game points count as equal to user_tasks results sum " do + get "finish_game", {id: game.id}, valid_session + expect(UserGame.where(user_id: user.id, game_id: game.id).first.result).to eq(user_task.result + user_task1.result) + end - it "check user_game points count as equal to user_tasks results sum " do - get "finish_game", {id: game.id}, valid_session - expect(UserGame.where(user_id: user.id, game_id: game.id).first.result).to eq(user_task.result + user_task1.result) end - end end end diff --git a/spec/models/game_spec.rb b/spec/models/game_spec.rb index 6a833d9..d596391 100644 --- a/spec/models/game_spec.rb +++ b/spec/models/game_spec.rb @@ -45,6 +45,20 @@ expect(game.process(user, task, "1234567890") == "Code was matched. Game completed") expect(UserGame.find(user_game.id).state).to eq(-1) end + + describe "code already inderted" do + + let!(:code_compare) {create :code_compare, code: code, user: user} + + it "insert already inserted code" do + count = CodeCompare.count + expect(game.process(user, task, code.code_string) == "Compare for this code created early") + expect(CodeCompare.count).to equal(count) + end + + end + + describe "assign next task" do let!(:new_task) {create :task, task_text: "sdgbhdrhndrbhzsgfr"} let!(:new_code) {create :code, code_string: "dfbdfbndfgnhdfgdfh"} @@ -106,4 +120,16 @@ end end + + describe "get_task_codes_count" do + + let!(:code_compare) {create :code_compare, code: code, user: user} + + it "returns array with code_compare forms count" do + Game.find(game.id).update(state: UserGame::CURRENT) + expect(game.get_task_codes_count(task, user) == [CodeCompare.count, TaskCode.count - CodeCompare.count]) + end + + end + end From f538b7362ed98378cbf00a503dba3a98f5fbce7c Mon Sep 17 00:00:00 2001 From: sorgal Date: Thu, 23 Jan 2014 08:39:55 +0400 Subject: [PATCH 11/15] homework --- .idea/workspace.xml | 444 +++++++++++++++------- app/admin/dashboard.rb | 6 - app/controllers/codes_controller.rb | 1 + app/controllers/games_controller.rb | 1 + app/controllers/hints_controller.rb | 6 +- app/controllers/home_controller.rb | 1 + app/controllers/tasks_controller.rb | 1 + app/controllers/user_games_controller.rb | 6 +- app/models/hint.rb | 3 +- app/models/task.rb | 2 +- app/views/codes/index.html.slim | 9 +- app/views/codes/show.html.slim | 4 + app/views/games/index.html.slim | 39 +- app/views/games/show.html.slim | 16 +- app/views/hints/index.html.slim | 8 +- app/views/hints/new.html.slim | 13 +- app/views/hints/show.html.slim | 4 + app/views/home/index.html.slim | 15 +- app/views/layouts/application.html.slim | 36 +- app/views/tasks/index.html.slim | 13 +- app/views/tasks/show.html.slim | 4 + app/views/user_games/index.html.slim | 15 +- config/initializers/active_admin.rb | 1 - db/schema.rb | 5 - spec/controllers/codes_controller_spec.rb | 2 + 25 files changed, 434 insertions(+), 221 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 40a6c1d..f1bd6c8 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -23,19 +23,19 @@ - - + + - + - - + + - + @@ -51,39 +51,15 @@ @@ -197,14 +197,156 @@ @@ -425,14 +567,14 @@ - + - + @@ -444,8 +586,8 @@ - + @@ -464,9 +606,9 @@ - - + + @@ -486,257 +628,285 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + - + - + - + - + - + - + - + - - - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + - + - + - + - + - + - + + + - + - + - + - + - + - + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + - + - + + + + + + + + + + + + + + + - + diff --git a/app/admin/dashboard.rb b/app/admin/dashboard.rb index 9162e18..048bb1e 100644 --- a/app/admin/dashboard.rb +++ b/app/admin/dashboard.rb @@ -3,12 +3,6 @@ menu :priority => 1, :label => proc{ I18n.t("active_admin.dashboard") } content :title => proc{ I18n.t("active_admin.dashboard") } do - div :class => "blank_slate_container", :id => "dashboard_default_message" do - span :class => "blank_slate" do - span I18n.t("active_admin.dashboard_welcome.welcome") - small I18n.t("active_admin.dashboard_welcome.call_to_action") - end - end # Here is an example of a simple dashboard with columns and panels. # diff --git a/app/controllers/codes_controller.rb b/app/controllers/codes_controller.rb index e4f9ca3..8c00445 100644 --- a/app/controllers/codes_controller.rb +++ b/app/controllers/codes_controller.rb @@ -13,6 +13,7 @@ def index # GET /codes/1 # GET /codes/1.json def show + @task = Task.find(@code.task_code.task_id) end # GET /codes/new diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index 58805af..bf564c0 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -4,6 +4,7 @@ class GamesController < ApplicationController before_filter :authenticate_admin_or_user, only: [:show] before_filter :check_game_status, only: [:show] before_filter :check_tasks, only: [:start_game] + # GET /games # GET /games.json def index diff --git a/app/controllers/hints_controller.rb b/app/controllers/hints_controller.rb index 9893b70..8d6a277 100644 --- a/app/controllers/hints_controller.rb +++ b/app/controllers/hints_controller.rb @@ -13,6 +13,7 @@ def index # GET /hints/1 # GET /hints/1.json def show + @task = Task.find(@hint.task_hint.task_id) end # GET /hints/new @@ -21,6 +22,7 @@ def new @cant_add = false if TaskHint.where(task_id: @task).count == 2 @cant_add = true + @error = "You can assign only two hints for one task" end @hint = Hint.new end @@ -100,10 +102,10 @@ def check_task_create if @task.hints.count > 0 if (@task.hints.count == 1) if @task.hints.first.queue_number == params.require(:hint)[:queue_number].to_i - redirect_to new_hint_path(task: @task.id), notice: "Parameter Queue number must be equal to " + (3 - params.require(:hint)[:queue_number].to_i).to_s + redirect_to new_hint_path(task: @task.id), error: "Parameter Queue number must be equal to " + (3 - params.require(:hint)[:queue_number].to_i).to_s end elsif @task.hints.count >= 2 - redirect_to tasks_path, notice: "Only two hints can be assigned with one task" + redirect_to tasks_path, error: "Only two hints can be assigned with one task" end end end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index cf99f3e..fee6c27 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -4,5 +4,6 @@ class HomeController < ApplicationController def index @games = Game.where("`state` >= ?", 0) + @user_games = current_user.user_games end end diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index 6cec3ab..1d99b15 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -12,6 +12,7 @@ def index # GET /tasks/1 # GET /tasks/1.json def show + @game = @task.game end # GET /tasks/new diff --git a/app/controllers/user_games_controller.rb b/app/controllers/user_games_controller.rb index 4c2d4fa..f43316a 100644 --- a/app/controllers/user_games_controller.rb +++ b/app/controllers/user_games_controller.rb @@ -9,8 +9,12 @@ class UserGamesController < ApplicationController def index @user_games = UserGame.where("`user_id` = ? AND `state` >= ?", current_user.id, 0) @games = [] + i = 0 @user_games.each do |user_game| - @games << Game.find(user_game.game_id) + @games[i] = {} + @games[i][:game] = Game.find(user_game.game_id) + @games[i][:user_game] = user_game.id + i += 1 end end diff --git a/app/models/hint.rb b/app/models/hint.rb index 8d99c80..c4d1774 100644 --- a/app/models/hint.rb +++ b/app/models/hint.rb @@ -1,7 +1,8 @@ class Hint < ActiveRecord::Base has_one :task_hint, dependent: :destroy has_many :user_hints, dependent: :destroy - validate :hint_text, :queue_number, presence: true + validates :hint_text, :queue_number, presence: true + #validates :queue_number, inclusion: {in: [1, 2]} RAISED = 1 NOT = 0 diff --git a/app/models/task.rb b/app/models/task.rb index 2ebb867..402e594 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -12,7 +12,7 @@ class Task < ActiveRecord::Base scope :for_user, -> id {joins(:user_tasks).where(user_tasks: {user_id: id})} def raise_hint - @hint = self.hints.where(raised: Hint::NOT).first + @hint = self.hints.where(raised: Hint::NOT).order(:queue_number).first if @hint self.user_tasks.each do |user_task| UserHint.create(user_id: user_task.user_id, hint_id: @hint.id) diff --git a/app/views/codes/index.html.slim b/app/views/codes/index.html.slim index fae20d7..20568e3 100644 --- a/app/views/codes/index.html.slim +++ b/app/views/codes/index.html.slim @@ -4,20 +4,15 @@ div class="page-header" table class="table table-striped" thead tr - th= model_class.human_attribute_name(:id) + th= "Show" th= model_class.human_attribute_name(:code_string) - th= model_class.human_attribute_name(:created_at) th=t '.actions', :default => t("helpers.actions") tbody - @codes.each do |code| tr - td= link_to code.id, code_path(code) + td= link_to "Show", code_path(code) td= code.code_string - td=l code.created_at td = link_to t('.edit', :default => t("helpers.links.edit")), edit_code_path(code), :class => 'btn btn-mini' ' = link_to t('.destroy', :default => t("helpers.links.destroy")), code_path(code), :method => :delete, :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-mini btn-danger' - -= link_to t('.new', :default => t("helpers.links.new")), new_code_path, :class => 'btn btn-primary' - diff --git a/app/views/codes/show.html.slim b/app/views/codes/show.html.slim index 8cbd14d..aeb6b1e 100644 --- a/app/views/codes/show.html.slim +++ b/app/views/codes/show.html.slim @@ -6,6 +6,10 @@ p strong= model_class.human_attribute_name(:code_string) + ':' br = @code.code_string +p + strong= 'Task:' + br + = link_to "Task", task_path(@task) .form-actions = link_to t('.back', :default => t("helpers.links.back")), codes_path, :class => 'btn' diff --git a/app/views/games/index.html.slim b/app/views/games/index.html.slim index 12f46b3..3708a7b 100644 --- a/app/views/games/index.html.slim +++ b/app/views/games/index.html.slim @@ -4,27 +4,38 @@ div class="page-header" table class="table table-striped" thead tr - th= model_class.human_attribute_name(:id) th= model_class.human_attribute_name(:title) th= model_class.human_attribute_name(:start_date) - th= model_class.human_attribute_name(:created_at) - th=t '.actions', :default => t("helpers.actions") + -if current_admin_user + th= model_class.human_attribute_name(:state) + th=t '.actions', :default => t("helpers.actions") tbody - @games.each do |game| + -if !current_admin_user && game.state < 0 + -next tr - td= link_to game.id, game_path(game) - td= game.title + -if current_admin_user + td= link_to game.title, game_path(game) + else + td= game.title td= game.start_date - td - = link_to t('.edit', :default => t("helpers.links.edit")), edit_game_path(game), :class => 'btn btn-mini' + -if current_admin_user + -if game.state == 0 + td= "Intact" + -elsif game.state > 0 + td= "Started" + -else + td= "Completed" + td + = link_to t('.edit', :default => t("helpers.links.edit")), edit_game_path(game), :class => 'btn btn-mini' - = link_to "New_task", new_task_path(game: game.id), :class => 'btn' + = link_to "Add task", new_task_path(game: game.id), :class => 'btn btn-mini' - = link_to "Start game", {controller: "games", action: "start_game", id: game.id}, :class => 'btn' + = link_to "Start game", {controller: "games", action: "start_game", id: game.id}, :class => 'btn btn-mini' - = link_to "Finish_game", {controller: "games", action: "finish_game", id: game.id}, :class => 'btn' - ' - = link_to t('.destroy', :default => t("helpers.links.destroy")), game_path(game), :method => :delete, :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-mini btn-danger' - -= link_to t('.new', :default => t("helpers.links.new")), new_game_path, :class => 'btn btn-primary' + = link_to "Finish game", {controller: "games", action: "finish_game", id: game.id}, :class => 'btn btn-mini' + ' + = link_to t('.destroy', :default => t("helpers.links.destroy")), game_path(game), :method => :delete, :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-mini btn-danger' +-if current_admin_user + = link_to t('.new', :default => t("helpers.links.new")), new_game_path, :class => 'btn btn-primary' diff --git a/app/views/games/show.html.slim b/app/views/games/show.html.slim index 1365ff5..2ce4525 100644 --- a/app/views/games/show.html.slim +++ b/app/views/games/show.html.slim @@ -10,20 +10,28 @@ div class="page-header" strong= model_class.human_attribute_name(:start_date) + ':' br = @game.start_date - br - = @status + p + strong= model_class.human_attribute_name(:state) + ':' + br + -if @game.state == 0 + = "Intact" + -elsif game.state > 0 + = "Started" + -else + = "Completed" .form-actions = link_to t('.back', :default => t("helpers.links.back")), games_path, :class => 'btn' - = link_to "New_task", new_task_path(game: @game.id), :class => 'btn' + = link_to "Add task", new_task_path(game: @game.id), :class => 'btn' = link_to "Start game", {controller: "games", action: "start_game", id: @game.id}, :class => 'btn' - = link_to "Finish_game", {controller: "games", action: "finish_game", id: @game.id}, :class => 'btn' + = link_to "Finish game", {controller: "games", action: "finish_game", id: @game.id}, :class => 'btn' = link_to t('.edit', :default => t("helpers.links.edit")), edit_game_path(@game), :class => 'btn' ' = link_to t('.destroy', :default => t("helpers.links.destroy")), game_path(@game), :method => "delete", :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-danger' + -elsif user_signed_in? -if notice = notice diff --git a/app/views/hints/index.html.slim b/app/views/hints/index.html.slim index 64bda85..797b3da 100644 --- a/app/views/hints/index.html.slim +++ b/app/views/hints/index.html.slim @@ -4,22 +4,18 @@ div class="page-header" table class="table table-striped" thead tr - th= model_class.human_attribute_name(:id) + th= "Show" th= model_class.human_attribute_name(:hint_text) th= model_class.human_attribute_name(:queue_number) - th= model_class.human_attribute_name(:created_at) th=t '.actions', :default => t("helpers.actions") tbody - @hints.each do |hint| tr - td= link_to hint.id, hint_path(hint) + td= link_to "Show", hint_path(hint) td= hint.hint_text td= hint.queue_number - td=l hint.created_at td = link_to t('.edit', :default => t("helpers.links.edit")), edit_hint_path(hint), :class => 'btn btn-mini' ' = link_to t('.destroy', :default => t("helpers.links.destroy")), hint_path(hint), :method => :delete, :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-mini btn-danger' -= link_to t('.new', :default => t("helpers.links.new")), new_hint_path, :class => 'btn btn-primary' - diff --git a/app/views/hints/new.html.slim b/app/views/hints/new.html.slim index fb0262d..29e6ce7 100644 --- a/app/views/hints/new.html.slim +++ b/app/views/hints/new.html.slim @@ -1,11 +1,16 @@ -model_class = Hint div class="page-header" - -unless @cant_add - -if notice - h1 = notice + -if !@cant_add h1=t '.title', :default => [:'helpers.titles.new', 'New %{model}'], :model => model_class.model_name.human.titleize = render :partial => "form" -else - h1 You can't add more hints for this task + -if @error + h1= @error + p + strong= 'Task:' + br + = link_to "Task", task_path(@task) + + diff --git a/app/views/hints/show.html.slim b/app/views/hints/show.html.slim index a751d4d..df2815e 100644 --- a/app/views/hints/show.html.slim +++ b/app/views/hints/show.html.slim @@ -10,6 +10,10 @@ p strong= model_class.human_attribute_name(:queue_number) + ':' br = @hint.queue_number +p + strong= 'Task:' + br + = link_to "Task", task_path(@task) .form-actions = link_to t('.back', :default => t("helpers.links.back")), hints_path, :class => 'btn' diff --git a/app/views/home/index.html.slim b/app/views/home/index.html.slim index 21100e8..40cd93c 100644 --- a/app/views/home/index.html.slim +++ b/app/views/home/index.html.slim @@ -2,21 +2,26 @@ table class="table table-striped" thead tr - th= Game.human_attribute_name(:id) th= Game.human_attribute_name(:title) th= Game.human_attribute_name(:start_date) - th= Game.human_attribute_name(:duration) - th= Game.human_attribute_name(:created_at) th=t '.actions', :default => t("helpers.actions") tbody - @games.each do |game| tr + -if game.state != 0 + -next td= game.title td= game.start_date - - if game.state == 0 - td + td + -detected = false + -@user_games.each do |user_game| + -if user_game.game_id == game.id + -detected = true + -break + -unless detected = button_to "Join", {controller: "user_games", action: "create", game_id: game.id}, :class => 'btn btn-mini' + - else div id="main" p diff --git a/app/views/layouts/application.html.slim b/app/views/layouts/application.html.slim index 4d48d5c..489c31b 100644 --- a/app/views/layouts/application.html.slim +++ b/app/views/layouts/application.html.slim @@ -25,19 +25,37 @@ html lang="en" .navbar-inner .container ul.nav - - if user_signed_in? - li = link_to 'Edit profile', edit_user_registration_path, :class => 'navbar-link' - li = link_to "Logout", destroy_user_session_path, method: :delete, :class => 'navbar-link' - li - span Logged in as #{current_user.email} - //strong = current_user.email + - if !current_admin_user + - if user_signed_in? + li = link_to 'Edit profile', edit_user_registration_path, :class => 'navbar-link' + li = link_to "Logout", destroy_user_session_path, method: :delete, :class => 'navbar-link' + li + span Logged in as #{current_user.email} + //strong = current_user.email + -else + li = link_to "Sign up", new_user_registration_path, :class => 'navbar-link' + li = link_to "Login", new_user_session_path, :class => 'navbar-link' -else - li = link_to "Sign up", new_user_registration_path, :class => 'navbar-link' - li = link_to "Login", new_user_session_path, :class => 'navbar-link' + li = link_to "Logout", "/admin/logout", method: :delete, :class => 'navbar-link' + li + span Logged in as #{current_admin_user.email} .container .row .span9 = bootstrap_flash - = yield \ No newline at end of file + = yield + .span3 + .well.sidebar-nav + ul.nav.nav-list + li.nav-header Sidebar + -if user_signed_in? + li= link_to "Home", root_path + li= link_to "User games", user_games_path + -elsif current_admin_user + li= link_to "Admin", "/admin" + li= link_to "Games", games_path + li= link_to "Tasks", tasks_path + li= link_to "Codes", codes_path + li= link_to "Hints", hintss_path \ No newline at end of file diff --git a/app/views/tasks/index.html.slim b/app/views/tasks/index.html.slim index 5f06089..5b93d47 100644 --- a/app/views/tasks/index.html.slim +++ b/app/views/tasks/index.html.slim @@ -4,26 +4,21 @@ div class="page-header" table class="table table-striped" thead tr - th= model_class.human_attribute_name(:id) + th= "Show" th= model_class.human_attribute_name(:task_text) th= model_class.human_attribute_name(:points) - th= model_class.human_attribute_name(:created_at) th=t '.actions', :default => t("helpers.actions") tbody - @tasks.each do |task| tr - td= link_to task.id, task_path(task) + td= link_to "Show", task_path(task) td= task.task_text td= task.points - td=l task.created_at td = link_to t('.edit', :default => t("helpers.links.edit")), edit_task_path(task), :class => 'btn btn-mini' - = link_to "Add code", new_code_path(task: task.id), :class => 'btn' + = link_to "Add code", new_code_path(task: task.id), :class => 'btn btn-mini' - = link_to "Add hint", new_hint_path(task: task.id), :class => 'btn' + = link_to "Add hint", new_hint_path(task: task.id), :class => 'btn btn-mini' ' = link_to t('.destroy', :default => t("helpers.links.destroy")), task_path(task), :method => :delete, :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-mini btn-danger' - - = link_to t('.new', :default => t("helpers.links.new")), new_task_path, :class => 'btn btn-primary' - diff --git a/app/views/tasks/show.html.slim b/app/views/tasks/show.html.slim index 77b8b9f..8af52bc 100644 --- a/app/views/tasks/show.html.slim +++ b/app/views/tasks/show.html.slim @@ -10,6 +10,10 @@ p strong= model_class.human_attribute_name(:points) + ':' br = @task.points +p + strong= 'Game:' + br + = link_to @game.title, game_path(@game) .form-actions = link_to t('.back', :default => t("helpers.links.back")), tasks_path, :class => 'btn' diff --git a/app/views/user_games/index.html.slim b/app/views/user_games/index.html.slim index b3a2b72..ce27db3 100644 --- a/app/views/user_games/index.html.slim +++ b/app/views/user_games/index.html.slim @@ -4,22 +4,19 @@ div class="page-header" table class="table table-striped" thead tr - th= model_class.human_attribute_name(:id) - th= model_class.human_attribute_name(:user_id) - th= model_class.human_attribute_name(:game_id) + th= "Game" th= model_class.human_attribute_name(:result) - th= model_class.human_attribute_name(:created_at) th=t '.actions', :default => t("helpers.actions") tbody - @games.each do |game| tr - -if game.state >0 + -if game[:game].state >0 td - = link_to game.title, game_path({id: game.id}), :method => :get, :class => 'btn btn-mini' + = link_to game[:game].title, game_path({id: game[:game].id}), :method => :get, :class => 'btn btn-mini' -else - td= game.title - td= game.start_date - //td + td= game[:game].title + td= game[:game].start_date + td = link_to t('.destroy', :default => t("helpers.links.destroy")), user_game_path({id: game[:user_game]}), :method => :delete, :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-mini btn-danger' diff --git a/config/initializers/active_admin.rb b/config/initializers/active_admin.rb index efbb1d0..b8321ec 100644 --- a/config/initializers/active_admin.rb +++ b/config/initializers/active_admin.rb @@ -233,5 +233,4 @@ # config.filters = true config.skip_before_filter :authorize_admin - end diff --git a/db/schema.rb b/db/schema.rb index 4ddfbdf..761aebd 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -89,11 +89,6 @@ t.integer "raised", default: 0 end - create_table "messages", force: true do |t| - t.datetime "created_at" - t.datetime "updated_at" - end - create_table "task_codes", force: true do |t| t.integer "task_id" t.integer "code_id" diff --git a/spec/controllers/codes_controller_spec.rb b/spec/controllers/codes_controller_spec.rb index 7b3bbf1..90a34a3 100644 --- a/spec/controllers/codes_controller_spec.rb +++ b/spec/controllers/codes_controller_spec.rb @@ -13,6 +13,8 @@ let!(:valid_attributes) {build :code, code_string: "eghrfhfhfh"} + let!(:task_code) {create :task_code, task: task, code: code} + describe "GET index" do it "assigns all codes as @codes" do get :index, {}, valid_session From 266294d47bf0d071642c393daf43b33016c1b6ef Mon Sep 17 00:00:00 2001 From: sorgal Date: Thu, 23 Jan 2014 18:03:53 +0400 Subject: [PATCH 12/15] prototype making --- .idea/workspace.xml | 483 +++++++++------------- app/admin/admin_user.rb | 4 + app/admin/dashboard.rb | 4 +- app/controllers/codes_controller.rb | 3 + app/controllers/games_controller.rb | 42 +- app/controllers/hints_controller.rb | 4 +- app/controllers/home_controller.rb | 6 +- app/controllers/tasks_controller.rb | 3 + app/controllers/user_games_controller.rb | 4 +- app/models/game.rb | 34 +- app/views/codes/index.html.slim | 2 + app/views/codes/show.html.slim | 2 +- app/views/games/index.html.slim | 6 +- app/views/games/show.html.slim | 24 +- app/views/hints/index.html.slim | 2 + app/views/hints/show.html.slim | 2 +- app/views/layouts/application.html.slim | 32 +- app/views/tasks/index.html.slim | 2 + app/views/tasks/show.html.slim | 3 +- app/views/user_games/index.html.slim | 10 +- config/initializers/active_admin.rb | 7 +- spec/controllers/codes_controller_spec.rb | 34 +- spec/controllers/games_controller_spec.rb | 79 +++- spec/controllers/hints_controller_spec.rb | 38 +- spec/controllers/tasks_controller_spec.rb | 24 +- spec/models/game_spec.rb | 43 +- 26 files changed, 516 insertions(+), 381 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f1bd6c8..db0721b 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -23,19 +23,10 @@ - - + + - - - - - - - - - - + @@ -51,9 +42,7 @@ @@ -137,6 +128,8 @@ + + @@ -155,56 +148,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -244,84 +183,8 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -338,15 +201,9 @@ - - @@ -566,7 +423,7 @@ - + @@ -574,7 +431,7 @@ - + @@ -586,8 +443,8 @@ - + @@ -606,9 +463,9 @@ - - + + @@ -628,285 +485,345 @@ - + - - - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + + + - + - + - + - + + + - + - + + + - + - + - + - + + + - + - + + + - + - + + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + diff --git a/app/admin/admin_user.rb b/app/admin/admin_user.rb index df27b58..a3de31f 100644 --- a/app/admin/admin_user.rb +++ b/app/admin/admin_user.rb @@ -20,4 +20,8 @@ f.actions end + controller do + layout 'application' + end + end diff --git a/app/admin/dashboard.rb b/app/admin/dashboard.rb index 048bb1e..8c30d49 100644 --- a/app/admin/dashboard.rb +++ b/app/admin/dashboard.rb @@ -1,6 +1,6 @@ ActiveAdmin.register_page "Dashboard" do - menu :priority => 1, :label => proc{ I18n.t("active_admin.dashboard") } + menu false content :title => proc{ I18n.t("active_admin.dashboard") } do @@ -25,6 +25,6 @@ # end end # content controller do - layout 'application' # <-- here + layout 'application' end end diff --git a/app/controllers/codes_controller.rb b/app/controllers/codes_controller.rb index 8c00445..f71eaad 100644 --- a/app/controllers/codes_controller.rb +++ b/app/controllers/codes_controller.rb @@ -85,6 +85,9 @@ def check_task unless params.require(:task) redirect_to games_path end + if Task.find(params.require(:task).to_i).game.state != 0 + redirect_to task_path(Task.find(params.require(:task).to_i)), notice: "You can't add new codes to tasks assigned with started or finished game" + end end def check_task_create diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index bf564c0..cb1b9f4 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -1,9 +1,10 @@ class GamesController < ApplicationController - before_action :set_game, only: [:show, :edit, :update, :destroy, :start_game, :check_game_status, :finish_game, :check_task] + before_action :set_game, only: [:show, :edit, :update, :destroy, :start_game, :check_game_status, :finish_game, :check_task_and_game, :check_started] skip_before_filter :authorize_admin, only: [:index, :show] before_filter :authenticate_admin_or_user, only: [:show] before_filter :check_game_status, only: [:show] - before_filter :check_tasks, only: [:start_game] + before_filter :check_tasks_and_game, only: [:start_game] + before_filter :check_started, only: [:finish_game] # GET /games # GET /games.json @@ -19,10 +20,13 @@ def show @status = "Wait" elsif @game.state > 0 @status = "Started" - @task = current_user.tasks.first - @arr = @game.get_task_codes_count(@task, current_user) - @code_compares_count = @arr[0] - @task_codes_count = @arr[1] + if user_signed_in? + @user_game = current_user.user_games.where(game_id: @game.id).first + @task = current_user.tasks.last + @arr = @game.get_task_codes_count(@task, current_user) + @code_compares_count = @arr[0] + @task_codes_count = @arr[1] + end elsif @game.state < 0 @status = "Finished" end @@ -96,12 +100,12 @@ def game_params #Заглушки для отложенных задач def start_game @game.start_game - redirect_to games_path + redirect_to game_path(@game), notice: "Game was started" end def finish_game @game.finish_game - redirect_to games_path + redirect_to game_path(@game), notice: "Game was finished" end protected @@ -118,9 +122,29 @@ def authenticate_admin_or_user end end - def check_tasks + def check_tasks_and_game if @game.tasks.count == 0 redirect_to game_path(@game.id), notice: "You can't start game without tasks" + elsif @game.state == UserGame::CURRENT + redirect_to game_path(@game.id), notice: "This game was already started" + elsif @game.state == UserGame::COMPLETED + redirect_to game_path(@game.id), notice: "This game was already finished" + else + tasks = @game.tasks + tasks.each do |task| + if task.codes.count == 0 + redirect_to game_path(@game.id), notice: "You can't start game without codes for game's tasks" + break + end + end + end + end + + def check_started + if @game.state == 0 + redirect_to game_path(@game.id), notice: "You can't finish this game, because it was not started " + elsif @game.state < 0 + redirect_to game_path(@game.id), notice: "You can't finish this game, because it was finished already " end end diff --git a/app/controllers/hints_controller.rb b/app/controllers/hints_controller.rb index 8d6a277..9229a4e 100644 --- a/app/controllers/hints_controller.rb +++ b/app/controllers/hints_controller.rb @@ -90,7 +90,9 @@ def check_task unless params.require(:task) redirect_to games_path end - + if Task.find(params.require(:task).to_i).game.state != 0 + redirect_to task_path(Task.find(params.require(:task).to_i)), notice: "You can't add new hints to tasks assigned with started or finished game" + end end def check_task_create diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index fee6c27..92b26fc 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -3,7 +3,9 @@ class HomeController < ApplicationController before_filter :authenticate_user!, only: [:game_code_compares, :create_code_compare] def index - @games = Game.where("`state` >= ?", 0) - @user_games = current_user.user_games + if user_signed_in? + @games = Game.where("`state` >= ?", 0) + @user_games = current_user.user_games + end end end diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index 1d99b15..83b8066 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -84,6 +84,9 @@ def check_game unless params.require(:game) redirect_to games_path end + if Game.find(params.require(:game).to_i).state != 0 + redirect_to game_path(Game.find(params.require(:game))), notice: "You can't add new tasks to started or finished game" + end end def check_game_create diff --git a/app/controllers/user_games_controller.rb b/app/controllers/user_games_controller.rb index f43316a..17bf0d7 100644 --- a/app/controllers/user_games_controller.rb +++ b/app/controllers/user_games_controller.rb @@ -7,13 +7,13 @@ class UserGamesController < ApplicationController # GET /user_games # GET /user_games.json def index - @user_games = UserGame.where("`user_id` = ? AND `state` >= ?", current_user.id, 0) + @user_games = UserGame.where("`user_id` = ?", current_user.id) @games = [] i = 0 @user_games.each do |user_game| @games[i] = {} @games[i][:game] = Game.find(user_game.game_id) - @games[i][:user_game] = user_game.id + @games[i][:user_game] = user_game i += 1 end end diff --git a/app/models/game.rb b/app/models/game.rb index 8535051..5e56df1 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -19,8 +19,9 @@ def process(user, task, try_text) else new_code_compare = @user.code_compares.build(code: matched_code) if new_code_compare.save + notice = "Code was matched" if all_codes_input? - notice = "Code was matched. #{next_task(@task.points - @user.hints.count)}" + notice += ". #{next_task(@task.points - @user.hints.count)}" end end end @@ -34,7 +35,7 @@ def process(user, task, try_text) end def all_codes_input? - if @user.code_compares.count == @task.codes.count + if get_task_codes_count(@task, @user)[1] == 0 return true else return false @@ -50,8 +51,9 @@ def pass_matching(text) end def next_task(points) + change_task_result(points) if has_more_tasks? - assign_next_task(points) + assign_next_task "Next task assigned" else end_game @@ -63,18 +65,23 @@ def has_more_tasks? untaken_tasks.any? end - def assign_next_task(points) - @user.user_tasks.last.update_attribute(:result, points) + def assign_next_task @user.user_tasks.create(task: untaken_tasks.first) end + def change_task_result(points) + @user.user_tasks.last.update_attribute(:result, points) + end + def end_game - @user.user_games.where(state: UserGame::CURRENT).first.update_attribute(:state, UserGame::COMPLETED) + get_current_game_tasks + points = @user.user_tasks.where("`user_tasks`.`task_id` IN (?)", @current_game_tasks).pluck(:result).inject{|sum, num| sum += num} + @user.user_games.where(game_id: self.id).first.update(state: UserGame::COMPLETED, result: points) end def untaken_tasks completed_ids = self.tasks.for_user(@user.id).pluck(:id) - @untaken_tasks ||= self.tasks.where('`tasks`.`id` NOT IN (?)', completed_ids.join(',') ) + @untaken_tasks ||= self.tasks.where('`tasks`.`id` NOT IN (?)', completed_ids ) end def start_game @@ -82,16 +89,23 @@ def start_game #Для каждого user_game генерится user_task. Я не придумал как убрать цикл self.user_games.update_all(state: UserGame::CURRENT) self.user_games.each do |user_game| - UserTask.create(user_id: user_game.id, task_id: self.tasks.first.id, result: 0) + UserTask.create(user_id: user_game.user_id, task_id: self.tasks.first.id, result: 0) end end + def get_current_game_tasks + @current_game_tasks = self.tasks.pluck(:id) + end + def finish_game self.update(state: UserGame::COMPLETED) + get_current_game_tasks #Здесь я тоже не придумал как убрать цикл self.user_games.each do |user_game| - points = UserTask.where(user_id: user_game.user_id).pluck(:result).inject{|sum, num| sum += num} - user_game.update(state: UserGame::COMPLETED, result: points) + if user_game.state > 0 + points = UserTask.where("`user_tasks`.`task_id` IN (?)", @current_game_tasks).where(user_id: user_game.user_id).pluck(:result).inject{|sum, num| sum += num} + user_game.update(state: UserGame::COMPLETED, result: points) + end end end diff --git a/app/views/codes/index.html.slim b/app/views/codes/index.html.slim index 20568e3..dc351c9 100644 --- a/app/views/codes/index.html.slim +++ b/app/views/codes/index.html.slim @@ -6,12 +6,14 @@ table class="table table-striped" tr th= "Show" th= model_class.human_attribute_name(:code_string) + th= "To assign new code " th=t '.actions', :default => t("helpers.actions") tbody - @codes.each do |code| tr td= link_to "Show", code_path(code) td= code.code_string + td= link_to code.task_code.task.task_text, task_path(code.task_code.task) td = link_to t('.edit', :default => t("helpers.links.edit")), edit_code_path(code), :class => 'btn btn-mini' ' diff --git a/app/views/codes/show.html.slim b/app/views/codes/show.html.slim index aeb6b1e..fe290a7 100644 --- a/app/views/codes/show.html.slim +++ b/app/views/codes/show.html.slim @@ -9,7 +9,7 @@ p p strong= 'Task:' br - = link_to "Task", task_path(@task) + = link_to @task.task_text, task_path(@task) .form-actions = link_to t('.back', :default => t("helpers.links.back")), codes_path, :class => 'btn' diff --git a/app/views/games/index.html.slim b/app/views/games/index.html.slim index 3708a7b..d91b165 100644 --- a/app/views/games/index.html.slim +++ b/app/views/games/index.html.slim @@ -14,10 +14,10 @@ table class="table table-striped" -if !current_admin_user && game.state < 0 -next tr - -if current_admin_user - td= link_to game.title, game_path(game) - else + -if !current_admin_user td= game.title + -else + td= link_to game.title, game_path(game) td= game.start_date -if current_admin_user -if game.state == 0 diff --git a/app/views/games/show.html.slim b/app/views/games/show.html.slim index 2ce4525..f737949 100644 --- a/app/views/games/show.html.slim +++ b/app/views/games/show.html.slim @@ -5,6 +5,8 @@ div class="page-header" strong= model_class.human_attribute_name(:title) + ':' br = @game.title +//-if notice + div.alert.alert-danger = notice -if current_admin_user p strong= model_class.human_attribute_name(:start_date) + ':' @@ -15,7 +17,7 @@ div class="page-header" br -if @game.state == 0 = "Intact" - -elsif game.state > 0 + -elsif @game.state > 0 = "Started" -else = "Completed" @@ -33,13 +35,17 @@ div class="page-header" = link_to t('.destroy', :default => t("helpers.links.destroy")), game_path(@game), :method => "delete", :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-danger' -elsif user_signed_in? - -if notice - = notice - -if @code_compares_count > 0 - = "Already inserted codes #{@code_compares_count}" - -i = 1 - -while i <= @task_codes_count - = render partial: "code_compare_form" - -i += 1 + -if @user_game.state < 0 + h1= "This game is already complete for you. Your result is: #{@user_game.result}" + -elsif @user_game.state == 0 + h1= "This game will start for you later" + -else + h1= "Your current task: #{@task.task_text}" + -if @code_compares_count > 0 + = "Already inserted codes #{@code_compares_count}" + -i = 1 + -while i <= @task_codes_count + = render partial: "code_compare_form" + -i += 1 diff --git a/app/views/hints/index.html.slim b/app/views/hints/index.html.slim index 797b3da..ff88a3e 100644 --- a/app/views/hints/index.html.slim +++ b/app/views/hints/index.html.slim @@ -7,6 +7,7 @@ table class="table table-striped" th= "Show" th= model_class.human_attribute_name(:hint_text) th= model_class.human_attribute_name(:queue_number) + th= "Task" th=t '.actions', :default => t("helpers.actions") tbody - @hints.each do |hint| @@ -14,6 +15,7 @@ table class="table table-striped" td= link_to "Show", hint_path(hint) td= hint.hint_text td= hint.queue_number + td= link_to hint.task_hint.task.task_text, task_path(hint.task_hint.task) td = link_to t('.edit', :default => t("helpers.links.edit")), edit_hint_path(hint), :class => 'btn btn-mini' ' diff --git a/app/views/hints/show.html.slim b/app/views/hints/show.html.slim index df2815e..b17be9d 100644 --- a/app/views/hints/show.html.slim +++ b/app/views/hints/show.html.slim @@ -13,7 +13,7 @@ p p strong= 'Task:' br - = link_to "Task", task_path(@task) + = link_to @task.task_text, task_path(@task) .form-actions = link_to t('.back', :default => t("helpers.links.back")), hints_path, :class => 'btn' diff --git a/app/views/layouts/application.html.slim b/app/views/layouts/application.html.slim index 489c31b..0cd1135 100644 --- a/app/views/layouts/application.html.slim +++ b/app/views/layouts/application.html.slim @@ -21,24 +21,24 @@ html lang="en" body - .navbar.navbar-fixed-top + .navbar .navbar-inner .container - ul.nav - - if !current_admin_user - - if user_signed_in? - li = link_to 'Edit profile', edit_user_registration_path, :class => 'navbar-link' - li = link_to "Logout", destroy_user_session_path, method: :delete, :class => 'navbar-link' - li - span Logged in as #{current_user.email} - //strong = current_user.email - -else - li = link_to "Sign up", new_user_registration_path, :class => 'navbar-link' - li = link_to "Login", new_user_session_path, :class => 'navbar-link' - -else - li = link_to "Logout", "/admin/logout", method: :delete, :class => 'navbar-link' + ul.nav + - if !current_admin_user + - if user_signed_in? + li = link_to 'Edit profile', edit_user_registration_path, :class => 'navbar-link' + li = link_to "Logout", destroy_user_session_path, method: :delete, :class => 'navbar-link' li - span Logged in as #{current_admin_user.email} + span= "Logged in as #{current_user.email}" + //strong = current_user.email + -else + li = link_to "Sign up", new_user_registration_path, :class => 'navbar-link' + li = link_to "Login", new_user_session_path, :class => 'navbar-link' + -else + li = link_to "Logout", "/admin/logout", method: :delete, :class => 'navbar-link' + li + span Logged in as #{current_admin_user.email} .container @@ -58,4 +58,4 @@ html lang="en" li= link_to "Games", games_path li= link_to "Tasks", tasks_path li= link_to "Codes", codes_path - li= link_to "Hints", hintss_path \ No newline at end of file + li= link_to "Hints", hints_path \ No newline at end of file diff --git a/app/views/tasks/index.html.slim b/app/views/tasks/index.html.slim index 5b93d47..67e24cc 100644 --- a/app/views/tasks/index.html.slim +++ b/app/views/tasks/index.html.slim @@ -7,6 +7,7 @@ table class="table table-striped" th= "Show" th= model_class.human_attribute_name(:task_text) th= model_class.human_attribute_name(:points) + th= "Game" th=t '.actions', :default => t("helpers.actions") tbody - @tasks.each do |task| @@ -14,6 +15,7 @@ table class="table table-striped" td= link_to "Show", task_path(task) td= task.task_text td= task.points + td= link_to task.game.title, game_path(task.game) td = link_to t('.edit', :default => t("helpers.links.edit")), edit_task_path(task), :class => 'btn btn-mini' diff --git a/app/views/tasks/show.html.slim b/app/views/tasks/show.html.slim index 8af52bc..2c1674a 100644 --- a/app/views/tasks/show.html.slim +++ b/app/views/tasks/show.html.slim @@ -1,7 +1,8 @@ - model_class = Task div class="page-header" h1=t '.title', :default => model_class.model_name.human.titleize - +//-if notice + div.alert.alert-danger = notice p strong= model_class.human_attribute_name(:task_text) + ':' br diff --git a/app/views/user_games/index.html.slim b/app/views/user_games/index.html.slim index ce27db3..602311c 100644 --- a/app/views/user_games/index.html.slim +++ b/app/views/user_games/index.html.slim @@ -5,6 +5,7 @@ table class="table table-striped" thead tr th= "Game" + th= model_class.human_attribute_name(:start_date) th= model_class.human_attribute_name(:result) th=t '.actions', :default => t("helpers.actions") tbody @@ -12,11 +13,16 @@ table class="table table-striped" tr -if game[:game].state >0 td - = link_to game[:game].title, game_path({id: game[:game].id}), :method => :get, :class => 'btn btn-mini' + = link_to game[:game].title, game_path({id: game[:game].id}), :method => :get -else td= game[:game].title td= game[:game].start_date td - = link_to t('.destroy', :default => t("helpers.links.destroy")), user_game_path({id: game[:user_game]}), :method => :delete, :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-mini btn-danger' + -if game[:game].state < 0 + = game[:user_game][:result] + -else + = "You will see your result when the game ends" + td + = link_to t('.destroy', :default => t("helpers.links.destroy")), user_game_path({id: game[:user_game][:id]}), :method => :delete, :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-mini btn-danger' diff --git a/config/initializers/active_admin.rb b/config/initializers/active_admin.rb index b8321ec..be67d63 100644 --- a/config/initializers/active_admin.rb +++ b/config/initializers/active_admin.rb @@ -6,7 +6,6 @@ # for each of the active admin pages. # config.site_title = "Nightzone" - # Set the link url for the title. For example, to take # users to your main site. Defaults to no link. # @@ -117,11 +116,11 @@ # # This allows your users to comment on any resource registered with Active Admin. # - # You can completely disable comments: - # config.allow_comments = false + #You can completely disable comments: + #config.allow_comments = false # # You can disable the menu item for the comments index page: - # config.show_comments_in_menu = false + #config.show_comments_in_menu = false # # You can change the name under which comments are registered: # config.comments_registration_name = 'AdminComment' diff --git a/spec/controllers/codes_controller_spec.rb b/spec/controllers/codes_controller_spec.rb index 90a34a3..46371f6 100644 --- a/spec/controllers/codes_controller_spec.rb +++ b/spec/controllers/codes_controller_spec.rb @@ -23,17 +23,47 @@ end describe "GET show" do + + let(:game) {create :game} + + let!(:task_game) {create :game_task, task: task, game: game} + it "assigns the requested code as @code" do get :show, {id: code.id}, valid_session expect(assigns(:code)).to eq(code) end + end describe "GET new" do + + let!(:game) {create :game} + + let!(:task_game) {create :game_task, task: task, game: game} + it "assigns a new code as @code" do get :new, {task: task.id}, valid_session expect(assigns(:code)).to be_a_new(Code) end + + describe "when game was started or finished" do + + it "tries to create a new Code when game was started" do + Game.find(game.id).update(state: UserGame::CURRENT) + get :new, {task: task.id}, valid_session + expect(response).to redirect_to(task_path(task)) + end + + it "tries to create a new Code when game was finished" do + Game.find(game.id).update(state: UserGame::COMPLETED) + get :new, {task: task.id}, valid_session + expect(response).to redirect_to(task_path(task)) + end + + + end + + end describe "GET edit" do @@ -50,6 +80,8 @@ @code_post_invalid = invalid_attributes.attributes @code_post_invalid[:task] = task.id end + + describe "with valid params" do it "creates a new Code" do expect { @@ -86,9 +118,7 @@ describe "PUT update" do describe "with valid params" do - #before (:each) do - #end it "updates the requested code" do expect_any_instance_of(Code).to receive(:update).with({ "code_string" => valid_attributes.code_string }) put :update, {id: code.id, code: { "code_string" => valid_attributes.code_string }}, valid_session diff --git a/spec/controllers/games_controller_spec.rb b/spec/controllers/games_controller_spec.rb index cbd104a..df93203 100644 --- a/spec/controllers/games_controller_spec.rb +++ b/spec/controllers/games_controller_spec.rb @@ -14,6 +14,10 @@ let(:valid_attributes) {build :game, title: "title_1"} + let(:code) {create :code} + + let!(:task_code) {create :task_code, task: task, code: code} + describe "without sign in" do describe "GET show" do it "assigns the requested game as @game" do @@ -37,10 +41,6 @@ describe "GET show" do login_user - let(:code) {create :code} - - let!(:task_code) {create :task_code, task: task, code: code} - let!(:code_compare) {create :code_compare, code: code, user: @user} let!(:user_task) {create :user_task, task: task, user: @user} @@ -164,20 +164,43 @@ let!(:user_game) {create :user_game, game: game, user: user} describe "Start game" do - it "execute action " do - expect { + describe "start intact game" do + it "execute action " do + expect { + get "start_game", {id: game.id}, valid_session + }.to change(UserTask, :count).by(1) + end + + it "check game state equal to CURRENT" do get "start_game", {id: game.id}, valid_session - }.to change(UserTask, :count).by(1) - end + expect(Game.find(game.id).state).to equal(UserGame::CURRENT) + end - it "check game state equal to CURRENT" do - get "start_game", {id: game.id}, valid_session - expect(Game.find(game.id).state).to equal(UserGame::CURRENT) + it "check user_game state equal to CURRENT" do + get "start_game", {id: game.id}, valid_session + expect(UserGame.where(user_id: user.id, game_id: game.id).first.state).to equal(UserGame::CURRENT) + end end - it "check user_game state equal to CURRENT" do - get "start_game", {id: game.id}, valid_session - expect(UserGame.where(user_id: user.id, game_id: game.id).first.state).to equal(UserGame::CURRENT) + describe "start already started or finished game" do + + it "starts already started game" do + Game.find(game.id).update(state: UserGame::CURRENT) + expect { + get "start_game", {id: game.id}, valid_session + }.to change(UserTask, :count).by(0) + expect(response).to redirect_to(game_path(game)) + end + + it "starts already finished game" do + Game.find(game.id).update(state: UserGame::COMPLETED) + expect { + get "start_game", {id: game.id}, valid_session + }.to change(UserTask, :count).by(0) + expect(response).to redirect_to(game_path(game)) + end + + end end @@ -190,6 +213,13 @@ let!(:user_task1) {create :user_task, user: user, task: task1, result: 3} + let!(:game_task1) {create :game_task, game: game, task: task1} + + before(:each) do + Game.find(game.id).update(state: UserGame::CURRENT) + UserGame.where(user_id: user.id, game_id: game.id).first.update(state: UserGame::CURRENT) + end + it "check game status as equal to COMPLETED" do get "finish_game", {id: game.id}, valid_session expect(Game.find(game.id).state).to eq(UserGame::COMPLETED) @@ -205,6 +235,27 @@ expect(UserGame.where(user_id: user.id, game_id: game.id).first.result).to eq(user_task.result + user_task1.result) end + describe "finsh not started or finished game" do + + it "finish not started game" do + Game.find(game.id).update(state: UserGame::INTACT) + expect { + get "finish_game", {id: game.id}, valid_session + }.to change(UserTask, :count).by(0) + expect(response).to redirect_to(game_path(game)) + end + + it "starts already finished game" do + Game.find(game.id).update(state: UserGame::COMPLETED) + expect { + get "start_game", {id: game.id}, valid_session + }.to change(UserTask, :count).by(0) + expect(response).to redirect_to(game_path(game)) + end + + + end + end end end diff --git a/spec/controllers/hints_controller_spec.rb b/spec/controllers/hints_controller_spec.rb index a8daa40..91b43b5 100644 --- a/spec/controllers/hints_controller_spec.rb +++ b/spec/controllers/hints_controller_spec.rb @@ -5,7 +5,7 @@ let(:valid_session) { {} } - let(:task) {create :task} + let!(:task) {create :task} let(:hint) {create :hint, queue_number: rand(1...2)} @@ -31,10 +31,33 @@ end describe "GET new" do + + + let!(:game) {create :game} + + let!(:task_game) {create :game_task, task: task, game: game} + it "assigns a new hint as @hint" do get :new, {task: task.id}, valid_session expect(assigns(:hint)).to be_a_new(Hint) end + + describe "when game was started and finished" do + + it "tries to create a new Hint when game was started" do + Game.find(game.id).update( state: UserGame::CURRENT) + get :new, {task: task.id}, valid_session + expect(response).to redirect_to(task_path(task)) + end + + it "tries to create a new Hint when game was finished" do + Game.find(game.id).update(state: UserGame::COMPLETED) + get :new, {task: task.id}, valid_session + expect(response).to redirect_to(task_path(task)) + end + + end + end describe "GET edit" do @@ -46,6 +69,13 @@ describe "POST create" do + before(:each) do + @hint_post = new_hint.attributes + @hint_post[:task] = task.id + @hint_post_invalid = invalid_attributes.attributes + @hint_post_invalid[:task] = task.id + end + let(:new_hint) {create :hint, queue_number: 3 - hint.queue_number} describe "third task hint" do @@ -60,12 +90,6 @@ end - before(:each) do - @hint_post = new_hint.attributes - @hint_post[:task] = task.id - @hint_post_invalid = invalid_attributes.attributes - @hint_post_invalid[:task] = task.id - end describe "with valid params" do it "creates a new Hint" do expect { diff --git a/spec/controllers/tasks_controller_spec.rb b/spec/controllers/tasks_controller_spec.rb index 7a07d20..43eb019 100644 --- a/spec/controllers/tasks_controller_spec.rb +++ b/spec/controllers/tasks_controller_spec.rb @@ -30,7 +30,7 @@ # TasksController. Be sure to keep this updated too. let(:valid_session) { {} } - let(:game) {create :game} + let!(:game) {create :game} let(:task) {create :task} @@ -69,6 +69,27 @@ get :new, {game: game.id}, valid_session expect(assigns(:task)).to be_a_new(Task) end + + describe "when game was started or finished" do + + before(:each) do + Game.find(game.id).update(state: UserGame::CURRENT) + end + + it "tries to create a new Task when game was started" do + get :new, {game: game.id}, valid_session + expect(response).to redirect_to(game_path(game)) + end + + it "tries to create a new Hint when game was finished" do + Game.find(game.id).update(state: UserGame::COMPLETED) + get :new, {game: game.id}, valid_session + expect(response).to redirect_to(game_path(game)) + end + + + end + end describe "GET edit" do @@ -85,6 +106,7 @@ @task_post_invalid = invalid_attributes.attributes @task_post_invalid[:game] = game.id end + describe "with valid params" do it "creates a new Task" do expect { diff --git a/spec/models/game_spec.rb b/spec/models/game_spec.rb index d596391..8bb3bd8 100644 --- a/spec/models/game_spec.rb +++ b/spec/models/game_spec.rb @@ -58,22 +58,38 @@ end + describe "more then one code" do - describe "assign next task" do - let!(:new_task) {create :task, task_text: "sdgbhdrhndrbhzsgfr"} let!(:new_code) {create :code, code_string: "dfbdfbndfgnhdfgdfh"} - let(:new_user_task) {build :user_task, user: user, task: new_task} - let!(:new_task_code) {create :task_code, task: new_task, code: new_code} - let!(:new_task_game) {create :game_task, game: game, task: new_task} - it "creates a new CodeCompare and change task" do - expect(game.process(user, task, "1234567890") == "Code was matched. Next task assigned") - expect(UserTask.last.task_id).to eq(new_task.id) + describe "with second code" do + + let!(:second_task_code) {create :task_code, task: task, code: new_code} + + it "creates code compare without ending game or changing task" do + count = CodeCompare.count + expect(game.process(user, task, code.code_string) == "Code was matched") + expect(CodeCompare.count).to equal(count + 1) + end + end - it "code compare processing and change task" do - expect(game.process(user, task, code.code_string) == "Code was matched. Next task assigned") - expect(UserTask.last.task_id).to eq(new_task.id) + + describe "assign next task" do + let!(:new_task) {create :task, task_text: "sdgbhdrhndrbhzsgfr"} + let(:new_user_task) {build :user_task, user: user, task: new_task} + let!(:new_task_code) {create :task_code, task: new_task, code: new_code} + let!(:new_task_game) {create :game_task, game: game, task: new_task} + + it "creates a new CodeCompare and change task" do + expect(game.process(user, task, "1234567890") == "Code was matched. Next task assigned") + expect(UserTask.last.task_id).to eq(new_task.id) + end + it "code compare processing and change task" do + expect(game.process(user, task, code.code_string) == "Code was matched. Next task assigned") + expect(UserTask.last.task_id).to eq(new_task.id) + end end + end end describe "Start and finish game" do @@ -100,8 +116,13 @@ let(:task1) {create :task} let!(:user_task1) {create :user_task, user: user, task: task1, result: 3} + let!(:game_task1) {create :game_task, game: game, task: task1} #user_task1 = FactoryGirl.create(:user_task, task_id: @task1.to_param, user_id: @user.to_param, result: 3) + before(:each) do + Game.find(game.id).update(state: UserGame::CURRENT) + UserGame.where(user_id: user.id, game_id: game.id).first.update(state: UserGame::CURRENT) + end it "check game status as equal to COMPLETED" do game.finish_game From bc91e0d63beb6490008c66c54b302b22a3eb58ff Mon Sep 17 00:00:00 2001 From: sorgal Date: Fri, 24 Jan 2014 09:41:55 +0400 Subject: [PATCH 13/15] prototype creating --- .idea/workspace.xml | 414 ++++++++++-------- app/controllers/hints_controller.rb | 4 +- app/controllers/tasks_controller.rb | 13 +- app/controllers/user_games_controller.rb | 2 +- app/models/game.rb | 3 +- app/models/task.rb | 5 + app/views/codes/index.html.slim | 5 +- app/views/codes/show.html.slim | 2 +- app/views/hints/index.html.slim | 5 +- app/views/hints/show.html.slim | 2 +- app/views/tasks/index.html.slim | 7 +- app/views/tasks/show.html.slim | 6 +- .../20140115090713_create_user_tasks.rb | 2 +- db/schema.rb | 2 +- spec/controllers/tasks_controller_spec.rb | 67 ++- 15 files changed, 314 insertions(+), 225 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index db0721b..08c9ef4 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -23,10 +23,46 @@ - - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -42,7 +78,6 @@ @@ -128,8 +164,6 @@ - - @@ -148,6 +182,38 @@ + + + + + + + + + + + + + + + + + + - @@ -202,8 +268,46 @@ + + @@ -423,7 +527,7 @@ - + @@ -431,7 +535,7 @@ - + @@ -443,8 +547,8 @@ - + @@ -485,345 +589,285 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - + - + - - - + - + - - - + - + - - - + - + - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + diff --git a/app/controllers/hints_controller.rb b/app/controllers/hints_controller.rb index 9229a4e..71d50cb 100644 --- a/app/controllers/hints_controller.rb +++ b/app/controllers/hints_controller.rb @@ -104,10 +104,10 @@ def check_task_create if @task.hints.count > 0 if (@task.hints.count == 1) if @task.hints.first.queue_number == params.require(:hint)[:queue_number].to_i - redirect_to new_hint_path(task: @task.id), error: "Parameter Queue number must be equal to " + (3 - params.require(:hint)[:queue_number].to_i).to_s + redirect_to new_hint_path(task: @task.id), notice: "Parameter Queue number must be equal to " + (3 - params.require(:hint)[:queue_number].to_i).to_s end elsif @task.hints.count >= 2 - redirect_to tasks_path, error: "Only two hints can be assigned with one task" + redirect_to tasks_path, notice: "Only two hints can be assigned with one task" end end end diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index 83b8066..fbd1e1c 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -1,8 +1,9 @@ class TasksController < ApplicationController - before_action :set_task, only: [:show, :edit, :update, :destroy, :raise_hint] + before_action :set_task, only: [:show, :edit, :update, :destroy, :raise_hint, :check_started] before_filter :check_game, only: [:new] before_filter :check_game_create, only: [:create] before_filter :no_tasks, except: [:new, :create] + before_filter :check_started, only: [:raise_hint] # GET /tasks # GET /tasks.json def index @@ -101,11 +102,17 @@ def no_tasks end end + def check_started + if @task.game.state <= 0 + redirect_to task_path(@task), notice: "You can't raise hints for this task because this task's game was not raised" + end + end + public #еще заглушка def raise_hint - @task.raise_hint - redirect_to tasks_path + notice = @task.raise_hint + redirect_to task_path(@task), notice: notice end end diff --git a/app/controllers/user_games_controller.rb b/app/controllers/user_games_controller.rb index 17bf0d7..75598d3 100644 --- a/app/controllers/user_games_controller.rb +++ b/app/controllers/user_games_controller.rb @@ -42,7 +42,7 @@ def create respond_to do |format| if @user_game.save - format.html { redirect_to user_games_url, notice: 'User game was join in game with success.' } + format.html { redirect_to user_games_url, notice: 'User was join in game with success.' } format.json { render action: 'index', status: :created, location: @user_game } else format.html { redirect_to root_path } diff --git a/app/models/game.rb b/app/models/game.rb index 5e56df1..91fefba 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -21,7 +21,8 @@ def process(user, task, try_text) if new_code_compare.save notice = "Code was matched" if all_codes_input? - notice += ". #{next_task(@task.points - @user.hints.count)}" + task_hints_ids = @task.hints.pluck(:id) + notice += ". #{next_task(@task.points - @user.hints.where("`hints`.`id` IN (?)", task_hints_ids).count)}" end end end diff --git a/app/models/task.rb b/app/models/task.rb index 402e594..bcf3123 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -17,7 +17,12 @@ def raise_hint self.user_tasks.each do |user_task| UserHint.create(user_id: user_task.user_id, hint_id: @hint.id) end + @hint.update(raised: Hint::RAISED) + notice = "Hint was raised with success" + else + notice = "All hints are rised early" end + @notice = notice end end diff --git a/app/views/codes/index.html.slim b/app/views/codes/index.html.slim index dc351c9..a9b85cd 100644 --- a/app/views/codes/index.html.slim +++ b/app/views/codes/index.html.slim @@ -6,7 +6,7 @@ table class="table table-striped" tr th= "Show" th= model_class.human_attribute_name(:code_string) - th= "To assign new code " + th= "Task (follow if you want add code to this task)" th=t '.actions', :default => t("helpers.actions") tbody - @codes.each do |code| @@ -18,3 +18,6 @@ table class="table table-striped" = link_to t('.edit', :default => t("helpers.links.edit")), edit_code_path(code), :class => 'btn btn-mini' ' = link_to t('.destroy', :default => t("helpers.links.destroy")), code_path(code), :method => :delete, :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-mini btn-danger' +div.alert.alert-info + = "NOTE: If you want add new code you must follow this link: " + =link_to "Tasks", tasks_path \ No newline at end of file diff --git a/app/views/codes/show.html.slim b/app/views/codes/show.html.slim index fe290a7..c3ca900 100644 --- a/app/views/codes/show.html.slim +++ b/app/views/codes/show.html.slim @@ -7,7 +7,7 @@ p br = @code.code_string p - strong= 'Task:' + strong= 'Task (follow if you want add new code to this task):' br = link_to @task.task_text, task_path(@task) diff --git a/app/views/hints/index.html.slim b/app/views/hints/index.html.slim index ff88a3e..589b851 100644 --- a/app/views/hints/index.html.slim +++ b/app/views/hints/index.html.slim @@ -7,7 +7,7 @@ table class="table table-striped" th= "Show" th= model_class.human_attribute_name(:hint_text) th= model_class.human_attribute_name(:queue_number) - th= "Task" + th= "Task (follow if you want add hint to this task)" th=t '.actions', :default => t("helpers.actions") tbody - @hints.each do |hint| @@ -21,3 +21,6 @@ table class="table table-striped" ' = link_to t('.destroy', :default => t("helpers.links.destroy")), hint_path(hint), :method => :delete, :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-mini btn-danger' +div.alert.alert-info + = "NOTE: If you want add new hint you must follow this link: " + =link_to "Tasks", tasks_path \ No newline at end of file diff --git a/app/views/hints/show.html.slim b/app/views/hints/show.html.slim index b17be9d..3c43eb3 100644 --- a/app/views/hints/show.html.slim +++ b/app/views/hints/show.html.slim @@ -11,7 +11,7 @@ p br = @hint.queue_number p - strong= 'Task:' + strong= 'Task (follow, if you want add hint to this task) :' br = link_to @task.task_text, task_path(@task) diff --git a/app/views/tasks/index.html.slim b/app/views/tasks/index.html.slim index 67e24cc..e7f41ed 100644 --- a/app/views/tasks/index.html.slim +++ b/app/views/tasks/index.html.slim @@ -7,7 +7,7 @@ table class="table table-striped" th= "Show" th= model_class.human_attribute_name(:task_text) th= model_class.human_attribute_name(:points) - th= "Game" + th= "Game (follow if you want add task to this game)" th=t '.actions', :default => t("helpers.actions") tbody - @tasks.each do |task| @@ -22,5 +22,10 @@ table class="table table-striped" = link_to "Add code", new_code_path(task: task.id), :class => 'btn btn-mini' = link_to "Add hint", new_hint_path(task: task.id), :class => 'btn btn-mini' + + = link_to "Raise hint", {controller: "tasks", action: "raise_hint", id: task.id}, :class => 'btn btn-mini' ' = link_to t('.destroy', :default => t("helpers.links.destroy")), task_path(task), :method => :delete, :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-mini btn-danger' +div.alert.alert-info + = "NOTE: If you want add new task you must follow this link: " + =link_to "Games", games_path \ No newline at end of file diff --git a/app/views/tasks/show.html.slim b/app/views/tasks/show.html.slim index 2c1674a..ad3e02c 100644 --- a/app/views/tasks/show.html.slim +++ b/app/views/tasks/show.html.slim @@ -12,7 +12,7 @@ p br = @task.points p - strong= 'Game:' + strong= 'Game (follow if you want add new task to this game):' br = link_to @game.title, game_path(@game) @@ -22,7 +22,9 @@ p = link_to "Add code", new_code_path(task: @task.id), :class => 'btn' = link_to "Add hint", new_hint_path(task: @task.id), :class => 'btn' - ' + + = link_to "Raise hint", {controller: "tasks", action: "raise_hint", id: @task.id}, :class => 'btn' + = link_to t('.edit', :default => t("helpers.links.edit")), edit_task_path(@task), :class => 'btn' ' = link_to t('.destroy', :default => t("helpers.links.destroy")), task_path(@task), :method => "delete", :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-danger' diff --git a/db/migrate/20140115090713_create_user_tasks.rb b/db/migrate/20140115090713_create_user_tasks.rb index 381311a..6ebdd23 100644 --- a/db/migrate/20140115090713_create_user_tasks.rb +++ b/db/migrate/20140115090713_create_user_tasks.rb @@ -3,7 +3,7 @@ def change create_table :user_tasks do |t| t.integer :user_id t.integer :task_id - t.integer :result, default: -1 + t.integer :result, default: 0 t.timestamps end diff --git a/db/schema.rb b/db/schema.rb index 761aebd..2c27142 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -129,7 +129,7 @@ create_table "user_tasks", force: true do |t| t.integer "user_id" t.integer "task_id" - t.integer "result", default: -1 + t.integer "result", default: 0 t.datetime "created_at" t.datetime "updated_at" end diff --git a/spec/controllers/tasks_controller_spec.rb b/spec/controllers/tasks_controller_spec.rb index 43eb019..1243284 100644 --- a/spec/controllers/tasks_controller_spec.rb +++ b/spec/controllers/tasks_controller_spec.rb @@ -44,6 +44,8 @@ let(:hint2) {create :hint, hint_text: " wvgrawrfswf", queue_number: 2} + let!(:game_task) {create :game_task, game: game, task: task} + let!(:task_hint2) {create :task_hint, task: task, hint: hint2} let(:invalid_attributes) {build :task, task_text: "впирапеи"} @@ -189,48 +191,65 @@ end describe "Raise hint" do - it "execute action " do - expect { - get "raise_hint", {id: task.id}, valid_session - }.to change(UserHint, :count).by(1) - end - it "raises first hint" do - expect { - get "raise_hint", {id: task.id}, valid_session - }.to change(UserHint, :count).by(1) - expect(UserHint.last.hint_id).to eq(hint.id) - expect(UserHint.count).to eq(1) + describe "game is not started" do + it "execute action with not started game" do + expect { + get "raise_hint", {id: task.id}, valid_session + }.to change(UserHint, :count).by(0) + expect(response).to redirect_to(task_path(task)) + end end - describe "hints count > 1" do - - let!(:user_hint) {create :user_hint, user: user, hint: hint} - + describe "game is started" do before(:each) do - Hint.find(hint.id).update_attribute(:raised, Hint::RAISED) + Game.find(game.id).update(state: UserGame::CURRENT) + end + + it "execute action " do + expect { + get "raise_hint", {id: task.id}, valid_session + }.to change(UserHint, :count).by(1) end - it "raises second hint" do + it "raises first hint" do expect { get "raise_hint", {id: task.id}, valid_session }.to change(UserHint, :count).by(1) - expect(UserHint.last.hint_id).to eq(hint2.id) - expect(UserHint.count).to eq(2) + expect(UserHint.last.hint_id).to eq(hint.id) + expect(UserHint.count).to eq(1) end - describe "third hint" do - let!(:user_hint2) {create :user_hint, user: user, hint: hint2} - it "doesn't raise third hint" do - Hint.find(hint2.id).update_attribute(:raised, Hint::RAISED) + describe "hints count > 1" do + + let!(:user_hint) {create :user_hint, user: user, hint: hint} + + before(:each) do + Hint.find(hint.id).update_attribute(:raised, Hint::RAISED) + end + + it "raises second hint" do expect { get "raise_hint", {id: task.id}, valid_session - }.to change(UserHint, :count).by(0) + }.to change(UserHint, :count).by(1) expect(UserHint.last.hint_id).to eq(hint2.id) expect(UserHint.count).to eq(2) end + + describe "third hint" do + let!(:user_hint2) {create :user_hint, user: user, hint: hint2} + it "doesn't raise third hint" do + Hint.find(hint2.id).update_attribute(:raised, Hint::RAISED) + expect { + get "raise_hint", {id: task.id}, valid_session + }.to change(UserHint, :count).by(0) + expect(UserHint.last.hint_id).to eq(hint2.id) + expect(UserHint.count).to eq(2) + end + end end end + end end From 8de20dd7c7250794d9018d70ebe2f757ff80d5e0 Mon Sep 17 00:00:00 2001 From: sorgal Date: Fri, 24 Jan 2014 11:34:27 +0400 Subject: [PATCH 14/15] create prototype --- .idea/workspace.xml | 244 +++++++++------------------- app/controllers/hints_controller.rb | 14 +- app/controllers/tasks_controller.rb | 2 +- app/views/codes/new.html.slim | 1 + app/views/games/_form.html.slim | 1 + 5 files changed, 93 insertions(+), 169 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 08c9ef4..f4c9e49 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -26,34 +26,16 @@ - + - - + + - - - - - - - - - - - - - - - - - - - + @@ -62,7 +44,7 @@ - + @@ -78,8 +60,6 @@ @@ -182,78 +164,6 @@ @@ -527,7 +437,7 @@ - + @@ -535,7 +445,7 @@ - + @@ -543,8 +453,8 @@ - + @@ -567,9 +477,9 @@ - - + + @@ -589,21 +499,6 @@ - - - - - - - - - - - - - - - @@ -694,21 +589,11 @@ - - - - - - - - - - @@ -763,16 +648,6 @@ - - - - - - - - - - @@ -788,13 +663,6 @@ - - - - - - - @@ -804,9 +672,7 @@ - - - + @@ -818,16 +684,12 @@ - - - + - - - + @@ -844,30 +706,86 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + - + + + + + + + + - + + + + + + + + + + + + + + + - + diff --git a/app/controllers/hints_controller.rb b/app/controllers/hints_controller.rb index 71d50cb..25584a8 100644 --- a/app/controllers/hints_controller.rb +++ b/app/controllers/hints_controller.rb @@ -101,12 +101,16 @@ def check_task_create end task = params.require(:hint)[:task].to_i @task = Task.find(task) + if params.require(:hint)[:queue_number].to_i <= 0 + redirect_to new_hint_path(task: @task.id), notice: "Parameter Queue number must be greater than 0" + end if @task.hints.count > 0 - if (@task.hints.count == 1) - if @task.hints.first.queue_number == params.require(:hint)[:queue_number].to_i - redirect_to new_hint_path(task: @task.id), notice: "Parameter Queue number must be equal to " + (3 - params.require(:hint)[:queue_number].to_i).to_s - end - elsif @task.hints.count >= 2 + #if (@task.hints.count == 1) + # if @task.hints.first.queue_number == params.require(:hint)[:queue_number].to_i + # redirect_to new_hint_path(task: @task.id), notice: "Parameter Queue number must be greater than" + params.require(:hint)[:queue_number] + # end + #els + if @task.hints.count >= 2 redirect_to tasks_path, notice: "Only two hints can be assigned with one task" end end diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index fbd1e1c..74af992 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -104,7 +104,7 @@ def no_tasks def check_started if @task.game.state <= 0 - redirect_to task_path(@task), notice: "You can't raise hints for this task because this task's game was not raised" + redirect_to task_path(@task), notice: "You can't raise hints for this task because this task's game was not started" end end diff --git a/app/views/codes/new.html.slim b/app/views/codes/new.html.slim index 2a78981..eefa359 100644 --- a/app/views/codes/new.html.slim +++ b/app/views/codes/new.html.slim @@ -2,3 +2,4 @@ div class="page-header" h1=t '.title', :default => [:'helpers.titles.new', 'New %{model}'], :model => model_class.model_name.human.titleize = render :partial => "form" +div.alert.alert-info= "NOTE: All codes are assigned to one task must be unique" \ No newline at end of file diff --git a/app/views/games/_form.html.slim b/app/views/games/_form.html.slim index f2e8c72..add33eb 100644 --- a/app/views/games/_form.html.slim +++ b/app/views/games/_form.html.slim @@ -11,3 +11,4 @@ = f.submit nil, :class => 'btn btn-primary' ' = link_to t('.cancel', :default => t("helpers.links.cancel")), games_path, :class => 'btn' +div.alert.alert-info= "NOTE: Please, enter valid date and time. I haven't validation for this field now" From 5e18bdfe25a1ca98f9987d9f6767f56813e50d8e Mon Sep 17 00:00:00 2001 From: sorgal Date: Fri, 24 Jan 2014 12:40:34 +0400 Subject: [PATCH 15/15] changing of hints controller and writitng tests for hints controller filters --- .idea/workspace.xml | 119 ++++++---------------- app/controllers/hints_controller.rb | 6 +- app/views/hints/new.html.slim | 12 +-- spec/controllers/hints_controller_spec.rb | 69 ++++++++----- 4 files changed, 83 insertions(+), 123 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f4c9e49..6d57200 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -23,28 +23,19 @@ - - - - - - - - - - + - - + + - + @@ -79,11 +70,9 @@ @@ -164,56 +155,6 @@ - + - + @@ -499,11 +440,6 @@ - - - - - @@ -529,11 +465,6 @@ - - - - - @@ -559,11 +490,6 @@ - - - - - @@ -771,21 +697,42 @@ - + - + - + - + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/controllers/hints_controller.rb b/app/controllers/hints_controller.rb index 25584a8..06627ae 100644 --- a/app/controllers/hints_controller.rb +++ b/app/controllers/hints_controller.rb @@ -21,8 +21,7 @@ def new @task = params[:task].to_i @cant_add = false if TaskHint.where(task_id: @task).count == 2 - @cant_add = true - @error = "You can assign only two hints for one task" + redirect_to task_path(@task), notice: "Only two hints can be assigned with one task" end @hint = Hint.new end @@ -93,6 +92,7 @@ def check_task if Task.find(params.require(:task).to_i).game.state != 0 redirect_to task_path(Task.find(params.require(:task).to_i)), notice: "You can't add new hints to tasks assigned with started or finished game" end + end def check_task_create @@ -111,7 +111,7 @@ def check_task_create # end #els if @task.hints.count >= 2 - redirect_to tasks_path, notice: "Only two hints can be assigned with one task" + redirect_to task_path(@task), notice: "Only two hints can be assigned with one task" end end end diff --git a/app/views/hints/new.html.slim b/app/views/hints/new.html.slim index 29e6ce7..dc96623 100644 --- a/app/views/hints/new.html.slim +++ b/app/views/hints/new.html.slim @@ -1,15 +1,7 @@ -model_class = Hint div class="page-header" - -if !@cant_add - h1=t '.title', :default => [:'helpers.titles.new', 'New %{model}'], :model => model_class.model_name.human.titleize - = render :partial => "form" - -else - -if @error - h1= @error - p - strong= 'Task:' - br - = link_to "Task", task_path(@task) + h1=t '.title', :default => [:'helpers.titles.new', 'New %{model}'], :model => model_class.model_name.human.titleize + = render :partial => "form" diff --git a/spec/controllers/hints_controller_spec.rb b/spec/controllers/hints_controller_spec.rb index 91b43b5..e5b519e 100644 --- a/spec/controllers/hints_controller_spec.rb +++ b/spec/controllers/hints_controller_spec.rb @@ -15,10 +15,23 @@ let(:valid_attributes) {build :hint, hint_text: "dzfvbzjfgnarmkgnhbmargtgbhsergnerf;"} + let(:new_hint) {create :hint} + + let!(:game) {create :game} + + let!(:task_game) {create :game_task, task: task, game: game} + + before(:each) do + @hint_post = new_hint.attributes + @hint_post[:task] = task.id + @hint_post_invalid = invalid_attributes.attributes + @hint_post_invalid[:task] = task.id + end + describe "GET index" do it "assigns all hints as @hints" do get :index, {}, valid_session - expect(assigns(:hints)).to eq([hint]) + expect(assigns(:hints)).to eq([hint, new_hint]) end end @@ -32,11 +45,6 @@ describe "GET new" do - - let!(:game) {create :game} - - let!(:task_game) {create :game_task, task: task, game: game} - it "assigns a new hint as @hint" do get :new, {task: task.id}, valid_session expect(assigns(:hint)).to be_a_new(Hint) @@ -60,36 +68,49 @@ end - describe "GET edit" do - it "assigns the requested hint as @hint" do - get :edit, {id: hint.id}, valid_session - expect(assigns(:hint)).to eq(hint) - end - end + describe "new and create actions performing when queue number <= 0 and try to create third task hint" do - describe "POST create" do - - before(:each) do - @hint_post = new_hint.attributes - @hint_post[:task] = task.id - @hint_post_invalid = invalid_attributes.attributes - @hint_post_invalid[:task] = task.id + describe "with queue_number <= 0" do + it "tries to create" do + @hint_post[:queue_number] = 0 + expect { + post :create, {:hint => @hint_post}, valid_session + }.to change(Hint, :count).by(0) + expect(response).to redirect_to(new_hint_path(task: task.id)) + end end - let(:new_hint) {create :hint, queue_number: 3 - hint.queue_number} - - describe "third task hint" do + describe "try to create third task hint and try to access new action" do let!(:new_task_hint) {create :task_hint, task: task, hint: new_hint} - it "creates a third Hint" do + it "creates third hint" do expect { - post :create, {:hint => @hint_post}, valid_session + post :create, {hint: @hint_post}, valid_session }.to change(Hint, :count).by(0) + expect(response).to redirect_to(task_path(task)) + end + + it "tries to new action access" do + get :new, {task: task.id}, valid_session + expect(response).to redirect_to(task_path(task)) end end + end + + describe "GET edit" do + it "assigns the requested hint as @hint" do + get :edit, {id: hint.id}, valid_session + expect(assigns(:hint)).to eq(hint) + end + end + + describe "POST create" do + + let(:new_hint) {create :hint, queue_number: 3 - hint.queue_number} + describe "with valid params" do it "creates a new Hint" do expect {