-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(TripPlanner.Form): use Ecto.Schema to create a form (#2159)
- Loading branch information
1 parent
2ce5e4c
commit 775f1fe
Showing
4 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
defmodule TripPlanner.InputForm do | ||
@moduledoc """ | ||
Describes the inputs users can fill to request trip plans. | ||
At minimum, this requires two locations. | ||
""" | ||
|
||
use TypedEctoSchema | ||
import Ecto.Changeset | ||
|
||
@error_messages %{ | ||
from: "Please specify an origin location.", | ||
to: "Please add a destination.", | ||
from_to_same: "Please select a destination at a different location from the origin." | ||
} | ||
|
||
@primary_key false | ||
typed_embedded_schema do | ||
embeds_one(:from, __MODULE__.Location) | ||
embeds_one(:to, __MODULE__.Location) | ||
end | ||
|
||
def changeset(params \\ %{}) do | ||
changeset(%__MODULE__{}, params) | ||
end | ||
|
||
def changeset(form, params) do | ||
form | ||
|> cast(params, []) | ||
|> cast_embed(:from, required: true) | ||
|> cast_embed(:to, required: true) | ||
end | ||
|
||
def validate_params(params) do | ||
changes = | ||
params | ||
|> changeset() | ||
|> update_change(:from, &update_location_change/1) | ||
|> update_change(:to, &update_location_change/1) | ||
|> validate_required(:from, message: error_message(:from)) | ||
|> validate_required(:to, message: error_message(:to)) | ||
|> validate_same_locations() | ||
|
||
if changes.errors == [] do | ||
changes | ||
else | ||
%Ecto.Changeset{changes | action: :update} | ||
end | ||
end | ||
|
||
# make the parent field blank if the location isn't valid | ||
defp update_location_change(%Ecto.Changeset{valid?: false, errors: [_ | _]}), do: nil | ||
defp update_location_change(changeset), do: changeset | ||
|
||
defp validate_same_locations(changeset) do | ||
with from_change when not is_nil(from_change) <- get_change(changeset, :from), | ||
to_change when to_change === from_change <- get_change(changeset, :to) do | ||
add_error( | ||
changeset, | ||
:to, | ||
error_message(:from_to_same) | ||
) | ||
else | ||
_ -> | ||
changeset | ||
end | ||
end | ||
|
||
def error_message(key), do: @error_messages[key] | ||
|
||
defmodule Location do | ||
@moduledoc """ | ||
Represents a location for requesting a trip plan. At minimum, coordinates | ||
are required. A stop_id is expected to be associated with an MBTA GTFS stop. | ||
If a name is not provided, one can be created based on the coordinates. | ||
""" | ||
|
||
use TypedEctoSchema | ||
|
||
@primary_key false | ||
typed_embedded_schema do | ||
field(:latitude, :float, null: false) | ||
field(:longitude, :float, null: false) | ||
field(:name, :string) | ||
field(:stop_id, :string) :: Stops.Stop.id_t() | ||
end | ||
|
||
def changeset(form \\ %__MODULE__{}, params \\ %{}) do | ||
form | ||
|> cast(params, [:latitude, :longitude, :name, :stop_id]) | ||
|> validate_required([:latitude, :longitude]) | ||
|> add_default_name() | ||
end | ||
|
||
defp add_default_name(changeset) do | ||
if is_nil(changeset.data.name) and | ||
(changed?(changeset, :latitude) or changed?(changeset, :longitude)) do | ||
{_, latitude} = fetch_field(changeset, :latitude) | ||
{_, longitude} = fetch_field(changeset, :longitude) | ||
|
||
put_change( | ||
changeset, | ||
:name, | ||
"#{latitude}, #{longitude}" | ||
) | ||
else | ||
changeset | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
defmodule TripPlanner.InputFormTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias TripPlanner.InputForm | ||
|
||
@from_params %{ | ||
"latitude" => "#{Faker.Address.latitude()}", | ||
"longitude" => "#{Faker.Address.longitude()}" | ||
} | ||
@to_params %{ | ||
"latitude" => "#{Faker.Address.latitude()}", | ||
"longitude" => "#{Faker.Address.longitude()}" | ||
} | ||
@params %{ | ||
"from" => @from_params, | ||
"to" => @to_params | ||
} | ||
|
||
test "from & to fields are required" do | ||
changeset = InputForm.changeset(%{}) | ||
assert {_, [validation: :required]} = changeset.errors[:from] | ||
assert {_, [validation: :required]} = changeset.errors[:to] | ||
end | ||
|
||
describe "validate_params/1" do | ||
test "validates to & from" do | ||
changeset = InputForm.validate_params(@params) | ||
assert changeset.valid? | ||
end | ||
|
||
test "adds from & to errors" do | ||
changeset = | ||
InputForm.validate_params(%{ | ||
"from" => %{ | ||
"latitude" => "", | ||
"longitude" => "", | ||
"name" => "", | ||
"stop_id" => "" | ||
}, | ||
"to" => %{ | ||
"latitude" => "", | ||
"longitude" => "", | ||
"name" => "", | ||
"stop_id" => "" | ||
} | ||
}) | ||
|
||
refute changeset.valid? | ||
assert changeset.errors[:to] | ||
assert changeset.errors[:from] | ||
end | ||
|
||
test "adds error if from & to are the same" do | ||
changeset = | ||
InputForm.validate_params(%{ | ||
"from" => @from_params, | ||
"to" => @from_params | ||
}) | ||
|
||
refute changeset.valid? | ||
|
||
expected_error = InputForm.error_message(:from_to_same) | ||
assert {^expected_error, _} = changeset.errors[:to] | ||
end | ||
end | ||
|
||
describe "Location" do | ||
test "longitude & latitude fields are required" do | ||
changeset = InputForm.Location.changeset() | ||
assert {_, [validation: :required]} = changeset.errors[:latitude] | ||
assert {_, [validation: :required]} = changeset.errors[:longitude] | ||
end | ||
|
||
test "adds a name if none provided" do | ||
lat = Faker.Address.latitude() | ||
lon = Faker.Address.longitude() | ||
|
||
changeset = | ||
InputForm.Location.changeset(%InputForm.Location{}, %{ | ||
"latitude" => "#{lat}", | ||
"longitude" => "#{lon}" | ||
}) | ||
|
||
assert changeset.changes.name == "#{lat}, #{lon}" | ||
end | ||
end | ||
end |