Skip to content

Commit

Permalink
Merge branch 'develop' into dp/fix-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsp45 committed Jul 10, 2023
2 parents 1ac1397 + 1f823f9 commit a429018
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 49 deletions.
108 changes: 108 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
[asdf-vm]: https://asdf-vm.com/

# 🚀 Getting Started

These instructions will get you a copy of the project up and running on your
local machine for development and testing purposes.

## 📥 Prerequisites

The following software is required to be installed on your system:

- [Erlang 25+](https://www.erlang.org/downloads)
- [Elixir 1.14+](https://elixir-lang.org/install.html)
- [PostgreSQL 13+](https://www.postgresql.org/download/)(^See [this section](#-docker) for setting up with docker.)

We recommend using [asdf version manager][asdf-vm] to install and manage all
the programming languages' requirements.

If you prefer to use docker, see the [section below](#-docker).

## 🔧 Setup

First, clone the repository:

```
git clone [email protected]:cesium/atomic.git
cd atomic
```

Then, run the setup script to get all dependencies configured. Make sure the database is up and running.

```
bin/setup
```

Then you should change the `.env.dev` file as needed. Run this script again if
needed.

## 🔨 Development

Start the development server and then you can visit `http://localhost:4000`
from your browser.

```
bin/server
```

Run the tests.

```
bin/test
```

Lint your code.

```
bin/lint
```

Format your code.

```
bin/format
```

## 🐳 Docker

For data persistence this project uses a PostgreSQL database. You should have
PostgreSQL up and running.

If you want to setup the required database using docker containers you can
easily do it with [docker-compose](https://docs.docker.com/compose/install/).

Create and start the database containers.

```
cp .env.dev.sample .env.dev
docker-compose -f docker-compose.dev.yml -f {linux,darwin}.yml up db
```

Start the previously created containers.

```
docker-compose -f docker-compose.dev.yml -f {linux,darwin}.yml start
```

Stop the containers.

```
docker-compose -f docker-compose.dev.yml -f {linux,darwin}.yml stop
```

Destroy the containers and volumes created.

```
docker-compose -f docker-compose.dev.yml -f {linux,darwin}.yml down -v
```

## 🔗 References

You can use these resources to learn more about the technologies this project
uses.

- [Getting Started with Elixir](https://elixir-lang.org/getting-started/introduction.html)
- [Erlang/Elixir Syntax: A Crash Course](https://elixir-lang.org/crash-course.html)
- [Elixir School Course](https://elixirschool.com/en/)
- [Phoenix Guides Overview](https://hexdocs.pm/phoenix/overview.html)
- [Phoenix Documentation](https://hexdocs.pm/phoenix)
4 changes: 3 additions & 1 deletion lib/atomic/activities/speaker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ defmodule Atomic.Activities.Speaker do
The person who speaks and provides the activity
"""
use Atomic.Schema
alias Atomic.Activities.Activity
alias Atomic.Activities.ActivitySpeaker

schema "speakers" do
field :bio, :string
field :name, :string

many_to_many :activities, Activity, join_through: ActivitySpeaker, on_replace: :delete
timestamps()
end

Expand Down
3 changes: 3 additions & 0 deletions lib/atomic_web/live/department_live/form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ defmodule AtomicWeb.DepartmentLive.FormComponent do
end

defp save_department(socket, :new, department_params) do
department_params =
Map.put(department_params, "organization_id", socket.assigns.organization.id)

case Departments.create_department(department_params) do
{:ok, _department} ->
{:noreply,
Expand Down
9 changes: 6 additions & 3 deletions lib/atomic_web/live/department_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ defmodule AtomicWeb.DepartmentLive.Index do

alias Atomic.Departments
alias Atomic.Departments.Department
alias Atomic.Organizations

@impl true
def mount(_params, _session, socket) do
{:ok, assign(socket, :departments, list_departments())}
end

@impl true
def handle_params(params, _url, socket) do
def handle_params(%{"org" => _id} = params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end

Expand All @@ -20,15 +21,17 @@ defmodule AtomicWeb.DepartmentLive.Index do
|> assign(:department, Departments.get_department!(id))
end

defp apply_action(socket, :new, _params) do
defp apply_action(socket, :new, %{"org" => id}) do
socket
|> assign(:page_title, "New Department")
|> assign(:organization, Organizations.get_organization!(id))
|> assign(:department, %Department{})
end

defp apply_action(socket, :index, _params) do
defp apply_action(socket, :index, %{"org" => id}) do
socket
|> assign(:page_title, "Listing Departments")
|> assign(:organization, Organizations.get_organization!(id))
|> assign(:department, nil)
end

Expand Down
10 changes: 5 additions & 5 deletions lib/atomic_web/live/department_live/index.html.heex
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<h1>Listing Departments</h1>

<%= if @live_action in [:new, :edit] do %>
<.modal return_to={Routes.department_index_path(@socket, :index)}>
<.live_component module={AtomicWeb.DepartmentLive.FormComponent} id={@department.id || :new} title={@page_title} action={@live_action} department={@department} return_to={Routes.department_index_path(@socket, :index)} />
<.modal return_to={Routes.department_index_path(@socket, :index, @organization)}>
<.live_component module={AtomicWeb.DepartmentLive.FormComponent} organization={@organization} id={@department.id || :new} title={@page_title} action={@live_action} department={@department} return_to={Routes.department_index_path(@socket, :index, @organization)} />
</.modal>
<% end %>

Expand All @@ -20,13 +20,13 @@
<td><%= department.name %></td>

<td>
<span><%= live_redirect("Show", to: Routes.department_show_path(@socket, :show, department)) %></span>
<span><%= live_patch("Edit", to: Routes.department_index_path(@socket, :edit, department)) %></span>
<span><%= live_redirect("Show", to: Routes.department_show_path(@socket, :show, department.organization_id, department)) %></span>
<span><%= live_patch("Edit", to: Routes.department_index_path(@socket, :edit, department.organization_id, department)) %></span>
<span><%= link("Delete", to: "#", phx_click: "delete", phx_value_id: department.id, data: [confirm: "Are you sure?"]) %></span>
</td>
</tr>
<% end %>
</tbody>
</table>

<span><%= live_patch("New Department", to: Routes.department_index_path(@socket, :new)) %></span>
<span><%= live_patch("New Department", to: Routes.department_index_path(@socket, :new, @organization)) %></span>
4 changes: 3 additions & 1 deletion lib/atomic_web/live/department_live/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ defmodule AtomicWeb.DepartmentLive.Show do
use AtomicWeb, :live_view

alias Atomic.Departments
alias Atomic.Organizations

@impl true
def mount(_params, _session, socket) do
{:ok, socket}
end

@impl true
def handle_params(%{"id" => id}, _, socket) do
def handle_params(%{"org" => org, "id" => id}, _, socket) do
{:noreply,
socket
|> assign(:page_title, page_title(socket.assigns.live_action))
|> assign(:organization, Organizations.get_organization!(org))
|> assign(:department, Departments.get_department!(id, preloads: :activities))}
end

Expand Down
8 changes: 4 additions & 4 deletions lib/atomic_web/live/department_live/show.html.heex
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<h1>Show Department</h1>

<%= if @live_action in [:edit] do %>
<.modal return_to={Routes.department_show_path(@socket, :show, @department)}>
<.live_component module={AtomicWeb.DepartmentLive.FormComponent} id={@department.id} title={@page_title} action={@live_action} department={@department} return_to={Routes.department_show_path(@socket, :show, @department)} />
<.modal return_to={Routes.department_show_path(@socket, :show, @organization, @department)}>
<.live_component module={AtomicWeb.DepartmentLive.FormComponent} id={@department.id} title={@page_title} action={@live_action} department={@department} return_to={Routes.department_show_path(@socket, :show, @organization, @department)} />
</.modal>
<% end %>

Expand All @@ -24,5 +24,5 @@
</li>
</ul>

<span><%= live_patch("Edit", to: Routes.department_show_path(@socket, :edit, @department), class: "button") %></span> |
<span><%= live_redirect("Back", to: Routes.department_index_path(@socket, :index)) %></span>
<span><%= live_patch("Edit", to: Routes.department_show_path(@socket, :edit, @organization, @department), class: "button") %></span> |
<span><%= live_redirect("Back", to: Routes.department_index_path(@socket, :index, @organization)) %></span>
2 changes: 1 addition & 1 deletion lib/atomic_web/live/organization_live/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<%= if @live_action in [:new, :edit] do %>
<.modal return_to={Routes.organization_index_path(@socket, :index)}>
<.live_component module={AtomicWeb.OrganizationLive.FormComponent} id={@organization.id || :new} title={@page_title} action={@live_action} organization={@organization} return_to={Routes.organization_index_path(@socket, :index)} />
<.live_component module={AtomicWeb.OrganizationLive.FormComponent} id={@organization || :new} title={@page_title} action={@live_action} organization={@organization} return_to={Routes.organization_index_path(@socket, :index)} />
</.modal>
<% end %>

Expand Down
2 changes: 1 addition & 1 deletion lib/atomic_web/live/organization_live/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<%= if @live_action in [:edit] do %>
<.modal return_to={Routes.organization_show_path(@socket, :show, @organization)}>
<.live_component module={AtomicWeb.OrganizationLive.FormComponent} id={@organization.id} title={@page_title} action={@live_action} organization={@organization} return_to={Routes.organization_show_path(@socket, :show, @organization)} />
<.live_component module={AtomicWeb.OrganizationLive.FormComponent} id={@organization} title={@page_title} action={@live_action} organization={@organization} return_to={Routes.organization_show_path(@socket, :show, @organization)} />
</.modal>
<% end %>

Expand Down
10 changes: 5 additions & 5 deletions lib/atomic_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ defmodule AtomicWeb.Router do
live "/activities/:id/edit", ActivityLive.Edit, :edit
live "/activities/:id", ActivityLive.Show, :show

live "/departments", DepartmentLive.Index, :index
live "/departments/new", DepartmentLive.Index, :new
live "/departments/:id/edit", DepartmentLive.Index, :edit
live "/departments/:id", DepartmentLive.Show, :show
live "/departments/:id/show/edit", DepartmentLive.Show, :edit
live "/departments/:org", DepartmentLive.Index, :index
live "/departments/:org/new", DepartmentLive.Index, :new
live "/departments/:org/:id/edit", DepartmentLive.Index, :edit
live "/departments/:org/:id", DepartmentLive.Show, :show
live "/departments/:org/:id/show/edit", DepartmentLive.Show, :edit

live "/partners", PartnerLive.Index, :index
live "/partners/new", PartnerLive.Index, :new
Expand Down
1 change: 0 additions & 1 deletion lib/atomic_web/templates/layout/root.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<nav>
<ul class="py-10 flex items-center space-x-4">
<li><%= live_redirect("Activities", to: Routes.activity_index_path(@conn, :index)) %></li>
<li><%= live_redirect("Departments", to: Routes.department_index_path(@conn, :index)) %></li>
<li><%= live_redirect("Partners", to: Routes.partner_index_path(@conn, :index)) %></li>
<li><%= live_redirect("Speakers", to: Routes.speaker_index_path(@conn, :index)) %></li>
<li><%= live_redirect("Organizations", to: Routes.organization_index_path(@conn, :index)) %></li>
Expand Down
27 changes: 0 additions & 27 deletions test/atomic_web/live/department_live_test.exs
Original file line number Diff line number Diff line change
@@ -1,30 +1,3 @@
defmodule AtomicWeb.DepartmentLiveTest do
use AtomicWeb.ConnCase

import Phoenix.LiveViewTest
import Atomic.Factory

defp create_department(_) do
department = insert(:department)
%{department: department}
end

describe "Index" do
setup [:create_department]
setup [:register_and_log_in_user]

test "lists all departments", %{conn: conn, department: department} do
{:ok, _index_live, html} = live(conn, Routes.department_index_path(conn, :index))

assert html =~ "Listing Departments"
assert html =~ department.name
end

test "deletes department in listing", %{conn: conn, department: department} do
{:ok, index_live, _html} = live(conn, Routes.department_index_path(conn, :index))

assert index_live |> element("#department-#{department.id} a", "Delete") |> render_click()
refute has_element?(index_live, "#department-#{department.id}")
end
end
end

0 comments on commit a429018

Please sign in to comment.