From 56f35dd11e0f10949172607694cafe43e92b016c Mon Sep 17 00:00:00 2001 From: William Mathewson Date: Mon, 24 Jun 2024 16:02:24 +0100 Subject: [PATCH] docs: use Hash#dig in docs ERB If one uses the suggested layout for the ViewComponent default layout, navigating to `localhost:3000/rails/view_components` will throw an error due to the deeply nested nature of the Lookbook params hash. Using `dig` allows for safe access into the hashes without causing problems if any of the keys don't exist. This sweeps other instances of deep hash access. Hash#dig was introduced in Ruby 2.3, so will be available in all supported versions of Lookbook. --- docs/src/_extend/other/data.md | 4 ++-- docs/src/_guide/previews/display.md | 12 ++++++------ docs/src/_guide/previews/layouts.md | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/src/_extend/other/data.md b/docs/src/_extend/other/data.md index 88b73cf53..53c1394e7 100644 --- a/docs/src/_extend/other/data.md +++ b/docs/src/_extend/other/data.md @@ -56,7 +56,7 @@ title: Global Data Store ...
<%%= yield %>
-

Built by <%%= params[:lookbook][:data][:company_name] %>

+

Built by <%%= params.dig(:lookbook, :data, :company_name) %>

... ``` @@ -77,4 +77,4 @@ title: Global Data Store <%% end %> ``` -<% end %> \ No newline at end of file +<% end %> diff --git a/docs/src/_guide/previews/display.md b/docs/src/_guide/previews/display.md index 9115c8504..da8e01b49 100644 --- a/docs/src/_guide/previews/display.md +++ b/docs/src/_guide/previews/display.md @@ -29,17 +29,17 @@ title: Display Options end ``` - Display param values can then be accessed via the `params` hash in your preview layout using `params[:lookbook][:display][]`: + Display param values can then be accessed via the `params` hash in your preview layout using `params.dig(:lookbook, :display, )`: ```html - + Preview Layout -
- <%% if params[:lookbook][:display][:wrapper] == true %> +
+ <%% if params.dig(:lookbook, :display, :wrapper) == true %>
<%%= yield %>
<%% else %> <%%= yield %> <%% end %>
@@ -136,7 +136,7 @@ title: Display Options - + <%%= yield %> @@ -163,4 +163,4 @@ title: Display Options Overridden values like these **are not persisted** across other previews. <% end %> -<% end %> \ No newline at end of file +<% end %> diff --git a/docs/src/_guide/previews/layouts.md b/docs/src/_guide/previews/layouts.md index 7d053b7a1..67bf8566f 100644 --- a/docs/src/_guide/previews/layouts.md +++ b/docs/src/_guide/previews/layouts.md @@ -56,7 +56,7 @@ title: Preview Layouts ```erb - "> + "> Component Preview @@ -67,7 +67,7 @@ title: Preview Layouts
+ max-width: <%%= params.dig(:lookbook, :display, :max_width) || "100%" %> "> <%%= yield %>
@@ -86,4 +86,4 @@ title: Preview Layouts <% s.with_block_api_module_methods(:preview_helper) %> -<% end %> \ No newline at end of file +<% end %>