-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: created
Rubric
schema in new Rubrics
context
- Loading branch information
Showing
12 changed files
with
618 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,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 |
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,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 |
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,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 |
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,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 |
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,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> |
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,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 |
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,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> |
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
Oops, something went wrong.