From f9d334fb365c45fab9e9d34ed7525fe93286f5e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sun, 22 Jan 2023 16:32:34 +0100 Subject: [PATCH 1/3] Move admin pages index to a component This way it'll be easier to write tests for it. --- .../pages/index_component.html.erb | 54 ++++++++++++++++++ .../pages/index_component.rb | 7 +++ .../site_customization/pages/index.html.erb | 55 +------------------ 3 files changed, 62 insertions(+), 54 deletions(-) create mode 100644 app/components/admin/site_customization/pages/index_component.html.erb create mode 100644 app/components/admin/site_customization/pages/index_component.rb diff --git a/app/components/admin/site_customization/pages/index_component.html.erb b/app/components/admin/site_customization/pages/index_component.html.erb new file mode 100644 index 000000000..7bdbc00ea --- /dev/null +++ b/app/components/admin/site_customization/pages/index_component.html.erb @@ -0,0 +1,54 @@ +<% provide :title do %> + <%= t("admin.header.title") %> - <%= t("admin.menu.site_customization.pages") %> +<% end %> + +<%= link_to t("admin.site_customization.pages.index.create"), new_admin_site_customization_page_path, class: "button float-right" %> +

<%= t("admin.site_customization.pages.index.title") %>

+ +<% if pages.any? %> +

<%= page_entries_info pages %>

+ + + + + + + + + + + + + + <% pages.each do |page| %> + + + + + + + + + <% end %> + +
<%= t("admin.site_customization.pages.page.title") %><%= t("admin.site_customization.pages.page.slug") %><%= t("admin.site_customization.pages.page.created_at") %><%= t("admin.site_customization.pages.page.updated_at") %><%= t("admin.site_customization.pages.page.status") %><%= t("admin.actions.actions") %>
<%= page.title %><%= page.slug %><%= I18n.l page.created_at, format: :short %><%= I18n.l page.created_at, format: :short %><%= t("admin.site_customization.pages.page.status_#{page.status}") %> + <%= render Admin::TableActionsComponent.new(page) do |actions| %> + <%= actions.action(:cards, + text: t("admin.site_customization.pages.page.see_cards"), + path: admin_site_customization_page_widget_cards_path(page)) %> + + <% if page.status == "published" %> + <%= actions.action(:show, + text: t("admin.site_customization.pages.index.see_page"), + path: page.url, + options: { target: "_blank" }) %> + <% end %> + <% end %> +
+ + <%= paginate pages %> +<% else %> +
+ <%= page_entries_info pages %> +
+<% end %> diff --git a/app/components/admin/site_customization/pages/index_component.rb b/app/components/admin/site_customization/pages/index_component.rb new file mode 100644 index 000000000..3f181f8f5 --- /dev/null +++ b/app/components/admin/site_customization/pages/index_component.rb @@ -0,0 +1,7 @@ +class Admin::SiteCustomization::Pages::IndexComponent < ApplicationComponent + attr_reader :pages + + def initialize(pages) + @pages = pages + end +end diff --git a/app/views/admin/site_customization/pages/index.html.erb b/app/views/admin/site_customization/pages/index.html.erb index 8be93c234..d990389ee 100644 --- a/app/views/admin/site_customization/pages/index.html.erb +++ b/app/views/admin/site_customization/pages/index.html.erb @@ -1,54 +1 @@ -<% provide :title do %> - <%= t("admin.header.title") %> - <%= t("admin.menu.site_customization.pages") %> -<% end %> - -<%= link_to t("admin.site_customization.pages.index.create"), new_admin_site_customization_page_path, class: "button float-right" %> -

<%= t("admin.site_customization.pages.index.title") %>

- -<% if @pages.any? %> -

<%= page_entries_info @pages %>

- - - - - - - - - - - - - - <% @pages.each do |page| %> - - - - - - - - - <% end %> - -
<%= t("admin.site_customization.pages.page.title") %><%= t("admin.site_customization.pages.page.slug") %><%= t("admin.site_customization.pages.page.created_at") %><%= t("admin.site_customization.pages.page.updated_at") %><%= t("admin.site_customization.pages.page.status") %><%= t("admin.actions.actions") %>
<%= page.title %><%= page.slug %><%= I18n.l page.created_at, format: :short %><%= I18n.l page.created_at, format: :short %><%= t("admin.site_customization.pages.page.status_#{page.status}") %> - <%= render Admin::TableActionsComponent.new(page) do |actions| %> - <%= actions.action(:cards, - text: t("admin.site_customization.pages.page.see_cards"), - path: admin_site_customization_page_widget_cards_path(page)) %> - - <% if page.status == "published" %> - <%= actions.action(:show, - text: t("admin.site_customization.pages.index.see_page"), - path: page.url, - options: { target: "_blank" }) %> - <% end %> - <% end %> -
- - <%= paginate @pages %> -<% else %> -
- <%= page_entries_info @pages %> -
-<% end %> +<%= render Admin::SiteCustomization::Pages::IndexComponent.new(@pages) %> From d5f440c9bbeeb4c07bee3c9cb583184860a321f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sun, 22 Jan 2023 17:14:20 +0100 Subject: [PATCH 2/3] Simplify header in custom pages admin index Now that we're using a component, we can reuse the Header module. --- .../site_customization/pages/index_component.html.erb | 7 ++----- .../admin/site_customization/pages/index_component.rb | 7 +++++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/app/components/admin/site_customization/pages/index_component.html.erb b/app/components/admin/site_customization/pages/index_component.html.erb index 7bdbc00ea..1eb35d59c 100644 --- a/app/components/admin/site_customization/pages/index_component.html.erb +++ b/app/components/admin/site_customization/pages/index_component.html.erb @@ -1,10 +1,7 @@ -<% provide :title do %> - <%= t("admin.header.title") %> - <%= t("admin.menu.site_customization.pages") %> +<%= header do %> + <%= link_to t("admin.site_customization.pages.index.create"), new_admin_site_customization_page_path %> <% end %> -<%= link_to t("admin.site_customization.pages.index.create"), new_admin_site_customization_page_path, class: "button float-right" %> -

<%= t("admin.site_customization.pages.index.title") %>

- <% if pages.any? %>

<%= page_entries_info pages %>

diff --git a/app/components/admin/site_customization/pages/index_component.rb b/app/components/admin/site_customization/pages/index_component.rb index 3f181f8f5..d68123b59 100644 --- a/app/components/admin/site_customization/pages/index_component.rb +++ b/app/components/admin/site_customization/pages/index_component.rb @@ -1,7 +1,14 @@ class Admin::SiteCustomization::Pages::IndexComponent < ApplicationComponent + include Header attr_reader :pages def initialize(pages) @pages = pages end + + private + + def title + t("admin.site_customization.pages.index.title") + end end From 119f64ac44899b8dfc1a8dbad758e6c49452e370 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sun, 22 Jan 2023 17:15:13 +0100 Subject: [PATCH 3/3] Fix dates in custom pages admin index We weren't showing the year when a page was created/updated, and we were displaying the created date instead of the updated one. Co-Authored-By: Diego Calvo --- .../pages/index_component.html.erb | 4 ++-- .../pages/index_component_spec.rb | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 spec/components/admin/site_customization/pages/index_component_spec.rb diff --git a/app/components/admin/site_customization/pages/index_component.html.erb b/app/components/admin/site_customization/pages/index_component.html.erb index 1eb35d59c..68db19b5f 100644 --- a/app/components/admin/site_customization/pages/index_component.html.erb +++ b/app/components/admin/site_customization/pages/index_component.html.erb @@ -21,8 +21,8 @@ <%= page.title %> <%= page.slug %> - <%= I18n.l page.created_at, format: :short %> - <%= I18n.l page.created_at, format: :short %> + <%= I18n.l page.created_at, format: :long %> + <%= I18n.l page.updated_at, format: :long %> <%= t("admin.site_customization.pages.page.status_#{page.status}") %> <%= render Admin::TableActionsComponent.new(page) do |actions| %> diff --git a/spec/components/admin/site_customization/pages/index_component_spec.rb b/spec/components/admin/site_customization/pages/index_component_spec.rb new file mode 100644 index 000000000..1792e52cb --- /dev/null +++ b/spec/components/admin/site_customization/pages/index_component_spec.rb @@ -0,0 +1,15 @@ +require "rails_helper" + +describe Admin::SiteCustomization::Pages::IndexComponent, controller: Admin::SiteCustomization::PagesController do + before { SiteCustomization::Page.delete_all } + + it "shows date in created_at and updated_at fields" do + custom_page = create(:site_customization_page, created_at: "2015-07-15 13:32:13") + custom_page.update!(updated_at: "2019-12-09 09:19:29") + + render_inline Admin::SiteCustomization::Pages::IndexComponent.new(SiteCustomization::Page.page(1)) + + expect(page).to have_content "July 15, 2015 13:32" + expect(page).to have_content "December 09, 2019 09:19" + end +end