Class: Message

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

aggregate_of, all_except, defaults, name_model_as, numbered, #save, #save_and_notify!, #save_and_notify_changes!, #update_and_notify!, update_or_create!

Class Method Details

.import_from_json!(json) ⇒ Object



35
36
37
38
39
40
41
42
# File 'app/models/message.rb', line 35

def self.import_from_json!(json)
  message_data = parse_json json
  Organization.find_by!(name: message_data.delete('organization')).switch!

  if message_data['submission_id'].present?
    Assignment.find_by(submission_id: message_data.delete('submission_id'))&.receive_answer! message_data
  end
end

.parse_json(json) ⇒ Object



24
25
26
27
28
29
# File 'app/models/message.rb', line 24

def self.parse_json(json)
  message = json.delete 'message'
  json
    .except('uid', 'exercise_id')
    .merge(message)
end

.read_all!Object



31
32
33
# File 'app/models/message.rb', line 31

def self.read_all!
  update_all read: true
end

Instance Method Details

#as_platform_jsonObject



14
15
16
17
18
# File 'app/models/message.rb', line 14

def as_platform_json
  as_json(except: [:id, :type],
          include: {exercise: {only: [:bibliotheca_id]}})
    .merge(organization: Organization.current.name)
end

#notify!Object



10
11
12
# File 'app/models/message.rb', line 10

def notify!
  Mumukit::Nuntius.notify! 'student-messages', as_platform_json
end

#read!Object



20
21
22
# File 'app/models/message.rb', line 20

def read!
  update! read: true
end