-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(TripPlan.ItineraryGroups): improve handling for "arrive by" selection #2329
Merged
+364
−144
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3c61a33
feat(TripPlan.ItineraryGroups): take last N itineraries instead of first
thecristen 3a8d241
fixup(Live.TripPlanner): this is NOT random. @joshlarson fixed this i…
thecristen 25a7cfb
chore(ItineraryGroups): decrease max_per_group to 4
thecristen f445fd3
feat(TripPlan.ItineraryGroup): use struct to describe group
thecristen 020eb43
refactor(TripPlanner.Results): use %ItineraryGroup{}
thecristen cd41855
Merge remote-tracking branch 'origin/main' into cbj/arrive-by
thecristen 913d0b0
fix credo checks
thecristen d38ceec
feedback(ItineraryGroup): break out separate tests
thecristen cebad12
feedback(ItineraryGroups): use keyword arg
thecristen 301302d
feedback: adjust phrasing
thecristen acc447f
Update lib/dotcom/trip_plan/itinerary_group.ex
thecristen b3aed81
feedback: arrive at
thecristen 88e82d6
feedback: representative_time_key
thecristen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat(TripPlan.ItineraryGroup): use struct to describe group
commit f445fd3c8b591865b42e77da41fa732b9105fd17
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,74 @@ | ||
defmodule Dotcom.TripPlan.ItineraryGroup do | ||
@moduledoc """ | ||
A single group of related itineraries | ||
""" | ||
|
||
alias Dotcom.TripPlan.Itinerary | ||
|
||
defstruct [:itineraries, :representative_index, :representative_time, :summary] | ||
|
||
@type summarized_leg :: %{ | ||
routes: [Routes.Route.t()], | ||
walk_minutes: non_neg_integer() | ||
} | ||
|
||
@type summary :: %{ | ||
accessible?: boolean() | nil, | ||
duration: non_neg_integer(), | ||
start: DateTime.t(), | ||
stop: DateTime.t(), | ||
summarized_legs: [summarized_leg()], | ||
tag: String.t(), | ||
total_cost: non_neg_integer(), | ||
walk_distance: float() | ||
} | ||
|
||
@type t :: %__MODULE__{ | ||
itineraries: [Itinerary.t()], | ||
representative_index: non_neg_integer(), | ||
representative_time: :start | :stop, | ||
thecristen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
summary: summary() | ||
} | ||
|
||
@doc """ | ||
List of either start times or stop times for this group | ||
""" | ||
@spec options_text(t()) :: String.t() | nil | ||
def options_text(%__MODULE__{itineraries: []}), do: nil | ||
def options_text(%__MODULE__{itineraries: [_single]}), do: nil | ||
|
||
def options_text( | ||
%__MODULE__{ | ||
representative_index: representative_index, | ||
representative_time: start_or_stop | ||
} = | ||
group | ||
) do | ||
{_, other_times} = | ||
group | ||
|> all_times() | ||
|> List.pop_at(representative_index) | ||
thecristen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
phrase = options_phrase(start_or_stop == :stop, Enum.count(other_times)) | ||
|
||
formatted_times = | ||
other_times | ||
|> Enum.map(&Timex.format!(&1, "%-I:%M", :strftime)) | ||
|> Enum.join(", ") | ||
|
||
"Similar #{phrase} #{formatted_times}" | ||
end | ||
|
||
defp options_phrase(true, 1), do: "trip arrives by" | ||
defp options_phrase(true, _), do: "trips arrive by" | ||
defp options_phrase(false, 1), do: "trip departs at" | ||
defp options_phrase(_, _), do: "trips depart at" | ||
thecristen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@doc """ | ||
List of either start times or stop times for this group | ||
""" | ||
@spec all_times(t()) :: [DateTime.t()] | ||
def all_times(%__MODULE__{itineraries: itineraries, representative_time: start_or_stop}) do | ||
Enum.map(itineraries, &Map.get(&1, start_or_stop)) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
defmodule Dotcom.TripPlan.ItineraryGroupTest do | ||
@moduledoc false | ||
|
||
use ExUnit.Case, async: true | ||
|
||
import Mox | ||
|
||
alias Dotcom.TripPlan.ItineraryGroup | ||
alias Test.Support.Factories.{Stops.Stop, TripPlanner.TripPlanner} | ||
|
||
setup :verify_on_exit! | ||
|
||
setup do | ||
stub(Stops.Repo.Mock, :get, &Stop.build(:stop, id: &1)) | ||
:ok | ||
end | ||
|
||
describe "options_text/1" do | ||
test "does nothing if no alternate times" do | ||
refute ItineraryGroup.options_text(%ItineraryGroup{itineraries: []}) | ||
|
||
refute ItineraryGroup.options_text(%ItineraryGroup{ | ||
itineraries: [TripPlanner.build(:itinerary)] | ||
}) | ||
end | ||
|
||
test "describes alternate times for the group" do | ||
# SETUP | ||
itineraries = TripPlanner.build_list(Faker.Util.pick(2..4), :itinerary) | ||
representative_index = Faker.Util.pick(0..(length(itineraries) - 1)) | ||
start_or_stop = Faker.Util.pick([:start, :stop]) | ||
|
||
group = %ItineraryGroup{ | ||
itineraries: itineraries, | ||
representative_index: representative_index, | ||
representative_time: start_or_stop | ||
} | ||
|
||
# EXERCISE | ||
text = ItineraryGroup.options_text(group) | ||
|
||
# VERIFY | ||
if Enum.count(itineraries) == 2 do | ||
thecristen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# only one alternate trip | ||
assert text =~ "trip " | ||
|
||
if group.representative_time == :start do | ||
assert text =~ "departs at" | ||
else | ||
assert text =~ "arrives by" | ||
end | ||
else | ||
assert text =~ "trips " | ||
|
||
if group.representative_time == :start do | ||
assert text =~ "depart at" | ||
else | ||
assert text =~ "arrive by" | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe "all_times/1" do | ||
test "uses representative_time to map to chosen times" do | ||
# SETUP | ||
itineraries = TripPlanner.build_list(4, :itinerary) | ||
|
||
# EXERCISE | ||
start_times = | ||
%ItineraryGroup{ | ||
itineraries: itineraries, | ||
representative_time: :start | ||
} | ||
|> ItineraryGroup.all_times() | ||
|
||
stop_times = | ||
%ItineraryGroup{ | ||
itineraries: itineraries, | ||
representative_time: :stop | ||
} | ||
|> ItineraryGroup.all_times() | ||
|
||
# VERIFY | ||
assert start_times != stop_times | ||
thecristen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit (non-blocking): The names
start
andstop
don't convey that these are times. Andstop
more frequently refers to a location where a bus allows passengers to embark and disembark. I would advocate forstart_time
andend_time
.(Note: This is only non-blocking because I know that the names aren't being introduced in this PR, and were like this already, and that fixing them could lead to PR bloat.)