Skip to content

Commit

Permalink
Limit data sent down from controller to cms modules
Browse files Browse the repository at this point in the history
- use more efficient empty checker in cms templates
- instead of sending full conn.query_params, send limited set of relevant data
  • Loading branch information
amaisano committed Jan 22, 2020
1 parent aa04315 commit 0fd2240
Show file tree
Hide file tree
Showing 17 changed files with 123 additions and 98 deletions.
32 changes: 23 additions & 9 deletions apps/cms/lib/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down
30 changes: 15 additions & 15 deletions apps/cms/lib/page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions apps/cms/lib/page/basic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions apps/cms/lib/page/landing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions apps/cms/lib/page/project.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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"),
Expand Down
6 changes: 3 additions & 3 deletions apps/cms/lib/page/project_update.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
64 changes: 32 additions & 32 deletions apps/cms/lib/partial/paragraph.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
6 changes: 3 additions & 3 deletions apps/cms/lib/partial/paragraph/accordion.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions apps/cms/lib/partial/paragraph/accordion_section.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions apps/cms/lib/partial/paragraph/column.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions apps/cms/lib/partial/paragraph/column_multi.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
Loading

0 comments on commit 0fd2240

Please sign in to comment.