Skip to content

Commit

Permalink
chore: created Rubric schema in new Rubrics context
Browse files Browse the repository at this point in the history
  • Loading branch information
endoooo committed Oct 26, 2023
1 parent 38668d8 commit 5b37ed4
Show file tree
Hide file tree
Showing 12 changed files with 618 additions and 0 deletions.
104 changes: 104 additions & 0 deletions lib/lanttern/rubrics.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
defmodule Lanttern.Rubrics do
@moduledoc """
The Rubrics context.
"""

import Ecto.Query, warn: false
alias Lanttern.Repo

alias Lanttern.Rubrics.Rubric

@doc """
Returns the list of rubrics.
## Examples
iex> list_rubrics()
[%Rubric{}, ...]
"""
def list_rubrics do
Repo.all(Rubric)
end

@doc """
Gets a single rubric.
Raises `Ecto.NoResultsError` if the Rubric does not exist.
## Examples
iex> get_rubric!(123)
%Rubric{}
iex> get_rubric!(456)
** (Ecto.NoResultsError)
"""
def get_rubric!(id), do: Repo.get!(Rubric, id)

@doc """
Creates a rubric.
## Examples
iex> create_rubric(%{field: value})
{:ok, %Rubric{}}
iex> create_rubric(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_rubric(attrs \\ %{}) do
%Rubric{}
|> Rubric.changeset(attrs)
|> Repo.insert()
end

@doc """
Updates a rubric.
## Examples
iex> update_rubric(rubric, %{field: new_value})
{:ok, %Rubric{}}
iex> update_rubric(rubric, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_rubric(%Rubric{} = rubric, attrs) do
rubric
|> Rubric.changeset(attrs)
|> Repo.update()
end

@doc """
Deletes a rubric.
## Examples
iex> delete_rubric(rubric)
{:ok, %Rubric{}}
iex> delete_rubric(rubric)
{:error, %Ecto.Changeset{}}
"""
def delete_rubric(%Rubric{} = rubric) do
Repo.delete(rubric)
end

@doc """
Returns an `%Ecto.Changeset{}` for tracking rubric changes.
## Examples
iex> change_rubric(rubric)
%Ecto.Changeset{data: %Rubric{}}
"""
def change_rubric(%Rubric{} = rubric, attrs \\ %{}) do
Rubric.changeset(rubric, attrs)
end
end
20 changes: 20 additions & 0 deletions lib/lanttern/rubrics/rubric.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Lanttern.Rubrics.Rubric do
use Ecto.Schema
import Ecto.Changeset

schema "rubrics" do
field :criteria, :string
field :is_differentiation, :boolean, default: false

belongs_to :scale, Lanttern.Grading.Scale

timestamps()
end

@doc false
def changeset(rubric, attrs) do
rubric
|> cast(attrs, [:criteria, :is_differentiation, :scale_id])
|> validate_required([:criteria, :is_differentiation, :scale_id])
end
end
106 changes: 106 additions & 0 deletions lib/lanttern_web/live/rubric_live/form_component.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
defmodule LantternWeb.RubricLive.FormComponent do
use LantternWeb, :live_component

alias Lanttern.Rubrics
import LantternWeb.GradingHelpers

@impl true
def render(assigns) do
~H"""
<div>
<.header>
<%= @title %>
<:subtitle>Use this form to manage rubric records in your database.</:subtitle>
</.header>
<.simple_form
for={@form}
id="rubric-form"
phx-target={@myself}
phx-change="validate"
phx-submit="save"
>
<.input field={@form[:criteria]} type="text" label="Criteria" />
<.input
field={@form[:scale_id]}
type="select"
label="Scale"
options={@scale_options}
prompt="Select scale"
/>
<.input field={@form[:is_differentiation]} type="checkbox" label="Is differentiation" />
<:actions>
<.button phx-disable-with="Saving...">Save Rubric</.button>
</:actions>
</.simple_form>
</div>
"""
end

@impl true
def mount(socket) do
scale_options = generate_scale_options()

{:ok, assign(socket, :scale_options, scale_options)}
end

@impl true
def update(%{rubric: rubric} = assigns, socket) do
changeset = Rubrics.change_rubric(rubric)

{:ok,
socket
|> assign(assigns)
|> assign_form(changeset)}
end

@impl true
def handle_event("validate", %{"rubric" => rubric_params}, socket) do
changeset =
socket.assigns.rubric
|> Rubrics.change_rubric(rubric_params)
|> Map.put(:action, :validate)

{:noreply, assign_form(socket, changeset)}
end

def handle_event("save", %{"rubric" => rubric_params}, socket) do
save_rubric(socket, socket.assigns.action, rubric_params)
end

defp save_rubric(socket, :edit, rubric_params) do
case Rubrics.update_rubric(socket.assigns.rubric, rubric_params) do
{:ok, rubric} ->
notify_parent({:saved, rubric})

{:noreply,
socket
|> put_flash(:info, "Rubric updated successfully")
|> push_patch(to: socket.assigns.patch)}

{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign_form(socket, changeset)}
end
end

defp save_rubric(socket, :new, rubric_params) do
case Rubrics.create_rubric(rubric_params) do
{:ok, rubric} ->
notify_parent({:saved, rubric})

{:noreply,
socket
|> put_flash(:info, "Rubric created successfully")
|> push_patch(to: socket.assigns.patch)}

{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign_form(socket, changeset)}
end
end

defp assign_form(socket, %Ecto.Changeset{} = changeset) do
assign(socket, :form, to_form(changeset))
end

defp notify_parent(msg), do: send(self(), {__MODULE__, msg})
end
47 changes: 47 additions & 0 deletions lib/lanttern_web/live/rubric_live/index.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
defmodule LantternWeb.RubricLive.Index do
use LantternWeb, :live_view

alias Lanttern.Rubrics
alias Lanttern.Rubrics.Rubric

@impl true
def mount(_params, _session, socket) do
{:ok, stream(socket, :rubrics, Rubrics.list_rubrics())}
end

@impl true
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end

defp apply_action(socket, :edit, %{"id" => id}) do
socket
|> assign(:page_title, "Edit Rubric")
|> assign(:rubric, Rubrics.get_rubric!(id))
end

defp apply_action(socket, :new, _params) do
socket
|> assign(:page_title, "New Rubric")
|> assign(:rubric, %Rubric{})
end

defp apply_action(socket, :index, _params) do
socket
|> assign(:page_title, "Listing Rubrics")
|> assign(:rubric, nil)
end

@impl true
def handle_info({LantternWeb.RubricLive.FormComponent, {:saved, rubric}}, socket) do
{:noreply, stream_insert(socket, :rubrics, rubric)}
end

@impl true
def handle_event("delete", %{"id" => id}, socket) do
rubric = Rubrics.get_rubric!(id)
{:ok, _} = Rubrics.delete_rubric(rubric)

{:noreply, stream_delete(socket, :rubrics, rubric)}
end
end
47 changes: 47 additions & 0 deletions lib/lanttern_web/live/rubric_live/index.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<.header>
Listing Rubrics
<:actions>
<.link patch={~p"/admin/rubrics/new"}>
<.button>New Rubric</.button>
</.link>
</:actions>
</.header>

<.table
id="rubrics"
rows={@streams.rubrics}
row_click={fn {_id, rubric} -> JS.navigate(~p"/admin/rubrics/#{rubric}") end}
>
<:col :let={{_id, rubric}} label="Criteria"><%= rubric.criteria %></:col>
<:col :let={{_id, rubric}} label="Is differentiation"><%= rubric.is_differentiation %></:col>
<:action :let={{_id, rubric}}>
<div class="sr-only">
<.link navigate={~p"/admin/rubrics/#{rubric}"}>Show</.link>
</div>
<.link patch={~p"/admin/rubrics/#{rubric}/edit"}>Edit</.link>
</:action>
<:action :let={{id, rubric}}>
<.link
phx-click={JS.push("delete", value: %{id: rubric.id}) |> hide("##{id}")}
data-confirm="Are you sure?"
>
Delete
</.link>
</:action>
</.table>

<.modal
:if={@live_action in [:new, :edit]}
id="rubric-modal"
show
on_cancel={JS.patch(~p"/admin/rubrics")}
>
<.live_component
module={LantternWeb.RubricLive.FormComponent}
id={@rubric.id || :new}
title={@page_title}
action={@live_action}
rubric={@rubric}
patch={~p"/admin/rubrics"}
/>
</.modal>
21 changes: 21 additions & 0 deletions lib/lanttern_web/live/rubric_live/show.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule LantternWeb.RubricLive.Show do
use LantternWeb, :live_view

alias Lanttern.Rubrics

@impl true
def mount(_params, _session, socket) do
{:ok, socket}
end

@impl true
def handle_params(%{"id" => id}, _, socket) do
{:noreply,
socket
|> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:rubric, Rubrics.get_rubric!(id))}
end

defp page_title(:show), do: "Show Rubric"
defp page_title(:edit), do: "Edit Rubric"
end
32 changes: 32 additions & 0 deletions lib/lanttern_web/live/rubric_live/show.html.heex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<.header>
Rubric <%= @rubric.id %>
<:subtitle>This is a rubric record from your database.</:subtitle>
<:actions>
<.link patch={~p"/admin/rubrics/#{@rubric}/show/edit"} phx-click={JS.push_focus()}>
<.button>Edit rubric</.button>
</.link>
</:actions>
</.header>

<.list>
<:item title="Criteria"><%= @rubric.criteria %></:item>
<:item title="Is differentiation"><%= @rubric.is_differentiation %></:item>
</.list>

<.back navigate={~p"/admin/rubrics"}>Back to rubrics</.back>

<.modal
:if={@live_action == :edit}
id="rubric-modal"
show
on_cancel={JS.patch(~p"/admin/rubrics/#{@rubric}")}
>
<.live_component
module={LantternWeb.RubricLive.FormComponent}
id={@rubric.id}
title={@page_title}
action={@live_action}
rubric={@rubric}
patch={~p"/admin/rubrics/#{@rubric}"}
/>
</.modal>
8 changes: 8 additions & 0 deletions lib/lanttern_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ defmodule LantternWeb.Router do
live "/assessment_points_filter_views/:id/show/edit",
AssessmentPointsFilterViewLive.Show,
:edit

# Rubrics context
live "/rubrics", RubricLive.Index, :index
live "/rubrics/new", RubricLive.Index, :new
live "/rubrics/:id/edit", RubricLive.Index, :edit

live "/rubrics/:id", RubricLive.Show, :show
live "/rubrics/:id/show/edit", RubricLive.Show, :edit
end

# Other scopes may use custom stacks.
Expand Down
Loading

0 comments on commit 5b37ed4

Please sign in to comment.