Browse Source

Account page: able to change name and email address

pull/110/head
Matthew Phelan 4 years ago
parent
commit
e1214debfb
  1. 6
      app/controllers/users/account_controller.rb
  2. 14
      app/helpers/account_helper.rb
  3. 4
      app/views/users/account/index.html.erb
  4. 26
      app/views/users/account/personal_details.html.erb
  5. 1
      config/routes.rb
  6. 7
      db/migrate/20211125114400_add_name_email_role_org_to_users.rb
  7. 5
      db/schema.rb
  8. 21
      spec/features/user_spec.rb

6
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

14
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

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

@ -12,6 +12,7 @@
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">
@ -24,6 +25,7 @@
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">
@ -47,6 +49,7 @@
Organisation
</dt>
<dd class="govuk-summary-list__value">
<%= current_user.organisation %>
</dd>
<dd class="govuk-summary-list__actions">
<a class="govuk-link" href="">
@ -58,6 +61,7 @@
Role
</dt>
<dd class="govuk-summary-list__value">
<%= current_user.role %>
</dd>
<dd class="govuk-summary-list__actions">
<a class="govuk-link" href="">

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

@ -1,3 +1,23 @@
<h1 class="govuk-heading-l">
Personal details
</h1>
<% 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 %>

1
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

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.
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

21
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

Loading…
Cancel
Save