diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 2ae73db94..f2c788762 100644 --- a/app/controllers/users_controller.rb +++ b/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 diff --git a/app/models/user.rb b/app/models/user.rb index 2010c30e3..214569c1d 100644 --- a/app/models/user.rb +++ b/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] diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb index 92e39c547..d90596db6 100644 --- a/app/views/users/new.html.erb +++ b/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| %>
<%= 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" } %> diff --git a/db/migrate/20220728092850_change_default_value_for_user_role.rb b/db/migrate/20220728092850_change_default_value_for_user_role.rb new file mode 100644 index 000000000..e6844003b --- /dev/null +++ b/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 diff --git a/db/schema.rb b/db/schema.rb index 39bc35e94..0f12622c0 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[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 diff --git a/db/seeds.rb b/db/seeds.rb index 86f6d3395..e30276fc1 100644 --- a/db/seeds.rb +++ b/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, ) diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb index f7ba72e7c..be47f30fc 100644 --- a/spec/requests/users_controller_spec.rb +++ b/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