Packages
ami
0.1.4
0.3.9
0.3.8
retired
0.3.7
retired
0.3.6
retired
0.3.5
retired
0.3.4
retired
0.3.3
retired
0.3.2
retired
0.3.1
retired
0.3.0
retired
0.2.9
retired
0.2.8
retired
0.2.7
retired
0.2.6
retired
0.2.5
retired
0.2.3
retired
0.2.2
retired
0.2.1
retired
0.2.0
retired
0.1.9
retired
0.1.8
retired
0.1.7
retired
0.1.6
retired
0.1.5
retired
0.1.4
retired
Package containing all AMI Models shared between micro-services.
Retired package: Security issue - retiring
Current section
Files
Jump to
Current section
Files
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