Class: HrLite::Policy
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- HrLite::Policy
- Includes:
- Audited
- Defined in:
- app/models/hr_lite/policy.rb
Overview
A written policy people are held to. Versioned, because "you agreed to this" is only true of the version somebody actually read.
Constant Summary
Constants included from Audited
Audited::REDACTED, Audited::SKIPPED_ATTRIBUTES
Class Method Summary collapse
-
.current ⇒ Object
The version in force today for each title — an older one stays for the acknowledgements attached to it, but nobody should be reading it.
Instance Method Summary collapse
-
#acknowledge!(user) ⇒ Object
Clicking twice is not an error and must not read as one.
- #acknowledged_by?(user) ⇒ Boolean
- #outstanding_for ⇒ Object
-
#supersede!(body:, effective_from: Date.current, acknowledgement_required: nil) ⇒ Object
Re-issuing asks everybody again, which is the whole point of versioning it: an acknowledgement of v1 says nothing about v2.
Class Method Details
.current ⇒ Object
The version in force today for each title — an older one stays for the acknowledgements attached to it, but nobody should be reading it.
21 22 23 24 25 |
# File 'app/models/hr_lite/policy.rb', line 21 def self.current live_on(Date.current).group_by(&:title).values.map do |versions| versions.max_by(&:version) end end |
Instance Method Details
#acknowledge!(user) ⇒ Object
Clicking twice is not an error and must not read as one. The find_or_create handles the second click; the rescue handles two requests arriving at once, which the unique index refuses.
51 52 53 54 55 56 57 |
# File 'app/models/hr_lite/policy.rb', line 51 def acknowledge!(user) policy_acknowledgements.find_or_create_by!(user_id: user.id) do |ack| ack.acknowledged_at = Time.current end rescue ActiveRecord::RecordNotUnique policy_acknowledgements.find_by!(user_id: user.id) end |
#acknowledged_by?(user) ⇒ Boolean
37 38 39 |
# File 'app/models/hr_lite/policy.rb', line 37 def acknowledged_by?(user) policy_acknowledgements.exists?(user_id: user.id) end |
#outstanding_for ⇒ Object
41 42 43 44 45 46 |
# File 'app/models/hr_lite/policy.rb', line 41 def outstanding_for return HrLite.user_klass.none unless acknowledgement_required && published HrLite.user_klass.where(id: HrLite.active_employees.map(&:id)) .where.not(id: policy_acknowledgements.select(:user_id)) end |
#supersede!(body:, effective_from: Date.current, acknowledgement_required: nil) ⇒ Object
Re-issuing asks everybody again, which is the whole point of versioning it: an acknowledgement of v1 says nothing about v2.
29 30 31 32 33 34 35 |
# File 'app/models/hr_lite/policy.rb', line 29 def supersede!(body:, effective_from: Date.current, acknowledgement_required: nil) self.class.create!( title: title, body: body, version: version + 1, effective_from: effective_from, acknowledgement_required: acknowledgement_required.nil? ? self.acknowledgement_required : acknowledgement_required, published: true ) end |