Class: EmployeeGoal

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable
Defined in:
app/models/employee_goal.rb

Overview

== Schema Information

Table name: employee_goals
Database name: primary

id :integer not null, primary key
description :text
due_date :date not null
earned_reward :decimal(6, 2)
employee_comments :text
name :string(255) not null
potential_reward :decimal(6, 2)
state :string(255) not null
supervisor_comments :text
created_at :datetime not null
updated_at :datetime not null
creator_id :integer
employee_review_id :integer
updater_id :integer

Indexes

employee_review_id_name (employee_review_id,name)
employee_review_id_state (employee_review_id,state)

Constant Summary collapse

FINALIZED_STATES =

Recognised finalized states.

%i[reached missed cancelled].freeze

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Constants included from Schedulable

Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary collapse

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models::Auditable

#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#descriptionObject (readonly)



38
# File 'app/models/employee_goal.rb', line 38

validates :description, presence: true

#due_dateObject (readonly)



39
# File 'app/models/employee_goal.rb', line 39

validates :due_date, presence: true

#earned_rewardObject (readonly)



42
# File 'app/models/employee_goal.rb', line 42

validates :earned_reward, numericality: { greather_than_or_equal: 0 }, if: -> { reached? && potential_reward.present? }

#employee_commentsObject (readonly)



43
# File 'app/models/employee_goal.rb', line 43

validates :employee_comments, presence: { message: 'required when goal missed.' }, if: :missed?

#nameObject (readonly)



37
# File 'app/models/employee_goal.rb', line 37

validates :name, presence: true, uniqueness: { scope: :employee_review_id }

#potential_rewardObject (readonly)



41
# File 'app/models/employee_goal.rb', line 41

validates :potential_reward, numericality: { greather_than_or_equal: 0 }, if: -> { potential_reward.present? }

#supervisor_commentsObject (readonly)



44
# File 'app/models/employee_goal.rb', line 44

validates :supervisor_comments, presence: { message: 'required when goal missed.' }, if: :missed?

Class Method Details

.activeActiveRecord::Relation<EmployeeGoal>

A relation of EmployeeGoals that are active. Active Record Scope

Returns:

See Also:



51
# File 'app/models/employee_goal.rb', line 51

scope :active, -> { where(EmployeeGoal[:state].not_eq('cancelled')) }

.departments_for_selectObject



88
89
90
# File 'app/models/employee_goal.rb', line 88

def self.departments_for_select
  joins(employee_review: :employee_record).distinct.pluck(EmployeeRecord[:department]).compact.sort
end

.finalizedActiveRecord::Relation<EmployeeGoal>

A relation of EmployeeGoals that are finalized. Active Record Scope

Returns:

See Also:



49
# File 'app/models/employee_goal.rb', line 49

scope :finalized, -> { where(EmployeeGoal[:state].in(FINALIZED_STATES)) }

.states_for_selectObject



84
85
86
# File 'app/models/employee_goal.rb', line 84

def self.states_for_select
  state_machines[:state].states.map { |s| [s.name.to_s.titleize, s.name.to_s] }.sort
end

.unfinalizedActiveRecord::Relation<EmployeeGoal>

A relation of EmployeeGoals that are unfinalized. Active Record Scope

Returns:

See Also:



50
# File 'app/models/employee_goal.rb', line 50

scope :unfinalized, -> { where(EmployeeGoal[:state].not_in(FINALIZED_STATES)) }

Instance Method Details

#allocated_reward_in_other_goalsObject



100
101
102
# File 'app/models/employee_goal.rb', line 100

def allocated_reward_in_other_goals
  employee_review.employee_goals.where.not(employee_goals: { id: id.to_i }).sum(:potential_reward) || 0.00
end

#employeeObject



92
93
94
# File 'app/models/employee_goal.rb', line 92

def employee
  employee_review&.employee_record&.employee
end

#employee_nameObject



96
97
98
# File 'app/models/employee_goal.rb', line 96

def employee_name
  employee&.full_name
end

#employee_reviewEmployeeReview

Validations:



34
# File 'app/models/employee_goal.rb', line 34

belongs_to :employee_review, inverse_of: :employee_goals, optional: true

#finalized?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'app/models/employee_goal.rb', line 112

def finalized?
  FINALIZED_STATES.include? state
end

#possible_eventsObject



104
105
106
# File 'app/models/employee_goal.rb', line 104

def possible_events
  state_transitions.map(&:event).sort
end

#possible_events_for_selectObject



108
109
110
# File 'app/models/employee_goal.rb', line 108

def possible_events_for_select
  possible_events.map { |evt| [evt.to_s.titleize, evt] }
end