Module: Models::EmployeeManagement
- Extended by:
- ActiveSupport::Concern
- Included in:
- Employee
- Defined in:
- app/concerns/models/employee_management.rb
Overview
Management hierarchy, customer assignment, and schema.org structure.
Mixed into Employee; combines subordinate-resolution (via the ltree
hierarchy plus role inheritance), customer-drop activity cleanup, and
schema.org structured-data output for public author pages.
Instance Method Summary collapse
-
#current_review ⇒ EmployeeReview?
The most recent active current-goals review for this employee.
-
#drop_customer_event(event) ⇒ void
Cancels open activities when a customer is dropped from this employee.
-
#manages ⇒ ActiveRecord::Relation<Employee>
Subordinates of this employee.
-
#manages?(employee) ⇒ Boolean
Whether the given employee reports to this one.
-
#manages_and_self ⇒ ActiveRecord::Relation<Employee>
Subordinates plus this employee.
-
#manages_ids ⇒ Array<Integer>
Ids of every employee this one manages, directly or transitively.
-
#role_cleanup ⇒ void
Reconciles this employee's account roles with their active state.
-
#schema_dot_org_structure ⇒ SchemaDotOrg::Person
schema.org Person representation of this employee.
-
#to_json_ld ⇒ String?
JSON-LD serialization of #schema_dot_org_structure.
Instance Method Details
#current_review ⇒ EmployeeReview?
The most recent active current-goals review for this employee.
94 95 96 |
# File 'app/concerns/models/employee_management.rb', line 94 def current_review employee_record.employee_reviews.current_goals.active.order(:created_at).reverse_order.first end |
#drop_customer_event(event) ⇒ void
This method returns an undefined value.
Cancels open activities when a customer is dropped from this employee.
No-ops while the customer still has any assigned sales rep; otherwise
marks every open activity for the customer as closed by the event's
creator with the "dropped" activity result type.
66 67 68 69 70 71 72 73 74 |
# File 'app/concerns/models/employee_management.rb', line 66 def drop_customer_event(event) return unless event.customer.sales_reps.empty? to_cancel = activities.open_activities.where(party_id: event.customer_id) logger.debug("Employee#drop_customer_event: #{event.inspect}, cancelling: #{to_cancel.count}") to_cancel.each do |activity| activity.update!(activity_result_type_id: 1, closed_by_id: event.creator_id) end end |
#manages ⇒ ActiveRecord::Relation<Employee>
Subordinates of this employee.
27 28 29 |
# File 'app/concerns/models/employee_management.rb', line 27 def manages Employee.where(id: manages_ids) end |
#manages?(employee) ⇒ Boolean
Whether the given employee reports to this one.
18 19 20 21 22 |
# File 'app/concerns/models/employee_management.rb', line 18 def manages?(employee) return false unless employee manages_ids.include?(employee.id) end |
#manages_and_self ⇒ ActiveRecord::Relation<Employee>
Subordinates plus this employee.
34 35 36 |
# File 'app/concerns/models/employee_management.rb', line 34 def manages_and_self Employee.where(id: manages_ids + [id]) end |
#manages_ids ⇒ Array<Integer>
Ids of every employee this one manages, directly or transitively.
Logic Details
Combines three sources and deduplicates them: descendants from the
ltree hierarchy, employees whose account roles are managed by this
employee's roles, and explicitly linked managed_employee_ids.
The result is memoized in Rails.cache keyed on the record's
cache_key.
48 49 50 51 52 53 54 55 56 |
# File 'app/concerns/models/employee_management.rb', line 48 def manages_ids Rails.cache.fetch([cache_key, :employee_subordinates]) do managed_role_ids = Role.managed_by_roles(role_ids).ids list = descendants.pluck('parties.id') list |= Employee.joins(account: :roles).where('roles.id' => managed_role_ids).pluck('parties.id') if managed_role_ids.present? list += managed_employee_ids list.uniq end end |
#role_cleanup ⇒ void
This method returns an undefined value.
Reconciles this employee's account roles with their active state.
Inactive employees lose all roles; active employees only shed roles
already covered by an inherited ancestor.
82 83 84 85 86 87 88 89 |
# File 'app/concerns/models/employee_management.rb', line 82 def role_cleanup if inactive logger.info "Employee #{id} Inactive, clearing all roles" account.roles.clear else prune_inherited_roles end end |
#schema_dot_org_structure ⇒ SchemaDotOrg::Person
schema.org Person representation of this employee.
101 102 103 104 105 106 107 108 109 110 |
# File 'app/concerns/models/employee_management.rb', line 101 def schema_dot_org_structure SchemaDotOrg::Person.new.tap do |person| person.givenName = first_name person.familyName = last_name person.name = full_name (person) assign_profile_image(person) assign_same_as_links(person) end end |
#to_json_ld ⇒ String?
Reports and swallows any error, returning nil, so structured
data can never break page rendering.
JSON-LD serialization of #schema_dot_org_structure.
117 118 119 120 121 122 |
# File 'app/concerns/models/employee_management.rb', line 117 def to_json_ld schema_dot_org_structure.to_s rescue StandardError => e ErrorReporting.error e logger.error e end |