From d2916e2a9b8e0f3341ba0092eb6ef1c9f1b219ea Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Sun, 22 Dec 2019 16:47:16 -0500 Subject: [PATCH] various filters fixes --- lib/philomena/filters.ex | 7 ++++++- lib/philomena/filters/filter.ex | 6 ++++++ lib/philomena/users/ability.ex | 5 +++-- .../controllers/comment_controller.ex | 3 +-- lib/philomena_web/controllers/filter_controller.ex | 14 ++++++++++---- .../templates/filter/_filter.html.slime | 11 +++++++++-- lib/philomena_web/templates/filter/show.html.slime | 4 ++-- .../templates/pow/session/new.html.slime | 4 ++-- 8 files changed, 39 insertions(+), 15 deletions(-) diff --git a/lib/philomena/filters.ex b/lib/philomena/filters.ex index 0b8b8e91..f1892c70 100644 --- a/lib/philomena/filters.ex +++ b/lib/philomena/filters.ex @@ -101,7 +101,9 @@ defmodule Philomena.Filters do """ def delete_filter(%Filter{} = filter) do - Repo.delete(filter) + filter + |> Filter.deletion_changeset() + |> Repo.delete() end @doc """ @@ -132,6 +134,9 @@ defmodule Philomena.Filters do union(recent_filters, ^user_filters) |> Repo.all() + |> Enum.sort_by(fn f -> + Enum.find_index(user.recent_filter_ids, fn id -> f.id == id end) + end) |> Enum.group_by( fn %{recent: "t"} -> "Recent Filters" diff --git a/lib/philomena/filters/filter.ex b/lib/philomena/filters/filter.ex index 70bcc8fc..79db7c6c 100644 --- a/lib/philomena/filters/filter.ex +++ b/lib/philomena/filters/filter.ex @@ -45,6 +45,12 @@ defmodule Philomena.Filters.Filter do |> unsafe_validate_unique([:user_id, :name], Repo) 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 change(filter, hidden_tag_ids: hidden_tag_ids) end diff --git a/lib/philomena/users/ability.ex b/lib/philomena/users/ability.ex index 164b5803..691d8ea7 100644 --- a/lib/philomena/users/ability.ex +++ b/lib/philomena/users/ability.ex @@ -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{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{public: true}), 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 # 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 def can?(%User{id: id}, :create_links, %User{id: id}), do: true diff --git a/lib/philomena_web/controllers/comment_controller.ex b/lib/philomena_web/controllers/comment_controller.ex index da8fca99..1cad838b 100644 --- a/lib/philomena_web/controllers/comment_controller.ex +++ b/lib/philomena_web/controllers/comment_controller.ex @@ -35,8 +35,7 @@ defmodule PhilomenaWeb.CommentController do ) rendered = - comments.entries - |> Renderer.render_collection(conn) + Renderer.render_collection(comments.entries, conn) comments = %{comments | entries: Enum.zip(rendered, comments.entries)} diff --git a/lib/philomena_web/controllers/filter_controller.ex b/lib/philomena_web/controllers/filter_controller.ex index a0a68bd5..e27d658f 100644 --- a/lib/philomena_web/controllers/filter_controller.ex +++ b/lib/philomena_web/controllers/filter_controller.ex @@ -94,10 +94,16 @@ defmodule PhilomenaWeb.FilterController do def delete(conn, _params) do 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 - |> put_flash(:info, "Filter deleted successfully.") - |> redirect(to: Routes.filter_path(conn, :index)) + _error -> + conn + |> put_flash(:error, "Filter is still in use, not deleted.") + |> redirect(to: Routes.filter_path(conn, :show, filter)) + end end end diff --git a/lib/philomena_web/templates/filter/_filter.html.slime b/lib/philomena_web/templates/filter/_filter.html.slime index 36617ac3..8be1bb96 100644 --- a/lib/philomena_web/templates/filter/_filter.html.slime +++ b/lib/philomena_web/templates/filter/_filter.html.slime @@ -6,7 +6,10 @@ p ' Maintained by = render PhilomenaWeb.UserAttributionView, "_user.html", object: @filter, conn: @conn - ' . + + = if @filter.system do + p + ' Maintained by staff .filter-options ul @@ -17,7 +20,11 @@ = length(@filter.hidden_tag_ids) 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 li diff --git a/lib/philomena_web/templates/filter/show.html.slime b/lib/philomena_web/templates/filter/show.html.slime index 8ecd5f5a..15608482 100644 --- a/lib/philomena_web/templates/filter/show.html.slime +++ b/lib/philomena_web/templates/filter/show.html.slime @@ -22,10 +22,10 @@ h1 = if can?(@conn, :edit, @filter) do 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 - = 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 p.filter-maintainer diff --git a/lib/philomena_web/templates/pow/session/new.html.slime b/lib/philomena_web/templates/pow/session/new.html.slime index 24b51147..f9baa1aa 100644 --- a/lib/philomena_web/templates/pow/session/new.html.slime +++ b/lib/philomena_web/templates/pow/session/new.html.slime @@ -8,7 +8,7 @@ h1 Sign in p = link "Forgot your password?", to: Routes.pow_reset_password_reset_password_path(@conn, :new) .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 .field @@ -25,4 +25,4 @@ p strong ' Haven't read the a<> href="/pages/rules" site rules - ' lately? Make sure you read them before posting or editing metadata! \ No newline at end of file + ' lately? Make sure you read them before posting or editing metadata!