Browse Source

Refactor visible organisations logic into a class method

pull/2646/head
Manny Dinssa 2 years ago
parent
commit
7af73979b0
  1. 11
      app/models/user.rb

11
app/models/user.rb

@ -10,6 +10,12 @@ class User < ApplicationRecord
Thread.current[:current_user] Thread.current[:current_user]
end end
def self.visible_organisations
return [] unless current
current.organisation.child_organisations + [current.organisation]
end
# Marked as optional because we validate organisation_id below instead so that # Marked as optional because we validate organisation_id below instead so that
# the error message is linked to the right field on the form # the error message is linked to the right field on the form
belongs_to :organisation, optional: true belongs_to :organisation, optional: true
@ -89,11 +95,10 @@ class User < ApplicationRecord
scope :deactivated, -> { where(active: false) } scope :deactivated, -> { where(active: false) }
scope :active_status, -> { where(active: true).where.not(last_sign_in_at: nil) } scope :active_status, -> { where(active: true).where.not(last_sign_in_at: nil) }
scope :visible, lambda { scope :visible, lambda {
current_user = User.current if User.current&.support?
if current_user&.support?
where(discarded_at: nil) where(discarded_at: nil)
else else
where(discarded_at: nil, organisation: current_user.organisation.child_organisations + [current_user.organisation]) where(discarded_at: nil, organisation: visible_organisations)
end end
} }

Loading…
Cancel
Save