From da12b71938ebbc7ea86752f161a1f8826a18e3fd Mon Sep 17 00:00:00 2001 From: Benjamin Cousaert Date: Tue, 9 Dec 2014 18:56:56 +0100 Subject: [PATCH] Remove email from Devise users and use nickname as primary key --- app/controllers/application_controller.rb | 2 +- app/models/user.rb | 41 ++++++++----------- app/views/devise/registrations/edit.html.erb | 23 +++-------- app/views/devise/registrations/new.html.erb | 1 - app/views/devise/sessions/new.html.erb | 5 +-- config/initializers/devise.rb | 8 ++-- ...09172818_remove_email_from_devise_users.rb | 19 +++++++++ db/schema.rb | 19 ++------- test/fixtures/users.yml | 35 +++++++--------- test/models/user_test.rb | 35 +++++++--------- 10 files changed, 80 insertions(+), 108 deletions(-) create mode 100644 db/migrate/20141209172818_remove_email_from_devise_users.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 038ae7f..44c5ca4 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 81aca66..e7a2dad 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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}" diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb index 3ea40f0..d5f689c 100644 --- a/app/views/devise/registrations/edit.html.erb +++ b/app/views/devise/registrations/edit.html.erb @@ -3,29 +3,18 @@ <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %> <%= devise_error_messages! %> -
- <%= f.label :email %>
- <%= f.email_field :email, autofocus: true %> -
- <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
Currently waiting confirmation for: <%= resource.unconfirmed_email %>
<% end %> -
- <%= f.label :password %> (leave blank if you don't want to change it)
- <%= f.password_field :password, autocomplete: "off" %> -
+ <%= form_text_field f, :nickname %> + <%= form_text_field f, :name %> + <%= form_text_field f, :last_name %> -
- <%= f.label :password_confirmation %>
- <%= f.password_field :password_confirmation, autocomplete: "off" %> -
-
- <%= f.label :current_password %> (we need your current password to confirm your changes)
- <%= f.password_field :current_password, autocomplete: "off" %> -
+ <%= form_password_field f, :password %> + <%= form_password_field f, :password_confirmation %> + <%= form_password_field f, :current_password %>
<%= f.submit "Update" %> diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index fe4bd48..11091d7 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -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 %> diff --git a/app/views/devise/sessions/new.html.erb b/app/views/devise/sessions/new.html.erb index d7cc711..fde04f2 100644 --- a/app/views/devise/sessions/new.html.erb +++ b/app/views/devise/sessions/new.html.erb @@ -2,10 +2,7 @@ <%= render partial: 'flash' %> <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> -
- <%= f.label :email %>
- <%= f.email_field :email, autofocus: true %> -
+ <%= form_text_field f, :nickname %>
<%= f.label :password %>
diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 1ef225f..1f4e17f 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -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 ] diff --git a/db/migrate/20141209172818_remove_email_from_devise_users.rb b/db/migrate/20141209172818_remove_email_from_devise_users.rb new file mode 100644 index 0000000..9676117 --- /dev/null +++ b/db/migrate/20141209172818_remove_email_from_devise_users.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 933881e..e52179a 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: 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 diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 7935295..a086a02 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -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 diff --git a/test/models/user_test.rb b/test/models/user_test.rb index d3ab980..4682454 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -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'