From 0fd2240f984b40a2330624c30ff05adb2425bc1d Mon Sep 17 00:00:00 2001 From: Adamo Maisano Date: Wed, 22 Jan 2020 11:06:27 -0500 Subject: [PATCH] Limit data sent down from controller to cms modules - use more efficient empty checker in cms templates - instead of sending full conn.query_params, send limited set of relevant data --- apps/cms/lib/helpers.ex | 32 +++++++--- apps/cms/lib/page.ex | 30 ++++----- apps/cms/lib/page/basic.ex | 6 +- apps/cms/lib/page/landing.ex | 6 +- apps/cms/lib/page/project.ex | 6 +- apps/cms/lib/page/project_update.ex | 6 +- apps/cms/lib/partial/paragraph.ex | 64 +++++++++---------- apps/cms/lib/partial/paragraph/accordion.ex | 6 +- .../partial/paragraph/accordion_section.ex | 6 +- apps/cms/lib/partial/paragraph/column.ex | 6 +- .../cms/lib/partial/paragraph/column_multi.ex | 8 +-- .../cms/lib/partial/paragraph/content_list.ex | 6 +- .../lib/partial/paragraph/description_list.ex | 8 +-- apps/cms/lib/partial/paragraph/fare_card.ex | 10 +-- apps/cms/lib/repo.ex | 6 +- apps/cms/test/helpers_test.exs | 11 +++- .../controllers/project_controller.ex | 4 +- 17 files changed, 123 insertions(+), 98 deletions(-) diff --git a/apps/cms/lib/helpers.ex b/apps/cms/lib/helpers.ex index 76f7a2ef58..6933d6af6a 100644 --- a/apps/cms/lib/helpers.ex +++ b/apps/cms/lib/helpers.ex @@ -117,20 +117,34 @@ defmodule CMS.Helpers do end end + @doc """ + Gathers information about desired view mode based on incoming + query parameters. Key presence is enough to count as true. + - page: whether the page is in preview mode (drafts are rendered) + - paragraphs: whether unpublished paragraphs should render + """ + @spec preview_opts(map) :: Keyword.t() + def preview_opts(query_params \\ %{}) do + [ + page: Map.has_key?(query_params, "preview"), + paragraphs: Map.has_key?(query_params, "paragraphs") + ] + end + @doc """ Expects raw JSON data for a CMS object that contains a paragraphs field. This field value will always be a list of potential paragraphs. """ - @spec parse_paragraphs(map, map, String.t()) :: [Paragraph.t()] - def parse_paragraphs(data, query_params \\ %{}, target_field \\ "field_paragraphs") do + @spec parse_paragraphs(map, Keyword.t(), String.t()) :: [Paragraph.t()] + def parse_paragraphs(data, preview_opts \\ [], target_field \\ "field_paragraphs") do data |> Map.get(target_field, []) - |> Enum.filter(&show_paragraph?(&1, query_params)) - |> Enum.map(&Paragraph.from_api(&1, query_params)) + |> Enum.filter(&show_paragraph?(&1, preview_opts)) + |> Enum.map(&Paragraph.from_api(&1, preview_opts)) end - @spec show_paragraph?(map, map) :: boolean - defp show_paragraph?(field_data, query_params) + @spec show_paragraph?(map, Keyword.t()) :: boolean + defp show_paragraph?(field_data, preview_opts) # Reusable paragraphs instance aren't automatically removed when their child # paragraphs are deleted from the database, so catch that here. @@ -141,18 +155,18 @@ defmodule CMS.Helpers do # Reusable paragraphs are not directly renderable since they act as instance containers. # However, these instances can be unpublished. If unpublished, stop and return false. # If published, continue checking the nested child paragraph for publish status. - defp show_paragraph?(%{"field_reusable_paragraph" => [child]} = parent, query_params) do + defp show_paragraph?(%{"field_reusable_paragraph" => [child]} = parent, preview_opts) do %{"status" => [parent_status]} = parent %{"paragraphs" => [paragraph]} = child case parent_status do %{"value" => false} -> false - _ -> show_paragraph?(paragraph, query_params) + _ -> show_paragraph?(paragraph, preview_opts) end end # In "preview" mode, allow unpublished paragraphs to be rendered if requested - defp show_paragraph?(_, %{"preview" => _, "paragraphs" => _}) do + defp show_paragraph?(_, preview: true, paragraphs: true) do true end diff --git a/apps/cms/lib/page.ex b/apps/cms/lib/page.ex index b83c41123f..9d8227b390 100644 --- a/apps/cms/lib/page.ex +++ b/apps/cms/lib/page.ex @@ -32,44 +32,44 @@ defmodule CMS.Page do @doc """ Expects parsed json from drupal CMS. Should be one item (not array of items) """ - @spec from_api(map, map) :: t - def from_api(data, query_params \\ %{}) do + @spec from_api(map, Keyword.t()) :: t + def from_api(data, preview_opts \\ []) do data - |> parse(query_params) + |> parse(preview_opts) |> fetch_content_lists() end - defp parse(%{"type" => [%{"target_id" => "event"}]} = api_data, _query_params) do + defp parse(%{"type" => [%{"target_id" => "event"}]} = api_data, _preview_opts) do Event.from_api(api_data) end - defp parse(%{"type" => [%{"target_id" => "landing_page"}]} = api_data, query_params) do - Landing.from_api(api_data, query_params) + defp parse(%{"type" => [%{"target_id" => "landing_page"}]} = api_data, preview_opts) do + Landing.from_api(api_data, preview_opts) end - defp parse(%{"type" => [%{"target_id" => "news_entry"}]} = api_data, _query_params) do + defp parse(%{"type" => [%{"target_id" => "news_entry"}]} = api_data, _preview_opts) do NewsEntry.from_api(api_data) end - defp parse(%{"type" => [%{"target_id" => "person"}]} = api_data, _query_params) do + defp parse(%{"type" => [%{"target_id" => "person"}]} = api_data, _preview_opts) do Person.from_api(api_data) end - defp parse(%{"type" => [%{"target_id" => "project"}]} = api_data, query_params) do - Project.from_api(api_data, query_params) + defp parse(%{"type" => [%{"target_id" => "project"}]} = api_data, preview_opts) do + Project.from_api(api_data, preview_opts) end - defp parse(%{"type" => [%{"target_id" => "project_update"}]} = api_data, query_params) do - ProjectUpdate.from_api(api_data, query_params) + defp parse(%{"type" => [%{"target_id" => "project_update"}]} = api_data, preview_opts) do + ProjectUpdate.from_api(api_data, preview_opts) end - defp parse(%{"type" => [%{"target_id" => "redirect"}]} = api_data, _query_params) do + defp parse(%{"type" => [%{"target_id" => "redirect"}]} = api_data, _preview_opts) do Redirect.from_api(api_data) end # For all other node/content types from the CMS, use a common struct/template - defp parse(%{"type" => [%{"target_type" => "node_type"}]} = api_data, query_params) do - Basic.from_api(api_data, query_params) + defp parse(%{"type" => [%{"target_type" => "node_type"}]} = api_data, preview_opts) do + Basic.from_api(api_data, preview_opts) end @spec fetch_content_lists(t) :: t diff --git a/apps/cms/lib/page/basic.ex b/apps/cms/lib/page/basic.ex index ef1d4b6566..fbded4ab86 100644 --- a/apps/cms/lib/page/basic.ex +++ b/apps/cms/lib/page/basic.ex @@ -32,13 +32,13 @@ defmodule CMS.Page.Basic do breadcrumbs: [Util.Breadcrumb.t()] } - @spec from_api(map, map) :: t - def from_api(%{} = data, query_params \\ %{}) do + @spec from_api(map, Keyword.t()) :: t + def from_api(%{} = data, preview_opts \\ []) do %__MODULE__{ id: int_or_string_to_int(field_value(data, "nid")), title: field_value(data, "title") || "", body: parse_body(data), - paragraphs: parse_paragraphs(data, query_params), + paragraphs: parse_paragraphs(data, preview_opts), sidebar_menu: parse_menu_links(data), breadcrumbs: Breadcrumbs.build(data) } diff --git a/apps/cms/lib/page/landing.ex b/apps/cms/lib/page/landing.ex index 2b1740b769..6bc45b4d41 100644 --- a/apps/cms/lib/page/landing.ex +++ b/apps/cms/lib/page/landing.ex @@ -35,15 +35,15 @@ defmodule CMS.Page.Landing do breadcrumbs: [Util.Breadcrumb.t()] } - @spec from_api(map, map) :: t - def from_api(%{} = data, query_params \\ %{}) do + @spec from_api(map, Keyword.t()) :: t + def from_api(%{} = data, preview_opts \\ []) do %__MODULE__{ id: field_value(data, "nid"), title: field_value(data, "title"), hero_desktop: parse_image(data, "field_hero_image_desktop"), hero_mobile: parse_image(data, "field_hero_image_mobile"), hero_mobile_2x: parse_image(data, "field_hero_image_mobile_2x"), - paragraphs: parse_paragraphs(data, query_params), + paragraphs: parse_paragraphs(data, preview_opts), subtitle: field_value(data, "field_subtitle"), breadcrumbs: Breadcrumbs.build(data) } diff --git a/apps/cms/lib/page/project.ex b/apps/cms/lib/page/project.ex index fe1eb25865..b8541ccec0 100644 --- a/apps/cms/lib/page/project.ex +++ b/apps/cms/lib/page/project.ex @@ -62,8 +62,8 @@ defmodule CMS.Page.Project do path_alias: String.t() | nil } - @spec from_api(map, map) :: t - def from_api(%{} = data, query_params \\ %{}) do + @spec from_api(map, Keyword.t()) :: t + def from_api(%{} = data, preview_opts \\ []) do %__MODULE__{ id: int_or_string_to_int(field_value(data, "nid")), body: parse_body(data), @@ -74,7 +74,7 @@ defmodule CMS.Page.Project do files: parse_files(data, "field_files"), media_email: field_value(data, "field_media_email"), media_phone: field_value(data, "field_media_phone"), - paragraphs: parse_paragraphs(data, query_params), + paragraphs: parse_paragraphs(data, preview_opts), photo_gallery: parse_images(data, "field_photo_gallery"), start_year: field_value(data, "field_start_year"), status: field_value(data, "field_project_status"), diff --git a/apps/cms/lib/page/project_update.ex b/apps/cms/lib/page/project_update.ex index 7b9b29967a..70cf0263e1 100644 --- a/apps/cms/lib/page/project_update.ex +++ b/apps/cms/lib/page/project_update.ex @@ -47,15 +47,15 @@ defmodule CMS.Page.ProjectUpdate do title: String.t() } - @spec from_api(map, map) :: t - def from_api(%{} = data, query_params \\ %{}) do + @spec from_api(map, Keyword.t()) :: t + def from_api(%{} = data, preview_opts \\ []) do {project_id, project_alias} = parse_project(data) %__MODULE__{ id: int_or_string_to_int(field_value(data, "nid")), body: parse_body(data), image: parse_image(data, "field_image"), - paragraphs: parse_paragraphs(data, query_params), + paragraphs: parse_paragraphs(data, preview_opts), photo_gallery: parse_images(data, "field_photo_gallery"), posted_on: parse_date(data, "field_posted_on"), project_id: project_id, diff --git a/apps/cms/lib/partial/paragraph.ex b/apps/cms/lib/partial/paragraph.ex index f8dc04f0b4..000bd20ad4 100644 --- a/apps/cms/lib/partial/paragraph.ex +++ b/apps/cms/lib/partial/paragraph.ex @@ -98,84 +98,84 @@ defmodule CMS.Partial.Paragraph do | TitleCardSet | Unknown - @spec from_api(map, map) :: t - def from_api(data, query_params \\ %{}) + @spec from_api(map, Keyword.t()) :: t + def from_api(data, preview_opts \\ []) - def from_api(%{"type" => [%{"target_id" => "entity_reference"}]} = para, _query_params) do + def from_api(%{"type" => [%{"target_id" => "entity_reference"}]} = para, _preview_opts) do Callout.from_api(para) end - def from_api(%{"type" => [%{"target_id" => "multi_column_header"}]} = para, _query_params) do + def from_api(%{"type" => [%{"target_id" => "multi_column_header"}]} = para, _preview_opts) do ColumnMultiHeader.from_api(para) end - def from_api(%{"type" => [%{"target_id" => "multi_column"}]} = para, query_params) do - ColumnMulti.from_api(para, query_params) + def from_api(%{"type" => [%{"target_id" => "multi_column"}]} = para, preview_opts) do + ColumnMulti.from_api(para, preview_opts) end - def from_api(%{"type" => [%{"target_id" => "column"}]} = para, query_params) do - Column.from_api(para, query_params) + def from_api(%{"type" => [%{"target_id" => "column"}]} = para, preview_opts) do + Column.from_api(para, preview_opts) end - def from_api(%{"type" => [%{"target_id" => "content_list"}]} = para, query_params) do - ContentList.from_api(para, query_params) + def from_api(%{"type" => [%{"target_id" => "content_list"}]} = para, preview_opts) do + ContentList.from_api(para, preview_opts) end - def from_api(%{"type" => [%{"target_id" => "custom_html"}]} = para, _query_params) do + def from_api(%{"type" => [%{"target_id" => "custom_html"}]} = para, _preview_opts) do CustomHTML.from_api(para) end - def from_api(%{"type" => [%{"target_id" => "definition"}]} = para, _query_params) do + def from_api(%{"type" => [%{"target_id" => "definition"}]} = para, _preview_opts) do Description.from_api(para) end - def from_api(%{"type" => [%{"target_id" => "description_list"}]} = para, query_params) do - DescriptionList.from_api(para, query_params) + def from_api(%{"type" => [%{"target_id" => "description_list"}]} = para, preview_opts) do + DescriptionList.from_api(para, preview_opts) end - def from_api(%{"type" => [%{"target_id" => "fare_card"}]} = para, query_params) do - FareCard.from_api(para, query_params) + def from_api(%{"type" => [%{"target_id" => "fare_card"}]} = para, preview_opts) do + FareCard.from_api(para, preview_opts) end - def from_api(%{"type" => [%{"target_id" => "files_grid"}]} = para, _query_params) do + def from_api(%{"type" => [%{"target_id" => "files_grid"}]} = para, _preview_opts) do FilesGrid.from_api(para) end - def from_api(%{"type" => [%{"target_id" => "people_grid"}]} = para, _query_params) do + def from_api(%{"type" => [%{"target_id" => "people_grid"}]} = para, _preview_opts) do PeopleGrid.from_api(para) end - def from_api(%{"type" => [%{"target_id" => "photo_gallery"}]} = para, _query_params) do + def from_api(%{"type" => [%{"target_id" => "photo_gallery"}]} = para, _preview_opts) do PhotoGallery.from_api(para) end - def from_api(%{"type" => [%{"target_id" => "tabs"}]} = para, query_params) do - Accordion.from_api(para, query_params) + def from_api(%{"type" => [%{"target_id" => "tabs"}]} = para, preview_opts) do + Accordion.from_api(para, preview_opts) end - def from_api(%{"type" => [%{"target_id" => "tab"}]} = para, query_params) do - AccordionSection.from_api(para, query_params) + def from_api(%{"type" => [%{"target_id" => "tab"}]} = para, preview_opts) do + AccordionSection.from_api(para, preview_opts) end - def from_api(%{"type" => [%{"target_id" => "title_card"}]} = para, _query_params) do + def from_api(%{"type" => [%{"target_id" => "title_card"}]} = para, _preview_opts) do DescriptiveLink.from_api(para) end - def from_api(%{"type" => [%{"target_id" => "title_card_set"}]} = para, _query_params) do + def from_api(%{"type" => [%{"target_id" => "title_card_set"}]} = para, _preview_opts) do TitleCardSet.from_api(para) end @doc "This ΒΆ type has a single paragraph reference within. Get the nested paragraph." - def from_api(%{"type" => [%{"target_id" => "from_library"}]} = para, query_params) do - parse_library_item(para, query_params) + def from_api(%{"type" => [%{"target_id" => "from_library"}]} = para, preview_opts) do + parse_library_item(para, preview_opts) end @doc "For directly accessing a reusable paragraph (from paragraphs API endpoint)" - def from_api(%{"paragraphs" => [para]}, query_params) do - from_api(para, query_params) + def from_api(%{"paragraphs" => [para]}, preview_opts) do + from_api(para, preview_opts) end - def from_api(unknown_paragraph_type, _query_params) do + def from_api(unknown_paragraph_type, _preview_opts) do Unknown.from_api(unknown_paragraph_type) end @@ -193,13 +193,13 @@ defmodule CMS.Partial.Paragraph do # Pass through the nested paragraph and host ID @spec parse_library_item(map, map) :: t - defp parse_library_item(data, query_params) do + defp parse_library_item(data, preview_opts) do data |> Map.get("field_reusable_paragraph") |> List.first() |> Map.get("paragraphs") |> List.first() |> Map.put("parent_id", Map.get(data, "parent_id")) - |> from_api(query_params) + |> from_api(preview_opts) end end diff --git a/apps/cms/lib/partial/paragraph/accordion.ex b/apps/cms/lib/partial/paragraph/accordion.ex index f4f2272e09..1a32c57410 100644 --- a/apps/cms/lib/partial/paragraph/accordion.ex +++ b/apps/cms/lib/partial/paragraph/accordion.ex @@ -21,11 +21,11 @@ defmodule CMS.Partial.Paragraph.Accordion do sections: [AccordionSection.t()] } - @spec from_api(map, map) :: t - def from_api(data, query_params \\ %{}) do + @spec from_api(map, Keyword.t()) :: t + def from_api(data, preview_opts \\ []) do %__MODULE__{ display: field_value(data, "field_tabs_display"), - sections: parse_paragraphs(data, query_params, "field_tabs") + sections: parse_paragraphs(data, preview_opts, "field_tabs") } end end diff --git a/apps/cms/lib/partial/paragraph/accordion_section.ex b/apps/cms/lib/partial/paragraph/accordion_section.ex index acc8f0934f..fcd879f35b 100644 --- a/apps/cms/lib/partial/paragraph/accordion_section.ex +++ b/apps/cms/lib/partial/paragraph/accordion_section.ex @@ -27,12 +27,12 @@ defmodule CMS.Partial.Paragraph.AccordionSection do content: [Paragraph.t()] } - @spec from_api(map, map) :: t - def from_api(%{} = data, query_params \\ %{}) do + @spec from_api(map, Keyword.t()) :: t + def from_api(%{} = data, preview_opts \\ []) do %__MODULE__{ title: field_value(data, "field_label"), prefix: "cms-#{field_value(data, "id")}", - content: parse_paragraphs(data, query_params, "field_content") + content: parse_paragraphs(data, preview_opts, "field_content") } end end diff --git a/apps/cms/lib/partial/paragraph/column.ex b/apps/cms/lib/partial/paragraph/column.ex index 658a266fe8..a8455dd251 100644 --- a/apps/cms/lib/partial/paragraph/column.ex +++ b/apps/cms/lib/partial/paragraph/column.ex @@ -12,10 +12,10 @@ defmodule CMS.Partial.Paragraph.Column do paragraphs: [Paragraph.t()] } - @spec from_api(map, map) :: t - def from_api(data, query_params \\ %{}) do + @spec from_api(map, Keyword.t()) :: t + def from_api(data, preview_opts \\ []) do %__MODULE__{ - paragraphs: parse_paragraphs(data, query_params, "field_content") + paragraphs: parse_paragraphs(data, preview_opts, "field_content") } end end diff --git a/apps/cms/lib/partial/paragraph/column_multi.ex b/apps/cms/lib/partial/paragraph/column_multi.ex index b0c78701ac..516ad9fac5 100644 --- a/apps/cms/lib/partial/paragraph/column_multi.ex +++ b/apps/cms/lib/partial/paragraph/column_multi.ex @@ -18,11 +18,11 @@ defmodule CMS.Partial.Paragraph.ColumnMulti do right_rail: boolean } - @spec from_api(map, map) :: t - def from_api(data, query_params \\ %{}) do + @spec from_api(map, Keyword.t()) :: t + def from_api(data, preview_opts \\ []) do %__MODULE__{ - header: data |> parse_paragraphs(query_params, "field_multi_column_header") |> List.first(), - columns: parse_paragraphs(data, query_params, "field_column"), + header: data |> parse_paragraphs(preview_opts, "field_multi_column_header") |> List.first(), + columns: parse_paragraphs(data, preview_opts, "field_column"), display_options: field_value(data, "field_display_options"), right_rail: field_value(data, "field_right_rail") } diff --git a/apps/cms/lib/partial/paragraph/content_list.ex b/apps/cms/lib/partial/paragraph/content_list.ex index 65b17df625..0f860bf982 100644 --- a/apps/cms/lib/partial/paragraph/content_list.ex +++ b/apps/cms/lib/partial/paragraph/content_list.ex @@ -39,8 +39,8 @@ defmodule CMS.Partial.Paragraph.ContentList do cta: map() } - @spec from_api(map, map) :: t - def from_api(data, query_params \\ %{}) do + @spec from_api(map, Keyword.t()) :: t + def from_api(data, preview_opts \\ []) do type = data |> field_value("field_content_type") @@ -80,7 +80,7 @@ defmodule CMS.Partial.Paragraph.ContentList do recipe = combine(ingredients) %__MODULE__{ - header: data |> parse_paragraphs(query_params, "field_multi_column_header") |> List.first(), + header: data |> parse_paragraphs(preview_opts, "field_multi_column_header") |> List.first(), right_rail: field_value(data, "field_right_rail"), ingredients: ingredients, recipe: recipe, diff --git a/apps/cms/lib/partial/paragraph/description_list.ex b/apps/cms/lib/partial/paragraph/description_list.ex index 3ee9b8a02c..b872685fe8 100644 --- a/apps/cms/lib/partial/paragraph/description_list.ex +++ b/apps/cms/lib/partial/paragraph/description_list.ex @@ -14,11 +14,11 @@ defmodule CMS.Partial.Paragraph.DescriptionList do descriptions: [Description.t()] } - @spec from_api(map, map) :: t - def from_api(data, query_params \\ %{}) do + @spec from_api(map, Keyword.t()) :: t + def from_api(data, preview_opts \\ []) do %__MODULE__{ - header: data |> parse_paragraphs(query_params, "field_multi_column_header") |> List.first(), - descriptions: parse_paragraphs(data, query_params, "field_definition") + header: data |> parse_paragraphs(preview_opts, "field_multi_column_header") |> List.first(), + descriptions: parse_paragraphs(data, preview_opts, "field_definition") } end end diff --git a/apps/cms/lib/partial/paragraph/fare_card.ex b/apps/cms/lib/partial/paragraph/fare_card.ex index 387ba0ccb4..a8478ef909 100644 --- a/apps/cms/lib/partial/paragraph/fare_card.ex +++ b/apps/cms/lib/partial/paragraph/fare_card.ex @@ -20,10 +20,10 @@ defmodule CMS.Partial.Paragraph.FareCard do show_media: boolean() } - @spec from_api(map, map) :: t - def from_api(data, query_params \\ %{}) do + @spec from_api(map, Keyword.t()) :: t + def from_api(data, preview_opts \\ []) do with fare_token <- fare_token(data), - note <- note(data, query_params) do + note <- note(data, preview_opts) do %__MODULE__{ fare_token: fare_token, note: note, @@ -39,9 +39,9 @@ defmodule CMS.Partial.Paragraph.FareCard do |> parse_token() end - defp note(data, query_params) do + defp note(data, preview_opts) do data - |> parse_paragraphs(query_params, "field_fare_notes") + |> parse_paragraphs(preview_opts, "field_fare_notes") # There is only ever 1 note element |> List.first() end diff --git a/apps/cms/lib/repo.ex b/apps/cms/lib/repo.ex index 9d183ccdd9..7ab67879b1 100644 --- a/apps/cms/lib/repo.ex +++ b/apps/cms/lib/repo.ex @@ -10,6 +10,8 @@ defmodule CMS.Repo do use RepoCache, ttl: :timer.minutes(1) + import CMS.Helpers, only: [preview_opts: 1] + alias CMS.Partial.{ Banner, Paragraph, @@ -30,7 +32,7 @@ defmodule CMS.Repo do @spec get_page(String.t(), map) :: Page.t() | {:error, API.error()} def get_page(path, query_params \\ %{}) do case view_or_preview(path, query_params) do - {:ok, api_data} -> Page.from_api(api_data, query_params) + {:ok, api_data} -> Page.from_api(api_data, preview_opts(query_params)) {:error, error} -> {:error, error} end end @@ -303,7 +305,7 @@ defmodule CMS.Repo do def get_paragraph(path, query_params \\ %{}) do case view_or_preview(path, query_params) do {:ok, api_data} -> - Paragraph.from_api(api_data, query_params) + Paragraph.from_api(api_data, preview_opts(query_params)) {:error, {:redirect, _status, to: new_path}} -> get_paragraph(new_path, query_params) diff --git a/apps/cms/test/helpers_test.exs b/apps/cms/test/helpers_test.exs index 0ac0ca5307..7be4c2b940 100644 --- a/apps/cms/test/helpers_test.exs +++ b/apps/cms/test/helpers_test.exs @@ -354,8 +354,8 @@ defmodule CMS.HelpersTest do ] } - query_params = %{"preview" => nil, "paragraphs" => true} - parsed_map = parse_paragraphs(map_data, query_params) + preview_opts = [preview: true, paragraphs: true] + parsed_map = parse_paragraphs(map_data, preview_opts) assert parsed_map == [ %CMS.Partial.Paragraph.CustomHTML{ @@ -520,4 +520,11 @@ defmodule CMS.HelpersTest do assert [%{id: "limited_service", mode: nil, group: "custom"}] = routes(tags) end end + + describe "preview_opts/1" do + test "parses conn.query_params map for CMS preview flags" do + query_params = %{"preview" => nil, "foo" => "bar"} + assert [page: true, paragraphs: false] = preview_opts(query_params) + end + end end diff --git a/apps/site/lib/site_web/controllers/project_controller.ex b/apps/site/lib/site_web/controllers/project_controller.ex index 5c8f9a1dac..bb2dbd7e6d 100644 --- a/apps/site/lib/site_web/controllers/project_controller.ex +++ b/apps/site/lib/site_web/controllers/project_controller.ex @@ -4,6 +4,8 @@ defmodule SiteWeb.ProjectController do """ use SiteWeb, :controller + import CMS.Helpers, only: [preview_opts: 1] + alias CMS.{Partial.Teaser, Repo} alias CMS.Page.{Project, ProjectUpdate} alias Plug.Conn @@ -95,7 +97,7 @@ defmodule SiteWeb.ProjectController do "/projects" |> Path.join(project_alias) - |> get_page_fn.(conn.query_params) + |> get_page_fn.(preview_opts(conn.query_params)) |> case do %Project{} = project -> breadcrumbs = [