Skip to content

Commit

Permalink
feat: add list of shuttles to form, update association
Browse files Browse the repository at this point in the history
  • Loading branch information
meagharty committed Oct 24, 2024
1 parent a6ea6bf commit d2244c8
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/arrow/shuttles/route.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ defmodule Arrow.Shuttles.Route do
@doc false
def changeset(route, attrs) do
route
|> cast(attrs, [:direction_id, :direction_desc, :destination, :waypoint, :suffix])
|> cast_assoc(:shape)
|> cast(attrs, [:direction_id, :direction_desc, :destination, :waypoint, :suffix, :shape_id])
|> foreign_key_constraint(:shape_id)
|> validate_required([:direction_id, :direction_desc, :destination])
end
end
2 changes: 1 addition & 1 deletion lib/arrow/shuttles/shuttle.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule Arrow.Shuttles.Shuttle do
def changeset(shuttle, attrs) do
shuttle
|> cast(attrs, [:shuttle_name, :disrupted_route_id, :status])
|> cast_assoc(:routes)
|> cast_assoc(:routes, with: &Arrow.Shuttles.Route.changeset/2)
|> validate_required([:shuttle_name, :status])
|> validate_required_for(:status)
|> foreign_key_constraint(:disrupted_route_id)
Expand Down
24 changes: 20 additions & 4 deletions lib/arrow_web/controllers/shuttle_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ defmodule ArrowWeb.ShuttleController do
})

gtfs_disruptable_routes = Shuttles.list_disruptable_routes()
render(conn, :new, changeset: changeset, gtfs_disruptable_routes: gtfs_disruptable_routes)
shapes = Shuttles.list_shapes()

render(conn, :new,
changeset: changeset,
gtfs_disruptable_routes: gtfs_disruptable_routes,
shapes: shapes
)
end

def create(conn, %{"shuttle" => shuttle_params}) do
Expand All @@ -28,7 +34,13 @@ defmodule ArrowWeb.ShuttleController do

{:error, %Ecto.Changeset{} = changeset} ->
gtfs_disruptable_routes = Shuttles.list_disruptable_routes()
render(conn, :new, changeset: changeset, gtfs_disruptable_routes: gtfs_disruptable_routes)
shapes = Shuttles.list_shapes()

render(conn, :new,
changeset: changeset,
gtfs_disruptable_routes: gtfs_disruptable_routes,
shapes: shapes
)
end
end

Expand All @@ -41,11 +53,13 @@ defmodule ArrowWeb.ShuttleController do
shuttle = Shuttles.get_shuttle!(id)
changeset = Shuttles.change_shuttle(shuttle)
gtfs_disruptable_routes = Shuttles.list_disruptable_routes()
shapes = Shuttles.list_shapes()

render(conn, :edit,
shuttle: shuttle,
changeset: changeset,
gtfs_disruptable_routes: gtfs_disruptable_routes
gtfs_disruptable_routes: gtfs_disruptable_routes,
shapes: shapes
)
end

Expand All @@ -60,11 +74,13 @@ defmodule ArrowWeb.ShuttleController do

{:error, %Ecto.Changeset{} = changeset} ->
gtfs_disruptable_routes = Shuttles.list_disruptable_routes()
shapes = Shuttles.list_shapes()

render(conn, :edit,
shuttle: shuttle,
changeset: changeset,
gtfs_disruptable_routes: gtfs_disruptable_routes
gtfs_disruptable_routes: gtfs_disruptable_routes,
shapes: shapes
)
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/arrow_web/controllers/shuttle_html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule ArrowWeb.ShuttleView do
attr :changeset, Ecto.Changeset, required: true
attr :action, :string, required: true
attr :gtfs_disruptable_routes, :list, required: true
attr :shapes, :list, required: true

def shuttle_form(assigns)
end
1 change: 1 addition & 0 deletions lib/arrow_web/controllers/shuttle_html/edit.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
changeset={@changeset}
action={~p"/shuttles/#{@shuttle}"}
gtfs_disruptable_routes={@gtfs_disruptable_routes}
shapes={@shapes}
/>

<.back navigate={~p"/shuttles"}>Back to shuttles</.back>
1 change: 1 addition & 0 deletions lib/arrow_web/controllers/shuttle_html/new.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
changeset={@changeset}
action={~p"/shuttles"}
gtfs_disruptable_routes={@gtfs_disruptable_routes}
shapes={@shapes}
/>

<.back navigate={~p"/shuttles"}>Back to shuttles</.back>
2 changes: 1 addition & 1 deletion lib/arrow_web/controllers/shuttle_html/show.html.heex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<.header>
Shuttle <%= @shuttle.id %>
shuttle <%= @shuttle.id %>
<:actions>
<.link href={~p"/shuttles/#{@shuttle}/edit"}>
<.button>Edit shuttle</.button>
Expand Down
10 changes: 8 additions & 2 deletions lib/arrow_web/controllers/shuttle_html/shuttle_form.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
type="select"
label="Disrupted Route"
prompt="Choose a route"
options={Enum.map(@gtfs_disruptable_routes, fn route -> route.id end)}
options={Enum.map(@gtfs_disruptable_routes, &{&1.long_name, &1.id})}
/>
</div>
<div class="col">
Expand All @@ -46,7 +46,13 @@
<.input field={f_route[:direction_desc]} type="text" label="Direction desc" />
</div>
<div class="col offset-md-1">
<.input field={f_route[:shape_id]} type="text" label="Shape id" />
<.input
field={f_route[:shape_id]}
type="select"
label="Shape"
prompt="Choose a shape"
options={Enum.map(@shapes, &{&1.name, &1.id})}
/>
</div>
</div>
<div class="row">
Expand Down

0 comments on commit d2244c8

Please sign in to comment.