admin forums

This commit is contained in:
byte[] 2019-12-15 12:54:27 -05:00
parent b14dbf46b5
commit 2a6b596765
8 changed files with 115 additions and 1 deletions

View file

@ -28,5 +28,7 @@ defmodule Philomena.Forums.Forum do
|> cast(attrs, [:name, :short_name, :description, :access_level])
|> validate_required([:name, :short_name, :description, :access_level])
|> validate_inclusion(:access_level, ~W(normal assistant staff))
|> validate_format(:short_name, ~r/\A[a-z]+\z/, message: "must consist only of lowercase letters")
|> unique_constraint(:short_name, name: :index_forums_on_short_name)
end
end

View file

@ -0,0 +1,54 @@
defmodule PhilomenaWeb.Admin.ForumController do
use PhilomenaWeb, :controller
alias Philomena.Forums.Forum
alias Philomena.Forums
plug :verify_authorized
plug :load_resource, model: Forum, id_field: "short_name"
def index(conn, _params) do
render(conn, "index.html")
end
def new(conn, _params) do
changeset = Forums.change_forum(%Forum{})
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"forum" => forum_params}) do
case Forums.create_forum(forum_params) do
{:ok, forum} ->
conn
|> put_flash(:info, "Forum created successfully.")
|> redirect(to: Routes.admin_forum_path(conn, :index))
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def edit(conn, _params) do
changeset = Forums.change_forum(conn.assigns.forum)
render(conn, "edit.html", changeset: changeset)
end
def update(conn, %{"forum" => forum_params}) do
case Forums.update_forum(conn.assigns.forum, forum_params) do
{:ok, forum} ->
conn
|> put_flash(:info, "Forum updated successfully.")
|> redirect(to: Routes.admin_forum_path(conn, :index))
{:error, changeset} ->
render(conn, "edit.html", changeset: changeset)
end
end
defp verify_authorized(conn, _opts) do
case Canada.Can.can?(conn.assigns.current_user, :edit, Forum) do
true -> conn
_false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
end
end
end

View file

@ -201,6 +201,8 @@ defmodule PhilomenaWeb.Router do
resources "/site_notices", SiteNoticeController, except: [:show]
resources "/adverts", AdvertController, except: [:show]
resources "/forums", ForumController, except: [:show, :delete]
end
resources "/duplicate_reports", DuplicateReportController, only: [] do

View file

@ -0,0 +1,26 @@
= form_for @changeset, @action, fn f ->
= if @changeset.action do
.alert.alert-danger
p Oops, something went wrong! Please check the errors below.
.field
=> label f, :name, "Name:"
= text_input f, :name, class: "input input--wide"
= error_tag f, :name
.field
=> label f, :short_name, "Slug:"
= text_input f, :short_name, class: "input input--wide"
= error_tag f, :short_name
.field
=> label f, :description, "Description:"
= textarea f, :description, class: "input input--wide"
= error_tag f, :description
.field
=> label f, :access_level, "Access level:"
= select f, :access_level, ["normal", "assistant", "staff"], class: "input"
= error_tag f, :access_level
= submit "Save Forum", class: "button"

View file

@ -0,0 +1,5 @@
h2 Edit Forum
= render "_form.html", Map.put(assigns, :action, Routes.admin_forum_path(@conn, :update, @forum))
= link "Back", to: Routes.admin_forum_path(@conn, :index)

View file

@ -0,0 +1,17 @@
h2 Listing Forums
table.table
thead
tr
th Name
th Options
tbody
= for forum <- @forums do
tr
td
= link forum.name, to: Routes.forum_path(@conn, :show, forum)
td class="text-right"
= link "Edit", to: Routes.admin_forum_path(@conn, :edit, forum)
= link "New Forum", to: Routes.admin_forum_path(@conn, :new)

View file

@ -0,0 +1,5 @@
h2 New Forum
= render "_form.html", Map.put(assigns, :action, Routes.admin_forum_path(@conn, :create))
= link "Back", to: Routes.admin_forum_path(@conn, :index)

View file

@ -0,0 +1,3 @@
defmodule PhilomenaWeb.Admin.ForumView do
use PhilomenaWeb, :view
end