defmodule Ami.Tool do use Ecto.Schema # import Ecto.{ # Query # } alias Ami.{ User, Community, Resource, Like, Share, DownloadedTool, # Repo, SectionTool, Video } schema "tools" do field(:title, :string) field(:description, :string) field(:available_to_other_community, :boolean) field(:downloaded_tools_count, :integer) field(:embedly_data, :string) field(:url, :string) field(:template, :integer) field(:views_count, :integer) field(:created_at, :utc_datetime) field(:updated_at, :utc_datetime) belongs_to(:community, Community) belongs_to(:author, User, foreign_key: :user_id) has_one(:resource, Resource, foreign_key: :resourceable_id) has_one(:video, Video, foreign_key: :videoable_id, on_delete: :delete_all) has_many(:likes, Like, foreign_key: :likeable_id) has_many(:shares, Share, foreign_key: :shareable_id) has_many(:downloaded_tools, DownloadedTool, on_delete: :delete_all) has_many(:section_tools, SectionTool) has_many(:section_tools_sections, through: [:section_tools, :section]) end def preload_resource(tool), do: Resource.preload_resource(tool, "Tool") def preload_likes(tool), do: Like.preload_likes(tool, "Tool") def preload_video(tool), do: Video.preload_video(tool, "Tool") def icon_url_for(%Ami.Tool{} = tool) do case tool.template do 0 -> "https://s3-us-west-2.amazonaws.com/ami-aws-s3/uploads/my_resources/word-icon.png" 1 -> "https://s3-us-west-2.amazonaws.com/ami-aws-s3/uploads/my_resources/ppt-icon.png" 2 -> "https://s3-us-west-2.amazonaws.com/ami-aws-s3/uploads/my_resources/pdf-icon.png" 3 -> "https://s3-us-west-2.amazonaws.com/ami-aws-s3/uploads/my_resources/excel-icon.png" 4 -> "https://s3-us-west-2.amazonaws.com/ami-aws-s3/uploads/my_resources/mp3-icon.png" 5 -> tool = Ami.Tool.preload_resource(tool) Ami.Resource.file_url({tool.resource.id, tool.resource.file}) 6 -> "https://s3-us-west-2.amazonaws.com/ami-aws-s3/uploads/my_resources/video-icon.png" 7 -> "https://s3-us-west-2.amazonaws.com/ami-aws-s3/uploads/my_resources/link-icon.png" 8 -> "https://s3-us-west-2.amazonaws.com/ami-aws-s3/uploads/my_resources/word-icon.png" end end def type_of(%Ami.Tool{} = tool) do case tool.template do 0 -> "Document: .docx" 1 -> "Document: .ppt" 2 -> "Document: .pdf" 3 -> "Document: .xlsx" 4 -> "Document: .mp3" 5 -> tool = Ami.Tool.preload_resource(tool) Ami.Resource.file_url({tool.resource.id, tool.resource.file}) 7 -> "Link" 8 -> "Document: .docx" 6 -> "Video" end end def parsed_embedly_datas(tool) do list = cond do length(String.split(tool.embedly_data, "\n:")) > 1 -> String.split(tool.embedly_data, "\n:") length(String.split(tool.embedly_data, "\n")) > 1 -> String.split(tool.embedly_data, "\n") end List.delete_at(list, 0) |> Enum.reject(fn e -> Regex.match?(~r{^\s+}, e) end) |> Enum.flat_map(fn e -> String.split(e, ~r{:}, parts: 2) end) |> Enum.chunk(2) |> Enum.map(fn [a, b] -> {String.to_atom(a), b} end) |> Map.new() end end