Skip to content

Commit

Permalink
#1 rename Objects to Object for consistency and remove "Context" that…
Browse files Browse the repository at this point in the history
… adds zero value!
  • Loading branch information
nelsonic committed Oct 4, 2023
1 parent e60af12 commit 38ae5cb
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 95 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,11 @@ we can
Any `person` that can afford to have someone `else` cleaning their house
has understood the value of their time.
But may be too busy working to keep their house _perfectly_ tidy.

With a tiny tool that allows
your assistant to easily ask
"Where does this belong?"
busy people can rapidly respond
and **systematically _organize_** their lives.

<hr />

Expand Down
2 changes: 1 addition & 1 deletion lib/tidy/comment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule Tidy.Comment do
## Examples
iex> create_comment(%{obj_id: 1, person_id: 1, url: "Candles on the kitchen counter"})
iex> create_comment(%{obj_id: 1, person_id: 1, text: "Candles on the kitchen counter"})
{:ok, %Image{}}
iex> create_comment(%{field: bad_value})
Expand Down
28 changes: 22 additions & 6 deletions lib/tidy/objects.ex → lib/tidy/object.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
defmodule Tidy.Objects do
@moduledoc """
The Objects context.
"""

defmodule Tidy.Object do
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query, warn: false
alias __MODULE__
alias Tidy.Repo

alias Tidy.Objects.Object
schema "objects" do
field :name, :binary
field :status, :integer
field :desc, :binary
field :location, :binary
field :color, :binary
field :person_id, :integer
field :owner_id, :integer

timestamps()
end

@doc false
def changeset(object, attrs) do
object
|> cast(attrs, [:name, :desc, :color, :person_id, :owner_id, :location, :status])
|> validate_required([:name, :desc, :color, :person_id, :owner_id, :location, :status])
end

@doc """
Returns the list of objects.
Expand Down
23 changes: 0 additions & 23 deletions lib/tidy/objects/object.ex

This file was deleted.

10 changes: 5 additions & 5 deletions lib/tidy_web/live/object_live/form_component.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule TidyWeb.ObjectLive.FormComponent do
use TidyWeb, :live_component

alias Tidy.Objects
alias Tidy.Object

@impl true
def render(assigns) do
Expand Down Expand Up @@ -36,7 +36,7 @@ defmodule TidyWeb.ObjectLive.FormComponent do

@impl true
def update(%{object: object} = assigns, socket) do
changeset = Objects.change_object(object)
changeset = Object.change_object(object)

{:ok,
socket
Expand All @@ -48,7 +48,7 @@ defmodule TidyWeb.ObjectLive.FormComponent do
def handle_event("validate", %{"object" => object_params}, socket) do
changeset =
socket.assigns.object
|> Objects.change_object(object_params)
|> Object.change_object(object_params)
|> Map.put(:action, :validate)

{:noreply, assign_form(socket, changeset)}
Expand All @@ -59,7 +59,7 @@ defmodule TidyWeb.ObjectLive.FormComponent do
end

defp save_object(socket, :edit, object_params) do
case Objects.update_object(socket.assigns.object, object_params) do
case Object.update_object(socket.assigns.object, object_params) do
{:ok, object} ->
notify_parent({:saved, object})

Expand All @@ -75,7 +75,7 @@ defmodule TidyWeb.ObjectLive.FormComponent do
end

defp save_object(socket, :new, object_params) do
case Objects.create_object(object_params) do
case Object.create_object(object_params) do
{:ok, object} ->
notify_parent({:saved, object})

Expand Down
12 changes: 5 additions & 7 deletions lib/tidy_web/live/object_live/index.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
defmodule TidyWeb.ObjectLive.Index do
use TidyWeb, :live_view

alias Tidy.Objects
alias Tidy.Objects.Object
alias Tidy.Object

@impl true
def mount(_params, _session, socket) do
{:ok, stream(socket, :objects, Objects.list_objects())}
{:ok, stream(socket, :objects, Object.list_objects())}
end

@impl true
Expand All @@ -17,7 +15,7 @@ defmodule TidyWeb.ObjectLive.Index do
defp apply_action(socket, :edit, %{"id" => id}) do
socket
|> assign(:page_title, "Edit Object")
|> assign(:object, Objects.get_object!(id))
|> assign(:object, Object.get_object!(id))
end

defp apply_action(socket, :new, _params) do
Expand All @@ -39,8 +37,8 @@ defmodule TidyWeb.ObjectLive.Index do

@impl true
def handle_event("delete", %{"id" => id}, socket) do
object = Objects.get_object!(id)
{:ok, _} = Objects.delete_object(object)
object = Object.get_object!(id)
{:ok, _} = Object.delete_object(object)

{:noreply, stream_delete(socket, :objects, object)}
end
Expand Down
12 changes: 6 additions & 6 deletions lib/tidy_web/live/object_live/index.html.heex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<.header>
Listing Objects
<:actions>
<.link patch={~p"/objects/new"}>
<.link patch={~p"/obj/new"}>
<.button>New Object</.button>
</.link>
</:actions>
Expand All @@ -10,7 +10,7 @@
<.table
id="objects"
rows={@streams.objects}
row_click={fn {_id, object} -> JS.navigate(~p"/objects/#{object}") end}
row_click={fn {_id, object} -> JS.navigate(~p"/obj/#{object}") end}
>
<:col :let={{_id, object}} label="Name"><%= object.name %></:col>
<:col :let={{_id, object}} label="Desc"><%= object.desc %></:col>
Expand All @@ -21,9 +21,9 @@
<:col :let={{_id, object}} label="Status"><%= object.status %></:col>
<:action :let={{_id, object}}>
<div class="sr-only">
<.link navigate={~p"/objects/#{object}"}>Show</.link>
<.link navigate={~p"/obj/#{object}"}>Show</.link>
</div>
<.link patch={~p"/objects/#{object}/edit"}>Edit</.link>
<.link patch={~p"/obj/#{object}/edit"}>Edit</.link>
</:action>
<:action :let={{id, object}}>
<.link
Expand All @@ -35,13 +35,13 @@
</:action>
</.table>

<.modal :if={@live_action in [:new, :edit]} id="object-modal" show on_cancel={JS.patch(~p"/objects")}>
<.modal :if={@live_action in [:new, :edit]} id="object-modal" show on_cancel={JS.patch(~p"/obj")}>
<.live_component
module={TidyWeb.ObjectLive.FormComponent}
id={@object.id || :new}
title={@page_title}
action={@live_action}
object={@object}
patch={~p"/objects"}
patch={~p"/obj"}
/>
</.modal>
5 changes: 2 additions & 3 deletions lib/tidy_web/live/object_live/show.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule TidyWeb.ObjectLive.Show do
use TidyWeb, :live_view

alias Tidy.Objects
alias Tidy.Object

@impl true
def mount(_params, _session, socket) do
Expand All @@ -13,7 +12,7 @@ defmodule TidyWeb.ObjectLive.Show do
{:noreply,
socket
|> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:object, Objects.get_object!(id))}
|> assign(:object, Object.get_object!(id))}
end

defp page_title(:show), do: "Show Object"
Expand Down
8 changes: 4 additions & 4 deletions lib/tidy_web/live/object_live/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Object <%= @object.id %>
<:subtitle>This is a object record from your database.</:subtitle>
<:actions>
<.link patch={~p"/objects/#{@object}/show/edit"} phx-click={JS.push_focus()}>
<.link patch={~p"/obj/#{@object}/show/edit"} phx-click={JS.push_focus()}>
<.button>Edit object</.button>
</.link>
</:actions>
Expand All @@ -18,15 +18,15 @@
<:item title="Status"><%= @object.status %></:item>
</.list>

<.back navigate={~p"/objects"}>Back to objects</.back>
<.back navigate={~p"/obj"}>Back to objects</.back>

<.modal :if={@live_action == :edit} id="object-modal" show on_cancel={JS.patch(~p"/objects/#{@object}")}>
<.modal :if={@live_action == :edit} id="object-modal" show on_cancel={JS.patch(~p"/obj/#{@object}")}>
<.live_component
module={TidyWeb.ObjectLive.FormComponent}
id={@object.id}
title={@page_title}
action={@live_action}
object={@object}
patch={~p"/objects/#{@object}"}
patch={~p"/obj/#{@object}"}
/>
</.modal>
13 changes: 6 additions & 7 deletions lib/tidy_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ defmodule TidyWeb.Router do

get "/", PageController, :home

live "/objects", ObjectLive.Index, :index
live "/objects/new", ObjectLive.Index, :new
live "/objects/:id/edit", ObjectLive.Index, :edit
live "/obj", ObjectLive.Index, :index
live "/obj/new", ObjectLive.Index, :new
live "/obj/:id/edit", ObjectLive.Index, :edit

live "/objects/:id", ObjectLive.Show, :show
live "/objects/:id/show/edit", ObjectLive.Show, :edit
live "/obj/:id", ObjectLive.Show, :show
live "/obj/:id/show/edit", ObjectLive.Show, :edit
end


# pipeline :api do
# pipeline :api do
# plug :accepts, ["json"]
# end
# Other scopes may use custom stacks.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Tidy.ObjectsFixtures do
defmodule Tidy.ObjectFixtures do
@moduledoc """
This module defines test helpers for creating
entities via the `Tidy.Objects` context.
Expand All @@ -19,7 +19,7 @@ defmodule Tidy.ObjectsFixtures do
person_id: 42,
owner_id: 42
})
|> Tidy.Objects.create_object()
|> Tidy.Object.create_object()

object
end
Expand Down
31 changes: 14 additions & 17 deletions test/tidy/objects_test.exs → test/tidy/object_test.exs
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
defmodule Tidy.ObjectsTest do
defmodule Tidy.ObjectTest do
use Tidy.DataCase
alias Tidy.Object
import Tidy.ObjectFixtures

alias Tidy.Objects

describe "objects" do
alias Tidy.Objects.Object

import Tidy.ObjectsFixtures
describe "object" do

@invalid_attrs %{name: nil, status: nil, desc: nil, location: nil, color: nil, person_id: nil, owner_id: nil}

test "list_objects/0 returns all objects" do
object = object_fixture()
assert Objects.list_objects() == [object]
assert Object.list_objects() == [object]
end

test "get_object!/1 returns the object with given id" do
object = object_fixture()
assert Objects.get_object!(object.id) == object
assert Object.get_object!(object.id) == object
end

test "create_object/1 with valid data creates a object" do
valid_attrs = %{name: "some name", status: 42, desc: "some desc", location: "some location", color: "some color", person_id: 42, owner_id: 42}

assert {:ok, %Object{} = object} = Objects.create_object(valid_attrs)
assert {:ok, %Object{} = object} = Object.create_object(valid_attrs)
assert object.name == "some name"
assert object.status == 42
assert object.desc == "some desc"
Expand All @@ -34,14 +31,14 @@ defmodule Tidy.ObjectsTest do
end

test "create_object/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Objects.create_object(@invalid_attrs)
assert {:error, %Ecto.Changeset{}} = Object.create_object(@invalid_attrs)
end

test "update_object/2 with valid data updates the object" do
object = object_fixture()
update_attrs = %{name: "some updated name", status: 43, desc: "some updated desc", location: "some updated location", color: "some updated color", person_id: 43, owner_id: 43}

assert {:ok, %Object{} = object} = Objects.update_object(object, update_attrs)
assert {:ok, %Object{} = object} = Object.update_object(object, update_attrs)
assert object.name == "some updated name"
assert object.status == 43
assert object.desc == "some updated desc"
Expand All @@ -53,19 +50,19 @@ defmodule Tidy.ObjectsTest do

test "update_object/2 with invalid data returns error changeset" do
object = object_fixture()
assert {:error, %Ecto.Changeset{}} = Objects.update_object(object, @invalid_attrs)
assert object == Objects.get_object!(object.id)
assert {:error, %Ecto.Changeset{}} = Object.update_object(object, @invalid_attrs)
assert object == Object.get_object!(object.id)
end

test "delete_object/1 deletes the object" do
object = object_fixture()
assert {:ok, %Object{}} = Objects.delete_object(object)
assert_raise Ecto.NoResultsError, fn -> Objects.get_object!(object.id) end
assert {:ok, %Object{}} = Object.delete_object(object)
assert_raise Ecto.NoResultsError, fn -> Object.get_object!(object.id) end
end

test "change_object/1 returns a object changeset" do
object = object_fixture()
assert %Ecto.Changeset{} = Objects.change_object(object)
assert %Ecto.Changeset{} = Object.change_object(object)
end
end
end
Loading

0 comments on commit 38ae5cb

Please sign in to comment.