simplify the profile settings form

pull/105/head
Alex Turchyn 2 years ago
parent 219ad2b4a7
commit c34ea8451f

@ -3,40 +3,28 @@
class ProfileController < ApplicationController
def update_contact
if current_user.update(contact_params)
redirect_to contact_settings_profile_index_path, notice: 'Contact information successfully updated'
redirect_to settings_profile_index_path, notice: 'Contact information successfully updated'
else
render :contact, status: :unprocessable_entity
render :index, status: :unprocessable_entity
end
end
def update_password
if current_user.update_with_password(password_params)
if current_user.update(password_params)
bypass_sign_in(current_user)
redirect_to password_settings_profile_index_path, notice: 'Password successfully changed'
redirect_to settings_profile_index_path, notice: 'Password successfully changed'
else
render :password, status: :unprocessable_entity
end
end
def update_email
if current_user.update_with_password(email_params)
redirect_to email_settings_profile_index_path, notice: 'Email successfully updated. Please check your new email for confirmation instructions.'
else
render :email, status: :unprocessable_entity
render :index, status: :unprocessable_entity
end
end
private
def contact_params
params.require(:user).permit(:first_name, :last_name, account_attributes: %i[name])
params.require(:user).permit(:first_name, :last_name, :email, account_attributes: %i[name])
end
def password_params
params.require(:user).permit(:current_password, :password, :password_confirmation)
end
def email_params
params.require(:user).permit(:current_password, :email)
params.require(:user).permit(:password, :password_confirmation)
end
end

@ -5,9 +5,6 @@
# Table name: users
#
# id :bigint not null, primary key
# confirmation_sent_at :datetime
# confirmation_token :string
# confirmed_at :datetime
# current_sign_in_at :datetime
# current_sign_in_ip :string
# deleted_at :datetime
@ -24,7 +21,6 @@
# reset_password_token :string
# role :string not null
# sign_in_count :integer default(0), not null
# unconfirmed_email :string
# unlock_token :string
# created_at :datetime not null
# updated_at :datetime not null
@ -49,7 +45,7 @@ class User < ApplicationRecord
belongs_to :account
devise :database_authenticatable, :recoverable, :rememberable, :validatable, :trackable, :confirmable
devise :database_authenticatable, :recoverable, :rememberable, :validatable, :trackable
devise :registerable # if ENV['APP_MULTITENANT']
attribute :role, :string, default: 'admin'

@ -1,14 +0,0 @@
<div class="max-w-xl mx-auto px-2">
<h1 class="text-4xl font-bold text-center my-8">Resend confirmation instructions</h1>
<%= form_for(resource, as: resource_name, url: confirmation_path(resource_name), html: { method: :post, class: 'space-y-6' }) do |f| %>
<%= render "devise/shared/error_messages", resource: resource %>
<div class="form-control">
<%= f.label :email, class: 'label' %>
<%= f.email_field :email, autofocus: true, autocomplete: "email", value: (resource.pending_reconfirmation? ? resource.unconfirmed_email : resource.email), class: 'base-input' %>
</div>
<div class="form-control">
<%= f.submit "Resend confirmation instructions", class: 'base-button' %>
</div>
<% end %>
<%= render "devise/shared/links" %>
</div>

@ -1,3 +0,0 @@
<p>Welcome <%= @email %>!</p>
<p>You can confirm your account email through the link below:</p>
<p><%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></p>

@ -1,22 +1,22 @@
<div class="flex flex-wrap justify-center space-x-4 mt-4">
<div class="flex justify-between mt-4">
<%- if controller_name != 'sessions' %>
<%= link_to 'Log in', new_session_path(resource_name), class: 'badge badge-outline my-1' %>
<%= link_to 'Log in', new_session_path(resource_name), class: 'link link-hover' %>
<% end %>
<%- if devise_mapping.registerable? && controller_name != 'registrations' %>
<%= link_to 'Sign up', new_registration_path, class: 'badge badge-outline my-1' %>
<%= link_to 'Sign up', new_registration_path, class: 'link link-hover' %>
<% end %>
<%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %>
<%= link_to 'Forgot your password?', new_password_path(resource_name), class: 'badge badge-outline my-1' %>
<%= link_to 'Forgot your password?', new_password_path(resource_name), class: 'link link-hover' %>
<% end %>
<%- if devise_mapping.confirmable? && controller_name != 'confirmations' %>
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name), class: 'badge badge-outline my-1' %>
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name), class: 'link link-hover' %>
<% end %>
<%- if devise_mapping.lockable? && resource_class.unlock_strategy_enabled?(:email) && controller_name != 'unlocks' %>
<%= link_to "Didn't receive unlock instructions?", new_unlock_path(resource_name), class: 'badge badge-outline my-1' %>
<%= link_to "Didn't receive unlock instructions?", new_unlock_path(resource_name), class: 'link link-hover' %>
<% end %>
<%- if devise_mapping.omniauthable? %>
<%- resource_class.omniauth_providers.each do |provider| %>
<%= button_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", omniauth_authorize_path(resource_name, provider), class: 'badge badge-outline my-1', data: { turbo: false } %>
<%= button_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", omniauth_authorize_path(resource_name, provider), class: 'link link-hover', data: { turbo: false } %>
<% end %>
<% end %>
</div>

@ -1,36 +0,0 @@
<menu-active>
<ul class="menu menu-vertical menu-md w-full md:menu-horizontal bg-base-200 rounded-box justify-center font-semibold">
<li>
<%= link_to contact_settings_profile_index_path do %>
<svg xmlns="http://www.w3.org/2000/svg" class="w-5 h-5" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
<path d="M8 7a4 4 0 1 0 8 0a4 4 0 0 0 -8 0" />
<path d="M6 21v-2a4 4 0 0 1 4 -4h3.5" />
<path d="M18.42 15.61a2.1 2.1 0 0 1 2.97 2.97l-3.39 3.42h-3v-3l3.42 -3.39z" />
</svg>
<span >Edit Contact</span>
<% end %>
</li>
<li>
<%= link_to password_settings_profile_index_path do %>
<svg xmlns="http://www.w3.org/2000/svg" class="w-5 h-5" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
<path d="M12 3a12 12 0 0 0 8.5 3a12 12 0 0 1 -8.5 15a12 12 0 0 1 -8.5 -15a12 12 0 0 0 8.5 -3" />
<path d="M12 11m-1 0a1 1 0 1 0 2 0a1 1 0 1 0 -2 0" />
<path d="M12 12l0 2.5" />
</svg>
<span>Change Password</span>
<% end %>
</li>
<li>
<%= link_to email_settings_profile_index_path do %>
<svg xmlns="http://www.w3.org/2000/svg" class="w-5 h-5" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
<path d="M12 12m-4 0a4 4 0 1 0 8 0a4 4 0 1 0 -8 0" />
<path d="M16 12v1.5a2.5 2.5 0 0 0 5 0v-1.5a9 9 0 1 0 -5.5 8.28" />
</svg>
<span>Change Email</span>
<% end %>
</li>
</ul>
</menu-active>

@ -1,30 +0,0 @@
<div class="flex flex-wrap space-y-4 md:flex-nowrap md:space-y-0">
<%= render 'shared/settings_nav' %>
<div class="flex-grow max-w-xl mx-auto">
<h1 class="text-4xl font-bold mb-4">Profile</h1>
<%= render 'navigation' %>
<%= form_for current_user, url: update_email_settings_profile_index_path, method: :patch, html: { autocomplete: 'off', class: 'space-y-4' } do |f| %>
<% if current_user.pending_reconfirmation? %>
<div class="alert">
<svg xmlns="http://www.w3.org/2000/svg" class="stroke-current shrink-0 h-6 w-6" fill="none" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" /></svg>
<div>
<h3 class="font-bold">Currently waiting confirmation</h3>
<div class="text-md">Unconfirmed email: <i><%= current_user.unconfirmed_email %></i></div>
</div>
</div>
<% end %>
<div class="form-control">
<%= f.label :email, 'New email', class: 'label' %>
<%= f.email_field :email, autocomplete: 'off', class: 'base-input' %>
</div>
<div class="form-control">
<%= f.label :current_password, 'Current password', class: 'label' %>
<%= f.password_field :current_password, autocomplete: 'off', class: 'base-input' %>
</div>
<div class="form-control mt-8">
<%= f.button button_title, class: 'base-button' %>
</div>
<% end %>
</div>
<div class="w-0 md:w-52"></div>
</div>

@ -2,7 +2,9 @@
<%= render 'shared/settings_nav' %>
<div class="flex-grow max-w-xl mx-auto">
<h1 class="text-4xl font-bold mb-4">Profile</h1>
<%= render 'navigation' %>
<p>
Manage your contact information
</p>
<%= form_for current_user, url: update_contact_settings_profile_index_path, method: :patch, html: { autocomplete: 'off', class: 'space-y-4' } do |f| %>
<div class="grid md:grid-cols-2 gap-4">
<div class="form-control">
@ -14,6 +16,10 @@
<%= f.text_field :last_name, required: true, class: 'base-input' %>
</div>
</div>
<div class="form-control">
<%= f.label :email, 'Email', class: 'label' %>
<%= f.email_field :email, autocomplete: 'off', class: 'base-input' %>
</div>
<%= f.fields_for :account do |ff| %>
<div class="form-control">
<%= ff.label :name, 'Company Name', class: 'label' %>
@ -24,6 +30,20 @@
<%= f.button button_title, class: 'base-button' %>
</div>
<% end %>
<h2 class="text-2xl font-bold mt-8 mb-4">Change Password</h2>
<%= form_for current_user, url: update_password_settings_profile_index_path, method: :patch, html: { autocomplete: 'off', class: 'space-y-4' } do |f| %>
<div class="form-control">
<%= f.label :password, 'New password', class: 'label' %>
<%= f.password_field :password, autocomplete: 'off', class: 'base-input' %>
</div>
<div class="form-control">
<%= f.label :password_confirmation, 'Confirm new password', class: 'label' %>
<%= f.password_field :password_confirmation, autocomplete: 'off', class: 'base-input' %>
</div>
<div class="form-control mt-8">
<%= f.button button_title, class: 'base-button' %>
</div>
<% end %>
</div>
<div class="w-0 md:w-52"></div>
</div>

@ -1,25 +0,0 @@
<div class="flex flex-wrap space-y-4 md:flex-nowrap md:space-y-0">
<%= render 'shared/settings_nav' %>
<div class="flex-grow max-w-xl mx-auto">
<h1 class="text-4xl font-bold mb-4">Profile</h1>
<%= render 'navigation' %>
<%= form_for current_user, url: update_password_settings_profile_index_path, method: :patch, html: { autocomplete: 'off', class: 'space-y-4' } do |f| %>
<div class="form-control">
<%= f.label :current_password, 'Current password', class: 'label' %>
<%= f.password_field :current_password, autocomplete: 'off', class: 'base-input' %>
</div>
<div class="form-control">
<%= f.label :password, 'New password', class: 'label' %>
<%= f.password_field :password, autocomplete: 'off', class: 'base-input' %>
</div>
<div class="form-control">
<%= f.label :password_confirmation, 'Confirm new password', class: 'label' %>
<%= f.password_field :password_confirmation, autocomplete: 'off', class: 'base-input' %>
</div>
<div class="form-control mt-8">
<%= f.button button_title, class: 'base-button' %>
</div>
<% end %>
</div>
<div class="w-0 md:w-52"></div>
</div>

@ -12,7 +12,7 @@
</label>
<ul tabindex="0" class="dropdown-content p-2 mt-2 shadow menu bg-base-100 rounded-box whitespace-nowrap">
<li>
<%= link_to 'Profile Settings', contact_settings_profile_index_path, class: 'text-right' %>
<%= link_to 'Profile', settings_profile_index_path, class: 'text-right' %>
</li>
<li>
<%= link_to 'Sign out', destroy_user_session_path, data: { turbo_method: :delete } %>

@ -4,7 +4,7 @@
<li class="menu-title !pt-0"><span>Settings</span></li>
<li></li>
<li>
<%= link_to 'Profile', contact_settings_profile_index_path, class: "text-base hover:bg-base-300" %>
<%= link_to 'Profile', settings_profile_index_path, class: "text-base hover:bg-base-300" %>
</li>
<li>
<%= link_to 'Email', settings_email_index_path, class: "text-base hover:bg-base-300" %>

@ -5,7 +5,7 @@ Rails.application.routes.draw do
root 'dashboard#index'
devise_for :users, path: '/', only: %i[sessions passwords confirmations]
devise_for :users, path: '/', only: %i[sessions passwords]
devise_scope :user do
if User.devise_modules.include?(:registerable)
@ -56,14 +56,10 @@ Rails.application.routes.draw do
resources :email, only: %i[index create], controller: 'email_settings'
resources :esign, only: %i[index create], controller: 'esign_settings'
resources :users, only: %i[index]
resources :profile, only: %i[] do
resources :profile, only: %i[index] do
collection do
get :contact
get :password
get :email
patch :contact, :update_contact
patch :password, :update_password
patch :email, :update_email
patch :update_contact
patch :update_password
end
end
end

@ -1,8 +0,0 @@
class AddConfirmationToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :confirmation_token, :string, unique: true
add_column :users, :confirmed_at, :datetime
add_column :users, :confirmation_sent_at, :datetime
add_column :users, :unconfirmed_email, :string, unique: true
end
end

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_06_06_210549) do
ActiveRecord::Schema[7.0].define(version: 2023_05_19_144036) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -115,10 +115,6 @@ ActiveRecord::Schema[7.0].define(version: 2023_06_06_210549) do
t.datetime "deleted_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.index ["account_id"], name: "index_users_on_account_id"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

Loading…
Cancel
Save