From 39d53ee7638dc13b30bd8d2b0b53c18e53badd21 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Sat, 1 Feb 2020 10:57:57 -0500 Subject: [PATCH] fix mandatory descriptions for filters, missing public option --- lib/philomena/filters.ex | 4 ++-- lib/philomena/filters/filter.ex | 16 +++++++++++++--- .../templates/filter/_form.html.slime | 18 +++++++++++++++++- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/lib/philomena/filters.ex b/lib/philomena/filters.ex index 907281a4..41bf1b29 100644 --- a/lib/philomena/filters.ex +++ b/lib/philomena/filters.ex @@ -66,7 +66,7 @@ defmodule Philomena.Filters do """ def create_filter(user, attrs \\ %{}) do %Filter{user_id: user.id} - |> Filter.changeset(attrs) + |> Filter.creation_changeset(attrs) |> Repo.insert() end @@ -84,7 +84,7 @@ defmodule Philomena.Filters do """ def update_filter(%Filter{} = filter, attrs) do filter - |> Filter.changeset(attrs) + |> Filter.update_changeset(attrs) |> Repo.update() end diff --git a/lib/philomena/filters/filter.ex b/lib/philomena/filters/filter.ex index 5e8b3928..b59aa9a5 100644 --- a/lib/philomena/filters/filter.ex +++ b/lib/philomena/filters/filter.ex @@ -11,7 +11,7 @@ defmodule Philomena.Filters.Filter do belongs_to :user, User field :name, :string - field :description, :string + field :description, :string, default: "" field :system, :boolean field :public, :boolean field :hidden_complex_str, :string @@ -29,7 +29,7 @@ defmodule Philomena.Filters.Filter do @doc false def changeset(filter, attrs) do user = - filter + change(filter).data |> Repo.preload(:user) |> Map.get(:user) @@ -44,7 +44,7 @@ defmodule Philomena.Filters.Filter do ]) |> TagList.propagate_tag_list(:spoilered_tag_list, :spoilered_tag_ids) |> TagList.propagate_tag_list(:hidden_tag_list, :hidden_tag_ids) - |> validate_required([:name, :description]) + |> validate_required([:name]) |> validate_my_downvotes(:spoilered_complex_str) |> validate_my_downvotes(:hidden_complex_str) |> Search.validate_search(:spoilered_complex_str, user) @@ -52,6 +52,16 @@ defmodule Philomena.Filters.Filter do |> unsafe_validate_unique([:user_id, :name], Repo) end + def creation_changeset(filter, attrs) do + filter + |> cast(attrs, [:public]) + |> changeset(attrs) + end + + def update_changeset(filter, attrs) do + changeset(filter, attrs) + end + def deletion_changeset(filter) do filter |> change() diff --git a/lib/philomena_web/templates/filter/_form.html.slime b/lib/philomena_web/templates/filter/_form.html.slime index 7f8ed180..77965906 100644 --- a/lib/philomena_web/templates/filter/_form.html.slime +++ b/lib/philomena_web/templates/filter/_form.html.slime @@ -65,4 +65,20 @@ strong> WARNING: ' This filter is applied along with your tag filters. Tag filters may hide images that you mean to filter more precisely here. Double-check to make sure they don't interfere. - = submit "Save Filter", class: "button" + = cond do + - is_nil(input_value(f, :id)) -> + .field + = checkbox f, :public, class: "checkbox" + = label f, :public + + .fieldlabel + strong You probably do not want to check this unless you know what you are doing - it cannot be changed later + | . Pulic filters can be shared with other users and used by them; if you make changes to a filter, it will update all users of that filter. + + - input_value(f, :public) == true -> + .fieldlabel + strong Changes made to this filter will affect anyone else using this filter! Check your changes, and make sure your changes don't change the filter's intent. + + - true -> + + = submit "Save Filter", class: "button"