Class: Lcms::Engine::CurriculumResourceSerializer

Inherits:
ActiveModel::Serializer
  • Object
show all
Defined in:
app/serializers/lcms/engine/curriculum_resource_serializer.rb

Instance Method Summary collapse

Constructor Details

#initialize(object, options = {}) ⇒ CurriculumResourceSerializer

Returns a new instance of CurriculumResourceSerializer.



9
10
11
12
13
# File 'app/serializers/lcms/engine/curriculum_resource_serializer.rb', line 9

def initialize(object, options = {})
  super(object, options)
  @depth = options[:depth] || 0
  @depth_branch = options[:depth_branch]
end

Instance Method Details

#childrenObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/serializers/lcms/engine/curriculum_resource_serializer.rb', line 23

def children
  return [] if !module? && @depth.zero?
  return [] if !module? && @depth_branch && !@depth_branch.include?(object.id)

  object.children
    .includes(:copyright_attributions)
    .eager_load(:standards)
    .ordered.map do |res|
    CurriculumResourceSerializer.new(
      res,
      depth: @depth - 1,
      depth_branch: @depth_branch
    ).as_json
  end
end

#descendantsObject



67
68
69
# File 'app/serializers/lcms/engine/curriculum_resource_serializer.rb', line 67

def descendants
  @descendants ||= object.self_and_descendants.ordered
end

#lesson_countObject



39
40
41
42
# File 'app/serializers/lcms/engine/curriculum_resource_serializer.rb', line 39

def lesson_count
  count = descendants.select(&:lesson?).count
  object.assessment? ? count + 1 : count
end

#module?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'app/serializers/lcms/engine/curriculum_resource_serializer.rb', line 48

def module?
  type.casecmp('module').zero?
end

#module_countObject



52
53
54
# File 'app/serializers/lcms/engine/curriculum_resource_serializer.rb', line 52

def module_count
  descendants.select(&:module?).count
end

#module_sizesObject



56
57
58
# File 'app/serializers/lcms/engine/curriculum_resource_serializer.rb', line 56

def module_sizes
  descendants.select(&:module?).map { |r| r.self_and_descendants.lessons.count }
end

#resourceObject



15
16
17
# File 'app/serializers/lcms/engine/curriculum_resource_serializer.rb', line 15

def resource
  ResourceDetailsSerializer.new(object).as_json
end

#typeObject



19
20
21
# File 'app/serializers/lcms/engine/curriculum_resource_serializer.rb', line 19

def type
  object.curriculum_type
end

#unit_countObject



44
45
46
# File 'app/serializers/lcms/engine/curriculum_resource_serializer.rb', line 44

def unit_count
  descendants.select(&:unit?).count
end

#unit_sizesObject



60
61
62
63
64
65
# File 'app/serializers/lcms/engine/curriculum_resource_serializer.rb', line 60

def unit_sizes
  descendants.select(&:unit?).map do |r|
    count = r.self_and_descendants.lessons.count
    r.assessment? ? count + 1 : count
  end
end