Browse Source

spike part 2

pull/787/head
JG 3 years ago
parent
commit
e2dbb54139
  1. 21
      app/controllers/users_controller.rb
  2. 15
      app/models/user.rb
  3. 12
      app/views/users/new.html.erb
  4. 5
      db/migrate/20220728092850_change_default_value_for_user_role.rb
  5. 4
      db/schema.rb
  6. 6
      db/seeds.rb
  7. 1
      spec/requests/users_controller_spec.rb

21
app/controllers/users_controller.rb

@ -64,18 +64,19 @@ class UsersController < ApplicationController
def new
@organisation_id = params["organisation_id"]
@user = User.new
@resource = User.new
debugger
end
def create
@user = User.new(user_params.merge(org_params).merge(password_params))
@resource = User.new(user_params.merge(org_params).merge(password_params))
if valid_attributes? && @user.save
if valid_attributes? && @resource.save
redirect_to created_user_redirect_path
else
unless @user.errors[:organisation].empty?
@user.errors.add(:organisation_id, message: @user.errors[:organisation])
@user.errors.delete(:organisation)
unless @resource.errors[:organisation].empty?
@resource.errors.add(:organisation_id, message: @user.errors[:organisation])
@resource.errors.delete(:organisation)
end
render :new, status: :unprocessable_entity
end
@ -105,11 +106,11 @@ class UsersController < ApplicationController
private
def valid_attributes?
@user.valid?
if user_params[:role] && !current_user.assignable_roles.key?(user_params[:role].to_sym)
@user.errors.add :role, I18n.t("validations.role.invalid")
@resource.valid?
if user_params[:role].present? && !current_user.assignable_roles.key?(user_params[:role].to_sym)
@resource.errors.add :role, I18n.t("validations.role.invalid")
end
@user.errors.empty?
@resource.errors.empty?
end
def format_error_messages

15
app/models/user.rb

@ -10,7 +10,7 @@ class User < ApplicationRecord
has_many :managed_case_logs, through: :organisation
validate :validate_email
validates :name, :role, :email, presence: true
validates :name, :email, presence: true
has_paper_trail ignore: %w[last_sign_in_at
current_sign_in_at
@ -122,6 +122,19 @@ class User < ApplicationRecord
ROLES.except(:support)
end
def assignable_roles_with_hints
roles_with_hints = {
data_provider: "Can view and submit logs for this this organisation",
data_coordinator: "data_coordinator",
support: "support",
}
return {} unless data_coordinator? || support?
return roles_with_hints if support?
roles_with_hints.except(:support)
end
def case_logs_filters(specific_org: false)
if support? && !specific_org
%w[status years user organisation]

12
app/views/users/new.html.erb

@ -4,7 +4,7 @@
<%= govuk_back_link(href: :back) %>
<% end %>
<%= form_for(@user, as: :user, html: { method: :post }) do |f| %>
<%= form_for(@resource, as: :user, html: { method: :post }) do |f| %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= f.govuk_error_summary %>
@ -21,7 +21,7 @@
label: { text: "Email address", size: "m" },
autocomplete: "email",
spellcheck: "false",
value: @user.email %>
value: @resource.email %>
<% if current_user.support? %>
<% null_option = [OpenStruct.new(id: "", name: "Select an option")] %>
@ -42,7 +42,7 @@
<% end %>
<% hints_for_roles = { data_provider: ["Can view and submit logs for this this organisation"], data_coordinator: ["Can view and submit logs for this this organisation"], support: [] } %>
<% hints_for_roles = { data_provider: ["Provider hint"], data_coordinator: ["Support hint", "Other"], support: [] } %>
<% roles_with_hints = current_user.assignable_roles.map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize, description: hints_for_roles[key.to_sym]) } %>
@ -51,11 +51,7 @@
:id,
:name,
->(option) do
content_tag(:ul, class: "govuk-list govuk-list--bullet govuk-hint") do
content_tag(:li, "1")
content_tag(:li, "2")
content_tag(:li, "3")
end
result = option.description.sum(nil) {|hint| content_tag(:li, hint) }
end,
legend: { text: "Role", size: "m" } %>

5
db/migrate/20220728092850_change_default_value_for_user_role.rb

@ -0,0 +1,5 @@
class ChangeDefaultValueForUserRole < ActiveRecord::Migration[7.0]
def change
change_column_default :users, :role, 1
end
end

4
db/schema.rb

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2022_07_22_083835) do
ActiveRecord::Schema[7.0].define(version: 2022_07_28_092850) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -341,7 +341,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_22_083835) do
t.datetime "last_sign_in_at", precision: nil
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.integer "role"
t.integer "role", default: 1
t.string "old_user_id"
t.string "phone"
t.integer "failed_attempts", default: 0

6
db/seeds.rb

@ -83,6 +83,8 @@ unless Rails.env.test?
primary_client_group: "O",
secondary_client_group: "H",
owning_organisation: org,
managing_organisation: org,
arrangement_type: "D",
confirmed: true,
created_at: Time.zone.now,
)
@ -97,6 +99,8 @@ unless Rails.env.test?
primary_client_group: "D",
secondary_client_group: "E",
owning_organisation: org,
managing_organisation: org,
arrangement_type: "D",
confirmed: true,
created_at: Time.zone.now,
)
@ -111,6 +115,8 @@ unless Rails.env.test?
primary_client_group: "G",
secondary_client_group: "R",
owning_organisation: dummy_org,
managing_organisation: dummy_org,
arrangement_type: "D",
confirmed: true,
created_at: Time.zone.now,
)

1
spec/requests/users_controller_spec.rb

@ -1613,7 +1613,6 @@ RSpec.describe UsersController, type: :request do
expect(response).to have_http_status(:unprocessable_entity)
expect(page).to have_content(I18n.t("activerecord.errors.models.user.attributes.name.blank"))
expect(page).to have_content(I18n.t("activerecord.errors.models.user.attributes.email.blank"))
expect(page).to have_content(I18n.t("activerecord.errors.models.user.attributes.role.blank"))
expect(page).to have_content(I18n.t("activerecord.errors.models.user.attributes.organisation_id.blank"))
end
end

Loading…
Cancel
Save