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 =
%i[reached missed cancelled].freeze

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

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 Models::EventPublishable

#publish_event

Instance Attribute Details

#descriptionObject (readonly)



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

validates :description, presence: true

#due_dateObject (readonly)



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

validates :due_date, presence: true

#earned_rewardObject (readonly)



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

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

#employee_commentsObject (readonly)



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

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

#nameObject (readonly)



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

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

#potential_rewardObject (readonly)



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

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

#supervisor_commentsObject (readonly)



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

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:



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

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

.departments_for_selectObject



86
87
88
# File 'app/models/employee_goal.rb', line 86

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:



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

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

.states_for_selectObject



82
83
84
# File 'app/models/employee_goal.rb', line 82

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:



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

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

Instance Method Details

#allocated_reward_in_other_goalsObject



98
99
100
# File 'app/models/employee_goal.rb', line 98

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



90
91
92
# File 'app/models/employee_goal.rb', line 90

def employee
  employee_review&.employee_record&.employee
end

#employee_nameObject



94
95
96
# File 'app/models/employee_goal.rb', line 94

def employee_name
  employee&.full_name
end

#employee_reviewEmployeeReview

Validations:



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

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

#finalized?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'app/models/employee_goal.rb', line 110

def finalized?
  FINALIZED_STATES.include? state
end

#possible_eventsObject



102
103
104
# File 'app/models/employee_goal.rb', line 102

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

#possible_events_for_selectObject



106
107
108
# File 'app/models/employee_goal.rb', line 106

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