Sha256: ae2b9c59dfcad390690751b01af7c88ff6790d47a85769746c8082a5ac9890d0

Contents?: true

Size: 1.37 KB

Versions: 4

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module Lcms
  module Engine
    class PreviewsMaterialSerializer < ActiveModel::Serializer
      self.root = false
      attributes :activity, :color, :content_type, :data, :for_group, :lesson, :subject
      attr_reader :document
      delegate :content_type, :subject, to: :document

      def initialize(props, document)
        super(document)
        @document = document
        @props = props
      end

      def activity
        {}.tap do |x|
          %w(title type).each { |m| x["activity_#{m}"] = @props['activity'].send(:[], m) }
        end
      end

      def color
        @props['color']
      end

      def data
        ordered_ids = @document.ordered_material_ids
        materials.to_a
          .sort_by { |m| ordered_ids.index(m.id) }
          .map do |material|
          MaterialSerializer.new(
            DocumentGenerator.material_presenter.new material, document: @document
          ).as_json
        end
      end

      def for_group
        @props['group']
      end

      def lesson
        @lesson ||=
          {}.tap do |x|
            %i(grade ld_module subject title lesson).each { |m| x["lesson_#{m}"] = @document.send m }
            x['lesson_unit_topic'] = @document.topic
          end
      end

      private

      def materials
        @materials ||= Material.where(id: @props['material_ids'])
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lcms-engine-0.1.4 app/serializers/lcms/engine/previews_material_serializer.rb
lcms-engine-0.1.3 app/serializers/lcms/engine/previews_material_serializer.rb
lcms-engine-0.1.2 app/serializers/lcms/engine/previews_material_serializer.rb
lcms-engine-0.1.0 app/serializers/lcms/engine/previews_material_serializer.rb