From 5ab2cd7088fcc0ace742ec17cb4908c42426f51b Mon Sep 17 00:00:00 2001 From: MDreW Date: Tue, 7 Oct 2025 10:08:24 +0200 Subject: [PATCH 1/4] base_component renamed on applivation_component. Added if_key method --- app/components/itacomp/base_component.rb | 8 -------- .../components/itacomp/application_component_test.rb | 12 ++++++++++++ 2 files changed, 12 insertions(+), 8 deletions(-) delete mode 100644 app/components/itacomp/base_component.rb create mode 100644 test/components/itacomp/application_component_test.rb diff --git a/app/components/itacomp/base_component.rb b/app/components/itacomp/base_component.rb deleted file mode 100644 index b15c4d4..0000000 --- a/app/components/itacomp/base_component.rb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -module Itacomp - class BaseComponent < ViewComponent::Base - include ApplicationHelper - include CommonHelper - end -end diff --git a/test/components/itacomp/application_component_test.rb b/test/components/itacomp/application_component_test.rb new file mode 100644 index 0000000..dfee2de --- /dev/null +++ b/test/components/itacomp/application_component_test.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require "test_helper" + +class Itacomp::ApplicationComponentTest < ViewComponent::TestCase + test 'if_key method' do + assert_equal 'yes', Itacomp::ApplicationComponent.new.if_key('yes', true) + assert_nil Itacomp::ApplicationComponent.new.if_key('yes', false) + assert_nil Itacomp::ApplicationComponent.new.if_key('yes', 'test') + assert_nil Itacomp::ApplicationComponent.new.if_key('yes', nil) + end +end From d80cf4ae6b632173fed14b105f3b5a0ba40c135f Mon Sep 17 00:00:00 2001 From: MDreW Date: Tue, 7 Oct 2025 10:09:31 +0200 Subject: [PATCH 2/4] renamed base component --- app/components/itacomp/alert_component.rb | 2 +- .../itacomp/application_component.rb | 22 +++++++++++++++++++ app/components/itacomp/avatar_component.rb | 2 +- app/components/itacomp/dimmer_component.rb | 2 +- .../itacomp/notification_component.rb | 2 +- app/components/itacomp/tab_component.rb | 2 +- .../itacomp/turbo_frame_component.rb | 2 +- .../components/itacomp/base_component_test.rb | 12 ---------- 8 files changed, 28 insertions(+), 18 deletions(-) create mode 100644 app/components/itacomp/application_component.rb delete mode 100644 test/components/itacomp/base_component_test.rb diff --git a/app/components/itacomp/alert_component.rb b/app/components/itacomp/alert_component.rb index 28256bb..2a4a9b3 100644 --- a/app/components/itacomp/alert_component.rb +++ b/app/components/itacomp/alert_component.rb @@ -27,7 +27,7 @@ module Itacomp # @example with other key params # <%= render ItacompAlertComponent.new(id: 'my-id', data: {test: 'test'}) %> # - class AlertComponent < BaseComponent + class AlertComponent < ApplicationComponent # @param [String,Sym] :type of alert, default 'primary' # @param [Boolean] :close if true is added close button # @param [String] :class add other class after "alsert alert-#{type}" classes diff --git a/app/components/itacomp/application_component.rb b/app/components/itacomp/application_component.rb new file mode 100644 index 0000000..40dbfc0 --- /dev/null +++ b/app/components/itacomp/application_component.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Itacomp + # Common method and incluion for Itacomp components + # + # ==== Inclusion + # * include ApplicationHelper + # * include CommonHelper + class ApplicationComponent < ViewComponent::Base + include ApplicationHelper + include CommonHelper + + # retur value if key is true + # + # ==== Options + # Value [Any], mandatory, return value if key is true + # Key [Boolean], if true, return value + def if_key(value, key) + return value if key == true + end + end +end diff --git a/app/components/itacomp/avatar_component.rb b/app/components/itacomp/avatar_component.rb index 6b6970b..3de38bd 100644 --- a/app/components/itacomp/avatar_component.rb +++ b/app/components/itacomp/avatar_component.rb @@ -34,7 +34,7 @@ module Itacomp # With other options # <%= render itacomp::avatar(id: "my_avatar", data: {turbo_frame: 'main'}).new.with_content('a') %> #
a
- class AvatarComponent < BaseComponent + class AvatarComponent < ApplicationComponent # Initialize avatar component # # ==== Options diff --git a/app/components/itacomp/dimmer_component.rb b/app/components/itacomp/dimmer_component.rb index fbd92af..6ebe8cc 100644 --- a/app/components/itacomp/dimmer_component.rb +++ b/app/components/itacomp/dimmer_component.rb @@ -72,7 +72,7 @@ module Itacomp # #

my dimmable content

# - class DimmerComponent < BaseComponent + class DimmerComponent < ApplicationComponent renders_one :inner_content # Initialize dimmer component # diff --git a/app/components/itacomp/notification_component.rb b/app/components/itacomp/notification_component.rb index f79da83..64c58ea 100644 --- a/app/components/itacomp/notification_component.rb +++ b/app/components/itacomp/notification_component.rb @@ -9,7 +9,7 @@ module Itacomp # Mo dismissable and no show # <%= render Itacomp::NotificationComponent.new(title: 'test', dismissable: false, show: false) %> # - class NotificationComponent < BaseComponent + class NotificationComponent < ApplicationComponent # Initialize notification component # # ==== Options diff --git a/app/components/itacomp/tab_component.rb b/app/components/itacomp/tab_component.rb index b35cf0f..4efc78e 100644 --- a/app/components/itacomp/tab_component.rb +++ b/app/components/itacomp/tab_component.rb @@ -15,7 +15,7 @@ module Itacomp # # # - class TabComponent < ViewComponent::Base + class TabComponent < ApplicationComponent # Initialize tab component # # ==== options diff --git a/app/components/itacomp/turbo_frame_component.rb b/app/components/itacomp/turbo_frame_component.rb index c070496..c9f3443 100644 --- a/app/components/itacomp/turbo_frame_component.rb +++ b/app/components/itacomp/turbo_frame_component.rb @@ -34,7 +34,7 @@ module Itacomp # text content # # text content - class TurboFrameComponent < BaseComponent + class TurboFrameComponent < ApplicationComponent # Initialize TurboFrameComponent # ==== Options # * * [Symbol], each key going as tag option diff --git a/test/components/itacomp/base_component_test.rb b/test/components/itacomp/base_component_test.rb deleted file mode 100644 index b412a4b..0000000 --- a/test/components/itacomp/base_component_test.rb +++ /dev/null @@ -1,12 +0,0 @@ -# frozen_string_literal: true - -require "test_helper" - -class Itacomp::BaseComponentTest < ViewComponent::TestCase - def test_component_renders_something_useful - # assert_equal( - # %(Hello, components!), - # render_inline(BaseComponent.new(message: "Hello, components!")).css("span").to_html - # ) - end -end From 858d84af44e5b8219b9de3c8fa700b10881b15c4 Mon Sep 17 00:00:00 2001 From: MDreW Date: Tue, 7 Oct 2025 10:09:42 +0200 Subject: [PATCH 3/4] added overlay component --- app/components/itacomp/overlay_component.rb | 43 +++++++++++++++++++ .../overlay_component.html.erb | 11 +++++ .../itacomp/overlay_component_test.rb | 27 ++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 app/components/itacomp/overlay_component.rb create mode 100644 app/components/itacomp/overlay_component/overlay_component.html.erb create mode 100644 test/components/itacomp/overlay_component_test.rb diff --git a/app/components/itacomp/overlay_component.rb b/app/components/itacomp/overlay_component.rb new file mode 100644 index 0000000..6e5be81 --- /dev/null +++ b/app/components/itacomp/overlay_component.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +module Itacomp + # make a strcture for [overlay] component](https://italia.github.io/bootstrap-italia/docs/componenti/ove/) + # ==== Example + # With only required img + # <%= render Itacomp::OverlayComponent.new('test.jpg') %> + #
+ # With text + # <%= render Itacomp::OverlayComponent.new('test.jpg', text: 'My text') %> + #
My text
+ # With black background + # <%= render Itacomp::OverlayComponent.new('test.jpg', text: 'My text', black: true) %> + #
My text
+ # With full height + # <%= render Itacomp::OverlayComponent.new('test.jpg', text: 'My text', full: true) %> + #
My text
+ # With icon and full height + # <%= render Itacomp::OverlayComponent.new('test.jpg', text: 'My text', icon: 'lock', full: true) %> + #
my text
+ # With content + # <%= render Itacomp::OverlayComponent.new('test.jpg').with_content('My text') %> + #
My text
+ class OverlayComponent < ApplicationComponent + # Initialize Overlay component + # ==== Options + # * img [String], mandatory, default: nil, image url or asset; + # * text [String], default nil, Overlay span content + # * icon [String] default nil if present is add icon content in overlay + # * black [Boolean] default false if true add overlay-black class + # * full [Boolean] default false if true add overlay-panel-fullheight clas + # * **opts each key is delegated as image_tag options. Default: {}, useful for add class or alternate image description + # * yield figcaption content in a span tag + def initialize(img = nil, text: nil, icon: nil, black: false, full: false, **opts) + @opts = opts + @figcaption_opts = {class: ['overlay-panel', if_key('overlay-panel-fullheight', full), if_key('overlay-black', black), if_key('overlay-icon', icon.present?)]} + + @text = text + @img = img + @icon = icon + end + end +end diff --git a/app/components/itacomp/overlay_component/overlay_component.html.erb b/app/components/itacomp/overlay_component/overlay_component.html.erb new file mode 100644 index 0000000..44d97cc --- /dev/null +++ b/app/components/itacomp/overlay_component/overlay_component.html.erb @@ -0,0 +1,11 @@ +
+ <%= image_tag @img, **@opts %> + <%= tag.figcaption **@figcaption_opts do %> + <% if @icon.present? %> + <%= content || @text %> + <%= ita_icon @icon %> + <% else %> + <%= content? ? content : @text %> + <% end %> + <% end %> +
diff --git a/test/components/itacomp/overlay_component_test.rb b/test/components/itacomp/overlay_component_test.rb new file mode 100644 index 0000000..7c52367 --- /dev/null +++ b/test/components/itacomp/overlay_component_test.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require "test_helper" + +class Itacomp::OverlayComponentTest < ViewComponent::TestCase + test 'with only image' do + render_inline(Itacomp::OverlayComponent.new('/image.png')) + assert_selector 'figure.overlay-wrapper img[src="/image.png"]' + assert_selector 'figure.overlay-wrapper figcaption span', text: nil + end + + test 'with text fegcaption description' do + render_inline(Itacomp::OverlayComponent.new('/image.png', text: 'test')) + assert_selector 'figure.overlay-wrapper figcaption span', text: 'test' + end + + test 'with black figcaption background' do + render_inline(Itacomp::OverlayComponent.new('/image.png', text: 'test', black: true)) + assert_selector 'figure.overlay-wrapper figcaption.overlay-black span', text: 'test' + end + + test 'with icon, fullheight figcaption overlay and visually hidden text description' do + render_inline(Itacomp::OverlayComponent.new('/image.png', text: 'test', full: true, icon: 'test')) + assert_selector 'figure.overlay-wrapper figcaption.overlay-panel-fullheight.overlay-icon span.visually-hidden', text: 'test' + assert_selector 'figure.overlay-wrapper figcaption.overlay-panel-fullheight.overlay-icon svg.icon use' + end +end From 2142505e7ba9297620fc6408cf6e02528bd60dec Mon Sep 17 00:00:00 2001 From: MDreW Date: Tue, 7 Oct 2025 10:16:07 +0200 Subject: [PATCH 4/4] rubocop fix --- .../itacomp/application_component.rb | 4 +-- app/components/itacomp/overlay_component.rb | 10 +++---- .../itacomp/application_component_test.rb | 10 +++---- .../itacomp/overlay_component_test.rb | 28 +++++++++---------- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/app/components/itacomp/application_component.rb b/app/components/itacomp/application_component.rb index 40dbfc0..ef2d01e 100644 --- a/app/components/itacomp/application_component.rb +++ b/app/components/itacomp/application_component.rb @@ -11,12 +11,12 @@ class ApplicationComponent < ViewComponent::Base include CommonHelper # retur value if key is true - # + # # ==== Options # Value [Any], mandatory, return value if key is true # Key [Boolean], if true, return value def if_key(value, key) - return value if key == true + value if key == true end end end diff --git a/app/components/itacomp/overlay_component.rb b/app/components/itacomp/overlay_component.rb index 6e5be81..7a81a8e 100644 --- a/app/components/itacomp/overlay_component.rb +++ b/app/components/itacomp/overlay_component.rb @@ -9,13 +9,13 @@ module Itacomp # With text # <%= render Itacomp::OverlayComponent.new('test.jpg', text: 'My text') %> #
My text
- # With black background + # With black background # <%= render Itacomp::OverlayComponent.new('test.jpg', text: 'My text', black: true) %> #
My text
- # With full height + # With full height # <%= render Itacomp::OverlayComponent.new('test.jpg', text: 'My text', full: true) %> #
My text
- # With icon and full height + # With icon and full height # <%= render Itacomp::OverlayComponent.new('test.jpg', text: 'My text', icon: 'lock', full: true) %> #
my text
# With content @@ -30,10 +30,10 @@ class OverlayComponent < ApplicationComponent # * black [Boolean] default false if true add overlay-black class # * full [Boolean] default false if true add overlay-panel-fullheight clas # * **opts each key is delegated as image_tag options. Default: {}, useful for add class or alternate image description - # * yield figcaption content in a span tag + # * yield figcaption content in a span tag def initialize(img = nil, text: nil, icon: nil, black: false, full: false, **opts) @opts = opts - @figcaption_opts = {class: ['overlay-panel', if_key('overlay-panel-fullheight', full), if_key('overlay-black', black), if_key('overlay-icon', icon.present?)]} + @figcaption_opts = { class: [ "overlay-panel", if_key("overlay-panel-fullheight", full), if_key("overlay-black", black), if_key("overlay-icon", icon.present?) ] } @text = text @img = img diff --git a/test/components/itacomp/application_component_test.rb b/test/components/itacomp/application_component_test.rb index dfee2de..2e48edf 100644 --- a/test/components/itacomp/application_component_test.rb +++ b/test/components/itacomp/application_component_test.rb @@ -3,10 +3,10 @@ require "test_helper" class Itacomp::ApplicationComponentTest < ViewComponent::TestCase - test 'if_key method' do - assert_equal 'yes', Itacomp::ApplicationComponent.new.if_key('yes', true) - assert_nil Itacomp::ApplicationComponent.new.if_key('yes', false) - assert_nil Itacomp::ApplicationComponent.new.if_key('yes', 'test') - assert_nil Itacomp::ApplicationComponent.new.if_key('yes', nil) + test "if_key method" do + assert_equal "yes", Itacomp::ApplicationComponent.new.if_key("yes", true) + assert_nil Itacomp::ApplicationComponent.new.if_key("yes", false) + assert_nil Itacomp::ApplicationComponent.new.if_key("yes", "test") + assert_nil Itacomp::ApplicationComponent.new.if_key("yes", nil) end end diff --git a/test/components/itacomp/overlay_component_test.rb b/test/components/itacomp/overlay_component_test.rb index 7c52367..d23c80f 100644 --- a/test/components/itacomp/overlay_component_test.rb +++ b/test/components/itacomp/overlay_component_test.rb @@ -3,25 +3,25 @@ require "test_helper" class Itacomp::OverlayComponentTest < ViewComponent::TestCase - test 'with only image' do - render_inline(Itacomp::OverlayComponent.new('/image.png')) - assert_selector 'figure.overlay-wrapper img[src="/image.png"]' - assert_selector 'figure.overlay-wrapper figcaption span', text: nil + test "with only image" do + render_inline(Itacomp::OverlayComponent.new("/image.png")) + assert_selector 'figure.overlay-wrapper img[src="/image.png"]' + assert_selector "figure.overlay-wrapper figcaption span", text: nil end - test 'with text fegcaption description' do - render_inline(Itacomp::OverlayComponent.new('/image.png', text: 'test')) - assert_selector 'figure.overlay-wrapper figcaption span', text: 'test' + test "with text fegcaption description" do + render_inline(Itacomp::OverlayComponent.new("/image.png", text: "test")) + assert_selector "figure.overlay-wrapper figcaption span", text: "test" end - test 'with black figcaption background' do - render_inline(Itacomp::OverlayComponent.new('/image.png', text: 'test', black: true)) - assert_selector 'figure.overlay-wrapper figcaption.overlay-black span', text: 'test' + test "with black figcaption background" do + render_inline(Itacomp::OverlayComponent.new("/image.png", text: "test", black: true)) + assert_selector "figure.overlay-wrapper figcaption.overlay-black span", text: "test" end - test 'with icon, fullheight figcaption overlay and visually hidden text description' do - render_inline(Itacomp::OverlayComponent.new('/image.png', text: 'test', full: true, icon: 'test')) - assert_selector 'figure.overlay-wrapper figcaption.overlay-panel-fullheight.overlay-icon span.visually-hidden', text: 'test' - assert_selector 'figure.overlay-wrapper figcaption.overlay-panel-fullheight.overlay-icon svg.icon use' + test "with icon, fullheight figcaption overlay and visually hidden text description" do + render_inline(Itacomp::OverlayComponent.new("/image.png", text: "test", full: true, icon: "test")) + assert_selector "figure.overlay-wrapper figcaption.overlay-panel-fullheight.overlay-icon span.visually-hidden", text: "test" + assert_selector "figure.overlay-wrapper figcaption.overlay-panel-fullheight.overlay-icon svg.icon use" end end