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/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 bcc6b51..6d57200 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -23,46 +23,19 @@ - - + + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -78,57 +51,57 @@ @@ -164,8 +137,6 @@ - - @@ -184,118 +155,26 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + @@ -473,6 +352,23 @@ + + @@ -483,14 +379,14 @@ - + - + @@ -498,7 +394,7 @@ - + @@ -511,7 +407,7 @@ - + @@ -521,9 +417,9 @@ - - + + @@ -544,359 +440,299 @@ - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - - - + - + - + - + - - - + - + - - - + - + - - - + - + - + - + - - - + - + - - - + - + - - - + - + - + - + - - - + - + - + - + - - - + - + - - - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + diff --git a/Gemfile b/Gemfile index a45a9c7..7bc0582 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', '~> 1.0.6' diff --git a/Gemfile.lock b/Gemfile.lock index 6b2ca2d..5eb0095 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 (~> 1.0.6) jbuilder (~> 1.2) jquery-rails mysql2 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 9162e18..8c30d49 100644 --- a/app/admin/dashboard.rb +++ b/app/admin/dashboard.rb @@ -1,14 +1,8 @@ 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 - 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. # @@ -31,6 +25,6 @@ # end end # content controller do - layout 'application' # <-- here + layout 'application' end end 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_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/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_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/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/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/code_compares_controller.rb b/app/controllers/code_compares_controller.rb index c85d6bc..e2443b8 100644 --- a/app/controllers/code_compares_controller.rb +++ b/app/controllers/code_compares_controller.rb @@ -1,17 +1,17 @@ class CodeComparesController < ApplicationController before_action :set_code_compare, only: [:destroy] skip_before_filter :authorize_admin - def create - @code_compare = CodeCompare.new(code_compare_params) + before_filter :check_code_compare_creation, only: [:create] + before_filter :authenticate_user! + + def create + @task = current_user.tasks.last + @game = @task.game + notice = @game.process(current_user, @task, params[:try_text]) 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: @game.id), notice: notice } + format.json { head :no_content } end end @@ -24,13 +24,24 @@ 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 + + + protected - # 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) + def check_code_compare_creation + unless params[:try_text] && params[:task] + @game = @task.game + redirect_to game_path(id: @game.game) end -end + end + +end \ No newline at end of file diff --git a/app/controllers/codes_controller.rb b/app/controllers/codes_controller.rb index e7cd73b..f71eaad 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 @@ -13,10 +13,12 @@ def index # GET /codes/1 # GET /codes/1.json def show + @task = Task.find(@code.task_code.task_id) end # GET /codes/new def new + @task = params[:task].to_i @code = Code.new end @@ -28,10 +30,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 +81,17 @@ def code_params protected - def check_game - unless params.require(:game) + 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_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..cb1b9f4 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -1,6 +1,10 @@ class GamesController < ApplicationController - before_action :set_game, only: [:show, :edit, :update, :destroy] + 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_and_game, only: [:start_game] + before_filter :check_started, only: [:finish_game] # GET /games # GET /games.json @@ -11,6 +15,21 @@ def index # GET /games/1 # GET /games/1.json def show + @status = "" + if @game.state == 0 + @status = "Wait" + elsif @game.state > 0 + @status = "Started" + 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 end # GET /games/new @@ -31,7 +50,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 @@ -73,7 +92,60 @@ 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) + def game_params + params.require(:game).permit(:title, :start_date) + end + + public + #Заглушки для отложенных задач + def start_game + @game.start_game + redirect_to game_path(@game), notice: "Game was started" + end + + def finish_game + @game.finish_game + redirect_to game_path(@game), notice: "Game was finished" + end + + protected + + def check_game_status + if user_signed_in? && @game.state <= 0 + redirect_to root_path + end + end + + def authenticate_admin_or_user + unless user_signed_in? || current_admin_user + redirect_to root_path + end end + + 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 + end diff --git a/app/controllers/hints_controller.rb b/app/controllers/hints_controller.rb index 5650bf3..06627ae 100644 --- a/app/controllers/hints_controller.rb +++ b/app/controllers/hints_controller.rb @@ -1,9 +1,8 @@ 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] # GET /hints # GET /hints.json @@ -14,12 +13,17 @@ def index # GET /hints/1 # GET /hints/1.json def show + @task = Task.find(@hint.task_hint.task_id) end # GET /hints/new def new + @task = params[:task].to_i + @cant_add = false + if TaskHint.where(task_id: @task).count == 2 + redirect_to task_path(@task), notice: "Only two hints can be assigned with one task" + end @hint = Hint.new - @game = params[:game] end # GET /hints/1/edit @@ -29,11 +33,11 @@ def edit # POST /hints # POST /hints.json def create + task = params.require(:hint)[:task].to_i @hint = Hint.new(hint_params) - game = params.require(:hint)[:game].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,15 +85,34 @@ def hint_params protected - def check_game - unless params.require(:game) + 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_game_create - unless params.require(:hint) - redirect_to games_path + def check_task_create + unless params.require(:hint)[:task] + redirect_to tasks_path + 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 greater than" + params.require(:hint)[:queue_number] + # end + #els + if @task.hints.count >= 2 + redirect_to task_path(@task), notice: "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 7c3997a..92b26fc 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -3,23 +3,9 @@ class HomeController < ApplicationController before_filter :authenticate_user!, only: [:game_code_compares, :create_code_compare] 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" + if user_signed_in? + @games = Game.where("`state` >= ?", 0) + @user_games = current_user.user_games 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..74af992 --- /dev/null +++ b/app/controllers/tasks_controller.rb @@ -0,0 +1,118 @@ +class TasksController < ApplicationController + 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 + @tasks = Task.all + end + + # GET /tasks/1 + # GET /tasks/1.json + def show + @game = @task.game + end + + # GET /tasks/new + def new + @game = params[:game].to_i + @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 + 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 + 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 + + 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 started" + end + end + + public + #еще заглушка + def raise_hint + 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 3c97edb..75598d3 100644 --- a/app/controllers/user_games_controller.rb +++ b/app/controllers/user_games_controller.rb @@ -3,13 +3,18 @@ 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) @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 + i += 1 end end @@ -30,15 +35,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 was join in game with success.' } format.json { render action: 'index', status: :created, location: @user_game } else format.html { redirect_to root_path } @@ -74,21 +78,28 @@ 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 def check_game_create - unless params.require(:user_game) + unless params.require(:game_id) redirect_to root_path end 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/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_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/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/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/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..91fefba 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -2,9 +2,126 @@ 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 + has_many :game_tasks, dependent: :destroy + has_many :tasks, through: :game_tasks - validate :title, :start_date, :duration, presence: true + 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 + 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 + notice = "Code was matched" + if all_codes_input? + 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 + end + end + + unless notice + notice = "Code or pass was not matched" + end + @notice = notice + end + + def all_codes_input? + if get_task_codes_count(@task, @user)[1] == 0 + 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) + change_task_result(points) + if has_more_tasks? + assign_next_task + "Next task assigned" + else + end_game + "Game completed" + end + end + + def has_more_tasks? + untaken_tasks.any? + end + + 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 + 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 ) + 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.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| + 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 + + 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/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..e42de19 --- /dev/null +++ b/app/models/game_task.rb @@ -0,0 +1,4 @@ +class GameTask < ActiveRecord::Base + belongs_to :game + belongs_to :task +end \ No newline at end of file diff --git a/app/models/hint.rb b/app/models/hint.rb index 6e77c9b..c4d1774 100644 --- a/app/models/hint.rb +++ b/app/models/hint.rb @@ -1,5 +1,10 @@ class Hint < ActiveRecord::Base - has_one :game_hint, dependent: :destroy + has_one :task_hint, dependent: :destroy + has_many :user_hints, dependent: :destroy + validates :hint_text, :queue_number, presence: true + #validates :queue_number, inclusion: {in: [1, 2]} + + RAISED = 1 + NOT = 0 - 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..bcf3123 --- /dev/null +++ b/app/models/task.rb @@ -0,0 +1,28 @@ +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})} + + def raise_hint + @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) + 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/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..e24c5d5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,6 +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 4ce4cda..d460448 100644 --- a/app/models/user_game.rb +++ b/app/models/user_game.rb @@ -2,6 +2,10 @@ class UserGame < ActiveRecord::Base belongs_to :user belongs_to :game - validate :user_id, :game_id, :result, presence: true + 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_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/models/user_task.rb b/app/models/user_task.rb new file mode 100644 index 0000000..add9f60 --- /dev/null +++ b/app/models/user_task.rb @@ -0,0 +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_message_controller.rb b/app/realtime/realtime_message_controller.rb new file mode 100644 index 0000000..e69de29 diff --git a/app/views/codes/_form.html.slim b/app/views/codes/_form.html.slim index 8457f0a..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 :game, value: @game + = f.hidden_field :task, value: @task .form-actions = f.submit nil, :class => 'btn btn-primary' ' diff --git a/app/views/codes/index.html.slim b/app/views/codes/index.html.slim index fae20d7..a9b85cd 100644 --- a/app/views/codes/index.html.slim +++ b/app/views/codes/index.html.slim @@ -4,20 +4,20 @@ 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= "Task (follow if you want add code to this task)" 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 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' ' = 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' - +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/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/codes/show.html.slim b/app/views/codes/show.html.slim index 8cbd14d..c3ca900 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 (follow if you want add new code to this task):' + br + = 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/_code_compare_form.html.slim b/app/views/games/_code_compare_form.html.slim new file mode 100644 index 0000000..1f41304 --- /dev/null +++ b/app/views/games/_code_compare_form.html.slim @@ -0,0 +1,9 @@ += form_tag "/code_compares/", method: "post" + .control-group + = label_tag "try_text", 'Try text', :class => 'control-label' + .controls + = text_field_tag "try_text", nil, :class => 'text_field' + .controls + = hidden_field_tag "task", :value => @task.id + .contrils + = submit_tag "Try" \ No newline at end of file diff --git a/app/views/games/_form.html.slim b/app/views/games/_form.html.slim index aa73253..add33eb 100644 --- a/app/views/games/_form.html.slim +++ b/app/views/games/_form.html.slim @@ -7,11 +7,8 @@ = 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' ' = 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" diff --git a/app/views/games/index.html.slim b/app/views/games/index.html.slim index 036bb96..d91b165 100644 --- a/app/views/games/index.html.slim +++ b/app/views/games/index.html.slim @@ -4,24 +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(:duration) - 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= game.title + -else + td= link_to game.title, game_path(game) 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' - ' - = 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 + -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 t('.new', :default => t("helpers.links.new")), new_game_path, :class => 'btn btn-primary' + = 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 btn-mini' + + = 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 f23d5e4..f737949 100644 --- a/app/views/games/show.html.slim +++ b/app/views/games/show.html.slim @@ -1,25 +1,51 @@ - 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 +//-if notice + div.alert.alert-danger = notice +-if current_admin_user + p + strong= model_class.human_attribute_name(:start_date) + ':' + br + = @game.start_date + 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' -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 + = link_to "Add task", new_task_path(game: @game.id), :class => 'btn' -.form-actions - = link_to t('.back', :default => t("helpers.links.back")), games_path, :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' + +-elsif user_signed_in? + -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 - = 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' diff --git a/app/views/hints/_form.html.slim b/app/views/hints/_form.html.slim index 3cc8f97..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 :game, 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/hints/index.html.slim b/app/views/hints/index.html.slim index 64bda85..589b851 100644 --- a/app/views/hints/index.html.slim +++ b/app/views/hints/index.html.slim @@ -4,22 +4,23 @@ 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= "Task (follow if you want add hint to this task)" 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 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' ' = 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' - +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/new.html.slim b/app/views/hints/new.html.slim index 8ac3066..dc96623 100644 --- a/app/views/hints/new.html.slim +++ b/app/views/hints/new.html.slim @@ -1,4 +1,8 @@ -- model_class = Hint +-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" + = render :partial => "form" + + + + diff --git a/app/views/hints/show.html.slim b/app/views/hints/show.html.slim index a751d4d..3c43eb3 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 (follow, if you want add hint to this task) :' + br + = 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/home/_form.html.slim b/app/views/home/_form.html.slim deleted file mode 100644 index 15ff142..0000000 --- a/app/views/home/_form.html.slim +++ /dev/null @@ -1,7 +0,0 @@ -= form_tag "home/create_code_compare", 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 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/home/index.html.slim b/app/views/home/index.html.slim index 23a8d94..40cd93c 100644 --- a/app/views/home/index.html.slim +++ b/app/views/home/index.html.slim @@ -2,22 +2,25 @@ 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 - //td= link_to game.id, game_path(game) + -if game.state != 0 + -next 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' + -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" diff --git a/app/views/layouts/application.html.slim b/app/views/layouts/application.html.slim index 4d48d5c..0cd1135 100644 --- a/app/views/layouts/application.html.slim +++ b/app/views/layouts/application.html.slim @@ -21,23 +21,41 @@ html lang="en" body - .navbar.navbar-fixed-top + .navbar .navbar-inner .container - ul.nav + 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 + 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 .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", hints_path \ 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..61d7e31 --- /dev/null +++ b/app/views/tasks/_form.html.slim @@ -0,0 +1,14 @@ += 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' + = f.hidden_field :game, value: @game + .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..e7f41ed --- /dev/null +++ b/app/views/tasks/index.html.slim @@ -0,0 +1,31 @@ +- 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= "Show" + th= model_class.human_attribute_name(:task_text) + th= model_class.human_attribute_name(:points) + th= "Game (follow if you want add task to this game)" + th=t '.actions', :default => t("helpers.actions") + tbody + - @tasks.each do |task| + tr + 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' + + = 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/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..ad3e02c --- /dev/null +++ b/app/views/tasks/show.html.slim @@ -0,0 +1,30 @@ +- 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 + = @task.task_text +p + strong= model_class.human_attribute_name(:points) + ':' + br + = @task.points +p + strong= 'Game (follow if you want add new task to this game):' + br + = link_to @game.title, game_path(@game) + +.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 "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/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..602311c 100644 --- a/app/views/user_games/index.html.slim +++ b/app/views/user_games/index.html.slim @@ -4,18 +4,25 @@ 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(:start_date) 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 - 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' + -if game[:game].state >0 + td + = link_to game[:game].title, game_path({id: game[:game].id}), :method => :get + -else + td= game[:game].title + td= game[:game].start_date + td + -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/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/initializers/active_admin.rb b/config/initializers/active_admin.rb index efbb1d0..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' @@ -233,5 +232,4 @@ # config.filters = true config.skip_before_filter :authorize_admin - end diff --git a/config/routes.rb b/config/routes.rb index a71daaa..07660f3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,37 +1,73 @@ 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' + 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" + 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 ActiveAdmin.routes(self) 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/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/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/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 1e8943c..2c27142 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: 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" @@ -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 @@ -83,10 +76,9 @@ 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: -1 + t.integer "state", default: 0 end create_table "hints", force: true do |t| @@ -94,14 +86,52 @@ t.datetime "created_at" t.datetime "updated_at" t.integer "queue_number" + t.integer "raised", default: 0 + 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" + 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.datetime "created_at" + t.datetime "updated_at" end create_table "users", force: true do |t| diff --git a/spec/controllers/admin_games_controller_spec.rb b/spec/controllers/admin_games_controller_spec.rb index 0a4e9e0..fcc0fe6 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_user: @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.attributes}, 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 c070fab..92127e7 100644 --- a/spec/controllers/code_compares_controller_spec.rb +++ b/spec/controllers/code_compares_controller_spec.rb @@ -1,56 +1,86 @@ 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) - @code_compare = FactoryGirl.create(:code_compare, user_id: @user_id, code_id: @code.to_param) - 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, {:code_compare => @code_compare.attributes}, 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 end the game of current user" do + 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.code_string + "sefbgdfgbnfg", task: task}, valid_session + expect(response).to redirect_to(game_path(game)) + end + + 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 + + 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" } + + 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 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 -end +end \ No newline at end of file diff --git a/spec/controllers/codes_controller_spec.rb b/spec/controllers/codes_controller_spec.rb index bc7731c..46371f6 100644 --- a/spec/controllers/codes_controller_spec.rb +++ b/spec/controllers/codes_controller_spec.rb @@ -1,100 +1,116 @@ 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 - @game = FactoryGirl.create(:game) - @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"} + + 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 - expect(assigns(:codes)).to eq([@code]) + expect(assigns(:codes)).to eq([code]) end 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.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 + + let!(:game) {create :game} + + let!(:task_game) {create :game_task, task: task, game: game} + it "assigns a new code as @code" do - get :new, {game: @game.to_param}, valid_session + 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 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.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.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 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 => @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 @@ -102,40 +118,34 @@ describe "PUT update" do describe "with valid params" do - before (:each) do - @valid_attributes = FactoryGirl.build(:code, code_string: "dzfvbzjfgnarmkgnhbmargtgbhsergnerf;").attributes - 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 @@ -144,12 +154,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_codes_controller_spec.rb b/spec/controllers/game_codes_controller_spec.rb deleted file mode 100644 index fab2441..0000000 --- a/spec/controllers/game_codes_controller_spec.rb +++ /dev/null @@ -1,57 +0,0 @@ -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 GameCodesController 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 - # adjust the attributes here as well. - let(:valid_attributes) { { "create" => "MyString" } } - - # 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. - 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) - end - - describe "POST create" do - describe "with valid params" do - it "creates a new GameCode" do - expect { - post :create, {:game_code => @game_code.attributes}, valid_session - }.to change(GameCode, :count).by(1) - end - end - end - - describe "DELETE destroy" do - it "destroys the requested game_code" do - expect { - delete :destroy, {:id => @game_code.to_param}, valid_session - }.to change(GameCode, :count).by(-1) - end - end - -end diff --git a/spec/controllers/game_hints_controller_spec.rb b/spec/controllers/game_hints_controller_spec.rb deleted file mode 100644 index 22e2b99..0000000 --- a/spec/controllers/game_hints_controller_spec.rb +++ /dev/null @@ -1,57 +0,0 @@ -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 GameHintsController 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 - # 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. - 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) - end - - describe "POST create" do - describe "with valid params" do - it "creates a new GameHint" do - expect { - post :create, {:game_hint => @game_hint.attributes}, valid_session - }.to change(GameHint, :count).by(1) - end - end - end - - describe "DELETE destroy" do - it "destroys the requested game_hint" do - expect { - delete :destroy, {:id => @game_hint.to_param}, valid_session - }.to change(GameHint, :count).by(-1) - end - end - -end diff --git a/spec/controllers/game_tasks_controller_spec.rb b/spec/controllers/game_tasks_controller_spec.rb new file mode 100644 index 0000000..cd90c6c --- /dev/null +++ b/spec/controllers/game_tasks_controller_spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper' + +describe GameTasksController do + login_admin + + let(:valid_attributes) { { "game_id" => "1" } } + + let(:valid_session) { {} } + + 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 + }.to change(GameTask, :count).by(1) + end + end + end + + describe "DELETE destroy" do + it "destroys the requested game_task" do + expect { + delete :destroy, {id: game_task.id}, valid_session + }.to change(GameTask, :count).by(-1) + end + end + +end diff --git a/spec/controllers/games_controller_spec.rb b/spec/controllers/games_controller_spec.rb index d5abbf8..df93203 100644 --- a/spec/controllers/games_controller_spec.rb +++ b/spec/controllers/games_controller_spec.rb @@ -1,158 +1,262 @@ 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) - @invalid_attributes = FactoryGirl.build(:game, title: "впирапеи", start_date: "01.01.2014 00:00:00".to_datetime, duration: 11).attributes - end + let!(:game) {create :game} - describe "GET index" do - it "assigns all games as @games" do - get :index, {}, valid_session - expect(assigns(:games)).to eq([@game]) - end - end + let!(:task) {create :task} - describe "GET show" do - it "assigns the requested game as @game" do - get :show, {:id => @game.to_param}, valid_session - expect(assigns(:game)).to eq(@game) - end - end + let!(:game_task) {create :game_task, game: game, task: task} - 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 + let(:invalid_attributes) {build :game, title: "впирапеи"} - 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) + 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 + get :show, {id: game.id}, valid_session + expect(response).to redirect_to(root_path) + end end end - describe "POST create" do - describe "with valid params" do - it "creates a new Game" do - expect { - post :create, {:game => @game.attributes}, valid_session - }.to change(Game, :count).by(1) + describe "with sign in" do + login_admin + + 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 - it "assigns a newly created game as @game" do - post :create, {:game => @game.attributes}, valid_session - expect(assigns(:game)).to be_a(Game) - expect(assigns(:game)).to be_persisted + describe "GET show" do + login_user + + let!(:code_compare) {create :code_compare, code: code, user: @user} + + 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 => @game.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 - # 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 + 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 - # 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 - 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 - 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 + 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.to_param, :game => @valid_attributes}, 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.to_param, :game => @valid_attributes}, 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 - # 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) + 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 - 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 - expect(response).to render_template("edit") + 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 - end - describe "DELETE destroy" do - it "destroys the requested game" do - expect { - delete :destroy, {:id => @game.to_param}, valid_session - }.to change(Game, :count).by(-1) + 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 + + it "redirects to the games list" do + delete :destroy, {id: game.id}, valid_session + expect(response).to redirect_to(games_url) + end end - it "redirects to the games list" do - delete :destroy, {:id => @game.to_param}, valid_session - expect(response).to redirect_to(games_url) + describe "Start and finish game" do + + let!(:user) {create :user} + + let!(:user_game) {create :user_game, game: game, user: user} + + describe "Start game" do + 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 + 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 + end + + 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 + + describe "Finish game" do + + 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} + + 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) + 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 + + 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 - end diff --git a/spec/controllers/hints_controller_spec.rb b/spec/controllers/hints_controller_spec.rb index f8b9d12..e5b519e 100644 --- a/spec/controllers/hints_controller_spec.rb +++ b/spec/controllers/hints_controller_spec.rb @@ -1,102 +1,145 @@ 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) - @game = FactoryGirl.create(:game) - @hint = FactoryGirl.create(:hint, queue_number: queue_number) - @invalid_attributes = FactoryGirl.build(:hint, hint_text: "акпыук").attributes + 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;"} + + 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 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, {game: @game.to_param}, valid_session + 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 "new and create actions performing when queue number <= 0 and try to create third task hint" do + + 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 + + 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 third hint" do + expect { + 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.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 "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 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 => @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 @@ -104,43 +147,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 @@ -149,12 +181,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/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/task_codes_controller_spec.rb b/spec/controllers/task_codes_controller_spec.rb new file mode 100644 index 0000000..94ff466 --- /dev/null +++ b/spec/controllers/task_codes_controller_spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper' + +describe TaskCodesController do + login_admin + + let(:valid_attributes) { { "task_id" => "1" } } + + let(:valid_session) { {} } + + let(:task) {create :task} + + 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 + }.to change(TaskCode, :count).by(1) + end + end + end + + describe "DELETE destroy" do + it "destroys the requested task_code" do + expect { + delete :destroy, {id: task_code.id}, valid_session + }.to change(TaskCode, :count).by(-1) + end + 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..77cdbee --- /dev/null +++ b/spec/controllers/task_hints_controller_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +describe TaskHintsController do + login_admin + + let(:valid_attributes) { { "task_id" => "1" } } + let(:valid_session) { {} } + + 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_hint.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.id}, 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..1243284 --- /dev/null +++ b/spec/controllers/tasks_controller_spec.rb @@ -0,0 +1,255 @@ +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) { {} } + + 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!(: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: "впирапеи"} + + 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]) + end + end + + describe "GET show" do + it "assigns the requested task as @task" do + 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.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 + it "assigns the requested task as @task" do + 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.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 + }.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 + 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 + 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 + + it "updates the requested task" do + expect_any_instance_of(Task).to receive(:update).with({ "task_text" => "arghbstghset" }) + put :update, {id: task.id, :task => { "task_text" => "arghbstghset" }}, valid_session + end + + it "assigns the requested task as @task" do + 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.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 + Task.any_instance.stub(:save).and_return(false) + 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 + Task.any_instance.stub(:save).and_return(false) + put :update, {id: task.id, task: invalid_attributes.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.id}, valid_session + }.to change(Task, :count).by(-1) + end + + it "redirects to the tasks list" do + delete :destroy, {id: task.id}, valid_session + expect(response).to redirect_to(tasks_url) + end + end + + describe "Raise hint" do + + 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 "game is started" do + before(:each) do + 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 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) + end + + 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(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 diff --git a/spec/controllers/user_games_controller_spec.rb b/spec/controllers/user_games_controller_spec.rb index 4ab416b..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, {:user_game => @user_game.attributes}, 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, {:user_game => @user_game.attributes}, 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, {:user_game => @user_game.attributes}, 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, {:user_game => @invalid_attributes}, 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, {:user_game => @invalid_attributes}, 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).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 new file mode 100644 index 0000000..fa28ce1 --- /dev/null +++ b/spec/controllers/user_hints_controller_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper' + +describe UserHintsController do + login_user + + let(:valid_attributes) { { "user_id" => "1" } } + + let(:valid_session) { {} } + + 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 + }.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.id}, valid_session + }.to change(UserHint, :count).by(-1) + end + end + +end diff --git a/spec/controllers/user_tasks_controller_spec.rb b/spec/controllers/user_tasks_controller_spec.rb new file mode 100644 index 0000000..60e08ca --- /dev/null +++ b/spec/controllers/user_tasks_controller_spec.rb @@ -0,0 +1,32 @@ +require 'spec_helper' + +describe UserTasksController do + login_user + + let(:valid_attributes) { { "user_id" => "1" } } + + let(:valid_session) { {} } + + 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 + }.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.id}, valid_session + }.to change(UserTask, :count).by(-1) + end + 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/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/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/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/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_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 new file mode 100644 index 0000000..86ddf0d --- /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/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_codes_helper_spec.rb b/spec/helpers/game_codes_helper_spec.rb deleted file mode 100644 index f4cec26..0000000 --- a/spec/helpers/game_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 GameCodesHelper. For example: -# -# describe GameCodesHelper 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 - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/helpers/game_hints_helper_spec.rb b/spec/helpers/game_hints_helper_spec.rb deleted file mode 100644 index 43d31a3..0000000 --- a/spec/helpers/game_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 GameHintsHelper. For example: -# -# describe GameHintsHelper 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 - 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/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/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_code_spec.rb b/spec/models/game_code_spec.rb deleted file mode 100644 index 21305ed..0000000 --- a/spec/models/game_code_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'spec_helper' - -describe GameCode do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/models/game_hint_spec.rb b/spec/models/game_hint_spec.rb deleted file mode 100644 index 7d1e3a6..0000000 --- a/spec/models/game_hint_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'spec_helper' - -describe GameHint do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/models/game_spec.rb b/spec/models/game_spec.rb index c273778..8bb3bd8 100644 --- a/spec/models/game_spec.rb +++ b/spec/models/game_spec.rb @@ -1,9 +1,156 @@ require 'spec_helper' describe Game do + + 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_1 = FactoryGirl.create(:game) + 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(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.id).state).to eq(-1) + end + + it "creates a new CodeCompare with code mismatched result" do + 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.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 "more then one code" do + + let!(:new_code) {create :code, code_string: "dfbdfbndfgnhdfgdfh"} + + describe "with second code" do - game_1.should be_valid + 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 + + 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 + describe "Start game" do + it "execute action " do + expect { + 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.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.id, game_id: game.id).first.state).to equal(UserGame::CURRENT) + end + + end + + describe "Finish game" do + + 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 + 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.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.id, game_id: game.id).first.result).to eq(user_task.result + user_task1.result) + end + + 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 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_spec.rb b/spec/models/task_spec.rb new file mode 100644 index 0000000..c8a7bb0 --- /dev/null +++ b/spec/models/task_spec.rb @@ -0,0 +1,67 @@ +require 'spec_helper' + +describe Task do + + 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 + 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.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.id).update_attribute(:raised, Hint::RAISED) + end + it "raises second hint" do + expect { + task.raise_hint + }.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 { + 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 + +end diff --git a/spec/models/user_game_spec.rb b/spec/models/user_game_spec.rb deleted file mode 100644 index 444a62d..0000000 --- a/spec/models/user_game_spec.rb +++ /dev/null @@ -1,9 +0,0 @@ -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 -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_codes_routing_spec.rb b/spec/routing/game_codes_routing_spec.rb deleted file mode 100644 index 15124ec..0000000 --- a/spec/routing/game_codes_routing_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require "spec_helper" - -describe GameCodesController do - describe "routing" do - - it "routes to #create" do - expect(:post => "/game_codes").to route_to("game_codes#create") - end - - it "routes to #destroy" do - expect(:delete => "/game_codes/1").to route_to("game_codes#destroy", :id => "1") - end - - end -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_tasks_routing_spec.rb b/spec/routing/game_tasks_routing_spec.rb new file mode 100644 index 0000000..7d61436 --- /dev/null +++ b/spec/routing/game_tasks_routing_spec.rb @@ -0,0 +1,22 @@ +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 + expect(:post => "/game_tasks").to route_to("game_tasks#create") + end + + it "routes to #destroy" do + expect(:delete => "/game_tasks/#{game_task.id}").to route_to("game_tasks#destroy", :id => "#{game_task.id}") + end + + 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 new file mode 100644 index 0000000..a02f47e --- /dev/null +++ b/spec/routing/task_codes_routing_spec.rb @@ -0,0 +1,22 @@ +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 + expect(:post => "/task_codes").to route_to("task_codes#create") + end + + it "routes to #destroy" do + expect(:delete => "/task_codes/#{task_code.id}").to route_to("task_codes#destroy", :id => "#{task_code.id}") + 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..85f1c58 --- /dev/null +++ b/spec/routing/task_hints_routing_spec.rb @@ -0,0 +1,22 @@ +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 + expect(:post => "/task_hints").to route_to("task_hints#create") + end + + it "routes to #destroy" do + expect(:delete => "/task_hints/#{task_hint.id}").to route_to("task_hints#destroy", :id => "#{task_hint.id}") + 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..55329cb --- /dev/null +++ b/spec/routing/tasks_routing_spec.rb @@ -0,0 +1,38 @@ +require "spec_helper" + +describe TasksController do + + let!(:task) {create :task} + + 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/#{task.id}").to route_to("tasks#show", :id => "#{task.id}") + end + + it "routes to #edit" do + expect(:get => "/tasks/#{task.id}/edit").to route_to("tasks#edit", :id => "#{task.id}") + end + + it "routes to #create" do + expect(:post => "/tasks").to route_to("tasks#create") + end + + it "routes to #update" do + expect(:put => "/tasks/#{task.id}").to route_to("tasks#update", :id => "#{task.id}") + end + + it "routes to #destroy" do + expect(:delete => "/tasks/#{task.id}").to route_to("tasks#destroy", :id => "#{task.id}") + end + + 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 new file mode 100644 index 0000000..8b3b74e --- /dev/null +++ b/spec/routing/user_hints_routing_spec.rb @@ -0,0 +1,22 @@ +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 + expect(:post => "/user_hints").to route_to("user_hints#create") + end + + it "routes to #destroy" do + expect(:delete => "/user_hints/#{user_hint.id}").to route_to("user_hints#destroy", :id => "#{user_hint.id}") + 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..013931a --- /dev/null +++ b/spec/routing/user_tasks_routing_spec.rb @@ -0,0 +1,22 @@ +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 + expect(:post => "/user_tasks").to route_to("user_tasks#create") + end + + it "routes to #destroy" do + expect(:delete => "/user_tasks/#{user_task.id}").to route_to("user_tasks#destroy", :id => "#{user_task.id}") + end + + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index eda9a2d..a925dd9 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -48,9 +48,13 @@ # --seed 1234 config.order = "random" - config.include Devise::TestHelpers, :type => :controller - 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 ff059c5..d0d6788 100644 --- a/spec/support/controller_macros.rb +++ b/spec/support/controller_macros.rb @@ -1,25 +1,24 @@ 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}") # Using factory girl as an example + @admin = admin sign_in @admin - @admin_id = @admin.to_param + end + after(:each) do + AdminUser.find(@admin.id).destroy end end def login_user + let!(:user) {create :user} before(:each) do - @request.env["devise.mapping"] = Devise.mappings[:user] - @user = FactoryGirl.create(:user) - #@user.confirm! + @user = user 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 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 62dc348..0000000 --- a/spec/views/hints/new.html.slim_spec.rb +++ /dev/null @@ -1,15 +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 - 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/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 ec6d6b2..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