From e1214debfbbe73d68af6ca0561556d640949e56a Mon Sep 17 00:00:00 2001 From: Matthew Phelan Date: Thu, 25 Nov 2021 14:01:50 +0000 Subject: [PATCH] Account page: able to change name and email address --- app/controllers/users/account_controller.rb | 6 +++++ app/helpers/account_helper.rb | 14 ++++++++++ app/views/users/account/index.html.erb | 4 +++ .../users/account/personal_details.html.erb | 26 ++++++++++++++++--- config/routes.rb | 1 + ...114400_add_name_email_role_org_to_users.rb | 7 +++++ db/schema.rb | 5 +++- spec/features/user_spec.rb | 21 ++++++++------- 8 files changed, 70 insertions(+), 14 deletions(-) create mode 100644 app/helpers/account_helper.rb create mode 100644 db/migrate/20211125114400_add_name_email_role_org_to_users.rb diff --git a/app/controllers/users/account_controller.rb b/app/controllers/users/account_controller.rb index ce617f836..96511c77a 100644 --- a/app/controllers/users/account_controller.rb +++ b/app/controllers/users/account_controller.rb @@ -1,4 +1,10 @@ class Users::AccountController < ApplicationController def index; end def personal_details; end + + def update + if current_user.update('name': params[:user][:name], 'email': params[:user][:email],) + redirect_to(users_account_path()) + end + end end \ No newline at end of file diff --git a/app/helpers/account_helper.rb b/app/helpers/account_helper.rb new file mode 100644 index 000000000..ae6f5fe75 --- /dev/null +++ b/app/helpers/account_helper.rb @@ -0,0 +1,14 @@ +module AccountHelper + def resource_name + :user + end + + def resource + @resource = current_user + end + + def devise_mapping + @devise_mapping ||= Devise.mappings[:user] + end +end + \ No newline at end of file diff --git a/app/views/users/account/index.html.erb b/app/views/users/account/index.html.erb index 37a4560de..90d07e690 100644 --- a/app/views/users/account/index.html.erb +++ b/app/views/users/account/index.html.erb @@ -12,6 +12,7 @@ Name
+ <%= current_user.name %>
@@ -24,6 +25,7 @@ Email address
+ <%= current_user.email %>
@@ -47,6 +49,7 @@ Organisation
+ <%= current_user.organisation %>
@@ -58,6 +61,7 @@ Role
+ <%= current_user.role %>
diff --git a/app/views/users/account/personal_details.html.erb b/app/views/users/account/personal_details.html.erb index 5c7a746d2..f6bc779be 100644 --- a/app/views/users/account/personal_details.html.erb +++ b/app/views/users/account/personal_details.html.erb @@ -1,3 +1,23 @@ -

- Personal details -

\ No newline at end of file +<% 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| %> +
+
+

Change your personal details

+ +
+ <%= f.label :name, class: "govuk-label" %> + <%= f.text_field :name, autocomplete: "name", class: "govuk-input" %> +
+ +
+ <%= f.label :email, class: "govuk-label" %> + <%= f.email_field :email, autocomplete: "email", class: "govuk-input" %> +
+ + <%= f.submit "Save changes", class: "govuk-button" %> +
+
+<% end %> diff --git a/config/routes.rb b/config/routes.rb index f535f361f..2c6e47208 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ Rails.application.routes.draw do 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 # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html diff --git a/db/migrate/20211125114400_add_name_email_role_org_to_users.rb b/db/migrate/20211125114400_add_name_email_role_org_to_users.rb new file mode 100644 index 000000000..90a88c484 --- /dev/null +++ b/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 diff --git a/db/schema.rb b/db/schema.rb index 18a7999e6..97b71f6a3 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: 2021_11_18_090831) do +ActiveRecord::Schema.define(version: 2021_11_25_114400) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -164,6 +164,9 @@ ActiveRecord::Schema.define(version: 2021_11_18_090831) do t.datetime "remember_created_at" t.datetime "created_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 ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end diff --git a/spec/features/user_spec.rb b/spec/features/user_spec.rb index 44c7e47e7..c2facb7c7 100644 --- a/spec/features/user_spec.rb +++ b/spec/features/user_spec.rb @@ -67,16 +67,7 @@ RSpec.describe "User Features" do it "personal details page is present and accessible" do visit("/users/account/personal_details") - expect(page).to have_content("Personal details") - end - - it "can navigate to personal details from main account page" do - visit("/users/account") - click_link("change-name") - expect(page).to have_content("Personal details") - visit("/users/account") - click_link("change-email") - expect(page).to have_content("Personal details") + expect(page).to have_content("Change your personal details") end it "edit password page present and accessible" do @@ -93,5 +84,15 @@ RSpec.describe "User Features" do 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