From 5148919c17a9db340399feabb17c0639c103ab41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 6 Jan 2021 20:05:00 +0100 Subject: [PATCH] Remove duplication in cards tables We were using the same code in two places, so we're extracting the code into a component in order to share it. --- .../widget/cards/row_component.html.erb} | 0 .../admin/widget/cards/row_component.rb | 7 ++++++ .../widget/cards/table_component.html.erb | 22 +++++++++++++++++++ .../admin/widget/cards/table_component.rb | 8 +++++++ app/views/admin/homepage/_cards.html.erb | 16 -------------- app/views/admin/homepage/show.html.erb | 22 +++++++------------ .../site_customization/cards/_card.html.erb | 22 ------------------- .../site_customization/cards/_cards.html.erb | 16 -------------- .../site_customization/cards/index.html.erb | 11 ++++------ 9 files changed, 49 insertions(+), 75 deletions(-) rename app/{views/admin/homepage/_card.html.erb => components/admin/widget/cards/row_component.html.erb} (100%) create mode 100644 app/components/admin/widget/cards/row_component.rb create mode 100644 app/components/admin/widget/cards/table_component.html.erb create mode 100644 app/components/admin/widget/cards/table_component.rb delete mode 100644 app/views/admin/homepage/_cards.html.erb delete mode 100644 app/views/admin/site_customization/cards/_card.html.erb delete mode 100644 app/views/admin/site_customization/cards/_cards.html.erb diff --git a/app/views/admin/homepage/_card.html.erb b/app/components/admin/widget/cards/row_component.html.erb similarity index 100% rename from app/views/admin/homepage/_card.html.erb rename to app/components/admin/widget/cards/row_component.html.erb diff --git a/app/components/admin/widget/cards/row_component.rb b/app/components/admin/widget/cards/row_component.rb new file mode 100644 index 000000000..fed569949 --- /dev/null +++ b/app/components/admin/widget/cards/row_component.rb @@ -0,0 +1,7 @@ +class Admin::Widget::Cards::RowComponent < ApplicationComponent + attr_reader :card + + def initialize(card) + @card = card + end +end diff --git a/app/components/admin/widget/cards/table_component.html.erb b/app/components/admin/widget/cards/table_component.html.erb new file mode 100644 index 000000000..01a52aec5 --- /dev/null +++ b/app/components/admin/widget/cards/table_component.html.erb @@ -0,0 +1,22 @@ +<% if cards.any? %> + + + + + + + + + + + + <% cards.each do |card| %> + <%= render Admin::Widget::Cards::RowComponent.new(card) %> + <% end %> + +
<%= ::Widget::Card.human_attribute_name(:title) %><%= ::Widget::Card.human_attribute_name(:description) %><%= ::Widget::Card.human_attribute_name(:link_text) %> / <%= ::Widget::Card.human_attribute_name(:link_url) %><%= t("admin.shared.image") %><%= t("admin.shared.actions") %>
+<% else %> +
+ <%= no_cards_message %> +
+<% end %> diff --git a/app/components/admin/widget/cards/table_component.rb b/app/components/admin/widget/cards/table_component.rb new file mode 100644 index 000000000..74e2112a3 --- /dev/null +++ b/app/components/admin/widget/cards/table_component.rb @@ -0,0 +1,8 @@ +class Admin::Widget::Cards::TableComponent < ApplicationComponent + attr_reader :cards, :no_cards_message + + def initialize(cards, no_cards_message:) + @cards = cards + @no_cards_message = no_cards_message + end +end diff --git a/app/views/admin/homepage/_cards.html.erb b/app/views/admin/homepage/_cards.html.erb deleted file mode 100644 index ae2fef6d0..000000000 --- a/app/views/admin/homepage/_cards.html.erb +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - <% cards.each do |card| %> - <%= render "card", card: card %> - <% end %> - -
<%= Widget::Card.human_attribute_name(:title) %><%= Widget::Card.human_attribute_name(:description) %><%= Widget::Card.human_attribute_name(:link_text) %> / <%= Widget::Card.human_attribute_name(:link_url) %><%= t("admin.shared.image") %><%= t("admin.shared.actions") %>
diff --git a/app/views/admin/homepage/show.html.erb b/app/views/admin/homepage/show.html.erb index b931feff5..3c933c961 100644 --- a/app/views/admin/homepage/show.html.erb +++ b/app/views/admin/homepage/show.html.erb @@ -9,13 +9,10 @@ <%= link_to t("admin.homepage.create_header"), new_admin_widget_card_path(header_card: true), class: "button" %> - <% if @header.present? %> - <%= render "cards", cards: @header %> - <% else %> -
- <%= t("admin.homepage.no_header") %> -
- <% end %> + <%= render Admin::Widget::Cards::TableComponent.new( + @header, + no_cards_message: t("admin.homepage.no_header") + ) %>
@@ -27,13 +24,10 @@ <%= link_to t("admin.homepage.create_card"), new_admin_widget_card_path, class: "button" %> - <% if @cards.present? %> - <%= render "cards", cards: @cards %> - <% else %> -
- <%= t("admin.homepage.no_cards") %> -
- <% end %> + <%= render Admin::Widget::Cards::TableComponent.new( + @cards, + no_cards_message: t("admin.homepage.no_cards") + ) %>
diff --git a/app/views/admin/site_customization/cards/_card.html.erb b/app/views/admin/site_customization/cards/_card.html.erb deleted file mode 100644 index 94025a7b1..000000000 --- a/app/views/admin/site_customization/cards/_card.html.erb +++ /dev/null @@ -1,22 +0,0 @@ - - - <%= card.label %>
- <%= card.title %> - - <%= card.description %> - - <%= card.link_text %>
- <%= card.link_url %> - - - - - <% if card.image.present? %> - <%= link_to t("admin.shared.show_image"), card.image_url(:large), - title: card.image.title, target: "_blank" %> - <% end %> - - - <%= render Admin::TableActionsComponent.new(card) %> - - diff --git a/app/views/admin/site_customization/cards/_cards.html.erb b/app/views/admin/site_customization/cards/_cards.html.erb deleted file mode 100644 index 8d9d87c96..000000000 --- a/app/views/admin/site_customization/cards/_cards.html.erb +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - <% cards.each do |card| %> - <%= render "card", card: card %> - <% end %> - -
<%= Widget::Card.human_attribute_name(:title) %><%= Widget::Card.human_attribute_name(:description) %><%= Widget::Card.human_attribute_name(:link_text) %> / <%= Widget::Card.human_attribute_name(:link_url) %><%= t("admin.shared.image") %><%= t("admin.shared.actions") %>
diff --git a/app/views/admin/site_customization/cards/index.html.erb b/app/views/admin/site_customization/cards/index.html.erb index ff94623f5..1957744ff 100644 --- a/app/views/admin/site_customization/cards/index.html.erb +++ b/app/views/admin/site_customization/cards/index.html.erb @@ -11,11 +11,8 @@ new_admin_site_customization_page_widget_card_path(@page), class: "button" %> - <% if @cards.present? %> - <%= render "cards", cards: @cards %> - <% else %> -
- <%= t("admin.site_customization.pages.cards.no_cards") %> -
- <% end %> + <%= render Admin::Widget::Cards::TableComponent.new( + @cards, + no_cards_message: t("admin.site_customization.pages.cards.no_cards") + ) %>