Skip to content

Commit

Permalink
Add minimum teaching days.
Browse files Browse the repository at this point in the history
  • Loading branch information
rhnonose committed Jun 19, 2017
1 parent cf2a52d commit ea76479
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule CoursePlanner.Repo.Migrations.AddMinimumTeachingDays do
use Ecto.Migration

def change do
alter table(:terms) do
add :minimum_teaching_days, :integer, null: false
end
end
end
1 change: 1 addition & 0 deletions test/controllers/offered_course_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule CoursePlanner.OfferedCourseControllerTest do
name: "Name",
start_date: "2017-01-01",
end_date: "2017-01-31",
minimum_teaching_days: 5,
status: "Active"
})
term
Expand Down
3 changes: 3 additions & 0 deletions test/controllers/term_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ defmodule CoursePlanner.TermControllerTest do
name: "Spring",
start_date: %{day: 01, month: 01, year: 2010},
end_date: %{day: 01, month: 06, year: 2010},
minimum_teaching_days: 5,
holidays:
%{
"0" => %{name: "Labor Day 1", date: %{day: 01, month: 05, year: 2010}},
Expand Down Expand Up @@ -84,6 +85,7 @@ defmodule CoursePlanner.TermControllerTest do
name: "Spring",
start_date: %{day: 01, month: 01, year: 2010},
end_date: %{day: 01, month: 06, year: 2010},
minimum_teaching_days: 5,
holidays:
%{
"0" => %{name: "Labor Day 1", date: %{day: 01, month: 5, year: 2008}}
Expand All @@ -99,6 +101,7 @@ defmodule CoursePlanner.TermControllerTest do
name: "Spring",
start_date: %{day: 01, month: 01, year: 2010},
end_date: %{day: 01, month: 06, year: 2010},
minimum_teaching_days: 5,
holidays:
%{
"0" => %{name: "Labor Day 1", date: %{day: 02, month: 01, year: 2011}}
Expand Down
1 change: 1 addition & 0 deletions test/lib/course_planner/offered_courses_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule CoursePlanner.OfferedCoursesTest do
name: "Fall",
start_date: "2017-01-01",
end_date: "2017-06-01",
minimum_teaching_days: 5,
status: "Active"
})
end
Expand Down
1 change: 1 addition & 0 deletions test/lib/course_planner/teachers_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule CoursePlanner.TeachersTest do
name: name,
start_date: start_date,
end_date: end_date,
minimum_teaching_days: 5,
courses: [course]
})
end
Expand Down
3 changes: 2 additions & 1 deletion test/models/offered_course_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ defmodule CoursePlanner.OfferedCourseTest do
%Term{
name: "Fall",
start_date: %Ecto.Date{day: 1, month: 1, year: 2017},
end_date: %Ecto.Date{day: 1, month: 6, year: 2017}
end_date: %Ecto.Date{day: 1, month: 6, year: 2017},
minimum_teaching_days: 5
})
end

Expand Down
1 change: 1 addition & 0 deletions test/models/students_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule CoursePlanner.StudentsTest do
name: name,
start_date: start_date,
end_date: end_date,
minimum_teaching_days: 5,
courses: [course]
})
end
Expand Down
37 changes: 34 additions & 3 deletions test/models/term_test.exs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
defmodule CoursePlanner.TermTest do
use CoursePlanner.ModelCase

alias CoursePlanner.Terms.Term
alias CoursePlanner.{Terms.Term, Terms}

test "changeset with valid attributes" do
valid_attrs =
%{
name: "Spring",
start_date: %{day: 17, month: 4, year: 2010},
end_date: %{day: 17, month: 5, year: 2010}
end_date: %{day: 17, month: 5, year: 2010},
minimum_teaching_days: 5
}
changeset = Term.changeset(%Term{}, valid_attrs)
assert changeset.valid?
Expand Down Expand Up @@ -36,9 +37,39 @@ defmodule CoursePlanner.TermTest do
%{
name: "Spring",
start_date: %{day: 17, month: 4, year: 2010},
end_date: %{day: 17, month: 3, year: 2010}
end_date: %{day: 17, month: 3, year: 2010},
minimum_teaching_days: 5,
}
changeset = Term.changeset(%Term{}, invalid_attrs)
refute changeset.valid?
end

test "term has minimum teaching days" do
{:ok, term} = Terms.create(%{
"name" => "Spring",
"start_date" => %{day: 17, month: 3, year: 2017},
"end_date" => %{day: 22, month: 3, year: 2017},
"minimum_teaching_days" => 3,
"holidays" => %{
"0" => %{name: "Labor Day 1", date: %{day: 18, month: 3, year: 2017}},
"1" => %{name: "Labor Day 2", date: %{day: 19, month: 3, year: 2017}}
}
})
assert length(term.holidays) == 2
end

test "term doesn't have minimum teaching days" do
{:error, changeset} = Terms.create(%{
"name" => "Spring",
"start_date" => %{day: 17, month: 3, year: 2017},
"end_date" => %{day: 20, month: 3, year: 2017},
"minimum_teaching_days" => 3,
"holidays" => %{
"0" => %{name: "Labor Day 1", date: %{day: 18, month: 3, year: 2017}},
"1" => %{name: "Labor Day 2", date: %{day: 19, month: 3, year: 2017}}
}
})
refute changeset.valid?
assert changeset.errors[:minimum_teaching_days] == {"There's not enough minimum teaching days.", []}
end
end
3 changes: 2 additions & 1 deletion test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ alias CoursePlanner.{User, Course, OfferedCourse, Class, Attendance}
%Term{
name: sequence(:name, &"term-#{&1}"),
start_date: %Ecto.Date{day: 1, month: 1, year: 2017},
end_date: %Ecto.Date{day: 1, month: 6, year: 2017}
end_date: %Ecto.Date{day: 1, month: 6, year: 2017},
minimum_teaching_days: 5
}
end

Expand Down
4 changes: 3 additions & 1 deletion web/models/terms.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ defmodule CoursePlanner.Terms do
|> Map.values()
|> Enum.map(&Holiday.changeset(%Holiday{}, start_date, end_date, &1))

Changeset.put_embed(changeset, :holidays, holidays)
changeset
|> Changeset.put_embed(:holidays, holidays)
|> Term.validate_minimum_teaching_days(holidays)
end

def delete(id) do
Expand Down
18 changes: 16 additions & 2 deletions web/models/terms/term.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule CoursePlanner.Terms.Term do
field :name, :string
field :start_date, :date
field :end_date, :date
field :minimum_teaching_days, :integer
embeds_many :holidays, Holiday, on_replace: :delete

has_many :offered_courses, OfferedCourse, on_replace: :delete
Expand All @@ -21,11 +22,24 @@ defmodule CoursePlanner.Terms.Term do

def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:name, :start_date, :end_date])
|> validate_required([:name, :start_date, :end_date])
|> cast(params, [:name, :start_date, :end_date, :minimum_teaching_days])
|> validate_required([:name, :start_date, :end_date, :minimum_teaching_days])
|> validate_date_range()
end

def validate_minimum_teaching_days(%{valid?: true} = changeset, holidays) do
st = Changeset.get_field(changeset, :start_date)
en = Changeset.get_field(changeset, :end_date)
min = Changeset.get_field(changeset, :minimum_teaching_days)
number_of_holidays = length(holidays)
if Timex.diff(en, st, :days) + 1 - number_of_holidays > min do
changeset
else
Changeset.add_error(changeset, :minimum_teaching_days, "There's not enough minimum teaching days.")
end
end
def validate_minimum_teaching_days(changeset, _holidays), do: changeset

defp validate_date_range(%{valid?: true} = changeset) do
st = changeset |> Changeset.get_field(:start_date) |> Date.cast!
en = changeset |> Changeset.get_field(:end_date) |> Date.cast!
Expand Down

0 comments on commit ea76479

Please sign in to comment.