Sha256: 0bb77d7fcf17ed17c59efd76e766f3491f94efe4ac40aa99b97bef06bd7c96be

Contents?: true

Size: 1.5 KB

Versions: 12

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module Lcms
  module Engine
    class DocumentBundle < ApplicationRecord
      CATEGORIES = %w(full tm sm).freeze
      CONTENT_TYPES = %w(pdf gdoc).freeze

      belongs_to :resource

      mount_uploader :file, DocumentBundleUploader

      validates :category, presence: true
      validates :category, inclusion: { in: CATEGORIES }
      validates :content_type, inclusion: { in: CONTENT_TYPES }

      def self.update_bundle(resource, category = 'full')
        update_pdf_bundle(resource, category)
        update_gdoc_bundle(resource) if category == 'full'
      end

      def self.update_pdf_bundle(resource, category)
        zip_path = LessonsPdfBundler.new(resource, category).bundle
        return unless File.exist?(zip_path.to_s)

        begin
          doc_bundle = find_or_create_by(resource: resource, category: category, content_type: 'pdf')

          File.open(zip_path) do |f|
            doc_bundle.file = f
            doc_bundle.save!
          end
        ensure
          FileUtils.rm(zip_path)
        end
      end
      private_class_method :update_pdf_bundle

      def self.update_gdoc_bundle(resource)
        return unless resource.unit?

        bundle_path = LessonsGdocBundler.new(resource).bundle
        return unless bundle_path

        doc_bundle = find_or_create_by(resource: resource, category: 'full', content_type: 'gdoc')
        doc_bundle.url = bundle_path
        doc_bundle.save!
      end
      private_class_method :update_gdoc_bundle
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
lcms-engine-0.5.5 app/models/lcms/engine/document_bundle.rb
lcms-engine-0.5.4 app/models/lcms/engine/document_bundle.rb
lcms-engine-0.5.3 app/models/lcms/engine/document_bundle.rb
lcms-engine-0.5.2 app/models/lcms/engine/document_bundle.rb
lcms-engine-0.5.1 app/models/lcms/engine/document_bundle.rb
lcms-engine-0.5.0 app/models/lcms/engine/document_bundle.rb
lcms-engine-0.4.2 app/models/lcms/engine/document_bundle.rb
lcms-engine-0.4.1 app/models/lcms/engine/document_bundle.rb
lcms-engine-0.4.0 app/models/lcms/engine/document_bundle.rb
lcms-engine-0.3.1 app/models/lcms/engine/document_bundle.rb
lcms-engine-0.3.0 app/models/lcms/engine/document_bundle.rb
lcms-engine-0.2.0 app/models/lcms/engine/document_bundle.rb