Sha256: 292ca448340497aa6f69f8a2c0fc34940705389c1f8ee12ed6dd8fbd62373c2e

Contents?: true

Size: 1.71 KB

Versions: 12

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

require 'pg_search'

module Lcms
  module Engine
    class Material < ApplicationRecord
      include PgSearch::Model
      include Partable

      validates :file_id, presence: true
      validates :identifier, uniqueness: true

      has_many :document_parts, as: :renderer, dependent: :delete_all
      has_and_belongs_to_many :documents, optional: true

      store_accessor :metadata

      pg_search_scope :search_identifier, against: :identifier, using: { tsearch: { prefix: true } }

      scope :gdoc, -> { where_metadata_not(type: 'pdf') }
      scope :pdf,  -> { where_metadata(type: 'pdf') }

      scope :where_metadata, ->(hash) { where('materials.metadata @> ?', hash.to_json) }
      scope :where_metadata_like, ->(key, val) { where('materials.metadata ->> ? ILIKE ?', key, "%#{val}%") }
      scope :where_metadata_not, ->(hash) { where.not('materials.metadata @> ?', hash.to_json) }

      def self.where_metadata_any_of(conditions)
        condition = Array.new(conditions.size, 'materials.metadata @> ?').join(' or ')
        where(condition, *conditions.map(&:to_json))
      end

      def file_url
        "https://docs.google.com/#{pdf? ? 'file' : 'document'}/d/#{file_id}"
      end

      # Material is optional if it's included in optional activity only
      def optional_for?(document)
        general_ids = document.document_parts.general.pluck(:materials).flatten
        optional_ids = document.document_parts.optional.pluck(:materials).flatten

        optional_ids.include?(id.to_s) && general_ids.exclude?(id.to_s)
      end

      def pdf?
        metadata['type'].to_s.casecmp('pdf').zero?
      end

      def source_type
        pdf? ? 'pdf' : 'gdoc'
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

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