# frozen_string_literal: true module MaterialRepr BASE = { only: [:id, :url, :version_no, :file_suppressed_at, :file_suppression_reason, :created_at, :updated_at], methods: [:content_type], include: { tag: TagRepr::BASE, created_by_user: UserRepr::BASE, updated_by_user: UserRepr::BASE } }.freeze module_function def base material, host: material.as_json(BASE).merge( file: if material.file.attached? && !material.file_suppressed? Rails.application.routes.url_helpers.rails_storage_proxy_url( material.file, host:) end, export_paths: export_paths(material), export_items: export_items(material)) end def many materials, host: materials.map { |m| base(m, host:) } end def export_paths material material.material_export_items.each_with_object({ }) do |item, hash| hash[item.profile] = item.enabled ? item.export_path : '' end end def export_items material material.material_export_items.map do |item| { id: item.id, profile: item.profile, export_path: item.export_path, enabled: item.enabled } end end end