defmodule Ami.Unit do use Ecto.Schema alias Ami.{ Course, Lesson, UnitLesson } schema "units" do field(:title, :string) field(:description, :string) field(:grade_value, :integer) field(:position, :integer) field(:created_at, :utc_datetime) field(:updated_at, :utc_datetime) belongs_to(:course, Course) # has_many(:lessons, Lesson) has_many(:unit_lessons, UnitLesson, on_delete: :delete_all) has_many(:lessons, through: [:unit_lessons, :lesson]) end def completed?(unit, enrollment) do Enum.all?(unit.lessons, &(Lesson.completed?(&1, enrollment) == true)) end end