Class: NextSgad::Message

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

Instance Method Summary collapse

Methods inherited from ApplicationRecord

#create_number, #create_number!

Instance Method Details

#create_messagesObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/next_sgad/message.rb', line 19

def create_messages
  return unless sent?
  empl_message = NextSgad::EmployeeMessage.new(message_id: id, title: title, body: body, signature: signature)
  
  if send_to_all
    all_employees_ids = NextSgad::Employee.ids
  else
    all_employees_ids = NextSgad::Position.where(function_id: functions.ids).map(&:efective_id) +
    NextSgad::Position.where(department_id: departments.ids).map(&:efective_id) +
    positions.map(&:efective_id) +
    employees.ids
  end
  all_employees_ids.each do |employee_id|
    duped_empl_message = empl_message.dup
    duped_empl_message.employee_id = employee_id
    duped_empl_message.save
  end
end