Current section

Files

Jump to
ami lib ami resource.ex
Raw

lib/ami/resource.ex

defmodule Ami.Resource do
@moduledoc """
Resource module is shared between the following modules:
[ "Lesson",
"Enrollment",
"Course",
"Post",
"Tool",
"Comment",
"ReportPage",
"CustomPage"
]
"""
alias Ami.{
Repo
}
import Ecto.Query
use Ecto.Schema
schema "resources" do
field(:title, :string)
field(:description, :string)
field(:file, :string)
field(:resourceable_id, :integer)
field(:resourceable_type, :string)
field(:created_at, :utc_datetime)
field(:updated_at, :utc_datetime)
field(:is_activity, :boolean)
field(:downloaded_resources_count, :integer)
end
def preload_resources(struct, resourceable_type) do
struct
|> Repo.preload(
resources:
from(r in __MODULE__, where: r.resourceable_type == ^resourceable_type, order_by: r.id)
)
end
def preload_resource(struct, resourceable_type) do
struct
|> Repo.preload(
[
resource:
from(r in __MODULE__, where: r.resourceable_type == ^resourceable_type, order_by: r.id)
],
force: true
)
end
def file_url({id, file}) do
case file do
nil -> ""
_ -> "https://ami-aws-s3.s3.amazonaws.com/uploads/resource/file/#{id}/#{file}"
end
end
def activity_url(resources) do
resource =
Enum.find(resources, fn resource ->
extension = String.split(resource.file, ".") |> Enum.at(1)
Regex.match?(~r/Activity/, resource.title) && Regex.match?(~r/pdf/i, extension)
end)
file_url({resource.id, resource.file})
end
end