From b5484b2cc4fe1c096b59c2fa033579a04a3c9290 Mon Sep 17 00:00:00 2001 From: Nevelito Date: Tue, 19 Nov 2024 15:05:05 +0100 Subject: [PATCH 1/7] Refactor tab_groups to have title and description --- .../avo/tab_group_component.html.erb | 1 + .../avo/tab_header_component.html.erb | 8 +++ app/components/avo/tab_header_component.rb | 4 ++ lib/avo/resources/items/holder.rb | 12 ++++- lib/avo/resources/items/tab_group.rb | 19 ++++--- .../avo/tab_header_component_spec.rb | 50 +++++++++++++++++++ spec/dummy/app/avo/resources/user.rb | 4 +- 7 files changed, 88 insertions(+), 10 deletions(-) create mode 100644 app/components/avo/tab_header_component.html.erb create mode 100644 app/components/avo/tab_header_component.rb create mode 100644 spec/components/avo/tab_header_component_spec.rb diff --git a/app/components/avo/tab_group_component.html.erb b/app/components/avo/tab_group_component.html.erb index b2c354f97e..9b2e4aea80 100644 --- a/app/components/avo/tab_group_component.html.erb +++ b/app/components/avo/tab_group_component.html.erb @@ -11,6 +11,7 @@ <% visible_tabs.each_with_index do |tab, index| %> <%= content_tag :div, **args(tab) do %>
+ <%= render Avo::TabHeaderComponent.new title: group.title, description: group.description %> <%= render Avo::TabSwitcherComponent.new resource: resource, current_tab: visible_tabs.first, group: group, active_tab_name: tab.name, view: view %> <% if !tab.is_empty? %>
diff --git a/app/components/avo/tab_header_component.html.erb b/app/components/avo/tab_header_component.html.erb new file mode 100644 index 0000000000..af4d3b9263 --- /dev/null +++ b/app/components/avo/tab_header_component.html.erb @@ -0,0 +1,8 @@ +
+ <% if title.present? %> +

<%= title %>

+ <% end %> + <% if description.present? %> +

<%= description %>

+ <% end %> +
diff --git a/app/components/avo/tab_header_component.rb b/app/components/avo/tab_header_component.rb new file mode 100644 index 0000000000..74aff618ff --- /dev/null +++ b/app/components/avo/tab_header_component.rb @@ -0,0 +1,4 @@ +class Avo::TabHeaderComponent < Avo::BaseComponent + prop :title, reader: :public + prop :description, reader: :public +end diff --git a/lib/avo/resources/items/holder.rb b/lib/avo/resources/items/holder.rb index 0d5a7daec0..12fc6df231 100644 --- a/lib/avo/resources/items/holder.rb +++ b/lib/avo/resources/items/holder.rb @@ -37,11 +37,19 @@ def field(field_name, **args, &block) add_item field_parser.instance end - def tabs(tab = nil, id: nil, name: nil, **kwargs, &block) + def tabs(tab = nil, id: nil, name: nil, title: nil, description: nil, **kwargs, &block) if tab.present? add_item tab else - add_item Avo::Resources::Items::TabGroup::Builder.parse_block(parent: @parent, id: id, name: name, **kwargs, &block) + add_item Avo::Resources::Items::TabGroup::Builder.parse_block( + parent: @parent, + id: id, + name: name, + title: title, + description: description, + **kwargs, + &block + ) end end diff --git a/lib/avo/resources/items/tab_group.rb b/lib/avo/resources/items/tab_group.rb index 1db7f7adc4..7b7567fc6e 100644 --- a/lib/avo/resources/items/tab_group.rb +++ b/lib/avo/resources/items/tab_group.rb @@ -6,15 +6,15 @@ class Avo::Resources::Items::TabGroup include Avo::Concerns::IsVisible include Avo::Concerns::VisibleInDifferentViews - attr_accessor :index - attr_accessor :style - attr_accessor :name + attr_accessor :index, :style, :name, :title, :description - def initialize(index: 0, view: nil, style: nil, id: nil, name: nil, **args) + def initialize(index: 0, view: nil, style: nil, title: nil, description: nil, id: nil, name: nil, **args) @index = index @items_holder = Avo::Resources::Items::Holder.new @view = Avo::ViewInquirer.new view @style = style + @title = title + @description = description @id = id @name = name @args = args @@ -67,8 +67,15 @@ def field(field_name, **args, &block) @items_holder.tabs tab end - def initialize(name:, id:, parent:, style: nil, **args) - @group = Avo::Resources::Items::TabGroup.new(name: name, id: id, style: style, **args) + def initialize(name:, id:, parent:, title: nil, description: nil, style: nil, **args) + @group = Avo::Resources::Items::TabGroup.new( + name: name, + id: id, + style: style, + title: title, + description: description, + **args + ) @items_holder = Avo::Resources::Items::Holder.new(parent: parent, from: self) end diff --git a/spec/components/avo/tab_header_component_spec.rb b/spec/components/avo/tab_header_component_spec.rb new file mode 100644 index 0000000000..1884e15a7d --- /dev/null +++ b/spec/components/avo/tab_header_component_spec.rb @@ -0,0 +1,50 @@ +require "rails_helper" + +RSpec.describe Avo::TabHeaderComponent, type: :component do + let(:title) { "Sample Title" } + let(:description) { "Sample Description" } + + describe "rendering" do + context "when both title and description are present" do + it "renders the title and description" do + render_inline(described_class.new(title: title, description: description)) + + expect(rendered_content).to have_content(title) + expect(rendered_content).to have_content(description) + end + end + + context "when title is absent" do + let(:title) { nil } + + it "renders only the description" do + render_inline(described_class.new(title: title, description: description)) + + expect(rendered_content).to have_content(description) + expect(rendered_content).not_to have_content("Sample Title") + end + end + + context "when description is absent" do + let(:description) { nil } + + it "renders only the title" do + render_inline(described_class.new(title: title, description: description)) + + expect(rendered_content).to have_content(title) + expect(rendered_content).not_to have_content("Sample Description") + end + end + + context "when both title and description are absent" do + let(:title) { nil } + let(:description) { nil } + + it "renders nothing" do + render_inline(described_class.new(title: title, description: description)) + + expect(rendered_content).to be_blank + end + end + end +end diff --git a/spec/dummy/app/avo/resources/user.rb b/spec/dummy/app/avo/resources/user.rb index d2528f5147..8effd8588a 100644 --- a/spec/dummy/app/avo/resources/user.rb +++ b/spec/dummy/app/avo/resources/user.rb @@ -227,7 +227,7 @@ def stacked_name end def first_tabs_group - tabs do + tabs title: "Title", description: "Description" do birthday_tab test_tab test_field("Inside tabs") @@ -241,7 +241,7 @@ def first_tabs_group end def second_tabs_group - tabs id: :second_tabs_group do + tabs title: "Title", description: "Description", id: :second_tabs_group do field :post, as: :has_one, name: "Main post", From 06a496272f1b92853698637566804374cf664f25 Mon Sep 17 00:00:00 2001 From: Nevelito Date: Tue, 19 Nov 2024 15:29:10 +0100 Subject: [PATCH 2/7] Fixes with rubocop --- lib/avo/resources/items/holder.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/avo/resources/items/holder.rb b/lib/avo/resources/items/holder.rb index 12fc6df231..e68ad9eac7 100644 --- a/lib/avo/resources/items/holder.rb +++ b/lib/avo/resources/items/holder.rb @@ -37,7 +37,7 @@ def field(field_name, **args, &block) add_item field_parser.instance end - def tabs(tab = nil, id: nil, name: nil, title: nil, description: nil, **kwargs, &block) + def tabs(tab = nil, id: nil, name: nil, title: nil, description: nil, **, &block) if tab.present? add_item tab else @@ -47,7 +47,7 @@ def tabs(tab = nil, id: nil, name: nil, title: nil, description: nil, **kwargs, name: name, title: title, description: description, - **kwargs, + **, &block ) end From 2361fb61a12993d579d9777841e1902f9cbf5560 Mon Sep 17 00:00:00 2001 From: Nevelito Date: Tue, 19 Nov 2024 15:37:18 +0100 Subject: [PATCH 3/7] Fix with rubocop --- lib/avo/resources/items/holder.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/avo/resources/items/holder.rb b/lib/avo/resources/items/holder.rb index e68ad9eac7..4262e9b19c 100644 --- a/lib/avo/resources/items/holder.rb +++ b/lib/avo/resources/items/holder.rb @@ -37,7 +37,7 @@ def field(field_name, **args, &block) add_item field_parser.instance end - def tabs(tab = nil, id: nil, name: nil, title: nil, description: nil, **, &block) + def tabs(tab = nil, id: nil, name: nil, title: nil, description: nil, **args, &block) if tab.present? add_item tab else @@ -47,7 +47,7 @@ def tabs(tab = nil, id: nil, name: nil, title: nil, description: nil, **, &block name: name, title: title, description: description, - **, + **args, &block ) end From 0cf6551185e4441cdee5c079c1675ce8151041d3 Mon Sep 17 00:00:00 2001 From: Nevelito Date: Wed, 20 Nov 2024 09:44:44 +0100 Subject: [PATCH 4/7] Request changes --- app/components/avo/tab_header_component.html.erb | 8 ++++---- app/components/avo/tab_header_component.rb | 8 ++++++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app/components/avo/tab_header_component.html.erb b/app/components/avo/tab_header_component.html.erb index af4d3b9263..9876deaad0 100644 --- a/app/components/avo/tab_header_component.html.erb +++ b/app/components/avo/tab_header_component.html.erb @@ -1,8 +1,8 @@
- <% if title.present? %> -

<%= title %>

+ <% if @title.present? %> +

<%= @title %>

<% end %> - <% if description.present? %> -

<%= description %>

+ <% if @description.present? %> +

<%= @description %>

<% end %>
diff --git a/app/components/avo/tab_header_component.rb b/app/components/avo/tab_header_component.rb index 74aff618ff..e2862b2b2a 100644 --- a/app/components/avo/tab_header_component.rb +++ b/app/components/avo/tab_header_component.rb @@ -1,4 +1,8 @@ class Avo::TabHeaderComponent < Avo::BaseComponent - prop :title, reader: :public - prop :description, reader: :public + prop :title + prop :description + + def render? + (@title || @description).present? + end end From 5831a6d16fee916ff2900441fa7da9eceb1273a1 Mon Sep 17 00:00:00 2001 From: Nevelito Date: Thu, 21 Nov 2024 09:34:08 +0100 Subject: [PATCH 5/7] Request changes --- .../avo/tab_group_component.html.erb | 4 +- .../avo/tab_header_component.html.erb | 8 --- app/components/avo/tab_header_component.rb | 8 --- .../avo/tab_header_component_spec.rb | 50 ------------------- 4 files changed, 2 insertions(+), 68 deletions(-) delete mode 100644 app/components/avo/tab_header_component.html.erb delete mode 100644 app/components/avo/tab_header_component.rb delete mode 100644 spec/components/avo/tab_header_component_spec.rb diff --git a/app/components/avo/tab_group_component.html.erb b/app/components/avo/tab_group_component.html.erb index 9b2e4aea80..61240c5a5f 100644 --- a/app/components/avo/tab_group_component.html.erb +++ b/app/components/avo/tab_group_component.html.erb @@ -10,8 +10,8 @@ } do %> <% visible_tabs.each_with_index do |tab, index| %> <%= content_tag :div, **args(tab) do %> -
- <%= render Avo::TabHeaderComponent.new title: group.title, description: group.description %> + <%= render Avo::PanelComponent.new name: group.title, description: group.description %> +
<%= render Avo::TabSwitcherComponent.new resource: resource, current_tab: visible_tabs.first, group: group, active_tab_name: tab.name, view: view %> <% if !tab.is_empty? %>
diff --git a/app/components/avo/tab_header_component.html.erb b/app/components/avo/tab_header_component.html.erb deleted file mode 100644 index 9876deaad0..0000000000 --- a/app/components/avo/tab_header_component.html.erb +++ /dev/null @@ -1,8 +0,0 @@ -
- <% if @title.present? %> -

<%= @title %>

- <% end %> - <% if @description.present? %> -

<%= @description %>

- <% end %> -
diff --git a/app/components/avo/tab_header_component.rb b/app/components/avo/tab_header_component.rb deleted file mode 100644 index e2862b2b2a..0000000000 --- a/app/components/avo/tab_header_component.rb +++ /dev/null @@ -1,8 +0,0 @@ -class Avo::TabHeaderComponent < Avo::BaseComponent - prop :title - prop :description - - def render? - (@title || @description).present? - end -end diff --git a/spec/components/avo/tab_header_component_spec.rb b/spec/components/avo/tab_header_component_spec.rb deleted file mode 100644 index 1884e15a7d..0000000000 --- a/spec/components/avo/tab_header_component_spec.rb +++ /dev/null @@ -1,50 +0,0 @@ -require "rails_helper" - -RSpec.describe Avo::TabHeaderComponent, type: :component do - let(:title) { "Sample Title" } - let(:description) { "Sample Description" } - - describe "rendering" do - context "when both title and description are present" do - it "renders the title and description" do - render_inline(described_class.new(title: title, description: description)) - - expect(rendered_content).to have_content(title) - expect(rendered_content).to have_content(description) - end - end - - context "when title is absent" do - let(:title) { nil } - - it "renders only the description" do - render_inline(described_class.new(title: title, description: description)) - - expect(rendered_content).to have_content(description) - expect(rendered_content).not_to have_content("Sample Title") - end - end - - context "when description is absent" do - let(:description) { nil } - - it "renders only the title" do - render_inline(described_class.new(title: title, description: description)) - - expect(rendered_content).to have_content(title) - expect(rendered_content).not_to have_content("Sample Description") - end - end - - context "when both title and description are absent" do - let(:title) { nil } - let(:description) { nil } - - it "renders nothing" do - render_inline(described_class.new(title: title, description: description)) - - expect(rendered_content).to be_blank - end - end - end -end From 1a744a599e23a10872ecdc4a2e1fb5117281f1a2 Mon Sep 17 00:00:00 2001 From: Nevelito Date: Fri, 22 Nov 2024 16:39:38 +0100 Subject: [PATCH 6/7] Create new component and move there header part from PanelComponent, add spec --- app/components/avo/panel_component.html.erb | 45 +++++-------------- app/components/avo/panel_component.rb | 15 +------ .../avo/panel_header_component.html.erb | 31 +++++++++++++ app/components/avo/panel_header_component.rb | 29 ++++++++++++ .../avo/tab_group_component.html.erb | 2 +- .../avo/panel_header_component_spec.rb | 11 +++++ 6 files changed, 85 insertions(+), 48 deletions(-) create mode 100644 app/components/avo/panel_header_component.html.erb create mode 100644 app/components/avo/panel_header_component.rb create mode 100644 spec/components/avo/panel_header_component_spec.rb diff --git a/app/components/avo/panel_component.html.erb b/app/components/avo/panel_component.html.erb index 9258d5b6cf..b35c2830d6 100644 --- a/app/components/avo/panel_component.html.erb +++ b/app/components/avo/panel_component.html.erb @@ -1,38 +1,17 @@ <%= content_tag :div, class: classes, data: data_attributes do %> <%= render Avo::CoverPhotoComponent.new cover_photo: @cover_photo %> - - <% if render_header? %> -
-
- <%= render Avo::ProfilePhotoComponent.new profile_photo: @profile_photo %> -
- <% if display_breadcrumbs? %> - - <% end %> -
-
- <% if name_slot? %> - <%= name_slot %> - <% else %> - <%= render Avo::PanelNameComponent.new name: @name %> - <% end %> - <% if description.present? %> -
- <%== description %> -
- <% end %> -
- <% if tools.present? %> -
- <%= tools %> -
- <% end %> -
-
-
-
+ <%= render Avo::PanelHeaderComponent.new( + name: @name, + description: @description, + display_breadcrumbs: @display_breadcrumbs, + profile_photo: @profile_photo + ) do |header| %> + <% header.name_slot do %> + <%= name_slot %> + <% end %> + <% header.tools do %> + <%= tools %> + <% end %> <% end %> <% if body? %>
diff --git a/app/components/avo/panel_component.rb b/app/components/avo/panel_component.rb index ca5a7631fe..543c4d4fe0 100644 --- a/app/components/avo/panel_component.rb +++ b/app/components/avo/panel_component.rb @@ -7,6 +7,7 @@ class Avo::PanelComponent < Avo::BaseComponent renders_one :cover_slot renders_one :name_slot + renders_one :header_slot renders_one :tools renders_one :body renders_one :sidebar @@ -37,18 +38,4 @@ def classes def data_attributes @data.merge({"panel-index": @index}) end - - def display_breadcrumbs? - @display_breadcrumbs == true && Avo.configuration.display_breadcrumbs == true - end - - def description - return @description if @description.present? - - "" - end - - def render_header? - @name.present? || description.present? || tools.present? || display_breadcrumbs? - end end diff --git a/app/components/avo/panel_header_component.html.erb b/app/components/avo/panel_header_component.html.erb new file mode 100644 index 0000000000..a073ebe937 --- /dev/null +++ b/app/components/avo/panel_header_component.html.erb @@ -0,0 +1,31 @@ +
+
+ <%= render Avo::ProfilePhotoComponent.new profile_photo: @profile_photo %> +
+ <% if @display_breadcrumbs.present? %> + + <% end %> +
+
+ <% if name_slot? %> + <%= name_slot %> + <% else %> + <%= render Avo::PanelNameComponent.new name: @name %> + <% end %> + <% if @description.present? %> +
+ <%== @description %> +
+ <% end %> +
+ <% if tools.present? %> +
+ <%= tools %> +
+ <% end %> +
+
+
+
diff --git a/app/components/avo/panel_header_component.rb b/app/components/avo/panel_header_component.rb new file mode 100644 index 0000000000..845773e8b3 --- /dev/null +++ b/app/components/avo/panel_header_component.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +class Avo::PanelHeaderComponent < Avo::BaseComponent + include Avo::ApplicationHelper + + renders_one :name_slot + renders_one :tools + + prop :name + prop :description + prop :display_breadcrumbs, default: false + prop :profile_photo + + private + + def display_breadcrumbs? + @display_breadcrumbs == true && Avo.configuration.display_breadcrumbs == true + end + + def description + return @description if @description.present? + + "" + end + + def render? + @name.present? || description.present? || tools.present? || display_breadcrumbs? + end +end diff --git a/app/components/avo/tab_group_component.html.erb b/app/components/avo/tab_group_component.html.erb index 61240c5a5f..f14a7bd888 100644 --- a/app/components/avo/tab_group_component.html.erb +++ b/app/components/avo/tab_group_component.html.erb @@ -10,7 +10,7 @@ } do %> <% visible_tabs.each_with_index do |tab, index| %> <%= content_tag :div, **args(tab) do %> - <%= render Avo::PanelComponent.new name: group.title, description: group.description %> + <%= render Avo::PanelHeaderComponent.new name: group.title, description: group.description %>
<%= render Avo::TabSwitcherComponent.new resource: resource, current_tab: visible_tabs.first, group: group, active_tab_name: tab.name, view: view %> <% if !tab.is_empty? %> diff --git a/spec/components/avo/panel_header_component_spec.rb b/spec/components/avo/panel_header_component_spec.rb new file mode 100644 index 0000000000..5a00f2febb --- /dev/null +++ b/spec/components/avo/panel_header_component_spec.rb @@ -0,0 +1,11 @@ +require "rails_helper" + +RSpec.describe Avo::PanelHeaderComponent, type: :component do + # it "renders something useful" do + # expect( + # render_inline(described_class.new(attr: "value")) { "Hello, components!" }.css("p").to_html + # ).to include( + # "Hello, components!" + # ) + # end +end From 45318a0b0980ddb85337cf3a168c80524e0a6370 Mon Sep 17 00:00:00 2001 From: Nevelito Date: Fri, 22 Nov 2024 16:59:55 +0100 Subject: [PATCH 7/7] Small fixes --- app/components/avo/panel_component.html.erb | 12 ++++++++---- app/components/avo/panel_header_component.html.erb | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/app/components/avo/panel_component.html.erb b/app/components/avo/panel_component.html.erb index b35c2830d6..63da39b304 100644 --- a/app/components/avo/panel_component.html.erb +++ b/app/components/avo/panel_component.html.erb @@ -6,11 +6,15 @@ display_breadcrumbs: @display_breadcrumbs, profile_photo: @profile_photo ) do |header| %> - <% header.name_slot do %> - <%= name_slot %> + <% if name_slot.present? %> + <% header.with_name_slot do %> + <%= name_slot %> + <% end %> <% end %> - <% header.tools do %> - <%= tools %> + <% if tools.present? %> + <% header.with_tools do %> + <%= tools %> + <% end %> <% end %> <% end %> <% if body? %> diff --git a/app/components/avo/panel_header_component.html.erb b/app/components/avo/panel_header_component.html.erb index a073ebe937..4f1b08fd02 100644 --- a/app/components/avo/panel_header_component.html.erb +++ b/app/components/avo/panel_header_component.html.erb @@ -2,7 +2,7 @@
<%= render Avo::ProfilePhotoComponent.new profile_photo: @profile_photo %>
- <% if @display_breadcrumbs.present? %> + <% if display_breadcrumbs? %>