From d184ee0817b67d0b089b5141e2d52163bb599e89 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Sat, 11 Jan 2020 14:24:30 -0500 Subject: [PATCH] copy and customize filters --- .../controllers/filter_controller.ex | 24 +++++++++++++++++++ .../templates/filter/_filter.html.slime | 2 ++ .../templates/filter/_form.html.slime | 3 ++- .../templates/profile/_commission.html.slime | 6 ++--- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/philomena_web/controllers/filter_controller.ex b/lib/philomena_web/controllers/filter_controller.ex index 217a70f4..8e9a0e2f 100644 --- a/lib/philomena_web/controllers/filter_controller.ex +++ b/lib/philomena_web/controllers/filter_controller.ex @@ -58,6 +58,30 @@ defmodule PhilomenaWeb.FilterController do ) end + def new(conn, %{"based_on" => filter_id}) do + # The last line here is a hack to get Ecto to save a new + # model instead of trying to update the existing one. + filter = + Filter + |> where(id: ^filter_id) + |> where( + [f], + f.system == true or f.public == true or f.user_id == ^conn.assigns.current_user.id + ) + |> Repo.one() + |> Kernel.||(%Filter{}) + |> TagList.assign_tag_list(:spoilered_tag_ids, :spoilered_tag_list) + |> TagList.assign_tag_list(:hidden_tag_ids, :hidden_tag_list) + |> Map.put(:__meta__, %Ecto.Schema.Metadata{ + state: :built, + source: "filters", + schema: Filter + }) + + changeset = Filters.change_filter(filter) + render(conn, "new.html", title: "New Filter", changeset: %{changeset | action: nil}) + end + def new(conn, _params) do changeset = Filters.change_filter(%Filter{}) render(conn, "new.html", title: "New Filter", changeset: changeset) diff --git a/lib/philomena_web/templates/filter/_filter.html.slime b/lib/philomena_web/templates/filter/_filter.html.slime index 8be1bb96..859608f7 100644 --- a/lib/philomena_web/templates/filter/_filter.html.slime +++ b/lib/philomena_web/templates/filter/_filter.html.slime @@ -21,6 +21,8 @@ li = link "View this filter", to: Routes.filter_path(@conn, :show, @filter), class: "button" + li + = link "Copy and Customize", to: Routes.filter_path(@conn, :new, based_on: @filter), class: "button" = if can?(@conn, :edit, @filter) do li diff --git a/lib/philomena_web/templates/filter/_form.html.slime b/lib/philomena_web/templates/filter/_form.html.slime index 9d19bb34..3cbbbc2d 100644 --- a/lib/philomena_web/templates/filter/_form.html.slime +++ b/lib/philomena_web/templates/filter/_form.html.slime @@ -1,5 +1,6 @@ .form = form_for @filter, @route, fn f -> + - IO.inspect @filter.data = if @filter.action do #error_explanation ' Oops, something went wrong! Please check the errors below. @@ -65,4 +66,4 @@ 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" \ No newline at end of file + = submit "Save Filter", class: "button" diff --git a/lib/philomena_web/templates/profile/_commission.html.slime b/lib/philomena_web/templates/profile/_commission.html.slime index 0c8cc427..d884529a 100644 --- a/lib/philomena_web/templates/profile/_commission.html.slime +++ b/lib/philomena_web/templates/profile/_commission.html.slime @@ -9,9 +9,9 @@ strong> Price Range: - {min, max} = Enum.min_max_by(@user.commission.items, & &1.base_price) | $ - => Decimal.round(min.base_price, 2) |> Decimal.to_string() + => Decimal.round(min.base_price || 0, 2) |> Decimal.to_string() | - $ - => Decimal.round(max.base_price, 2) |> Decimal.to_string() + => Decimal.round(max.base_price || 0, 2) |> Decimal.to_string() ' USD br @@ -29,4 +29,4 @@ => link "Click here", to: Routes.profile_commission_path(@conn, :new, @user) ' to set it up. - - true -> \ No newline at end of file + - true ->