work on pipelines

This commit is contained in:
shibao 2022-11-24 14:31:16 -05:00
parent ad457b428a
commit 7195c7fba6
17 changed files with 787 additions and 242 deletions

View File

@ -4,21 +4,88 @@ defmodule Memex.Pipelines do
""" """
import Ecto.Query, warn: false import Ecto.Query, warn: false
alias Memex.Repo alias Ecto.Changeset
alias Memex.{Accounts.User, Pipelines.Pipeline, Repo}
alias Memex.Pipelines.Pipeline
@doc """ @doc """
Returns the list of pipelines. Returns the list of pipelines.
## Examples ## Examples
iex> list_pipelines() iex> list_pipelines(%User{id: 123})
[%Pipeline{}, ...] [%Pipeline{}, ...]
iex> list_pipelines("my pipeline", %User{id: 123})
[%Pipeline{title: "my pipeline"}, ...]
""" """
def list_pipelines do @spec list_pipelines(User.t()) :: [Pipeline.t()]
Repo.all(Pipeline) @spec list_pipelines(search :: String.t() | nil, User.t()) :: [Pipeline.t()]
def list_pipelines(search \\ nil, user)
def list_pipelines(search, %{id: user_id}) when search |> is_nil() or search == "" do
Repo.all(from p in Pipeline, where: p.user_id == ^user_id, order_by: p.title)
end
def list_pipelines(search, %{id: user_id}) when search |> is_binary() do
trimmed_search = String.trim(search)
Repo.all(
from p in Pipeline,
where: p.user_id == ^user_id,
where:
fragment(
"search @@ to_tsquery(websearch_to_tsquery(?)::text || ':*')",
^trimmed_search
),
order_by: {
:desc,
fragment(
"ts_rank_cd(search, to_tsquery(websearch_to_tsquery(?)::text || ':*'), 4)",
^trimmed_search
)
}
)
end
@doc """
Returns the list of public pipelines for viewing
## Examples
iex> list_public_pipelines()
[%Pipeline{}, ...]
iex> list_public_pipelines("my pipeline")
[%Pipeline{title: "my pipeline"}, ...]
"""
@spec list_public_pipelines() :: [Pipeline.t()]
@spec list_public_pipelines(search :: String.t() | nil) :: [Pipeline.t()]
def list_public_pipelines(search \\ nil)
def list_public_pipelines(search) when search |> is_nil() or search == "" do
Repo.all(from p in Pipeline, where: p.visibility == :public, order_by: p.title)
end
def list_public_pipelines(search) when search |> is_binary() do
trimmed_search = String.trim(search)
Repo.all(
from p in Pipeline,
where: p.visibility == :public,
where:
fragment(
"search @@ to_tsquery(websearch_to_tsquery(?)::text || ':*')",
^trimmed_search
),
order_by: {
:desc,
fragment(
"ts_rank_cd(search, to_tsquery(websearch_to_tsquery(?)::text || ':*'), 4)",
^trimmed_search
)
}
)
end end
@doc """ @doc """
@ -28,31 +95,47 @@ defmodule Memex.Pipelines do
## Examples ## Examples
iex> get_pipeline!(123) iex> get_pipeline!(123, %User{id: 123})
%Pipeline{} %Pipeline{}
iex> get_pipeline!(456) iex> get_pipeline!(456, %User{id: 123})
** (Ecto.NoResultsError) ** (Ecto.NoResultsError)
""" """
def get_pipeline!(id), do: Repo.get!(Pipeline, id) @spec get_pipeline!(Pipeline.id(), User.t()) :: Pipeline.t()
def get_pipeline!(id, %{id: user_id}) do
Repo.one!(
from p in Pipeline,
where: p.id == ^id,
where: p.user_id == ^user_id or p.visibility in [:public, :unlisted]
)
end
def get_pipeline!(id, _invalid_user) do
Repo.one!(
from p in Pipeline,
where: p.id == ^id,
where: p.visibility in [:public, :unlisted]
)
end
@doc """ @doc """
Creates a pipeline. Creates a pipeline.
## Examples ## Examples
iex> create_pipeline(%{field: value}) iex> create_pipeline(%{field: value}, %User{id: 123})
{:ok, %Pipeline{}} {:ok, %Pipeline{}}
iex> create_pipeline(%{field: bad_value}) iex> create_pipeline(%{field: bad_value}, %User{id: 123})
{:error, %Ecto.Changeset{}} {:error, %Ecto.Changeset{}}
""" """
def create_pipeline(attrs \\ %{}) do @spec create_pipeline(User.t()) :: {:ok, Pipeline.t()} | {:error, Pipeline.changeset()}
%Pipeline{} @spec create_pipeline(attrs :: map(), User.t()) ::
|> Pipeline.changeset(attrs) {:ok, Pipeline.t()} | {:error, Pipeline.changeset()}
|> Repo.insert() def create_pipeline(attrs \\ %{}, user) do
Pipeline.create_changeset(attrs, user) |> Repo.insert()
end end
@doc """ @doc """
@ -60,16 +143,18 @@ defmodule Memex.Pipelines do
## Examples ## Examples
iex> update_pipeline(pipeline, %{field: new_value}) iex> update_pipeline(pipeline, %{field: new_value}, %User{id: 123})
{:ok, %Pipeline{}} {:ok, %Pipeline{}}
iex> update_pipeline(pipeline, %{field: bad_value}) iex> update_pipeline(pipeline, %{field: bad_value}, %User{id: 123})
{:error, %Ecto.Changeset{}} {:error, %Ecto.Changeset{}}
""" """
def update_pipeline(%Pipeline{} = pipeline, attrs) do @spec update_pipeline(Pipeline.t(), attrs :: map(), User.t()) ::
{:ok, Pipeline.t()} | {:error, Pipeline.changeset()}
def update_pipeline(%Pipeline{} = pipeline, attrs, user) do
pipeline pipeline
|> Pipeline.changeset(attrs) |> Pipeline.update_changeset(attrs, user)
|> Repo.update() |> Repo.update()
end end
@ -78,15 +163,24 @@ defmodule Memex.Pipelines do
## Examples ## Examples
iex> delete_pipeline(pipeline) iex> delete_pipeline(%Pipeline{user_id: 123}, %User{id: 123})
{:ok, %Pipeline{}} {:ok, %Pipeline{}}
iex> delete_pipeline(pipeline) iex> delete_pipeline(%Pipeline{}, %User{role: :admin})
{:ok, %Pipeline{}}
iex> delete_pipeline(%Pipeline{}, %User{id: 123})
{:error, %Ecto.Changeset{}} {:error, %Ecto.Changeset{}}
""" """
def delete_pipeline(%Pipeline{} = pipeline) do @spec delete_pipeline(Pipeline.t(), User.t()) ::
Repo.delete(pipeline) {:ok, Pipeline.t()} | {:error, Pipeline.changeset()}
def delete_pipeline(%Pipeline{user_id: user_id} = pipeline, %{id: user_id}) do
pipeline |> Repo.delete()
end
def delete_pipeline(%Pipeline{} = pipeline, %{role: :admin}) do
pipeline |> Repo.delete()
end end
@doc """ @doc """
@ -94,11 +188,30 @@ defmodule Memex.Pipelines do
## Examples ## Examples
iex> change_pipeline(pipeline) iex> change_pipeline(pipeline, %User{id: 123})
%Ecto.Changeset{data: %Pipeline{}}
iex> change_pipeline(pipeline, %{title: "new title"}, %User{id: 123})
%Ecto.Changeset{data: %Pipeline{}} %Ecto.Changeset{data: %Pipeline{}}
""" """
def change_pipeline(%Pipeline{} = pipeline, attrs \\ %{}) do @spec change_pipeline(Pipeline.t(), User.t()) :: Pipeline.changeset()
Pipeline.changeset(pipeline, attrs) @spec change_pipeline(Pipeline.t(), attrs :: map(), User.t()) :: Pipeline.changeset()
def change_pipeline(%Pipeline{} = pipeline, attrs \\ %{}, user) do
pipeline |> Pipeline.update_changeset(attrs, user)
end
@doc """
Gets a canonical string representation of the `:tags` field for a Pipeline
"""
@spec get_tags_string(Pipeline.t() | Pipeline.changeset() | [String.t()] | nil) :: String.t()
def get_tags_string(nil), do: ""
def get_tags_string(tags) when tags |> is_list(), do: tags |> Enum.join(",")
def get_tags_string(%Pipeline{tags: tags}), do: tags |> get_tags_string()
def get_tags_string(%Changeset{} = changeset) do
changeset
|> Changeset.get_field(:tags)
|> get_tags_string()
end end
end end

View File

@ -1,21 +1,68 @@
defmodule Memex.Pipelines.Pipeline do defmodule Memex.Pipelines.Pipeline do
@moduledoc """
Represents a chain of considerations to take to accomplish a task
"""
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
alias Ecto.{Changeset, UUID}
alias Memex.Accounts.User
@primary_key {:id, :binary_id, autogenerate: true} @primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id @foreign_key_type :binary_id
schema "pipelines" do schema "pipelines" do
field :description, :string
field :title, :string field :title, :string
field :description, :string
field :tags, {:array, :string}
field :tags_string, :string, virtual: true
field :visibility, Ecto.Enum, values: [:public, :private, :unlisted] field :visibility, Ecto.Enum, values: [:public, :private, :unlisted]
belongs_to :user, User
timestamps() timestamps()
end end
@type t :: %__MODULE__{
title: String.t(),
description: String.t(),
tags: [String.t()] | nil,
tags_string: String.t(),
visibility: :public | :private | :unlisted,
user: User.t() | Ecto.Association.NotLoaded.t(),
user_id: User.id(),
inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t()
}
@type id :: UUID.t()
@type changeset :: Changeset.t(t())
@doc false @doc false
def changeset(pipeline, attrs) do @spec create_changeset(attrs :: map(), User.t()) :: changeset()
def create_changeset(attrs, %User{id: user_id}) do
%__MODULE__{}
|> cast(attrs, [:title, :description, :tags, :visibility])
|> change(user_id: user_id)
|> cast_tags_string(attrs)
|> validate_required([:title, :user_id, :visibility])
end
@spec update_changeset(t(), attrs :: map(), User.t()) :: changeset()
def update_changeset(%{user_id: user_id} = pipeline, attrs, %User{id: user_id}) do
pipeline pipeline
|> cast(attrs, [:title, :description, :visibility]) |> cast(attrs, [:title, :description, :tags, :visibility])
|> validate_required([:title, :description, :visibility]) |> cast_tags_string(attrs)
|> validate_required([:title, :visibility])
end end
defp cast_tags_string(changeset, %{"tags_string" => tags_string})
when tags_string |> is_binary() do
tags =
tags_string
|> String.split(",", trim: true)
|> Enum.map(fn str -> str |> String.trim() end)
|> Enum.sort()
changeset |> change(tags: tags)
end
defp cast_tags_string(changeset, _attrs), do: changeset
end end

View File

@ -0,0 +1,135 @@
defmodule MemexWeb.Components.PipelinesTableComponent do
@moduledoc """
A component that displays a list of pipelines
"""
use MemexWeb, :live_component
alias Ecto.UUID
alias Memex.{Accounts.User, Pipelines, Pipelines.Pipeline}
alias Phoenix.LiveView.{Rendered, Socket}
@impl true
@spec update(
%{
required(:id) => UUID.t(),
required(:current_user) => User.t(),
required(:pipelines) => [Pipeline.t()],
optional(any()) => any()
},
Socket.t()
) :: {:ok, Socket.t()}
def update(%{id: _id, pipelines: _pipelines, current_user: _current_user} = assigns, socket) do
socket =
socket
|> assign(assigns)
|> assign_new(:actions, fn -> [] end)
|> display_pipelines()
{:ok, socket}
end
defp display_pipelines(
%{
assigns: %{
pipelines: pipelines,
current_user: current_user,
actions: actions
}
} = socket
) do
columns =
if actions == [] or current_user |> is_nil() do
[]
else
[%{label: nil, key: :actions, sortable: false}]
end
columns = [
%{label: gettext("title"), key: :title},
%{label: gettext("description"), key: :description},
%{label: gettext("tags"), key: :tags},
%{label: gettext("visibility"), key: :visibility}
| columns
]
rows =
pipelines
|> Enum.map(fn pipeline ->
pipeline
|> get_row_data_for_pipeline(%{
columns: columns,
current_user: current_user,
actions: actions
})
end)
socket |> assign(columns: columns, rows: rows)
end
@impl true
def render(assigns) do
~H"""
<div class="w-full">
<.live_component
module={MemexWeb.Components.TableComponent}
id={@id}
columns={@columns}
rows={@rows}
/>
</div>
"""
end
@spec get_row_data_for_pipeline(Pipeline.t(), additional_data :: map()) :: map()
defp get_row_data_for_pipeline(pipeline, %{columns: columns} = additional_data) do
columns
|> Map.new(fn %{key: key} ->
{key, get_value_for_key(key, pipeline, additional_data)}
end)
end
@spec get_value_for_key(atom(), Pipeline.t(), additional_data :: map()) ::
any() | {any(), Rendered.t()}
defp get_value_for_key(:title, %{id: id, title: title}, _additional_data) do
assigns = %{id: id, title: title}
title_block = ~H"""
<.link
navigate={Routes.pipeline_show_path(Endpoint, :show, @id)}
class="link"
data-qa={"pipeline-show-#{@id}"}
>
<%= @title %>
</.link>
"""
{title, title_block}
end
defp get_value_for_key(:description, %{description: description}, _additional_data) do
assigns = %{description: description}
description_block = ~H"""
<div class="truncate max-w-sm">
<%= @description %>
</div>
"""
{description, description_block}
end
defp get_value_for_key(:tags, %{tags: tags}, _additional_data) do
tags |> Pipelines.get_tags_string()
end
defp get_value_for_key(:actions, pipeline, %{actions: actions}) do
assigns = %{actions: actions, pipeline: pipeline}
~H"""
<div class="flex justify-center items-center space-x-4">
<%= render_slot(@actions, @pipeline) %>
</div>
"""
end
defp get_value_for_key(key, pipeline, _additional_data), do: pipeline |> Map.get(key)
end

View File

@ -4,8 +4,8 @@ defmodule MemexWeb.PipelineLive.FormComponent do
alias Memex.Pipelines alias Memex.Pipelines
@impl true @impl true
def update(%{pipeline: pipeline} = assigns, socket) do def update(%{pipeline: pipeline, current_user: current_user} = assigns, socket) do
changeset = Pipelines.change_pipeline(pipeline) changeset = Pipelines.change_pipeline(pipeline, current_user)
{:ok, {:ok,
socket socket
@ -14,39 +14,56 @@ defmodule MemexWeb.PipelineLive.FormComponent do
end end
@impl true @impl true
def handle_event("validate", %{"pipeline" => pipeline_params}, socket) do def handle_event(
"validate",
%{"pipeline" => pipeline_params},
%{assigns: %{pipeline: pipeline, current_user: current_user}} = socket
) do
changeset = changeset =
socket.assigns.pipeline pipeline
|> Pipelines.change_pipeline(pipeline_params) |> Pipelines.change_pipeline(pipeline_params, current_user)
|> Map.put(:action, :validate) |> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)} {:noreply, assign(socket, :changeset, changeset)}
end end
def handle_event("save", %{"pipeline" => pipeline_params}, socket) do def handle_event(
save_pipeline(socket, socket.assigns.action, pipeline_params) "save",
%{"pipeline" => pipeline_params},
%{assigns: %{action: action}} = socket
) do
save_pipeline(socket, action, pipeline_params)
end end
defp save_pipeline(socket, :edit, pipeline_params) do defp save_pipeline(
case Pipelines.update_pipeline(socket.assigns.pipeline, pipeline_params) do %{assigns: %{pipeline: pipeline, return_to: return_to, current_user: current_user}} =
{:ok, _pipeline} -> socket,
:edit,
pipeline_params
) do
case Pipelines.update_pipeline(pipeline, pipeline_params, current_user) do
{:ok, %{title: title}} ->
{:noreply, {:noreply,
socket socket
|> put_flash(:info, "pipeline updated successfully") |> put_flash(:info, gettext("%{title} saved", title: title))
|> push_navigate(to: socket.assigns.return_to)} |> push_navigate(to: return_to)}
{:error, %Ecto.Changeset{} = changeset} -> {:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)} {:noreply, assign(socket, :changeset, changeset)}
end end
end end
defp save_pipeline(socket, :new, pipeline_params) do defp save_pipeline(
case Pipelines.create_pipeline(pipeline_params) do %{assigns: %{return_to: return_to, current_user: current_user}} = socket,
{:ok, _pipeline} -> :new,
pipeline_params
) do
case Pipelines.create_pipeline(pipeline_params, current_user) do
{:ok, %{title: title}} ->
{:noreply, {:noreply,
socket socket
|> put_flash(:info, "pipeline created successfully") |> put_flash(:info, gettext("%{title} created", title: title))
|> push_navigate(to: socket.assigns.return_to)} |> push_navigate(to: return_to)}
{:error, %Ecto.Changeset{} = changeset} -> {:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, changeset: changeset)} {:noreply, assign(socket, changeset: changeset)}

View File

@ -1,6 +1,4 @@
<div> <div class="h-full flex flex-col justify-start items-stretch space-y-4">
<h2><%= @title %></h2>
<.form <.form
:let={f} :let={f}
for={@changeset} for={@changeset}
@ -8,23 +6,44 @@
phx-target={@myself} phx-target={@myself}
phx-change="validate" phx-change="validate"
phx-submit="save" phx-submit="save"
phx-debounce="300"
class="flex flex-col justify-start items-stretch space-y-4"
> >
<%= label(f, :title) %> <%= text_input(f, :title,
<%= text_input(f, :title) %> class: "input input-primary",
placeholder: gettext("title")
) %>
<%= error_tag(f, :title) %> <%= error_tag(f, :title) %>
<%= label(f, :description) %> <%= textarea(f, :description,
<%= textarea(f, :description) %> id: "pipeline-form-description",
class: "input input-primary h-64 min-h-64",
phx_hook: "MaintainAttrs",
phx_update: "ignore",
placeholder: gettext("description")
) %>
<%= error_tag(f, :description) %> <%= error_tag(f, :description) %>
<%= label(f, :visibility) %> <%= text_input(f, :tags_string,
<%= select(f, :visibility, Ecto.Enum.values(Memex.Pipelines.Pipeline, :visibility), id: "tags-input",
prompt: "Choose a value" class: "input input-primary",
placeholder: gettext("tag1,tag2"),
phx_update: "ignore",
value: Pipelines.get_tags_string(@changeset)
) %> ) %>
<%= error_tag(f, :visibility) %> <%= error_tag(f, :tags_string) %>
<div> <div class="flex justify-center items-stretch space-x-4">
<%= submit("Save", phx_disable_with: "Saving...") %> <%= select(f, :visibility, Ecto.Enum.values(Memex.Pipelines.Pipeline, :visibility),
class: "grow input input-primary",
prompt: gettext("select privacy")
) %>
<%= submit(dgettext("actions", "save"),
phx_disable_with: gettext("saving..."),
class: "mx-auto btn btn-primary"
) %>
</div> </div>
<%= error_tag(f, :visibility) %>
</.form> </.form>
</div> </div>

View File

@ -1,46 +1,80 @@
defmodule MemexWeb.PipelineLive.Index do defmodule MemexWeb.PipelineLive.Index do
use MemexWeb, :live_view use MemexWeb, :live_view
alias Memex.{Pipelines, Pipelines.Pipeline}
alias Memex.Pipelines
alias Memex.Pipelines.Pipeline
@impl true @impl true
def mount(%{"search" => search}, _session, socket) do
{:ok, socket |> assign(search: search) |> display_pipelines()}
end
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
{:ok, assign(socket, :pipelines, list_pipelines())} {:ok, socket |> assign(search: nil) |> display_pipelines()}
end end
@impl true @impl true
def handle_params(params, _url, socket) do def handle_params(params, _url, %{assigns: %{live_action: live_action}} = socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)} {:noreply, apply_action(socket, live_action, params)}
end end
defp apply_action(socket, :edit, %{"id" => id}) do defp apply_action(%{assigns: %{current_user: current_user}} = socket, :edit, %{"id" => id}) do
%{title: title} = pipeline = Pipelines.get_pipeline!(id, current_user)
socket socket
|> assign(:page_title, "edit pipeline") |> assign(page_title: gettext("edit %{title}", title: title))
|> assign(:pipeline, Pipelines.get_pipeline!(id)) |> assign(pipeline: pipeline)
end end
defp apply_action(socket, :new, _params) do defp apply_action(%{assigns: %{current_user: %{id: current_user_id}}} = socket, :new, _params) do
socket socket
|> assign(:page_title, "new Pipeline") |> assign(page_title: gettext("new pipeline"))
|> assign(:pipeline, %Pipeline{}) |> assign(pipeline: %Pipeline{user_id: current_user_id})
end end
defp apply_action(socket, :index, _params) do defp apply_action(socket, :index, _params) do
socket socket
|> assign(:page_title, "listing pipelines") |> assign(page_title: gettext("pipelines"))
|> assign(:pipeline, nil) |> assign(search: nil)
|> assign(pipeline: nil)
|> display_pipelines()
end
defp apply_action(socket, :search, %{"search" => search}) do
socket
|> assign(page_title: gettext("pipelines"))
|> assign(search: search)
|> assign(pipeline: nil)
|> display_pipelines()
end end
@impl true @impl true
def handle_event("delete", %{"id" => id}, socket) do def handle_event("delete", %{"id" => id}, %{assigns: %{current_user: current_user}} = socket) do
pipeline = Pipelines.get_pipeline!(id) pipeline = Pipelines.get_pipeline!(id, current_user)
{:ok, _} = Pipelines.delete_pipeline(pipeline) {:ok, %{title: title}} = Pipelines.delete_pipeline(pipeline, current_user)
{:noreply, assign(socket, :pipelines, list_pipelines())} socket =
socket
|> assign(pipelines: Pipelines.list_pipelines(current_user))
|> put_flash(:info, gettext("%{title} deleted", title: title))
{:noreply, socket}
end end
defp list_pipelines do @impl true
Pipelines.list_pipelines() def handle_event("search", %{"search" => %{"search_term" => ""}}, socket) do
{:noreply, socket |> push_patch(to: Routes.pipeline_index_path(Endpoint, :index))}
end
def handle_event("search", %{"search" => %{"search_term" => search_term}}, socket) do
{:noreply,
socket |> push_patch(to: Routes.pipeline_index_path(Endpoint, :search, search_term))}
end
defp display_pipelines(%{assigns: %{current_user: current_user, search: search}} = socket)
when not (current_user |> is_nil()) do
socket |> assign(pipelines: Pipelines.list_pipelines(search, current_user))
end
defp display_pipelines(%{assigns: %{search: search}} = socket) do
socket |> assign(pipelines: Pipelines.list_public_pipelines(search))
end end
end end

View File

@ -1,10 +1,69 @@
<h1>listing pipelines</h1> <div class="mx-auto flex flex-col justify-center items-start space-y-4 max-w-3xl">
<h1 class="text-xl">
<%= gettext("pipelines") %>
</h1>
<.form
:let={f}
for={:search}
phx-change="search"
phx-submit="search"
class="self-stretch flex flex-col items-stretch"
>
<%= text_input(f, :search_term,
class: "input input-primary",
value: @search,
phx_debounce: 300,
placeholder: gettext("search")
) %>
</.form>
<%= if @pipelines |> Enum.empty?() do %>
<h1 class="self-center text-primary-500">
<%= gettext("no pipelines found") %>
</h1>
<% else %>
<.live_component
module={MemexWeb.Components.PipelinesTableComponent}
id="pipelines-index-table"
current_user={@current_user}
pipelines={@pipelines}
>
<:actions :let={pipeline}>
<%= if @current_user do %>
<.link
patch={Routes.pipeline_index_path(@socket, :edit, pipeline)}
data-qa={"pipeline-edit-#{pipeline.id}"}
>
<%= dgettext("actions", "edit") %>
</.link>
<.link
href="#"
phx-click="delete"
phx-value-id={pipeline.id}
data-confirm={dgettext("prompts", "are you sure?")}
data-qa={"delete-pipeline-#{pipeline.id}"}
>
<%= dgettext("actions", "delete") %>
</.link>
<% end %>
</:actions>
</.live_component>
<% end %>
<%= if @current_user do %>
<.link patch={Routes.pipeline_index_path(@socket, :new)} class="self-end btn btn-primary">
<%= dgettext("actions", "new pipeline") %>
</.link>
<% end %>
</div>
<%= if @live_action in [:new, :edit] do %> <%= if @live_action in [:new, :edit] do %>
<.modal return_to={Routes.pipeline_index_path(@socket, :index)}> <.modal return_to={Routes.pipeline_index_path(@socket, :index)}>
<.live_component <.live_component
module={MemexWeb.PipelineLive.FormComponent} module={MemexWeb.PipelineLive.FormComponent}
id={@pipeline.id || :new} id={@pipeline.id || :new}
current_user={@current_user}
title={@page_title} title={@page_title}
action={@live_action} action={@live_action}
pipeline={@pipeline} pipeline={@pipeline}
@ -12,53 +71,3 @@
/> />
</.modal> </.modal>
<% end %> <% end %>
<table>
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Visibility</th>
<th></th>
</tr>
</thead>
<tbody id="pipelines">
<%= for pipeline <- @pipelines do %>
<tr id={"pipeline-#{pipeline.id}"}>
<td><%= pipeline.title %></td>
<td><%= pipeline.description %></td>
<td><%= pipeline.visibility %></td>
<td>
<span>
<.link navigate={Routes.pipeline_show_path(@socket, :show, pipeline)}>
<%= dgettext("actions", "show") %>
</.link>
</span>
<span>
<.link patch={Routes.pipeline_index_path(@socket, :edit, pipeline)}>
<%= dgettext("actions", "edit") %>
</.link>
</span>
<span>
<.link
href="#"
phx-click="delete"
phx-value-id={pipeline.id}
data-confirm={dgettext("prompts", "are you sure?")}
>
<%= dgettext("actions", "delete") %>
</.link>
</span>
</td>
</tr>
<% end %>
</tbody>
</table>
<span>
<.link patch={Routes.pipeline_index_path(@socket, :new)}>
<%= dgettext("actions", "new pipeline") %>
</.link>
</span>

View File

@ -9,13 +9,33 @@ defmodule MemexWeb.PipelineLive.Show do
end end
@impl true @impl true
def handle_params(%{"id" => id}, _, socket) do def handle_params(
%{"id" => id},
_,
%{assigns: %{live_action: live_action, current_user: current_user}} = socket
) do
{:noreply, {:noreply,
socket socket
|> assign(:page_title, page_title(socket.assigns.live_action)) |> assign(:page_title, page_title(live_action))
|> assign(:pipeline, Pipelines.get_pipeline!(id))} |> assign(:pipeline, Pipelines.get_pipeline!(id, current_user))}
end end
defp page_title(:show), do: "show pipeline" @impl true
defp page_title(:edit), do: "edit pipeline" def handle_event(
"delete",
_params,
%{assigns: %{pipeline: pipeline, current_user: current_user}} = socket
) do
{:ok, %{title: title}} = Pipelines.delete_pipeline(pipeline, current_user)
socket =
socket
|> put_flash(:info, gettext("%{title} deleted", title: title))
|> push_navigate(to: Routes.pipeline_index_path(Endpoint, :index))
{:noreply, socket}
end
defp page_title(:show), do: gettext("show pipeline")
defp page_title(:edit), do: gettext("edit pipeline")
end end

View File

@ -1,10 +1,51 @@
<h1>show pipeline</h1> <div class="mx-auto flex flex-col justify-center items-stretch space-y-4 max-w-3xl">
<h1 class="text-xl">
<%= @pipeline.title %>
</h1>
<p><%= if @pipeline.tags, do: @pipeline.tags |> Enum.join(", ") %></p>
<textarea
id="show-pipeline-description"
class="input input-primary h-128 min-h-128"
phx-hook="MaintainAttrs"
phx-update="ignore"
readonly
phx-no-format
><%= @pipeline.description %></textarea>
<p class="self-end">
<%= gettext("Visibility: %{visibility}", visibility: @pipeline.visibility) %>
</p>
<div class="self-end flex space-x-4">
<.link class="btn btn-primary" patch={Routes.pipeline_index_path(@socket, :index)}>
<%= dgettext("actions", "back") %>
</.link>
<%= if @current_user do %>
<.link class="btn btn-primary" patch={Routes.pipeline_show_path(@socket, :edit, @pipeline)}>
<%= dgettext("actions", "edit") %>
</.link>
<button
type="button"
class="btn btn-primary"
phx-click="delete"
data-confirm={dgettext("prompts", "are you sure?")}
data-qa={"delete-pipeline-#{@pipeline.id}"}
>
<%= dgettext("actions", "delete") %>
</button>
<% end %>
</div>
</div>
<%= if @live_action in [:edit] do %> <%= if @live_action in [:edit] do %>
<.modal return_to={Routes.pipeline_show_path(@socket, :show, @pipeline)}> <.modal return_to={Routes.pipeline_show_path(@socket, :show, @pipeline)}>
<.live_component <.live_component
module={MemexWeb.PipelineLive.FormComponent} module={MemexWeb.PipelineLive.FormComponent}
id={@pipeline.id} id={@pipeline.id}
current_user={@current_user}
title={@page_title} title={@page_title}
action={@live_action} action={@live_action}
pipeline={@pipeline} pipeline={@pipeline}
@ -12,32 +53,3 @@
/> />
</.modal> </.modal>
<% end %> <% end %>
<ul>
<li>
<strong>Title:</strong>
<%= @pipeline.title %>
</li>
<li>
<strong>Description:</strong>
<%= @pipeline.description %>
</li>
<li>
<strong>Visibility:</strong>
<%= @pipeline.visibility %>
</li>
</ul>
<span>
<.link patch={Routes.pipeline_show_path(@socket, :edit, @pipeline)} class="button">
<%= dgettext("actions", "edit") %>
</.link>
</span>
|
<span>
<.link patch={Routes.pipeline_index_path(@socket, :index)}>
<%= dgettext("actions", "Back") %>
</.link>
</span>

View File

@ -87,7 +87,8 @@ defmodule MemexWeb.Router do
live "/context/:id", ContextLive.Show, :show live "/context/:id", ContextLive.Show, :show
live "/pipelines", PipelineLive.Index, :index live "/pipelines", PipelineLive.Index, :index
live "/pipelines/:id", PipelineLive.Show, :show live "/pipelines/:search", PipelineLive.Index, :search
live "/pipeline/:id", PipelineLive.Show, :show
end end
end end

View File

@ -10,11 +10,6 @@
msgid "" msgid ""
msgstr "" msgstr ""
#: lib/memex_web/live/pipeline_live/show.html.heex:41
#, elixir-autogen, elixir-format
msgid "Back"
msgstr ""
#: lib/memex_web/live/invite_live/index.html.heex:30 #: lib/memex_web/live/invite_live/index.html.heex:30
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Copy to clipboard" msgid "Copy to clipboard"
@ -76,7 +71,8 @@ msgstr ""
#: lib/memex_web/live/context_live/show.html.heex:37 #: lib/memex_web/live/context_live/show.html.heex:37
#: lib/memex_web/live/note_live/index.html.heex:47 #: lib/memex_web/live/note_live/index.html.heex:47
#: lib/memex_web/live/note_live/show.html.heex:37 #: lib/memex_web/live/note_live/show.html.heex:37
#: lib/memex_web/live/pipeline_live/index.html.heex:51 #: lib/memex_web/live/pipeline_live/index.html.heex:47
#: lib/memex_web/live/pipeline_live/show.html.heex:37
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "delete" msgid "delete"
msgstr "" msgstr ""
@ -90,8 +86,8 @@ msgstr ""
#: lib/memex_web/live/context_live/show.html.heex:27 #: lib/memex_web/live/context_live/show.html.heex:27
#: lib/memex_web/live/note_live/index.html.heex:38 #: lib/memex_web/live/note_live/index.html.heex:38
#: lib/memex_web/live/note_live/show.html.heex:27 #: lib/memex_web/live/note_live/show.html.heex:27
#: lib/memex_web/live/pipeline_live/index.html.heex:41 #: lib/memex_web/live/pipeline_live/index.html.heex:38
#: lib/memex_web/live/pipeline_live/show.html.heex:35 #: lib/memex_web/live/pipeline_live/show.html.heex:27
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "edit" msgid "edit"
msgstr "" msgstr ""
@ -122,7 +118,7 @@ msgstr ""
msgid "new note" msgid "new note"
msgstr "" msgstr ""
#: lib/memex_web/live/pipeline_live/index.html.heex:62 #: lib/memex_web/live/pipeline_live/index.html.heex:56
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "new pipeline" msgid "new pipeline"
msgstr "" msgstr ""
@ -140,17 +136,14 @@ msgstr ""
#: lib/memex_web/live/context_live/form_component.html.heex:42 #: lib/memex_web/live/context_live/form_component.html.heex:42
#: lib/memex_web/live/note_live/form_component.html.heex:42 #: lib/memex_web/live/note_live/form_component.html.heex:42
#: lib/memex_web/live/pipeline_live/form_component.html.heex:42
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "save" msgid "save"
msgstr "" msgstr ""
#: lib/memex_web/live/pipeline_live/index.html.heex:36
#, elixir-autogen, elixir-format
msgid "show"
msgstr ""
#: lib/memex_web/live/context_live/show.html.heex:23 #: lib/memex_web/live/context_live/show.html.heex:23
#: lib/memex_web/live/note_live/show.html.heex:23 #: lib/memex_web/live/note_live/show.html.heex:23
#: lib/memex_web/live/pipeline_live/show.html.heex:23
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "back" msgid "back"
msgstr "" msgstr ""

View File

@ -12,6 +12,7 @@ msgstr ""
#: lib/memex_web/live/context_live/form_component.ex:61 #: lib/memex_web/live/context_live/form_component.ex:61
#: lib/memex_web/live/note_live/form_component.ex:60 #: lib/memex_web/live/note_live/form_component.ex:60
#: lib/memex_web/live/pipeline_live/form_component.ex:65
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "%{title} created" msgid "%{title} created"
msgstr "" msgstr ""
@ -20,12 +21,15 @@ msgstr ""
#: lib/memex_web/live/context_live/show.ex:33 #: lib/memex_web/live/context_live/show.ex:33
#: lib/memex_web/live/note_live/index.ex:57 #: lib/memex_web/live/note_live/index.ex:57
#: lib/memex_web/live/note_live/show.ex:33 #: lib/memex_web/live/note_live/show.ex:33
#: lib/memex_web/live/pipeline_live/index.ex:57
#: lib/memex_web/live/pipeline_live/show.ex:33
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "%{title} deleted" msgid "%{title} deleted"
msgstr "" msgstr ""
#: lib/memex_web/live/context_live/form_component.ex:44 #: lib/memex_web/live/context_live/form_component.ex:44
#: lib/memex_web/live/note_live/form_component.ex:43 #: lib/memex_web/live/note_live/form_component.ex:43
#: lib/memex_web/live/pipeline_live/form_component.ex:48
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "%{title} saved" msgid "%{title} saved"
msgstr "" msgstr ""
@ -107,6 +111,7 @@ msgstr ""
#: lib/memex_web/live/context_live/show.html.heex:18 #: lib/memex_web/live/context_live/show.html.heex:18
#: lib/memex_web/live/note_live/show.html.heex:18 #: lib/memex_web/live/note_live/show.html.heex:18
#: lib/memex_web/live/pipeline_live/show.html.heex:18
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Visibility: %{visibility}" msgid "Visibility: %{visibility}"
msgstr "" msgstr ""
@ -180,6 +185,7 @@ msgstr ""
#: lib/memex_web/live/context_live/index.ex:23 #: lib/memex_web/live/context_live/index.ex:23
#: lib/memex_web/live/note_live/index.ex:23 #: lib/memex_web/live/note_live/index.ex:23
#: lib/memex_web/live/pipeline_live/index.ex:23
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "edit %{title}" msgid "edit %{title}"
msgstr "" msgstr ""
@ -298,6 +304,9 @@ msgid "notes:"
msgstr "" msgstr ""
#: lib/memex_web/components/topbar.ex:61 #: lib/memex_web/components/topbar.ex:61
#: lib/memex_web/live/pipeline_live/index.ex:35
#: lib/memex_web/live/pipeline_live/index.ex:43
#: lib/memex_web/live/pipeline_live/index.html.heex:3
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "pipelines" msgid "pipelines"
msgstr "" msgstr ""
@ -344,12 +353,14 @@ msgstr ""
#: lib/memex_web/live/context_live/form_component.html.heex:43 #: lib/memex_web/live/context_live/form_component.html.heex:43
#: lib/memex_web/live/note_live/form_component.html.heex:43 #: lib/memex_web/live/note_live/form_component.html.heex:43
#: lib/memex_web/live/pipeline_live/form_component.html.heex:43
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "saving..." msgid "saving..."
msgstr "" msgstr ""
#: lib/memex_web/live/context_live/form_component.html.heex:39 #: lib/memex_web/live/context_live/form_component.html.heex:39
#: lib/memex_web/live/note_live/form_component.html.heex:39 #: lib/memex_web/live/note_live/form_component.html.heex:39
#: lib/memex_web/live/pipeline_live/form_component.html.heex:39
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "select privacy" msgid "select privacy"
msgstr "" msgstr ""
@ -366,20 +377,24 @@ msgstr ""
#: lib/memex_web/live/context_live/form_component.html.heex:30 #: lib/memex_web/live/context_live/form_component.html.heex:30
#: lib/memex_web/live/note_live/form_component.html.heex:30 #: lib/memex_web/live/note_live/form_component.html.heex:30
#: lib/memex_web/live/pipeline_live/form_component.html.heex:30
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "tag1,tag2" msgid "tag1,tag2"
msgstr "" msgstr ""
#: lib/memex_web/components/contexts_table_component.ex:49 #: lib/memex_web/components/contexts_table_component.ex:49
#: lib/memex_web/components/notes_table_component.ex:49 #: lib/memex_web/components/notes_table_component.ex:49
#: lib/memex_web/components/pipelines_table_component.ex:49
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "tags" msgid "tags"
msgstr "" msgstr ""
#: lib/memex_web/components/contexts_table_component.ex:47 #: lib/memex_web/components/contexts_table_component.ex:47
#: lib/memex_web/components/notes_table_component.ex:47 #: lib/memex_web/components/notes_table_component.ex:47
#: lib/memex_web/components/pipelines_table_component.ex:47
#: lib/memex_web/live/context_live/form_component.html.heex:14 #: lib/memex_web/live/context_live/form_component.html.heex:14
#: lib/memex_web/live/note_live/form_component.html.heex:14 #: lib/memex_web/live/note_live/form_component.html.heex:14
#: lib/memex_web/live/pipeline_live/form_component.html.heex:14
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "title" msgid "title"
msgstr "" msgstr ""
@ -411,6 +426,7 @@ msgstr ""
#: lib/memex_web/components/contexts_table_component.ex:50 #: lib/memex_web/components/contexts_table_component.ex:50
#: lib/memex_web/components/notes_table_component.ex:50 #: lib/memex_web/components/notes_table_component.ex:50
#: lib/memex_web/components/pipelines_table_component.ex:50
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "visibility" msgid "visibility"
msgstr "" msgstr ""
@ -432,6 +448,7 @@ msgstr ""
#: lib/memex_web/live/context_live/index.html.heex:17 #: lib/memex_web/live/context_live/index.html.heex:17
#: lib/memex_web/live/note_live/index.html.heex:17 #: lib/memex_web/live/note_live/index.html.heex:17
#: lib/memex_web/live/pipeline_live/index.html.heex:17
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "search" msgid "search"
msgstr "" msgstr ""
@ -460,3 +477,29 @@ msgstr ""
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "show context" msgid "show context"
msgstr "" msgstr ""
#: lib/memex_web/components/pipelines_table_component.ex:48
#: lib/memex_web/live/pipeline_live/form_component.html.heex:23
#, elixir-autogen, elixir-format
msgid "description"
msgstr ""
#: lib/memex_web/live/pipeline_live/show.ex:40
#, elixir-autogen, elixir-format
msgid "edit pipeline"
msgstr ""
#: lib/memex_web/live/pipeline_live/index.ex:29
#, elixir-autogen, elixir-format
msgid "new pipeline"
msgstr ""
#: lib/memex_web/live/pipeline_live/index.html.heex:23
#, elixir-autogen, elixir-format
msgid "no pipelines found"
msgstr ""
#: lib/memex_web/live/pipeline_live/show.ex:39
#, elixir-autogen, elixir-format
msgid "show pipeline"
msgstr ""

View File

@ -145,7 +145,8 @@ msgstr ""
#: lib/memex_web/live/context_live/show.html.heex:34 #: lib/memex_web/live/context_live/show.html.heex:34
#: lib/memex_web/live/note_live/index.html.heex:44 #: lib/memex_web/live/note_live/index.html.heex:44
#: lib/memex_web/live/note_live/show.html.heex:34 #: lib/memex_web/live/note_live/show.html.heex:34
#: lib/memex_web/live/pipeline_live/index.html.heex:49 #: lib/memex_web/live/pipeline_live/index.html.heex:44
#: lib/memex_web/live/pipeline_live/show.html.heex:34
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "are you sure?" msgid "are you sure?"
msgstr "" msgstr ""

View File

@ -6,9 +6,30 @@ defmodule Memex.Repo.Migrations.CreatePipelines do
add :id, :binary_id, primary_key: true add :id, :binary_id, primary_key: true
add :title, :string add :title, :string
add :description, :text add :description, :text
add :tags, {:array, :citext}
add :visibility, :string add :visibility, :string
add :user_id, references(:users, on_delete: :delete_all, type: :binary_id)
timestamps() timestamps()
end end
flush()
execute """
ALTER TABLE pipelines
ADD COLUMN search tsvector
GENERATED ALWAYS AS (
setweight(to_tsvector('english', coalesce(title, '')), 'A') ||
setweight(to_tsvector('english', coalesce(immutable_array_to_string(tags, ' '), '')), 'B') ||
setweight(to_tsvector('english', coalesce(description, '')), 'C')
) STORED
"""
execute("CREATE INDEX pipelines_trgm_idx ON pipelines USING GIN (search)")
end
def down do
drop table(:pipelines)
end end
end end

View File

@ -1,68 +1,120 @@
defmodule Memex.PipelinesTest do defmodule Memex.PipelinesTest do
use Memex.DataCase use Memex.DataCase
import Memex.PipelinesFixtures
alias Memex.Pipelines alias Memex.{Pipelines, Pipelines.Pipeline}
@moduletag :pipelines_test
@invalid_attrs %{description: nil, tag: nil, title: nil, visibility: nil}
describe "pipelines" do describe "pipelines" do
alias Memex.Pipelines.Pipeline setup do
[user: user_fixture()]
import Memex.PipelinesFixtures
@invalid_attrs %{description: nil, title: nil, visibility: nil}
test "list_pipelines/0 returns all pipelines" do
pipeline = pipeline_fixture()
assert Pipelines.list_pipelines() == [pipeline]
end end
test "get_pipeline!/1 returns the pipeline with given id" do test "list_pipelines/1 returns all pipelines for a user", %{user: user} do
pipeline = pipeline_fixture() pipeline_a = pipeline_fixture(%{title: "a", visibility: :public}, user)
assert Pipelines.get_pipeline!(pipeline.id) == pipeline pipeline_b = pipeline_fixture(%{title: "b", visibility: :unlisted}, user)
pipeline_c = pipeline_fixture(%{title: "c", visibility: :private}, user)
assert Pipelines.list_pipelines(user) == [pipeline_a, pipeline_b, pipeline_c]
end end
test "create_pipeline/1 with valid data creates a pipeline" do test "list_public_pipelines/0 returns public pipelines", %{user: user} do
valid_attrs = %{description: "some description", title: "some title", visibility: :public} public_pipeline = pipeline_fixture(%{visibility: :public}, user)
pipeline_fixture(%{visibility: :unlisted}, user)
pipeline_fixture(%{visibility: :private}, user)
assert Pipelines.list_public_pipelines() == [public_pipeline]
end
assert {:ok, %Pipeline{} = pipeline} = Pipelines.create_pipeline(valid_attrs) test "get_pipeline!/1 returns the pipeline with given id", %{user: user} do
pipeline = pipeline_fixture(%{visibility: :public}, user)
assert Pipelines.get_pipeline!(pipeline.id, user) == pipeline
pipeline = pipeline_fixture(%{visibility: :unlisted}, user)
assert Pipelines.get_pipeline!(pipeline.id, user) == pipeline
pipeline = pipeline_fixture(%{visibility: :private}, user)
assert Pipelines.get_pipeline!(pipeline.id, user) == pipeline
end
test "get_pipeline!/1 only returns unlisted or public pipelines for other users", %{
user: user
} do
another_user = user_fixture()
pipeline = pipeline_fixture(%{visibility: :public}, another_user)
assert Pipelines.get_pipeline!(pipeline.id, user) == pipeline
pipeline = pipeline_fixture(%{visibility: :unlisted}, another_user)
assert Pipelines.get_pipeline!(pipeline.id, user) == pipeline
pipeline = pipeline_fixture(%{visibility: :private}, another_user)
assert_raise Ecto.NoResultsError, fn ->
Pipelines.get_pipeline!(pipeline.id, user)
end
end
test "create_pipeline/1 with valid data creates a pipeline", %{user: user} do
valid_attrs = %{
"description" => "some description",
"tags_string" => "tag1,tag2",
"title" => "some title",
"visibility" => :public
}
assert {:ok, %Pipeline{} = pipeline} = Pipelines.create_pipeline(valid_attrs, user)
assert pipeline.description == "some description" assert pipeline.description == "some description"
assert pipeline.tags == ["tag1", "tag2"]
assert pipeline.title == "some title" assert pipeline.title == "some title"
assert pipeline.visibility == :public assert pipeline.visibility == :public
end end
test "create_pipeline/1 with invalid data returns error changeset" do test "create_pipeline/1 with invalid data returns error changeset", %{user: user} do
assert {:error, %Ecto.Changeset{}} = Pipelines.create_pipeline(@invalid_attrs) assert {:error, %Ecto.Changeset{}} = Pipelines.create_pipeline(@invalid_attrs, user)
end end
test "update_pipeline/2 with valid data updates the pipeline" do test "update_pipeline/2 with valid data updates the pipeline", %{user: user} do
pipeline = pipeline_fixture() pipeline = pipeline_fixture(user)
update_attrs = %{ update_attrs = %{
description: "some updated description", "description" => "some updated description",
title: "some updated title", "tags_string" => "tag1,tag2",
visibility: :private "title" => "some updated title",
"visibility" => :private
} }
assert {:ok, %Pipeline{} = pipeline} = Pipelines.update_pipeline(pipeline, update_attrs) assert {:ok, %Pipeline{} = pipeline} =
Pipelines.update_pipeline(pipeline, update_attrs, user)
assert pipeline.description == "some updated description" assert pipeline.description == "some updated description"
assert pipeline.tags == ["tag1", "tag2"]
assert pipeline.title == "some updated title" assert pipeline.title == "some updated title"
assert pipeline.visibility == :private assert pipeline.visibility == :private
end end
test "update_pipeline/2 with invalid data returns error changeset" do test "update_pipeline/2 with invalid data returns error changeset", %{user: user} do
pipeline = pipeline_fixture() pipeline = pipeline_fixture(user)
assert {:error, %Ecto.Changeset{}} = Pipelines.update_pipeline(pipeline, @invalid_attrs)
assert pipeline == Pipelines.get_pipeline!(pipeline.id) assert {:error, %Ecto.Changeset{}} =
Pipelines.update_pipeline(pipeline, @invalid_attrs, user)
assert pipeline == Pipelines.get_pipeline!(pipeline.id, user)
end end
test "delete_pipeline/1 deletes the pipeline" do test "delete_pipeline/1 deletes the pipeline", %{user: user} do
pipeline = pipeline_fixture() pipeline = pipeline_fixture(user)
assert {:ok, %Pipeline{}} = Pipelines.delete_pipeline(pipeline) assert {:ok, %Pipeline{}} = Pipelines.delete_pipeline(pipeline, user)
assert_raise Ecto.NoResultsError, fn -> Pipelines.get_pipeline!(pipeline.id) end assert_raise Ecto.NoResultsError, fn -> Pipelines.get_pipeline!(pipeline.id, user) end
end end
test "change_pipeline/1 returns a pipeline changeset" do test "delete_pipeline/1 deletes the pipeline for an admin user", %{user: user} do
pipeline = pipeline_fixture() admin_user = admin_fixture()
assert %Ecto.Changeset{} = Pipelines.change_pipeline(pipeline) pipeline = pipeline_fixture(user)
assert {:ok, %Pipeline{}} = Pipelines.delete_pipeline(pipeline, admin_user)
assert_raise Ecto.NoResultsError, fn -> Pipelines.get_pipeline!(pipeline.id, user) end
end
test "change_pipeline/1 returns a pipeline changeset", %{user: user} do
pipeline = pipeline_fixture(user)
assert %Ecto.Changeset{} = Pipelines.change_pipeline(pipeline, user)
end end
end end
end end

View File

@ -4,26 +4,36 @@ defmodule MemexWeb.PipelineLiveTest do
import Phoenix.LiveViewTest import Phoenix.LiveViewTest
import Memex.PipelinesFixtures import Memex.PipelinesFixtures
@create_attrs %{description: "some description", title: "some title", visibility: :public} @create_attrs %{
@update_attrs %{ "description" => "some description",
description: "some updated description", "tags_string" => "tag1",
title: "some updated title", "title" => "some title",
visibility: :private "visibility" => :public
}
@update_attrs %{
"description" => "some updated description",
"tags_string" => "tag1,tag2",
"title" => "some updated title",
"visibility" => :private
}
@invalid_attrs %{
"description" => nil,
"tags_string" => "",
"title" => nil,
"visibility" => nil
} }
@invalid_attrs %{description: nil, title: nil, visibility: nil}
defp create_pipeline(_) do defp create_pipeline(%{user: user}) do
pipeline = pipeline_fixture() [pipeline: pipeline_fixture(user)]
%{pipeline: pipeline}
end end
describe "Index" do describe "Index" do
setup [:create_pipeline] setup [:register_and_log_in_user, :create_pipeline]
test "lists all pipelines", %{conn: conn, pipeline: pipeline} do test "lists all pipelines", %{conn: conn, pipeline: pipeline} do
{:ok, _index_live, html} = live(conn, Routes.pipeline_index_path(conn, :index)) {:ok, _index_live, html} = live(conn, Routes.pipeline_index_path(conn, :index))
assert html =~ "listing pipelines" assert html =~ "pipelines"
assert html =~ pipeline.description assert html =~ pipeline.description
end end
@ -45,15 +55,15 @@ defmodule MemexWeb.PipelineLiveTest do
|> render_submit() |> render_submit()
|> follow_redirect(conn, Routes.pipeline_index_path(conn, :index)) |> follow_redirect(conn, Routes.pipeline_index_path(conn, :index))
assert html =~ "pipeline created successfully" assert html =~ "#{@create_attrs |> Map.get("title")} created"
assert html =~ "some description" assert html =~ "some description"
end end
test "updates pipeline in listing", %{conn: conn, pipeline: pipeline} do test "updates pipeline in listing", %{conn: conn, pipeline: pipeline} do
{:ok, index_live, _html} = live(conn, Routes.pipeline_index_path(conn, :index)) {:ok, index_live, _html} = live(conn, Routes.pipeline_index_path(conn, :index))
assert index_live |> element("#pipeline-#{pipeline.id} a", "edit") |> render_click() =~ assert index_live |> element("[data-qa=\"pipeline-edit-#{pipeline.id}\"]") |> render_click() =~
"edit pipeline" "edit"
assert_patch(index_live, Routes.pipeline_index_path(conn, :edit, pipeline)) assert_patch(index_live, Routes.pipeline_index_path(conn, :edit, pipeline))
@ -67,20 +77,23 @@ defmodule MemexWeb.PipelineLiveTest do
|> render_submit() |> render_submit()
|> follow_redirect(conn, Routes.pipeline_index_path(conn, :index)) |> follow_redirect(conn, Routes.pipeline_index_path(conn, :index))
assert html =~ "pipeline updated successfully" assert html =~ "#{@update_attrs |> Map.get("title")} saved"
assert html =~ "some updated description" assert html =~ "some updated description"
end end
test "deletes pipeline in listing", %{conn: conn, pipeline: pipeline} do test "deletes pipeline in listing", %{conn: conn, pipeline: pipeline} do
{:ok, index_live, _html} = live(conn, Routes.pipeline_index_path(conn, :index)) {:ok, index_live, _html} = live(conn, Routes.pipeline_index_path(conn, :index))
assert index_live |> element("#pipeline-#{pipeline.id} a", "delete") |> render_click() assert index_live
|> element("[data-qa=\"delete-pipeline-#{pipeline.id}\"]")
|> render_click()
refute has_element?(index_live, "#pipeline-#{pipeline.id}") refute has_element?(index_live, "#pipeline-#{pipeline.id}")
end end
end end
describe "show" do describe "show" do
setup [:create_pipeline] setup [:register_and_log_in_user, :create_pipeline]
test "displays pipeline", %{conn: conn, pipeline: pipeline} do test "displays pipeline", %{conn: conn, pipeline: pipeline} do
{:ok, _show_live, html} = live(conn, Routes.pipeline_show_path(conn, :show, pipeline)) {:ok, _show_live, html} = live(conn, Routes.pipeline_show_path(conn, :show, pipeline))
@ -92,8 +105,7 @@ defmodule MemexWeb.PipelineLiveTest do
test "updates pipeline within modal", %{conn: conn, pipeline: pipeline} do test "updates pipeline within modal", %{conn: conn, pipeline: pipeline} do
{:ok, show_live, _html} = live(conn, Routes.pipeline_show_path(conn, :show, pipeline)) {:ok, show_live, _html} = live(conn, Routes.pipeline_show_path(conn, :show, pipeline))
assert show_live |> element("a", "edit") |> render_click() =~ assert show_live |> element("a", "edit") |> render_click() =~ "edit"
"edit pipeline"
assert_patch(show_live, Routes.pipeline_show_path(conn, :edit, pipeline)) assert_patch(show_live, Routes.pipeline_show_path(conn, :edit, pipeline))
@ -107,8 +119,20 @@ defmodule MemexWeb.PipelineLiveTest do
|> render_submit() |> render_submit()
|> follow_redirect(conn, Routes.pipeline_show_path(conn, :show, pipeline)) |> follow_redirect(conn, Routes.pipeline_show_path(conn, :show, pipeline))
assert html =~ "pipeline updated successfully" assert html =~ "#{@update_attrs |> Map.get("title")} saved"
assert html =~ "some updated description" assert html =~ "some updated description"
end end
test "deletes pipeline", %{conn: conn, pipeline: pipeline} do
{:ok, show_live, _html} = live(conn, Routes.pipeline_show_path(conn, :show, pipeline))
{:ok, index_live, _html} =
show_live
|> element("[data-qa=\"delete-pipeline-#{pipeline.id}\"]")
|> render_click()
|> follow_redirect(conn, Routes.pipeline_index_path(conn, :index))
refute has_element?(index_live, "#pipeline-#{pipeline.id}")
end
end end
end end

View File

@ -3,19 +3,23 @@ defmodule Memex.PipelinesFixtures do
This module defines test helpers for creating This module defines test helpers for creating
entities via the `Memex.Pipelines` context. entities via the `Memex.Pipelines` context.
""" """
alias Memex.{Accounts.User, Pipelines, Pipelines.Pipeline}
@doc """ @doc """
Generate a pipeline. Generate a pipeline.
""" """
def pipeline_fixture(attrs \\ %{}) do @spec pipeline_fixture(User.t()) :: Pipeline.t()
@spec pipeline_fixture(attrs :: map(), User.t()) :: Pipeline.t()
def pipeline_fixture(attrs \\ %{}, user) do
{:ok, pipeline} = {:ok, pipeline} =
attrs attrs
|> Enum.into(%{ |> Enum.into(%{
description: "some description", description: "some description",
tag: [],
title: "some title", title: "some title",
visibility: :public visibility: :private
}) })
|> Memex.Pipelines.create_pipeline() |> Pipelines.create_pipeline(user)
pipeline pipeline
end end