Skip to content

Commit

Permalink
Make shuttle route page use LiveView (#1033)
Browse files Browse the repository at this point in the history
* chore: migration to separate out foreign keys

* fix: get seeds.exs working with schema changes

* chore: various small schema updates

* refactor: migrate shuttle page to LiveView

* test: test for shuttle route LiveView

* refactor: make use of helper functions in Arrow.Shuttles for changes

* fixup! refactor: make use of helper functions in Arrow.Shuttles for changes

* refactor: rename erroneous references to shuttle_route

* refactor: remove unneeded on_replace: :delete

* fix: update shuttle_live to assign initial shuttle for validate call, update seeds for FK duplication (#1034)


---------

Co-authored-by: Eddie Maldonado <emaldonado@mbta.com>

---------

Co-authored-by: meagharty <149533950+meagharty@users.noreply.github.com>
  • Loading branch information
lemald and meagharty authored Nov 7, 2024
1 parent ddfa96c commit c48ea51
Show file tree
Hide file tree
Showing 15 changed files with 526 additions and 316 deletions.
3 changes: 2 additions & 1 deletion lib/arrow/shuttles/route.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Arrow.Shuttles.Route do
field :waypoint, :string
belongs_to :shuttle, Arrow.Shuttles.Shuttle
belongs_to :shape, Arrow.Shuttles.Shape
has_many :route_stop, Arrow.Shuttles.RouteStop, foreign_key: :shuttle_route_id

timestamps(type: :utc_datetime)
end
Expand All @@ -19,7 +20,7 @@ defmodule Arrow.Shuttles.Route do
def changeset(route, attrs) do
route
|> cast(attrs, [:direction_id, :direction_desc, :destination, :waypoint, :suffix, :shape_id])
|> foreign_key_constraint(:shape_id)
|> validate_required([:direction_id, :direction_desc, :destination])
|> assoc_constraint(:shape)
end
end
12 changes: 8 additions & 4 deletions lib/arrow/shuttles/route_stop.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ defmodule Arrow.Shuttles.RouteStop do

schema "shuttle_route_stops" do
field :direction_id, Ecto.Enum, values: [:"0", :"1"]
field :stop_id, :string
field :stop_sequence, :integer
field :time_to_next_stop, :decimal
belongs_to :route, Arrow.Shuttles.Route
belongs_to :shuttle_route, Arrow.Shuttles.Route
belongs_to :stop, Arrow.Shuttles.Stop
belongs_to :gtfs_stop, Arrow.Gtfs.Stop, type: :string

timestamps(type: :utc_datetime)
end

@doc false
def changeset(route_stop, attrs) do
route_stop
|> cast(attrs, [:direction_id, :stop_id, :stop_sequence, :time_to_next_stop])
|> validate_required([:direction_id, :stop_id, :stop_sequence, :time_to_next_stop])
|> cast(attrs, [:direction_id, :stop_id, :gtfs_stop_id, :stop_sequence, :time_to_next_stop])
|> validate_required([:direction_id, :stop_sequence, :time_to_next_stop])
|> assoc_constraint(:shuttle_route)
|> assoc_constraint(:stop)
|> assoc_constraint(:gtfs_stop)
end
end
1 change: 1 addition & 0 deletions lib/arrow/shuttles/shuttle.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Arrow.Shuttles.Shuttle do
field :status, Ecto.Enum, values: [:draft, :active, :inactive]
field :shuttle_name, :string
field :disrupted_route_id, :string

has_many :routes, Arrow.Shuttles.Route, preload_order: [asc: :direction_id]

timestamps(type: :utc_datetime)
Expand Down
73 changes: 0 additions & 73 deletions lib/arrow_web/controllers/shuttle_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,87 +2,14 @@ defmodule ArrowWeb.ShuttleController do
use ArrowWeb, :controller

alias Arrow.Shuttles
alias Arrow.Shuttles.Shuttle

def index(conn, _params) do
shuttles = Shuttles.list_shuttles()
render(conn, :index, shuttles: shuttles)
end

def new(conn, _params) do
changeset =
Shuttles.change_shuttle(%Shuttle{
status: :draft,
routes: [%Shuttles.Route{direction_id: :"0"}, %Shuttles.Route{direction_id: :"1"}]
})

gtfs_disruptable_routes = Shuttles.list_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
case Shuttles.create_shuttle(shuttle_params) do
{:ok, shuttle} ->
conn
|> put_flash(:info, "Shuttle created successfully.")
|> redirect(to: ~p"/shuttles/#{shuttle}")

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

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

def show(conn, %{"id" => id}) do
shuttle = Shuttles.get_shuttle!(id)
render(conn, :show, shuttle: shuttle)
end

def edit(conn, %{"id" => id}) 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,
shapes: shapes
)
end

def update(conn, %{"id" => id, "shuttle" => shuttle_params}) do
shuttle = Shuttles.get_shuttle!(id)

case Shuttles.update_shuttle(shuttle, shuttle_params) do
{:ok, shuttle} ->
conn
|> put_flash(:info, "Shuttle updated successfully.")
|> redirect(to: ~p"/shuttles/#{shuttle}")

{: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,
shapes: shapes
)
end
end
end
10 changes: 0 additions & 10 deletions lib/arrow_web/controllers/shuttle_html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,4 @@ defmodule ArrowWeb.ShuttleView do
use ArrowWeb, :html

embed_templates "shuttle_html/*"

@doc """
Renders a shuttle form.
"""
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
12 changes: 0 additions & 12 deletions lib/arrow_web/controllers/shuttle_html/edit.html.heex

This file was deleted.

80 changes: 0 additions & 80 deletions lib/arrow_web/controllers/shuttle_html/shuttle_form.html.heex

This file was deleted.

Loading

0 comments on commit c48ea51

Please sign in to comment.