Browse Source

Your account page (#110)

* Your account page / personal details brochureware

* Edit password page working and added

Co-authored-by: Dushan <dushan-madetech@users.noreply.github.com>

* update password directing to the right place

* update from put to patch

As per the notice in this documenation:
https://github.com/heartcombo/devise/wiki/How-To:-Allow-users-to-edit-their-password

* update routes file

* Account page: able to change name and email address

* Check if logged in before accessing

* Your account page - rubocop

* Prettify

* Fix spec for merge

* Remove unused helper

* Use permitted params

Co-authored-by: Matthew Phelan <matthew.phelan@madetech.com>
Co-authored-by: Dushan <dushan-madetech@users.noreply.github.com>
Co-authored-by: baarkerlounger <db@slothlife.xyz>
pull/112/head
Dushan 3 years ago committed by GitHub
parent
commit
a522d1e151
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      Gemfile.lock
  2. 25
      app/controllers/users/account_controller.rb
  3. 7
      app/controllers/users/registrations_controller.rb
  4. 9
      app/helpers/account_helper.rb
  5. 63
      app/views/devise/registrations/edit.html.erb
  6. 73
      app/views/users/account/index.html.erb
  7. 23
      app/views/users/account/personal_details.html.erb
  8. 7
      config/routes.rb
  9. 7
      db/migrate/20211125114400_add_name_email_role_org_to_users.rb
  10. 5
      db/schema.rb
  11. 51
      spec/features/user_spec.rb

1
Gemfile.lock

@ -394,6 +394,7 @@ GEM
zeitwerk (2.5.1) zeitwerk (2.5.1)
PLATFORMS PLATFORMS
x86_64-darwin-19
x86_64-darwin-20 x86_64-darwin-20
x86_64-linux x86_64-linux

25
app/controllers/users/account_controller.rb

@ -0,0 +1,25 @@
class Users::AccountController < ApplicationController
def check_logged_in
if current_user.nil?
redirect_to(new_user_session_path)
end
end
def index
check_logged_in
end
def personal_details
check_logged_in
end
def update
if current_user.update(user_params)
redirect_to(users_account_path)
end
end
def user_params
params.require(:user).permit(:email, :name, :password)
end
end

7
app/controllers/users/registrations_controller.rb

@ -0,0 +1,7 @@
class Users::RegistrationsController < Devise::RegistrationsController
protected
def after_update_path_for(_resource)
users_account_path
end
end

9
app/helpers/account_helper.rb

@ -0,0 +1,9 @@
module AccountHelper
def resource_name
:user
end
def resource
@resource = current_user
end
end

63
app/views/devise/registrations/edit.html.erb

@ -1,43 +1,26 @@
<h2>Edit <%= resource_name.to_s.humanize %></h2> <% content_for :before_content do %>
<%= link_to 'Back', :back, class: "govuk-back-link" %>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %> <% end %>
<%= render "devise/shared/error_messages", resource: resource %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</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: "new-password" %>
<% if @minimum_password_length %>
<br />
<em><%= @minimum_password_length %> characters minimum</em>
<% end %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "new-password" %>
</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: "current-password" %>
</div>
<div class="actions"> <%= form_for(resource, as: resource_name, url: user_registration_path(), html: { method: :patch }) do |f| %>
<%= f.submit "Update" %> <div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-l">Change your password</h1>
<div class="govuk-form-group">
<%= f.label :current_password, class: "govuk-label" %>
<%= f.password_field :current_password, autocomplete: "current-password", class: "govuk-input" %>
</div>
<div class="govuk-form-group">
<%= f.label :password, class: "govuk-label" %>
<div id="undefined-hint" class="govuk-hint">
Your password must be at least 8 characters and hard to guess.
</div>
<%= f.password_field :password, autocomplete: "new-password", class: "govuk-input" %>
</div>
<%= f.submit "Update", class: "govuk-button" %>
</div>
</div> </div>
<% end %> <% end %>
<h3>Cancel my account</h3>
<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>
<%= link_to "Back", :back %>

73
app/views/users/account/index.html.erb

@ -0,0 +1,73 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-l">
Your account
</h1>
<h2 class="govuk-heading-m">
Personal details
</h2>
<dl class="govuk-summary-list">
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">
Name
</dt>
<dd class="govuk-summary-list__value">
<%= current_user.name %>
</dd>
<dd class="govuk-summary-list__actions">
<a class="govuk-link" href="/users/account/personal_details" id="change-name">
Change<span class="govuk-visually-hidden">
name</span>
</a>
</dd>
</div>
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">
Email address
</dt>
<dd class="govuk-summary-list__value">
<%= current_user.email %>
</dd>
<dd class="govuk-summary-list__actions">
<a class="govuk-link" href="/users/account/personal_details" id="change-email">
Change<span class="govuk-visually-hidden">
email address</span>
</a>
</dd>
</div>
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">
Password
</dt>
<dd class="govuk-summary-list__value">
••••••••
</dd>
<dd class="govuk-summary-list__actions">
<%= link_to "Change", edit_user_registration_path, id: "change-password" %>
</dd>
</div>
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">
Organisation
</dt>
<dd class="govuk-summary-list__value">
<%= current_user.organisation %>
</dd>
<dd class="govuk-summary-list__actions">
<a class="govuk-link" href=""></a>
</dd>
</div>
<div class="govuk-summary-list__row">
<dt class="govuk-summary-list__key">
Role
</dt>
<dd class="govuk-summary-list__value">
<%= current_user.role %>
</dd>
<dd class="govuk-summary-list__actions">
<a class="govuk-link" href=""></a>
</dd>
</div>
</dl>
</div>
</div>

23
app/views/users/account/personal_details.html.erb

@ -0,0 +1,23 @@
<% content_for :before_content do %>
<%= link_to 'Back', :back, class: "govuk-back-link" %>
<% end %>
<%= form_for(resource, as: resource_name, url: account_update_path(), html: { method: :patch }) do |f| %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-l">Change your personal details</h1>
<div class="govuk-form-group">
<%= f.label :name, class: "govuk-label" %>
<%= f.text_field :name, autocomplete: "name", class: "govuk-input" %>
</div>
<div class="govuk-form-group">
<%= f.label :email, class: "govuk-label" %>
<%= f.email_field :email, autocomplete: "email", class: "govuk-input" %>
</div>
<%= f.submit "Save changes", class: "govuk-button" %>
</div>
</div>
<% end %>

7
config/routes.rb

@ -1,14 +1,19 @@
Rails.application.routes.draw do Rails.application.routes.draw do
devise_for :admin_users, ActiveAdmin::Devise.config devise_for :admin_users, ActiveAdmin::Devise.config
devise_for :users, controllers: { passwords: "users/passwords" } devise_for :users, controllers: { passwords: "users/passwords" }, skip: [:registrations]
devise_scope :user do devise_scope :user do
get "confirmations/reset", to: "users/passwords#reset_confirmation" get "confirmations/reset", to: "users/passwords#reset_confirmation"
get "users/edit" => "devise/registrations#edit", :as => "edit_user_registration"
patch "users" => "users/registrations#update", :as => "user_registration"
patch "details" => "users/account#update", :as => "account_update"
end end
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
ActiveAdmin.routes(self) ActiveAdmin.routes(self)
root to: "test#index" root to: "test#index"
get "about", to: "about#index" get "about", to: "about#index"
get "/users/account", to: "users/account#index"
get "/users/account/personal_details", to: "users/account#personal_details"
form_handler = FormHandler.instance form_handler = FormHandler.instance
form = form_handler.get_form("2021_2022") form = form_handler.get_form("2021_2022")

7
db/migrate/20211125114400_add_name_email_role_org_to_users.rb

@ -0,0 +1,7 @@
class AddNameEmailRoleOrgToUsers < ActiveRecord::Migration[6.1]
def change
add_column :users, :name, :string
add_column :users, :role, :string
add_column :users, :organisation, :string
end
end

5
db/schema.rb

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_11_24_174732) do ActiveRecord::Schema.define(version: 2021_11_25_114400) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -177,6 +177,9 @@ ActiveRecord::Schema.define(version: 2021_11_24_174732) do
t.datetime "remember_created_at" t.datetime "remember_created_at"
t.datetime "created_at", precision: 6, null: false t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false
t.string "name"
t.string "role"
t.string "organisation"
t.index ["email"], name: "index_users_on_email", 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 t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end end

51
spec/features/user_spec.rb

@ -57,4 +57,55 @@ RSpec.describe "User Features" do
expect(page).to have_css '.govuk-notification-banner.govuk-notification-banner--success' expect(page).to have_css '.govuk-notification-banner.govuk-notification-banner--success'
end end
end end
context "If a not logged in user tries to access pages that need permissions" do
it "redirects to log in page" do
visit("/users/account")
expect(page).to have_content("Sign in to your account to submit CORE data")
end
end
context "Your Account " do
before(:each) do
visit("/case_logs")
fill_in("user_email", with: user.email)
fill_in("user_password", with: "pAssword1")
click_button("Sign in")
end
it "main page is present and accessible" do
visit("/users/account")
expect(page).to have_content("Your account")
end
it "personal details page is present and accessible" do
visit("/users/account/personal_details")
expect(page).to have_content("Change your personal details")
end
it "edit password page present and accessible" do
visit("users/edit")
expect(page).to have_content("Change your password")
end
it "can navigate to change your password page from main account page" do
visit("/users/account")
click_link("change-password")
expect(page).to have_content("Change your password")
fill_in("user_current_password", with: "pAssword1")
fill_in("user_password", with: "Password123!")
click_button("Update")
expect(page).to have_current_path("/users/account")
end
it "allow user to change name" do
visit("/users/account")
click_link("change-name")
expect(page).to have_content("Change your personal details")
fill_in("user_name", with: "Test New")
click_button("Save changes")
expect(page).to have_current_path("/users/account")
expect(page).to have_content("Test New")
end
end
end end

Loading…
Cancel
Save