Remove email from Devise users and use nickname as primary key

This commit is contained in:
Benjamin Cousaert 2014-12-09 18:56:56 +01:00
parent bb234d7463
commit da12b71938
10 changed files with 80 additions and 108 deletions

View file

@ -19,7 +19,7 @@ class ApplicationController < ActionController::Base
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(
:email, :nickname, :name, :last_name, :password, :password_confirmation
:nickname, :name, :last_name, :password, :password_confirmation
) }
end
end

View file

@ -2,39 +2,32 @@
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# last_name :string(255)
# balance :integer default(0)
# nickname :string(255)
# created_at :datetime
# updated_at :datetime
# email :string(255) default(""), not null
# encrypted_password :string(255) default(""), not null
# reset_password_token :string(255)
# reset_password_sent_at :datetime
# remember_created_at :datetime
# sign_in_count :integer default(0), not null
# current_sign_in_at :datetime
# last_sign_in_at :datetime
# current_sign_in_ip :string(255)
# last_sign_in_ip :string(255)
# confirmation_token :string(255)
# confirmed_at :datetime
# confirmation_sent_at :datetime
# unconfirmed_email :string(255)
# id :integer not null, primary key
# name :string(255)
# last_name :string(255)
# balance :integer default(0)
# nickname :string(255)
# created_at :datetime
# updated_at :datetime
# encrypted_password :string(255) default(""), not null
# remember_created_at :datetime
# sign_in_count :integer default(0), not null
# current_sign_in_at :datetime
# last_sign_in_at :datetime
# current_sign_in_ip :string(255)
# last_sign_in_ip :string(255)
#
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable
:rememberable, :trackable
has_many :orders, -> { includes :products }
validates :nickname, presence: true, uniqueness: true
validates :name, presence: true
validates :last_name, presence: true
validates :nickname, presence: true, uniqueness: true
validates :password, length: { in: 8..128 }, confirmation: true
def full_name
"#{name} #{last_name}"

View file

@ -3,29 +3,18 @@
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="field">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<%= form_text_field f, :nickname %>
<%= form_text_field f, :name %>
<%= form_text_field f, :last_name %>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off" %>
</div>
<%= form_password_field f, :password %>
<%= form_password_field f, :password_confirmation %>
<%= form_password_field f, :current_password %>
<div class="actions">
<%= f.submit "Update" %>

View file

@ -3,7 +3,6 @@
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= form_email_field f, :email %>
<%= form_text_field f, :nickname %>
<%= form_text_field f, :name %>
<%= form_text_field f, :last_name %>

View file

@ -2,10 +2,7 @@
<%= render partial: 'flash' %>
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<%= form_text_field f, :nickname %>
<div class="field">
<%= f.label :password %><br />

View file

@ -29,7 +29,7 @@ Devise.setup do |config|
# session. If you need permissions, you should implement that in a before filter.
# You can also supply a hash where the value is a boolean determining whether
# or not authentication should be aborted when the value is not present.
# config.authentication_keys = [ :email ]
config.authentication_keys = [ :nickname ]
# Configure parameters from the request object used for authentication. Each entry
# given should be a request method and it will automatically be passed to the
@ -41,12 +41,12 @@ Devise.setup do |config|
# Configure which authentication keys should be case-insensitive.
# These keys will be downcased upon creating or modifying a user and when used
# to authenticate or find a user. Default is :email.
config.case_insensitive_keys = [ :email ]
config.case_insensitive_keys = [ ]
# Configure which authentication keys should have whitespace stripped.
# These keys will have whitespace before and after removed upon creating or
# modifying a user and when used to authenticate or find a user. Default is :email.
config.strip_whitespace_keys = [ :email ]
config.strip_whitespace_keys = [ ]
# Tell if authentication through request.params is enabled. True by default.
# It can be set to an array that will enable params authentication only for the
@ -119,7 +119,7 @@ Devise.setup do |config|
# initial account confirmation) to be applied. Requires additional unconfirmed_email
# db field (see migrations). Until confirmed, new email is stored in
# unconfirmed_email column, and copied to email column on successful confirmation.
config.reconfirmable = true
# config.reconfirmable = true
# Defines which key will be used when confirming an account
# config.confirmation_keys = [ :email ]

View file

@ -0,0 +1,19 @@
class RemoveEmailFromDeviseUsers < ActiveRecord::Migration
def change
remove_index :users, :confirmation_token
remove_index :users, :email
remove_index :users, :reset_password_token
change_table(:users) do |t|
t.remove :email
t.remove :reset_password_token
t.remove :reset_password_sent_at
t.remove :confirmation_token
t.remove :confirmed_at
t.remove :confirmation_sent_at
t.remove :unconfirmed_email
end
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20141209144521) do
ActiveRecord::Schema.define(version: 20141209172818) do
create_table "order_products", force: true do |t|
t.integer "order_id"
@ -43,28 +43,17 @@ ActiveRecord::Schema.define(version: 20141209144521) do
create_table "users", force: true do |t|
t.string "name"
t.string "last_name"
t.integer "balance", default: 0
t.integer "balance", default: 0
t.string "nickname"
t.datetime "created_at"
t.datetime "updated_at"
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.string "encrypted_password", default: "", null: false
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
end
add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

View file

@ -2,27 +2,20 @@
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# last_name :string(255)
# balance :integer default(0)
# nickname :string(255)
# created_at :datetime
# updated_at :datetime
# email :string(255) default(""), not null
# encrypted_password :string(255) default(""), not null
# reset_password_token :string(255)
# reset_password_sent_at :datetime
# remember_created_at :datetime
# sign_in_count :integer default(0), not null
# current_sign_in_at :datetime
# last_sign_in_at :datetime
# current_sign_in_ip :string(255)
# last_sign_in_ip :string(255)
# confirmation_token :string(255)
# confirmed_at :datetime
# confirmation_sent_at :datetime
# unconfirmed_email :string(255)
# id :integer not null, primary key
# name :string(255)
# last_name :string(255)
# balance :integer default(0)
# nickname :string(255)
# created_at :datetime
# updated_at :datetime
# encrypted_password :string(255) default(""), not null
# remember_created_at :datetime
# sign_in_count :integer default(0), not null
# current_sign_in_at :datetime
# last_sign_in_at :datetime
# current_sign_in_ip :string(255)
# last_sign_in_ip :string(255)
#
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

View file

@ -2,27 +2,20 @@
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# last_name :string(255)
# balance :integer default(0)
# nickname :string(255)
# created_at :datetime
# updated_at :datetime
# email :string(255) default(""), not null
# encrypted_password :string(255) default(""), not null
# reset_password_token :string(255)
# reset_password_sent_at :datetime
# remember_created_at :datetime
# sign_in_count :integer default(0), not null
# current_sign_in_at :datetime
# last_sign_in_at :datetime
# current_sign_in_ip :string(255)
# last_sign_in_ip :string(255)
# confirmation_token :string(255)
# confirmed_at :datetime
# confirmation_sent_at :datetime
# unconfirmed_email :string(255)
# id :integer not null, primary key
# name :string(255)
# last_name :string(255)
# balance :integer default(0)
# nickname :string(255)
# created_at :datetime
# updated_at :datetime
# encrypted_password :string(255) default(""), not null
# remember_created_at :datetime
# sign_in_count :integer default(0), not null
# current_sign_in_at :datetime
# last_sign_in_at :datetime
# current_sign_in_ip :string(255)
# last_sign_in_ip :string(255)
#
require 'test_helper'