From ee144aea7ae7552f930c79e9427e5c09cc5ee41a Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 11 Aug 2016 02:26:48 +0000 Subject: [PATCH 1/9] Show all games on Index page --- app/controllers/games_controller.rb | 12 +++++++----- app/views/games/index.html.erb | 15 +++++---------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index b608a4a..937886c 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -1,23 +1,25 @@ # Main controller for chess session for CRUD # logic for our app class GamesController < ApplicationController - before_action :authenticate_user!, only: [:create, :new] + before_action :authenticate_user!, only: [:create] def index + @games = Game.all end def create @game = Game.new + # set game_status + @game.save! + @game.white.user_id = current_user.id + @game.white.save! + @game.populate! end def show @piece_positions = Game.find(params[:id]).pieces end - def new - @game = Game.new - end - def update @game = Game.find(params[:id]) @piece = @game.pieces.find(params[:piece_id]) diff --git a/app/views/games/index.html.erb b/app/views/games/index.html.erb index 1b7f355..89d1351 100644 --- a/app/views/games/index.html.erb +++ b/app/views/games/index.html.erb @@ -6,15 +6,10 @@

Your opponent awaits you! Join the battle!

-

Available gameJoin game!

-
-

Available gameJoin game!

-
-

Available gameJoin game!

-
-

Available gameJoin game!

-
-

Available gameJoin game!

+ <% @games.each do |game| %> +

Available game<%= button_to "Join game!", game_path(game), class: "btn btn-success pull-right" %>

+
+ <% end %>
@@ -25,7 +20,7 @@ User Dashboard
- Create a Game + <%= link_to "Create a Game", games_path, method: :post, class: "center-block btn btn-success btn-lg dash-button" %>
Account Settings From 22ff3af73b25d8b94459b037040241468634c5cd Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 11 Aug 2016 18:39:36 +0000 Subject: [PATCH 2/9] Hook-up create & join game actions --- app/controllers/games_controller.rb | 14 +++++++++----- app/views/games/index.html.erb | 2 +- config/routes.rb | 1 + 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/app/controllers/games_controller.rb b/app/controllers/games_controller.rb index 937886c..0ebd993 100644 --- a/app/controllers/games_controller.rb +++ b/app/controllers/games_controller.rb @@ -8,12 +8,10 @@ def index end def create - @game = Game.new - # set game_status - @game.save! - @game.white.user_id = current_user.id - @game.white.save! + @game = Game.create + @game.white.update(user_id: current_user.id) @game.populate! + redirect_to game_path(@game) end def show @@ -28,6 +26,12 @@ def update @piece.move!(x, y) end + def join + @game = Game.find(params[:id]) + @game.black.update(user_id: current_user.id) + redirect_to game_path(@game) + end + private def game_params diff --git a/app/views/games/index.html.erb b/app/views/games/index.html.erb index 89d1351..3b7c9b7 100644 --- a/app/views/games/index.html.erb +++ b/app/views/games/index.html.erb @@ -7,7 +7,7 @@

Your opponent awaits you! Join the battle!

<% @games.each do |game| %> -

Available game<%= button_to "Join game!", game_path(game), class: "btn btn-success pull-right" %>

+

Available game<%= button_to "Join game!", join_game_path(game), class: "btn btn-success pull-right" %>


<% end %>
diff --git a/config/routes.rb b/config/routes.rb index 8686462..aca21d7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,7 @@ root 'static_pages#index' resources :games + post 'games/:id', to: 'games#join', as: :join_game resources :players, only: [:show] From 4c707c41ab69c2e884538068cf2ced336b8a456a Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 11 Aug 2016 20:08:47 +0000 Subject: [PATCH 3/9] Add username column to users table --- db/migrate/20160811200617_add_username_to_users.rb | 5 +++++ db/schema.rb | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20160811200617_add_username_to_users.rb diff --git a/db/migrate/20160811200617_add_username_to_users.rb b/db/migrate/20160811200617_add_username_to_users.rb new file mode 100644 index 0000000..d110a94 --- /dev/null +++ b/db/migrate/20160811200617_add_username_to_users.rb @@ -0,0 +1,5 @@ +class AddUsernameToUsers < ActiveRecord::Migration[5.0] + def change + add_column :users, :username, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 97891e4..83d4ec0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160806192848) do +ActiveRecord::Schema.define(version: 20160811200617) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -59,6 +59,7 @@ t.inet "last_sign_in_ip" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "username" t.index ["email"], name: "index_users_on_email", unique: true, using: :btree t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree end From 75d8461d0fbbdf1a338712ea500ee89dffbd86bc Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 11 Aug 2016 20:43:46 +0000 Subject: [PATCH 4/9] Add support for username in Devise --- app/controllers/application_controller.rb | 9 +++++++++ app/models/user.rb | 17 +++++++++++++++++ app/views/devise/registrations/new.html.erb | 1 + app/views/games/index.html.erb | 2 +- app/views/players/show.html.erb | 2 +- app/views/static_pages/index.html.erb | 6 +++--- config/initializers/devise.rb | 4 ++-- 7 files changed, 34 insertions(+), 7 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4a058de..86a5b4c 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,4 +3,13 @@ class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception + before_action :configure_permitted_parameters, if: :devise_controller? + + protected + + def configure_permitted_parameters + added_attrs = [:username, :email, :password, :password_confirmation, :remember_me] + devise_parameter_sanitizer.permit :sign_up, keys: added_attrs + devise_parameter_sanitizer.permit :account_update, keys: added_attrs + end end diff --git a/app/models/user.rb b/app/models/user.rb index ec0ce95..9b73925 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,4 +7,21 @@ class User < ActiveRecord::Base has_many :players has_many :games, through: :players + + def login=(login) + @login = login + end + + def login + @login || self.username || self.email + end + + def self.find_for_database_authentication(warden_conditions) + conditions = warden_conditions.dup + if login = conditions.delete(:login) + where(conditions.to_hash).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first + elsif conditions.has_key?(:username) || conditions.has_key?(:email) + where(conditions.to_hash).first + end + end end diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index 79feab5..7ab1fbf 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -3,6 +3,7 @@ <%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> <%= f.error_notification %> <%= f.email_field :email, required: true, autofocus: true, class:'devise-inputs', placeholder: 'Email Address' %> + <%= f.text_field :username, class:'devise-inputs', placeholder: 'Username' %> <%= f.password_field :password, required: true, class:'devise-inputs', placeholder: 'Password', hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length) %> <%= f.password_field :password_confirmation, required: true, class: 'devise-inputs', placeholder:'Confirm Password' %> <%= f.button :submit, "Sign up", class: 'btn btn-success' %> diff --git a/app/views/games/index.html.erb b/app/views/games/index.html.erb index 3b7c9b7..63f5a9a 100644 --- a/app/views/games/index.html.erb +++ b/app/views/games/index.html.erb @@ -29,7 +29,7 @@
-

<%= current_user.email %>

+

<%= current_user.username %>

<%= image_tag 'yoda.jpg', class: 'img-responsive' %>
diff --git a/app/views/players/show.html.erb b/app/views/players/show.html.erb index d250223..012266c 100644 --- a/app/views/players/show.html.erb +++ b/app/views/players/show.html.erb @@ -3,7 +3,7 @@
<%= image_tag 'user.png', class:'img-responsive' %> -

<%= current_user.email %>

+

<%= current_user.username %>

Member Since: <%= current_user.created_at.strftime("%B %d, %Y") %>

diff --git a/app/views/static_pages/index.html.erb b/app/views/static_pages/index.html.erb index 45229c2..318c1de 100644 --- a/app/views/static_pages/index.html.erb +++ b/app/views/static_pages/index.html.erb @@ -10,15 +10,15 @@
<%= f.email_field :email, class: 'form-control', required: true, autofocus: true, placeholder: 'Email Address' %>
-
+
<%= f.password_field :password, class: 'form-control', required: true, hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length), placeholder: 'Password' %>
-
+
<%= f.password_field :password_confirmation, class: 'form-control', required: true, placeholder: 'Confirm Password' %>
-
+
<%= f.button :submit, "Create An Account", class: "btn btn-success form-control" %>
diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index a10c673..d8551af 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -39,7 +39,7 @@ # determining whether # or not authentication should be aborted when the value is # not present. - # config.authentication_keys = [:email] + config.authentication_keys = [:login] # Configure parameters from the request object used for # authentication. Each entry @@ -279,7 +279,7 @@ # first check for # "users/sessions/new". It's turned off by default because it's # slower if you are using only default views. - # config.scoped_views = false + config.scoped_views = true # Configure the default scope given to Warden. By default it's the # first devise role declared in your routes (usually :user). From 97e708b4ec0e72f1a675b156c20d718350deb742 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 11 Aug 2016 20:53:05 +0000 Subject: [PATCH 5/9] Alter views to accept username --- app/views/devise/sessions/new.html.erb | 2 +- app/views/layouts/_navbar.html.erb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/views/devise/sessions/new.html.erb b/app/views/devise/sessions/new.html.erb index 6bfecfd..bebfd89 100644 --- a/app/views/devise/sessions/new.html.erb +++ b/app/views/devise/sessions/new.html.erb @@ -2,7 +2,7 @@

Log in

<%= simple_form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
- <%= f.email_field :email, required: false, autofocus: true, class:'devise-inputs', placeholder: 'Email Address' %> + <%= f.text_field :login, required: false, autofocus: true, class:'devise-inputs', placeholder: 'Username' %> <%= f.password_field :password, required: false, class: 'devise-inputs', placeholder: 'Password' %>