From bd1d2bb55dbec846de24f60ee84866f1c2f8e41b Mon Sep 17 00:00:00 2001 From: "Jesse C. Fisher" Date: Thu, 7 Jan 2016 02:32:01 -0800 Subject: [PATCH] Add soft_token for gradual User engagement --- app/controllers/application_controller.rb | 12 ++++ .../users/registrations_controller.rb | 60 +++++++++++++++++++ app/models/game.rb | 2 +- app/models/player.rb | 1 + app/models/user.rb | 8 +++ app/views/games/show.json.jbuilder | 4 +- app/views/layouts/_navigation_links.html.erb | 4 +- config/routes.rb | 2 +- ...240_add_soft_token_to_users_and_players.rb | 6 ++ db/schema.rb | 4 +- spec/factories/users.rb | 1 + 11 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 app/controllers/users/registrations_controller.rb create mode 100644 db/migrate/20151231035240_add_soft_token_to_users_and_players.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c1577c7d..b008b3cc 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -21,6 +21,14 @@ class ApplicationController < ActionController::Base end end + def authenticate_user!(args = nil) + current_user.present? + end + + def current_user + super || User.where(soft_token: soft_token).first_or_initialize + end + private def flash_to_http_header @@ -30,4 +38,8 @@ class ApplicationController < ActionController::Base flash.discard # don't want the flash to appear when you reload page end + def soft_token + session[:user_token] ||= SecureRandom.hex(8) + end + end diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb new file mode 100644 index 00000000..a83b243a --- /dev/null +++ b/app/controllers/users/registrations_controller.rb @@ -0,0 +1,60 @@ +class Users::RegistrationsController < Devise::RegistrationsController + before_filter :configure_sign_up_params, only: [:create] +# before_filter :configure_account_update_params, only: [:update] + + # GET /resource/sign_up + # def new + # super + # end + + # POST /resource + # def create + # super + # end + + # GET /resource/edit + # def edit + # super + # end + + # PUT /resource + # def update + # super + # end + + # DELETE /resource + # def destroy + # super + # end + + # GET /resource/cancel + # Forces the session data which is usually expired after sign + # in to be expired now. This is useful if the user wants to + # cancel oauth signing in/up in the middle of the process, + # removing all OAuth session data. + # def cancel + # super + # end + + protected + + # If you have extra params to permit, append them to the sanitizer. + def configure_sign_up_params + devise_parameter_sanitizer.for(:sign_up) << :soft_token + end + + # If you have extra params to permit, append them to the sanitizer. + # def configure_account_update_params + # devise_parameter_sanitizer.for(:account_update) << :attribute + # end + + # The path used after sign up. + # def after_sign_up_path_for(resource) + # super(resource) + # end + + # The path used after sign up for inactive accounts. + # def after_inactive_sign_up_path_for(resource) + # super(resource) + # end +end diff --git a/app/models/game.rb b/app/models/game.rb index c534d1dd..e45a2d7a 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -65,7 +65,7 @@ class Game < ActiveRecord::Base def add_player_from_user(user) return false unless can_accomodate(user) - self.players.create(user: user) + new_player = self.players.create(user: user, soft_token: user.soft_token) end def can_accomodate(user) diff --git a/app/models/player.rb b/app/models/player.rb index 24ba76f3..6b24556b 100644 --- a/app/models/player.rb +++ b/app/models/player.rb @@ -6,6 +6,7 @@ class Player < ActiveRecord::Base has_many :player_cards has_many :plays + #TODO: soft_user will not have a user_id validates :user_id, presence: true validates :game_id, presence: true diff --git a/app/models/user.rb b/app/models/user.rb index 2f13e49f..2f865b65 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,6 +6,14 @@ class User < ActiveRecord::Base self.role ||= :user end + def soft_user? + self.email.empty? + end + + def signed_in? + !soft_user? + end + # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :invitable, :database_authenticatable, :registerable, # :confirmable, diff --git a/app/views/games/show.json.jbuilder b/app/views/games/show.json.jbuilder index ef6d6bda..0c13d036 100644 --- a/app/views/games/show.json.jbuilder +++ b/app/views/games/show.json.jbuilder @@ -14,9 +14,9 @@ json.current_user_id current_user.try(:id) json.players @game.players do |player| json.id player.id - json.email player.user.email + json.email player.user.try(:email) json.inventory_count player.inventory.count - json.user_id player.user.id + json.user_id player.user.try(:id) if player.user == current_user json.inventory player.inventory diff --git a/app/views/layouts/_navigation_links.html.erb b/app/views/layouts/_navigation_links.html.erb index 0617046d..825d30fb 100644 --- a/app/views/layouts/_navigation_links.html.erb +++ b/app/views/layouts/_navigation_links.html.erb @@ -2,14 +2,14 @@