Skip to content
Permalink
Browse files

Listing Roles

  • Loading branch information...
dnsbty committed Aug 18, 2019
1 parent befb98d commit 6dcc37606b0a418302b1c78eaa5fd767f37a65d5
@@ -1,6 +1,6 @@
defmodule UserAdmin.Users do
alias UserAdmin.Repo
alias UserAdmin.Users.User
alias UserAdmin.Users.{Role, User}

@doc """
Create a changeset to change a user.
@@ -19,4 +19,12 @@ defmodule UserAdmin.Users do
|> User.changeset(attrs)
|> Repo.insert()
end

@doc """
List all the roles in the database.
"""
@spec list_roles :: list(Role.t())
def list_roles do
Repo.all(Role)
end
end
@@ -7,8 +7,9 @@ defmodule UserAdminWeb.UserLive.New do

def mount(_session, socket) do
changeset = Users.change_user(%User{})
roles = Users.list_roles()

{:ok, assign(socket, changeset: changeset)}
{:ok, assign(socket, changeset: changeset, roles: roles)}
end

def render(assigns) do
@@ -5,13 +5,9 @@
<%= text_input f, :name, id: "nameField" %>
<%= error_tag f, :name %>

<label for="roleField">Role</label>
<select id="roleField">
<option value="admin">Administrator</option>
<option value="mod">Moderator</option>
<option value="user">User</option>
<option value="guest">Guest</option>
</select>
<%= label f, :role_id, for: "roleField" %>
<%= select f, :role_id, Enum.map(@roles, &{&1.name, &1.id}), id: "roleField" %>
<%= error_tag f, :role_id %>

<div>
<%= submit "Create", phx_disable_with: "Creating..." %>
@@ -9,3 +9,12 @@
#
# We recommend using the bang functions (`insert!`, `update!`
# and so on) as they will fail if something goes wrong.

alias UserAdmin.Repo
alias UserAdmin.Users.Role

roles = ~w(Administrator Moderator Editor User Guest)

Enum.each(roles, fn name ->
Repo.insert!(%Role{name: name})
end)

0 comments on commit 6dcc376

Please sign in to comment.
You can’t perform that action at this time.