cirandas.net

ref: master

plugins/elearning_secretary/serializers/elearning_secretary_plugin/context_serializer.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class ElearningSecretaryPlugin::ContextSerializer < ApplicationSerializer

  has_many :sub_organizations, serializer: ElearningSecretaryPlugin::OrganizationSerializer

  has_many :students, serializer: ElearningSecretaryPlugin::StudentSerializer

  has_many :documents, serializer: ElearningSecretaryPlugin::DocumentSerializer

  def students
    Person.members_of(self.organizations).order('name ASC').eager_load(:memberships)
  end

  def sub_organizations
    @sub_organizations ||= Organization.children(self.object).order('name ASC').all
  end
  def organizations
    @organizations ||= [self.object] + self.sub_organizations
  end

  def documents
    parent = Organization.parents(self.object).first
    organizations = if parent then [parent] else [] end
    organizations.concat self.organizations
    organizations.flat_map{ |o| o.web_odf_documents.includes :profile }
  end

end