Class: NextSgad::Goal

Inherits:
ApplicationRecord show all
Defined in:
app/models/next_sgad/goal.rb

Instance Method Summary collapse

Methods inherited from ApplicationRecord

#create_number, #create_number!

Instance Method Details

#create_employee_goalsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/next_sgad/goal.rb', line 22

def create_employee_goals

  # checks the positions who should have this Goal
  # if the employee goal is for everyone
  positions_to = if for_everyone
    NextSgad::Position.where.not(efective_id: nil).can_be_assessed
  # if its a skill
  elsif skill?
    NextSgad::Position.where(function_id: function_ids).where.not(efective_id: nil).can_be_assessed
  # if its an objective
  elsif objective?
    positions.where.not(efective_id: nil).can_be_assessed
  end

  positions_ids = positions_to.map(&:id)
  # Update existing employee goals
  employee_goals_to_update = employee_goals.where(position_id: positions_ids).to_a
  employee_goals_to_update.each do |employee_goal|
    employee_goal.update(status: status, goal_type: goal_type, name: name, unit: unit, nature: nature, target: target, assessment_id: assessment_id, goal_id: id)
  end

  # Destroy employee_goals deselected from the list
  employee_goals.where.not(position_id: positions_ids).each(&:destroy)
  
  # create new employee goals for the remainder
  eg = NextSgad::EmployeeGoal.new(status: status, goal_type: goal_type, name: name, unit: unit, nature: nature, target: target, assessment_id: assessment_id, goal_id: id)
  positions_to.where.not(id: employee_goals.map(&:position_id)).each do |position|
    dupped_eg = eg.dup
    dupped_eg.employee_id = position.efective_id
    dupped_eg.position_id = position.id
    dupped_eg.save
  end
end