Class: Renalware::User
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Renalware::User
- Includes:
- Deviseable, Personable
- Defined in:
- app/models/renalware/user.rb
Instance Attribute Summary collapse
-
#with_extended_validation ⇒ Object
Non-persistent attribute to signify we want to use extended validation.
Class Method Summary collapse
- .policy_class ⇒ Object
-
.ransackable_scopes(_auth_object = nil) ⇒ Object
So we can uses these scopes as Ransack predicates eg.
Instance Method Summary collapse
-
#auth_token ⇒ Object
Create a sha that can be saved in another model to indicate a user has authenticated (or perhaps more correctly, authorised) an action - e.g.
- #generate_new_authentication_token! ⇒ Object
-
#has_role?(name) ⇒ Boolean
rubocop:disable Naming/PredicateName.
-
#professional_signature ⇒ Object
Official name for use when displaying e.g.
-
#role_names ⇒ Object
rubocop:enable Naming/PredicateName.
-
#wants_feature?(flag) ⇒ Boolean
We can enable experiment features for particular users using the bitmask user#feature_flags property and bitwise operators.
Instance Attribute Details
#with_extended_validation ⇒ Object
Non-persistent attribute to signify we want to use extended validation. We need to refactor this by ising a form object for updating a user.
49 50 51 |
# File 'app/models/renalware/user.rb', line 49 def with_extended_validation @with_extended_validation end |
Class Method Details
.policy_class ⇒ Object
51 52 53 |
# File 'app/models/renalware/user.rb', line 51 def self.policy_class UserPolicy end |
.ransackable_scopes(_auth_object = nil) ⇒ Object
So we can uses these scopes as Ransack predicates eg. { expired: true }
56 57 58 |
# File 'app/models/renalware/user.rb', line 56 def self.ransackable_scopes(_auth_object = nil) %i(unapproved inactive expired) end |
Instance Method Details
#auth_token ⇒ Object
Create a sha that can be saved in another model to indicate a user has authenticated (or perhaps more correctly, authorised) an action - e.g. in HD Session form where a nurse and witness both enter their credentials against a prescription administered on HD. The idea is that we can check the token belongs to the user buy regenerating the token at any time and checking it still matches. Unlike Devise.friendly_token, we can always regenerate the same token here for any user as it is salted with the same secret. This secret is not stored git for staging and production environments.
91 92 93 94 95 |
# File 'app/models/renalware/user.rb', line 91 def auth_token digest = OpenSSL::Digest.new("sha256") key = Rails.application.secrets.secret_key_base OpenSSL::HMAC.hexdigest(digest, key, id.to_s) end |
#generate_new_authentication_token! ⇒ Object
78 79 80 81 82 |
# File 'app/models/renalware/user.rb', line 78 def generate_new_authentication_token! build_authentication_token.tap do |token| update_column(:authentication_token, token) end end |
#has_role?(name) ⇒ Boolean
rubocop:disable Naming/PredicateName
61 62 63 |
# File 'app/models/renalware/user.rb', line 61 def has_role?(name) role_names.include?(name.to_s) end |
#professional_signature ⇒ Object
Official name for use when displaying e.g. on a letter. For example:
Dr Isaac Newton (Consultant Gravitationalist)
72 73 74 75 76 |
# File 'app/models/renalware/user.rb', line 72 def professional_signature signed = signature || full_name signed += " (#{professional_position})" if professional_position? signed end |
#role_names ⇒ Object
rubocop:enable Naming/PredicateName
66 67 68 |
# File 'app/models/renalware/user.rb', line 66 def role_names @role_names ||= roles.pluck(:name) end |
#wants_feature?(flag) ⇒ Boolean
We can enable experiment features for particular users using the bitmask user#feature_flags property and bitwise operators. For example given the the feature flag FANCY_GRAPHS = 2, we if they user should see these with
FANCY_GRAPHS & feature_flags == FANCY_GRAPHS
109 110 111 |
# File 'app/models/renalware/user.rb', line 109 def wants_feature?(flag) (flag & feature_flags) == flag end |