Class: Mumuki::Classroom::Notification

Inherits:
Document
  • Object
show all
Includes:
Mongoid::Timestamps
Defined in:
lib/mumuki/classroom/models/notification.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Document

inherited, whitelist_attributes

Class Method Details

.allowed(options, permissions) ⇒ Object



15
16
17
# File 'lib/mumuki/classroom/models/notification.rb', line 15

def self.allowed(options, permissions)
  where(options).select {|notification| permissions.has_permission? :teacher, notification.course}.map(&:with_assignment)
end

.import_from_json!(type, assignment) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/mumuki/classroom/models/notification.rb', line 32

def self.import_from_json!(type, assignment)
  Mumuki::Classroom::Notification.create! organization: assignment.organization,
                       course: assignment.course,
                       type: type,
                       sender: assignment.student[:uid],
                       assignment: assignment
end

.page(organization, permissions, page, per_page) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/mumuki/classroom/models/notification.rb', line 19

def self.page(organization, permissions, page, per_page)
  where(organization: organization)
    .sort(created_at: :desc)
    .skip(per_page * (page - 1))
    .limit(per_page)
    .select {|notification| permissions.has_permission? :teacher, notification.course}
    .map(&:with_assignment)
end

.unread(organization, permissions) ⇒ Object



28
29
30
# File 'lib/mumuki/classroom/models/notification.rb', line 28

def self.unread(organization, permissions)
  allowed({organization: organization, read: false}, permissions)
end

Instance Method Details

#read!Object



40
41
42
# File 'lib/mumuki/classroom/models/notification.rb', line 40

def read!
  update! read: true
end

#unread!Object



44
45
46
# File 'lib/mumuki/classroom/models/notification.rb', line 44

def unread!
  update! read: false
end

#with_assignmentObject



48
49
50
# File 'lib/mumuki/classroom/models/notification.rb', line 48

def with_assignment
  as_json.except('assignment_id').merge(assignment: assignment.notification_preview, id: id.to_s)
end