Skip to content

Commit

Permalink
Refactor User models to use STI
Browse files Browse the repository at this point in the history
WIP

Rspec passing and seeds working

Improve rspec testing
  • Loading branch information
Jamie committed Jan 26, 2024
1 parent de648e1 commit 2c63a12
Show file tree
Hide file tree
Showing 65 changed files with 416 additions and 412 deletions.
31 changes: 31 additions & 0 deletions app/components/persona_sign_in_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<h2 class="govuk-heading-m">
<%= persona.first_name %>
</h2>
<strong class="govuk-tag govuk-tag--<%= type_tag_colour %> govuk-!-margin-bottom-5">
<%= t(".#{persona.first_name.downcase}.persona_type", default: "") %>
</strong>
<p class="govuk-body">
<%= t(".#{persona.first_name.downcase}.description", default: "") %>
</p>

<% if t(".#{persona.first_name.downcase}.roles", default: "").present? %>
<p class="govuk-body">
<%= t(".roles_include", persona_name: persona.first_name, default: "") %>
</p>
<ul class="govuk-list govuk-list--bullet">
<% t(".#{persona.first_name.downcase}.roles").each do |role| %>
<li><%= role %></li>
<% end %>
</ul>
<% end %>

<%= form_tag("/auth/developer/callback", method: "post", data: { turbo: false }) do %>
<%= hidden_field_tag "email", persona.email %>
<%= hidden_field_tag "first_name", persona.first_name %>
<%= hidden_field_tag "last_name", persona.last_name %>
<button type="submit" class="govuk-button govuk-!-margin-bottom-2">
<%= t(".sign_in_as", persona_name: persona.first_name) %>
</button>
<% end %>

<hr class="govuk-section-break govuk-section-break--visible govuk-section-break--l">
24 changes: 24 additions & 0 deletions app/components/persona_sign_in_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class PersonaSignInComponent < ApplicationComponent
attr_reader :persona

def initialize(persona, classes: [], html_attributes: {})
super(classes:, html_attributes:)

@persona = persona
end

def type_tag_colour
case persona.first_name
when "Anne"
"purple"
when "Patricia"
"orange"
when "Mary"
"yellow"
when "Colin"
"blue"
else
"turquoise"
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def current_user
end

def after_sign_in_path
if current_user.support_user?
if current_user.is_support_user?
support_root_path
else
organisations_path
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/claims/support/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Claims::Support::ApplicationController < ApplicationController
private

def authorize_user!
return if current_user.support_user?
return if current_user.is_support_user?

redirect_to claims_root_path, alert: t("you_cannot_perform_this_action")
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/concerns/claims/belongs_to_school.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def set_school
end

def scoped_schools
return Claims::School.all if current_user.support_user?
return Claims::School.all if current_user.is_support_user?

current_user.schools
end
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/personas_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class PersonasController < ApplicationController
skip_before_action :authenticate_user!, only: %i[index]

def index
@personas = Persona.public_send(current_service).decorate
@personas = User.where(email: PERSONA_EMAILS)
.where(type: ["#{current_service.to_s.titleize}::User",
"#{current_service.to_s.titleize}::SupportUser"])
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Placements::Support::ApplicationController < ApplicationController
private

def authorize_user!
return if current_user.support_user?
return if current_user.is_support_user?

redirect_to placements_root_path, alert: t("you_cannot_perform_this_action")
end
Expand Down
2 changes: 1 addition & 1 deletion app/forms/user_invite_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class UserInviteForm
def invite
return false unless valid?

UserInviteService.call(user, organisation) if save_user
UserInviteService.call(user, organisation, service) if save_user
end

def as_form_params
Expand Down
6 changes: 3 additions & 3 deletions app/mailers/notify_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
class NotifyMailer < ApplicationMailer
# NotifyMailer.send_organisation_invite_email.deliver_later
def send_organisation_invite_email(user, organisation, sign_in_url)
body = t("#{user.service}.mailers.notify_mailer.body",
def send_organisation_invite_email(user, organisation, service, sign_in_url)
body = t("#{service}.mailers.notify_mailer.body",
user_name: user.full_name,
organisation_name: organisation.name,
sign_in_url:)

mailer_options = { to: user.email,
subject: t(
"#{user.service}.mailers.notify_mailer.subject",
"#{service}.mailers.notify_mailer.subject",
organisation_name: organisation.name,
),
body: }
Expand Down
21 changes: 21 additions & 0 deletions app/models/claims/support_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# == Schema Information
#
# Table name: users
#
# id :uuid not null, primary key
# email :string not null
# first_name :string not null
# last_name :string not null
# type :string
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_users_on_type_and_email (type,email) UNIQUE
#
class Claims::SupportUser < User
def is_support_user?
true
end
end
19 changes: 8 additions & 11 deletions app/models/claims/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@
#
# Table name: users
#
# id :uuid not null, primary key
# email :string not null
# first_name :string not null
# last_name :string not null
# service :enum not null
# support_user :boolean default(FALSE)
# created_at :datetime not null
# updated_at :datetime not null
# id :uuid not null, primary key
# email :string not null
# first_name :string not null
# last_name :string not null
# type :string
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_users_on_service_and_email (service,email) UNIQUE
# index_users_on_type_and_email (type,email) UNIQUE
#
class Claims::User < User
default_scope { claims }

has_many :schools,
-> { claims_service },
through: :memberships,
Expand Down
22 changes: 1 addition & 21 deletions app/models/dfe_sign_in_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,10 @@ def self.load_from_session(session)

def user
# TODO: When dfe sign-in is fully implemented, we will be able to find the user by the id == id_token.
@user ||=
begin
user = user_klass.find_by(email:)

if service == :placements && user&.support_user?
user = user.becomes(Placements::SupportUser)
end

user
end
@user ||= User.find_by(email:)
end

def self.end_session!(session)
session.clear
end

private

def user_klass
case service
when :claims
Claims::User
when :placements
Placements::User
end
end
end
21 changes: 0 additions & 21 deletions app/models/persona.rb

This file was deleted.

23 changes: 12 additions & 11 deletions app/models/placements/support_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
#
# Table name: users
#
# id :uuid not null, primary key
# email :string not null
# first_name :string not null
# last_name :string not null
# service :enum not null
# support_user :boolean default(FALSE)
# created_at :datetime not null
# updated_at :datetime not null
# id :uuid not null, primary key
# email :string not null
# first_name :string not null
# last_name :string not null
# type :string
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_users_on_service_and_email (service,email) UNIQUE
# index_users_on_type_and_email (type,email) UNIQUE
#
class Placements::SupportUser < Placements::User
default_scope { support_users }
class Placements::SupportUser < User
def is_support_user?
true
end
end
19 changes: 8 additions & 11 deletions app/models/placements/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@
#
# Table name: users
#
# id :uuid not null, primary key
# email :string not null
# first_name :string not null
# last_name :string not null
# service :enum not null
# support_user :boolean default(FALSE)
# created_at :datetime not null
# updated_at :datetime not null
# id :uuid not null, primary key
# email :string not null
# first_name :string not null
# last_name :string not null
# type :string
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_users_on_service_and_email (service,email) UNIQUE
# index_users_on_type_and_email (type,email) UNIQUE
#
class Placements::User < User
default_scope { placements }

has_many :schools,
-> { placements_service },
through: :memberships,
Expand Down
32 changes: 16 additions & 16 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@
#
# Table name: users
#
# id :uuid not null, primary key
# email :string not null
# first_name :string not null
# last_name :string not null
# service :enum not null
# support_user :boolean default(FALSE)
# created_at :datetime not null
# updated_at :datetime not null
# id :uuid not null, primary key
# email :string not null
# first_name :string not null
# last_name :string not null
# type :string
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_users_on_service_and_email (service,email) UNIQUE
# index_users_on_type_and_email (type,email) UNIQUE
#
class User < ApplicationRecord
has_many :memberships

enum :service,
{ no_service: "no_service", claims: "claims", placements: "placements" },
validate: true

validates :first_name, presence: true
validates :last_name, presence: true
validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
validates :service, presence: true
validates :email, uniqueness: { scope: :service, case_sensitive: false }
validates :type, presence: true
validates :email, uniqueness: { scope: :type, case_sensitive: false }

scope :support_users, -> { where(support_user: true) }
scope :claims, -> { where(type: "Claims::User") }
scope :placements, -> { where(type: "Placements::User") }

def full_name
"#{first_name} #{last_name}".strip
end

def is_support_user?
false
end
end
8 changes: 4 additions & 4 deletions app/services/user_invite_service.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
class UserInviteService
include ServicePattern

def initialize(user, organisation)
def initialize(user, organisation, service)
@user = user
@organisation = organisation
@service = user.service
@service = service
end

attr_reader :user, :organisation, :service

def call
NotifyMailer.send_organisation_invite_email(user, organisation, url).deliver_later
NotifyMailer.send_organisation_invite_email(user, organisation, service.to_s, url).deliver_later
end

private
Expand All @@ -21,6 +21,6 @@ def url

def host
{ "claims" => ENV["CLAIMS_HOST"],
"placements" => ENV["PLACEMENTS_HOST"] }.fetch service
"placements" => ENV["PLACEMENTS_HOST"] }.fetch service.to_s
end
end
Loading

0 comments on commit 2c63a12

Please sign in to comment.