Browse Source

add settings

main
Inhji Y. 10 months ago
parent
commit
432fe2f033
  1. 120
      lib/mirage/settings.ex
  2. 19
      lib/mirage/settings/setting.ex
  3. 39
      lib/mirage_web/controllers/setting_controller.ex
  4. 4
      lib/mirage_web/live/page_live.ex
  5. 2
      lib/mirage_web/live/page_live.html.leex
  6. 1
      lib/mirage_web/router.ex
  7. 12
      lib/mirage_web/templates/layout/_user_menu.html.eex
  8. 5
      lib/mirage_web/templates/setting/edit.html.eex
  9. 19
      lib/mirage_web/templates/setting/form.html.eex
  10. 25
      lib/mirage_web/templates/setting/index.html.eex
  11. 5
      lib/mirage_web/templates/setting/new.html.eex
  12. 18
      lib/mirage_web/templates/setting/show.html.eex
  13. 3
      lib/mirage_web/views/setting_view.ex
  14. 19
      priv/repo/migrations/20210109130905_create_settings.exs
  15. 14
      priv/repo/migrations/20210111174337_add_settings_data.exs
  16. 50
      test/mirage/settings_test.exs
  17. 54
      test/mirage_web/controllers/setting_controller_test.exs

120
lib/mirage/settings.ex

@ -0,0 +1,120 @@
defmodule Mirage.Settings do
@moduledoc """
The Settings context.
"""
import Ecto.Query, warn: false
alias Mirage.Repo
alias Mirage.Settings.Setting
@doc """
Returns the list of settings.
## Examples
iex> list_settings()
[%Setting{}, ...]
"""
def list_settings do
Repo.all(Setting)
end
@doc """
Gets a single setting.
Raises `Ecto.NoResultsError` if the Setting does not exist.
## Examples
iex> get_setting!(123)
%Setting{}
iex> get_setting!(456)
** (Ecto.NoResultsError)
"""
def get_setting!(id), do: Repo.get!(Setting, id)
@doc """
Gets a single setting by name.
Raises `Ecto.NoResultsError` if the Setting does not exist.
## Examples
iex> get_setting!("somename)
%Setting{}
iex> get_setting!("someothername")
** (Ecto.NoResultsError)
"""
def get_setting_by_name!(name), do: Repo.get_by!(Setting, name: name)
@doc """
Creates a setting.
## Examples
iex> create_setting(%{field: value})
{:ok, %Setting{}}
iex> create_setting(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_setting(attrs \\ %{}) do
%Setting{}
|> Setting.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a setting.
## Examples
iex> update_setting(setting, %{field: new_value})
{:ok, %Setting{}}
iex> update_setting(setting, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_setting(%Setting{} = setting, attrs) do
setting
|> Setting.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a setting.
## Examples
iex> delete_setting(setting)
{:ok, %Setting{}}
iex> delete_setting(setting)
{:error, %Ecto.Changeset{}}
"""
def delete_setting(%Setting{} = setting) do
Repo.delete(setting)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking setting changes.
## Examples
iex> change_setting(setting)
%Ecto.Changeset{data: %Setting{}}
"""
def change_setting(%Setting{} = setting, attrs \\ %{}) do
Setting.changeset(setting, attrs)
end
end

19
lib/mirage/settings/setting.ex

@ -0,0 +1,19 @@
defmodule Mirage.Settings.Setting do
use Ecto.Schema
import Ecto.Changeset
schema "settings" do
field :name, :string
field :value, :string
timestamps()
end
@doc false
def changeset(setting, attrs) do
setting
|> cast(attrs, [:name, :value])
|> validate_required([:name, :value])
|> unique_constraint(:name)
end
end

39
lib/mirage_web/controllers/setting_controller.ex

@ -0,0 +1,39 @@
defmodule MirageWeb.SettingController do
use MirageWeb, :controller
alias Mirage.Settings
import MirageWeb.UserAuth, only: [require_authenticated_user: 2]
plug :require_authenticated_user
def index(conn, _params) do
settings = Settings.list_settings()
render(conn, "index.html", settings: settings)
end
def show(conn, %{"id" => id}) do
setting = Settings.get_setting!(id)
render(conn, "show.html", setting: setting)
end
def edit(conn, %{"id" => id}) do
setting = Settings.get_setting!(id)
changeset = Settings.change_setting(setting)
render(conn, "edit.html", setting: setting, changeset: changeset)
end
def update(conn, %{"id" => id, "setting" => setting_params}) do
setting = Settings.get_setting!(id)
case Settings.update_setting(setting, setting_params) do
{:ok, setting} ->
conn
|> put_flash(:info, "Setting updated successfully.")
|> redirect(to: Routes.setting_path(conn, :show, setting))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "edit.html", setting: setting, changeset: changeset)
end
end
end

4
lib/mirage_web/live/page_live.ex

@ -3,6 +3,8 @@ defmodule MirageWeb.PageLive do
@impl true
def mount(_params, _session, socket) do
{:ok, socket}
tagline = Mirage.Settings.get_setting_by_name!("user_tagline")
{:ok, assign(socket, tagline: tagline.value)}
end
end

2
lib/mirage_web/live/page_live.html.leex

@ -1,6 +1,6 @@
<section class="phx-hero">
<h1><%= gettext "Hello my name is %{name}!", name: "Inhji" %></h1>
<h3>Bike dude 🚴‍♂️, shitty guy 🤦‍♂️, programmer by day and night 🖥️, Lovecraft follower 🐙, Modular tinkerer 🎚️, Music addict 🎹, Dreamer ⛅, Child at heart ❤️</h3>
<h3><%= @tagline %></h3>
</section>
<section class="row">

1
lib/mirage_web/router.ex

@ -23,6 +23,7 @@ defmodule MirageWeb.Router do
live "/", PageLive, :index
resources "/notes", NoteController
resources "/settings", SettingController, only: [:index, :show, :edit, :update]
end
# Other scopes may use custom stacks.

12
lib/mirage_web/templates/layout/_user_menu.html.eex

@ -1,10 +1,10 @@
<ul class="row">
<ul>
<%= if @current_user do %>
<li class="column"><%= @current_user.email %></li>
<li class="column"><%= link "Settings", to: Routes.user_settings_path(@conn, :edit) %></li>
<li class="column"><%= link "Log out", to: Routes.user_session_path(@conn, :delete), method: :delete %></li>
<li><%= link @current_user.email, to: Routes.user_settings_path(@conn, :edit) %></li>
<li><%= link "Settings", to: Routes.setting_path(@conn, :index) %></li>
<li><%= link "Log out", to: Routes.user_session_path(@conn, :delete), method: :delete %></li>
<% else %>
<li class="column"><%= link "Register", to: Routes.user_registration_path(@conn, :new) %></li>
<li class="column"><%= link "Log in", to: Routes.user_session_path(@conn, :new) %></li>
<li><%= link "Register", to: Routes.user_registration_path(@conn, :new) %></li>
<li><%= link "Log in", to: Routes.user_session_path(@conn, :new) %></li>
<% end %>
</ul>

5
lib/mirage_web/templates/setting/edit.html.eex

@ -0,0 +1,5 @@
<h1>Edit Setting</h1>
<%= render "form.html", Map.put(assigns, :action, Routes.setting_path(@conn, :update, @setting)) %>
<span><%= link "Back", to: Routes.setting_path(@conn, :index) %></span>

19
lib/mirage_web/templates/setting/form.html.eex

@ -0,0 +1,19 @@
<%= form_for @changeset, @action, fn f -> %>
<%= if @changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<% end %>
<%= label f, :name %>
<%= text_input f, :name, readonly: true %>
<%= error_tag f, :name %>
<%= label f, :value %>
<%= text_input f, :value %>
<%= error_tag f, :value %>
<div>
<%= submit "Save" %>
</div>
<% end %>

25
lib/mirage_web/templates/setting/index.html.eex

@ -0,0 +1,25 @@
<h1>Listing Settings</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Value</th>
<th></th>
</tr>
</thead>
<tbody>
<%= for setting <- @settings do %>
<tr>
<td><%= setting.name %></td>
<td><%= setting.value %></td>
<td>
<span><%= link "Show", to: Routes.setting_path(@conn, :show, setting) %></span>
<span><%= link "Edit", to: Routes.setting_path(@conn, :edit, setting) %></span>
</td>
</tr>
<% end %>
</tbody>
</table>

5
lib/mirage_web/templates/setting/new.html.eex

@ -0,0 +1,5 @@
<h1>New Setting</h1>
<%= render "form.html", Map.put(assigns, :action, Routes.setting_path(@conn, :create)) %>
<span><%= link "Back", to: Routes.setting_path(@conn, :index) %></span>

18
lib/mirage_web/templates/setting/show.html.eex

@ -0,0 +1,18 @@
<h1>Show Setting</h1>
<ul>
<li>
<strong>Name:</strong>
<%= @setting.name %>
</li>
<li>
<strong>Value:</strong>
<%= @setting.value %>
</li>
</ul>
<span><%= link "Edit", to: Routes.setting_path(@conn, :edit, @setting) %></span>
<span><%= link "Back", to: Routes.setting_path(@conn, :index) %></span>

3
lib/mirage_web/views/setting_view.ex

@ -0,0 +1,3 @@
defmodule MirageWeb.SettingView do
use MirageWeb, :view
end

19
priv/repo/migrations/20210109130905_create_settings.exs

@ -0,0 +1,19 @@
defmodule Mirage.Repo.Migrations.CreateSettings do
use Ecto.Migration
def up do
create table(:settings) do
add :name, :string
add :value, :string
timestamps()
end
create unique_index(:settings, [:name])
end
def down do
drop unique_index(:settings, [:name])
drop table(:settings)
end
end

14
priv/repo/migrations/20210111174337_add_settings_data.exs

@ -0,0 +1,14 @@
defmodule Mirage.Repo.Migrations.AddSettingsData do
use Ecto.Migration
alias Mirage.Settings.Setting
def up do
%Setting{}
|> Setting.changeset(%{name: "user_tagline", value: "some tagline"})
|> repo().insert()
end
def down do
repo().delete_all(Setting)
end
end

50
test/mirage/settings_test.exs

@ -0,0 +1,50 @@
defmodule Mirage.SettingsTest do
use Mirage.DataCase
alias Mirage.Settings
describe "settings" do
alias Mirage.Settings.Setting
@valid_attrs %{name: "some name", value: "some value"}
@update_attrs %{name: "some updated name", value: "some updated value"}
@invalid_attrs %{name: nil, value: nil}
def setting_fixture(attrs \\ %{}) do
{:ok, setting} =
attrs
|> Enum.into(@valid_attrs)
|> Settings.create_setting()
setting
end
test "list_settings/0 returns all settings" do
setting = setting_fixture()
assert Settings.list_settings() == [%Setting{}, setting]
end
test "get_setting!/1 returns the setting with given id" do
setting = setting_fixture()
assert Settings.get_setting!(setting.id) == setting
end
test "update_setting/2 with valid data updates the setting" do
setting = setting_fixture()
assert {:ok, %Setting{} = setting} = Settings.update_setting(setting, @update_attrs)
assert setting.name == "some updated name"
assert setting.value == "some updated value"
end
test "update_setting/2 with invalid data returns error changeset" do
setting = setting_fixture()
assert {:error, %Ecto.Changeset{}} = Settings.update_setting(setting, @invalid_attrs)
assert setting == Settings.get_setting!(setting.id)
end
test "change_setting/1 returns a setting changeset" do
setting = setting_fixture()
assert %Ecto.Changeset{} = Settings.change_setting(setting)
end
end
end

54
test/mirage_web/controllers/setting_controller_test.exs

@ -0,0 +1,54 @@
defmodule MirageWeb.SettingControllerTest do
use MirageWeb.ConnCase
alias Mirage.Settings
setup :register_and_log_in_user
@create_attrs %{name: "some name", value: "some value"}
@update_attrs %{name: "some updated name", value: "some updated value"}
@invalid_attrs %{name: nil, value: nil}
def fixture(:setting) do
{:ok, setting} = Settings.create_setting(@create_attrs)
setting
end
describe "index" do
test "lists all settings", %{conn: conn} do
conn = get(conn, Routes.setting_path(conn, :index))
assert html_response(conn, 200) =~ "Listing Settings"
end
end
describe "edit setting" do
setup [:create_setting]
test "renders form for editing chosen setting", %{conn: conn, setting: setting} do
conn = get(conn, Routes.setting_path(conn, :edit, setting))
assert html_response(conn, 200) =~ "Edit Setting"
end
end
describe "update setting" do
setup [:create_setting]
test "redirects when data is valid", %{conn: conn, setting: setting} do
conn = put(conn, Routes.setting_path(conn, :update, setting), setting: @update_attrs)
assert redirected_to(conn) == Routes.setting_path(conn, :show, setting)
conn = get(conn, Routes.setting_path(conn, :show, setting))
assert html_response(conn, 200) =~ "some updated name"
end
test "renders errors when data is invalid", %{conn: conn, setting: setting} do
conn = put(conn, Routes.setting_path(conn, :update, setting), setting: @invalid_attrs)
assert html_response(conn, 200) =~ "Edit Setting"
end
end
defp create_setting(_) do
setting = fixture(:setting)
%{setting: setting}
end
end
Loading…
Cancel
Save