Add soft_token for gradual User engagement
This commit is contained in:
parent
199914b231
commit
bd1d2bb55d
@ -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
|
||||
|
||||
60
app/controllers/users/registrations_controller.rb
Normal file
60
app/controllers/users/registrations_controller.rb
Normal file
@ -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
|
||||
@ -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)
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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
|
||||
|
||||
@ -2,14 +2,14 @@
|
||||
<ul>
|
||||
<li><%= link_to 'Main Menu', root_path %></li>
|
||||
<li><%= link_to 'Tables', tables_path %></li>
|
||||
<% if user_signed_in? %>
|
||||
<% if current_user.signed_in? %>
|
||||
<li><%= link_to 'Edit account', edit_user_registration_path %></li>
|
||||
<li><%= link_to 'Sign out', destroy_user_session_path, :method=>'delete' %></li>
|
||||
<% else %>
|
||||
<li><%= link_to 'Sign in', new_user_session_path %></li>
|
||||
<li><%= link_to 'Sign up', new_user_registration_path %></li>
|
||||
<% end %>
|
||||
<% if user_signed_in? %>
|
||||
<% if current_user.signed_in? %>
|
||||
<% if current_user.admin? %>
|
||||
<li><%= link_to 'Admin', '/admin' %></li>
|
||||
<li><%= link_to 'Users', users_path %></li>
|
||||
|
||||
@ -14,7 +14,7 @@ Rails.application.routes.draw do
|
||||
mount Upmin::Engine => '/admin'
|
||||
root to: 'visitors#index'
|
||||
get '/quick-play', to: 'visitors#quick-play'
|
||||
devise_for :users
|
||||
devise_for :users, controllers: { registrations: "users/registrations" }
|
||||
resources :users
|
||||
get '/loaderio-456644f4db1fcd1e8578be882d2fc476.txt', to: redirect('/assets/loaderio-456644f4db1fcd1e8578be882d2fc476.txt')
|
||||
get '/loaderio-eb4afd672bf5b9af467fbadcba243f26.txt', to: redirect('/assets/loaderio-eb4afd672bf5b9af467fbadcba243f26.txt')
|
||||
|
||||
@ -0,0 +1,6 @@
|
||||
class AddSoftTokenToUsersAndPlayers < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :users, :soft_token, :string
|
||||
add_column :players, :soft_token, :string
|
||||
end
|
||||
end
|
||||
@ -11,7 +11,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20151125100745) do
|
||||
ActiveRecord::Schema.define(version: 20151231035240) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
@ -55,6 +55,7 @@ ActiveRecord::Schema.define(version: 20151125100745) do
|
||||
t.integer "user_id"
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
t.string "soft_token"
|
||||
end
|
||||
|
||||
add_index "players", ["game_id"], name: "index_players_on_game_id", using: :btree
|
||||
@ -114,6 +115,7 @@ ActiveRecord::Schema.define(version: 20151125100745) do
|
||||
t.integer "invited_by_id"
|
||||
t.string "invited_by_type"
|
||||
t.integer "invitations_count", default: 0
|
||||
t.string "soft_token"
|
||||
end
|
||||
|
||||
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
|
||||
|
||||
@ -4,6 +4,7 @@ FactoryGirl.define do
|
||||
name 'Test User'
|
||||
sequence(:email) { |n| "test#{n}@example.com" }
|
||||
password 'please123'
|
||||
sequence(:soft_token) { |n| "soft_token#{n}" }
|
||||
|
||||
trait :admin do
|
||||
role 'admin'
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user