From 983841800c9485f338cf347d2fc4f991fddfd653 Mon Sep 17 00:00:00 2001 From: riggraz Date: Mon, 19 Aug 2019 15:45:44 +0200 Subject: [PATCH] Add full name to users --- app/controllers/application_controller.rb | 8 ++++++ app/models/user.rb | 2 ++ app/views/devise/registrations/edit.html.erb | 25 +++++++++++-------- app/views/devise/registrations/new.html.erb | 19 ++++++++------ .../20190819131723_add_fullname_to_users.rb | 5 ++++ db/schema.rb | 3 ++- 6 files changed, 44 insertions(+), 18 deletions(-) create mode 100644 db/migrate/20190819131723_add_fullname_to_users.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 09705d12..bbe0963e 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,2 +1,10 @@ class ApplicationController < ActionController::Base + before_action :configure_permitted_parameters, if: :devise_controller? + + protected + + def configure_permitted_parameters + devise_parameter_sanitizer.permit(:sign_up, keys: [:full_name]) + devise_parameter_sanitizer.permit(:account_update, keys: [:full_name]) + end end diff --git a/app/models/user.rb b/app/models/user.rb index 62e9587a..45af2ee7 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,6 +2,8 @@ class User < ApplicationRecord enum role: [:user, :moderator, :admin] after_initialize :set_default_role, if: :new_record? + validates :full_name, presence: true + def set_default_role self.role ||= :user end diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb index 38d95b85..b7a78915 100644 --- a/app/views/devise/registrations/edit.html.erb +++ b/app/views/devise/registrations/edit.html.erb @@ -3,41 +3,46 @@ <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %> <%= render "devise/shared/error_messages", resource: resource %> -
+
+ <%= f.label :full_name %>
+ <%= f.text_field :full_name, autofocus: true, autocomplete: "full-name", class: "form-control" %> +
+ +
<%= f.label :email %>
- <%= f.email_field :email, autofocus: true, autocomplete: "email" %> + <%= f.email_field :email, autocomplete: "email", class: "form-control" %>
<% 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: "new-password" %> + <%= f.password_field :password, autocomplete: "new-password", class: "form-control" %> <% if @minimum_password_length %>
<%= @minimum_password_length %> characters minimum <% end %>
-
+
<%= f.label :password_confirmation %>
- <%= f.password_field :password_confirmation, autocomplete: "new-password" %> + <%= f.password_field :password_confirmation, autocomplete: "new-password", class: "form-control" %>
-
+
<%= f.label :current_password %> (we need your current password to confirm your changes)
- <%= f.password_field :current_password, autocomplete: "current-password" %> + <%= f.password_field :current_password, autocomplete: "current-password", class: "form-control" %>
- <%= f.submit "Update" %> + <%= f.submit "Update", class: "btn btn-primary" %>
<% end %>

Cancel my account

-

Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %>

+

Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete, class: "btn btn-danger" %>

<%= link_to "Back", :back %> diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index d655b66f..3a11453d 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -3,26 +3,31 @@ <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> <%= render "devise/shared/error_messages", resource: resource %> -
+
+ <%= f.label :full_name %>
+ <%= f.text_field :full_name, autofocus: true, autocomplete: "full-name", class: "form-control" %> +
+ +
<%= f.label :email %>
- <%= f.email_field :email, autofocus: true, autocomplete: "email" %> + <%= f.email_field :email, autocomplete: "email", class: "form-control" %>
-
+
<%= f.label :password %> <% if @minimum_password_length %> (<%= @minimum_password_length %> characters minimum) <% end %>
- <%= f.password_field :password, autocomplete: "new-password" %> + <%= f.password_field :password, autocomplete: "new-password", class: "form-control" %>
-
+
<%= f.label :password_confirmation %>
- <%= f.password_field :password_confirmation, autocomplete: "new-password" %> + <%= f.password_field :password_confirmation, autocomplete: "new-password", class: "form-control" %>
- <%= f.submit "Sign up" %> + <%= f.submit "Sign up", class: "btn btn-primary" %>
<% end %> diff --git a/db/migrate/20190819131723_add_fullname_to_users.rb b/db/migrate/20190819131723_add_fullname_to_users.rb new file mode 100644 index 00000000..9192cdc1 --- /dev/null +++ b/db/migrate/20190819131723_add_fullname_to_users.rb @@ -0,0 +1,5 @@ +class AddFullnameToUsers < ActiveRecord::Migration[6.0] + def change + add_column :users, :full_name, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 60acf722..9881e027 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: 2019_08_18_191513) do +ActiveRecord::Schema.define(version: 2019_08_19_131723) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -28,6 +28,7 @@ ActiveRecord::Schema.define(version: 2019_08_18_191513) do t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.integer "role" + t.string "full_name" t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true