various filters fixes

This commit is contained in:
byte[] 2019-12-22 16:47:16 -05:00
parent b7ee2f9c50
commit d2916e2a9b
8 changed files with 39 additions and 15 deletions

View file

@ -101,7 +101,9 @@ defmodule Philomena.Filters do
""" """
def delete_filter(%Filter{} = filter) do def delete_filter(%Filter{} = filter) do
Repo.delete(filter) filter
|> Filter.deletion_changeset()
|> Repo.delete()
end end
@doc """ @doc """
@ -132,6 +134,9 @@ defmodule Philomena.Filters do
union(recent_filters, ^user_filters) union(recent_filters, ^user_filters)
|> Repo.all() |> Repo.all()
|> Enum.sort_by(fn f ->
Enum.find_index(user.recent_filter_ids, fn id -> f.id == id end)
end)
|> Enum.group_by( |> Enum.group_by(
fn fn
%{recent: "t"} -> "Recent Filters" %{recent: "t"} -> "Recent Filters"

View file

@ -45,6 +45,12 @@ defmodule Philomena.Filters.Filter do
|> unsafe_validate_unique([:user_id, :name], Repo) |> unsafe_validate_unique([:user_id, :name], Repo)
end end
def deletion_changeset(filter) do
filter
|> change()
|> foreign_key_constraint(:id, name: :fk_rails_d2b4c2768f)
end
def hidden_tags_changeset(filter, hidden_tag_ids) do def hidden_tags_changeset(filter, hidden_tag_ids) do
change(filter, hidden_tag_ids: hidden_tag_ids) change(filter, hidden_tag_ids: hidden_tag_ids)
end end

View file

@ -196,13 +196,14 @@ defimpl Canada.Can, for: [Atom, Philomena.Users.User] do
def can?(%User{id: id}, :show, %Conversation{to_id: id}), do: true def can?(%User{id: id}, :show, %Conversation{to_id: id}), do: true
def can?(%User{id: id}, :show, %Conversation{from_id: id}), do: true def can?(%User{id: id}, :show, %Conversation{from_id: id}), do: true
# View filters they own and system filters # View filters they own and public/system filters
def can?(_user, :show, %Filter{system: true}), do: true def can?(_user, :show, %Filter{system: true}), do: true
def can?(_user, :show, %Filter{public: true}), do: true
def can?(%User{}, action, Filter) when action in [:index, :new, :create], do: true def can?(%User{}, action, Filter) when action in [:index, :new, :create], do: true
def can?(%User{id: id}, action, %Filter{user_id: id}) when action in [:show, :edit, :update], do: true def can?(%User{id: id}, action, %Filter{user_id: id}) when action in [:show, :edit, :update], do: true
# Edit filters they own # Edit filters they own
def can?(%User{id: id}, action, %Filter{user_id: id}) when action in [:edit, :update], do: true def can?(%User{id: id}, action, %Filter{user_id: id}) when action in [:edit, :update, :delete], do: true
# View user links they've created # View user links they've created
def can?(%User{id: id}, :create_links, %User{id: id}), do: true def can?(%User{id: id}, :create_links, %User{id: id}), do: true

View file

@ -35,8 +35,7 @@ defmodule PhilomenaWeb.CommentController do
) )
rendered = rendered =
comments.entries Renderer.render_collection(comments.entries, conn)
|> Renderer.render_collection(conn)
comments = comments =
%{comments | entries: Enum.zip(rendered, comments.entries)} %{comments | entries: Enum.zip(rendered, comments.entries)}

View file

@ -94,10 +94,16 @@ defmodule PhilomenaWeb.FilterController do
def delete(conn, _params) do def delete(conn, _params) do
filter = conn.assigns.filter filter = conn.assigns.filter
{:ok, _filter} = Filters.delete_filter(filter) case Filters.delete_filter(filter) do
{:ok, _filter} ->
conn
|> put_flash(:info, "Filter deleted successfully.")
|> redirect(to: Routes.filter_path(conn, :index))
conn _error ->
|> put_flash(:info, "Filter deleted successfully.") conn
|> redirect(to: Routes.filter_path(conn, :index)) |> put_flash(:error, "Filter is still in use, not deleted.")
|> redirect(to: Routes.filter_path(conn, :show, filter))
end
end end
end end

View file

@ -6,7 +6,10 @@
p p
' Maintained by ' Maintained by
= render PhilomenaWeb.UserAttributionView, "_user.html", object: @filter, conn: @conn = render PhilomenaWeb.UserAttributionView, "_user.html", object: @filter, conn: @conn
' .
= if @filter.system do
p
' Maintained by staff
.filter-options .filter-options
ul ul
@ -17,7 +20,11 @@
= length(@filter.hidden_tag_ids) = length(@filter.hidden_tag_ids)
li li
= link("View this filter", to: Routes.filter_path(@conn, :show, @filter), class: "button") = link "View this filter", to: Routes.filter_path(@conn, :show, @filter), class: "button"
= if can?(@conn, :edit, @filter) do
li
= link "Edit this filter", to: Routes.filter_path(@conn, :edit, @filter), class: "button"
= if @filter.id == @conn.assigns.current_filter.id do = if @filter.id == @conn.assigns.current_filter.id do
li li

View file

@ -22,10 +22,10 @@ h1
= if can?(@conn, :edit, @filter) do = if can?(@conn, :edit, @filter) do
li li
= link("Edit this filter", to: Routes.filter_path(@conn, :edit, @filter)) = link "Edit this filter", to: Routes.filter_path(@conn, :edit, @filter), class: "button"
= if can?(@conn, :delete, @filter) do = if can?(@conn, :delete, @filter) do
= button_to "Destroy this filter", Routes.filter_path(@conn, :delete, @filter), method: "delete", class: "button", data: [confirm: "Are you sure?"] = button_to "Destroy this filter", Routes.filter_path(@conn, :delete, @filter), method: "delete", class: "button", data: [confirm: "Are you really, really sure?"]
= if @filter.user do = if @filter.user do
p.filter-maintainer p.filter-maintainer

View file

@ -8,7 +8,7 @@ h1 Sign in
p = link "Forgot your password?", to: Routes.pow_reset_password_reset_password_path(@conn, :new) p = link "Forgot your password?", to: Routes.pow_reset_password_reset_password_path(@conn, :new)
.field .field
= text_input f, :email, class: "input", required: true, placeholder: "Email" = text_input f, :email, class: "input", required: true, placeholder: "Email", autofocus: true
= error_tag f, :email = error_tag f, :email
.field .field
@ -25,4 +25,4 @@ p
strong strong
' Haven't read the ' Haven't read the
a<> href="/pages/rules" site rules a<> href="/pages/rules" site rules
' lately? Make sure you read them before posting or editing metadata! ' lately? Make sure you read them before posting or editing metadata!