diff --git a/lib/code_corps/github/sync/comment/comment/changeset.ex b/lib/code_corps/github/sync/comment/comment/changeset.ex index 272ff3a7d..f0d2cd735 100644 --- a/lib/code_corps/github/sync/comment/comment/changeset.ex +++ b/lib/code_corps/github/sync/comment/comment/changeset.ex @@ -76,7 +76,7 @@ defmodule CodeCorps.GitHub.Sync.Comment.Comment.Changeset do |> Changeset.cast(CommentAdapter.to_comment(attrs), @update_attrs) |> MarkdownRendererService.render_markdown_to_html(:markdown, :body) |> Changeset.put_change(:modified_from, "github") - |> TimeValidator.validate_time_after(:modified_at) + |> TimeValidator.validate_time_not_before(:modified_at) |> Changeset.validate_required([:markdown, :body]) end end diff --git a/lib/code_corps/github/sync/issue/task/changeset.ex b/lib/code_corps/github/sync/issue/task/changeset.ex index 4785ad9a6..2b8eb32ee 100644 --- a/lib/code_corps/github/sync/issue/task/changeset.ex +++ b/lib/code_corps/github/sync/issue/task/changeset.ex @@ -89,7 +89,7 @@ defmodule CodeCorps.GitHub.Sync.Issue.Task.Changeset do |> Changeset.cast(IssueAdapter.to_task(issue_attrs), @update_attrs) |> MarkdownRendererService.render_markdown_to_html(:markdown, :body) |> Changeset.put_change(:modified_from, "github") - |> TimeValidator.validate_time_after(:modified_at) + |> TimeValidator.validate_time_not_before(:modified_at) |> Changeset.validate_required([:project_id, :title, :user_id]) |> Changeset.assoc_constraint(:github_repo) |> Changeset.assoc_constraint(:project) diff --git a/lib/code_corps/validators/time_validator.ex b/lib/code_corps/validators/time_validator.ex index 71f758ea6..53ef4a8c5 100644 --- a/lib/code_corps/validators/time_validator.ex +++ b/lib/code_corps/validators/time_validator.ex @@ -6,24 +6,21 @@ defmodule CodeCorps.Validators.TimeValidator do alias Ecto.Changeset @doc """ - Validates a time after a given time. + Validates the new time is not before the previous time. """ - def validate_time_after(%{data: data} = changeset, field) do + def validate_time_not_before(%{data: data} = changeset, field) do previous_time = Map.get(data, field) current_time = Changeset.get_change(changeset, field) case current_time do nil -> changeset - _ -> do_validate_time_after(changeset, field, previous_time, current_time) + _ -> do_validate_time_not_before(changeset, field, previous_time, current_time) end end - defp do_validate_time_after(changeset, field, previous_time, current_time) do - is_after = current_time |> Timex.after?(previous_time) - is_equal = current_time |> Timex.equal?(previous_time) - after_or_equal = is_after || is_equal - case after_or_equal do - true -> changeset - false -> Changeset.add_error(changeset, field, "cannot be before the last recorded time") + defp do_validate_time_not_before(changeset, field, previous_time, current_time) do + case Timex.before?(current_time, previous_time) do + true -> Changeset.add_error(changeset, field, "cannot be before the last recorded time") + false -> changeset end end end diff --git a/test/lib/code_corps/validators/time_validator_test.exs b/test/lib/code_corps/validators/time_validator_test.exs index 088569f1b..3a4ec75f4 100644 --- a/test/lib/code_corps/validators/time_validator_test.exs +++ b/test/lib/code_corps/validators/time_validator_test.exs @@ -5,20 +5,27 @@ defmodule CodeCorps.Validators.TimeValidatorTest do @previous_time DateTime.utc_now - describe "validate_time_after/2" do + describe "validate_time_not_before/2" do test "when the time happened before" do # set the time to 1 day before the previous (recorded) time current_time = @previous_time |> Timex.shift(days: -1) changeset = cast_times(@previous_time, current_time, :modified_at) - changeset = changeset |> validate_time_after(:modified_at) + changeset = changeset |> validate_time_not_before(:modified_at) refute changeset.valid? end + test "when the time happened at the same time" do + current_time = @previous_time + changeset = cast_times(@previous_time, current_time, :modified_at) + changeset = changeset |> validate_time_not_before(:modified_at) + assert changeset.valid? + end + test "when the time happened after" do # set the time to 1 day after the previous (recorded) time current_time = @previous_time |> Timex.shift(days: 1) changeset = cast_times(@previous_time, current_time, :modified_at) - changeset = changeset |> validate_time_after(:modified_at) + changeset = changeset |> validate_time_not_before(:modified_at) assert changeset.valid? end end