From d03c1d7e5b98fd75b416ffa1b1948c862aaae055 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Sat, 2 May 2020 12:58:11 -0400 Subject: [PATCH] fixes #76: filter public conversion route --- lib/philomena/filters.ex | 6 +++++ lib/philomena/filters/filter.ex | 4 ++++ .../controllers/filter/public_controller.ex | 23 +++++++++++++++++++ lib/philomena_web/router.ex | 4 +++- .../templates/filter/edit.html.slime | 22 +++++++++++++++++- 5 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 lib/philomena_web/controllers/filter/public_controller.ex diff --git a/lib/philomena/filters.ex b/lib/philomena/filters.ex index 41bf1b29..d5a56fba 100644 --- a/lib/philomena/filters.ex +++ b/lib/philomena/filters.ex @@ -88,6 +88,12 @@ defmodule Philomena.Filters do |> Repo.update() end + def make_filter_public(%Filter{} = filter) do + filter + |> Filter.public_changeset() + |> Repo.update() + end + @doc """ Deletes a Filter. diff --git a/lib/philomena/filters/filter.ex b/lib/philomena/filters/filter.ex index b72ae9ea..1ef4dd22 100644 --- a/lib/philomena/filters/filter.ex +++ b/lib/philomena/filters/filter.ex @@ -68,6 +68,10 @@ defmodule Philomena.Filters.Filter do |> foreign_key_constraint(:id, name: :fk_rails_d2b4c2768f) end + def public_changeset(filter) do + change(filter, public: true) + end + def hidden_tags_changeset(filter, hidden_tag_ids) do change(filter, hidden_tag_ids: hidden_tag_ids) end diff --git a/lib/philomena_web/controllers/filter/public_controller.ex b/lib/philomena_web/controllers/filter/public_controller.ex new file mode 100644 index 00000000..80e23308 --- /dev/null +++ b/lib/philomena_web/controllers/filter/public_controller.ex @@ -0,0 +1,23 @@ +defmodule PhilomenaWeb.Filter.PublicController do + use PhilomenaWeb, :controller + + alias Philomena.Filters + alias Philomena.Filters.Filter + + plug PhilomenaWeb.CanaryMapPlug, create: :edit + plug :load_and_authorize_resource, model: Filter, id_name: "filter_id", persisted: true + + def create(conn, _params) do + case Filters.make_filter_public(conn.assigns.filter) do + {:ok, filter} -> + conn + |> put_flash(:info, "Successfully made filter public.") + |> redirect(to: Routes.filter_path(conn, :show, filter)) + + _error -> + conn + |> put_flash(:error, "Couldn't make filter public!") + |> redirect(to: Routes.filter_path(conn, :show, conn.assigns.filter)) + end + end +end diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index 7d4fe2cc..5a3c33a0 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -457,7 +457,9 @@ defmodule PhilomenaWeb.Router do resources "/current", CurrentController, only: [:update], singleton: true end - resources "/filters", FilterController + resources "/filters", FilterController do + resources "/public", Filter.PublicController, only: [:create], singleton: true + end resources "/profiles", ProfileController, only: [:show] do resources "/reports", Profile.ReportController, only: [:new, :create] diff --git a/lib/philomena_web/templates/filter/edit.html.slime b/lib/philomena_web/templates/filter/edit.html.slime index 14a90897..4ec94253 100644 --- a/lib/philomena_web/templates/filter/edit.html.slime +++ b/lib/philomena_web/templates/filter/edit.html.slime @@ -1,2 +1,22 @@ h2 Editing Filter -= render PhilomenaWeb.FilterView, "_form.html", filter: @changeset, route: Routes.filter_path(@conn, :update, @filter) \ No newline at end of file += render PhilomenaWeb.FilterView, "_form.html", filter: @changeset, route: Routes.filter_path(@conn, :update, @filter) + += if not @filter.public and not @filter.system do + br + br + + input.toggle-box id="public-tools" type="checkbox" checked=false + label for="public-tools" Advanced Options + .toggle-box-container + .toggle-box-container__content + p + ' This filter is currently private and only accessible to you. If you + ' click the button below, this will make the filter public and + ' accessible to other users. + + p + ' This process is + strong irreversible + ' . Once you make a filter public, you cannot make it private again. + + = button_to "Convert to Public Filter", Routes.filter_public_path(@conn, :create, @filter), class: "button", method: "create", data: [confirm: "Are you really, really sure?"]