From 2107be148fd5441651bf5c497a7ba11ff09bd3a9 Mon Sep 17 00:00:00 2001 From: Hasarinda Thenuwara Date: Wed, 28 Feb 2024 15:33:11 +1030 Subject: [PATCH] Add attachment component --- app/components/koi/tables/body.rb | 15 +++++++++++++++ app/components/koi/tables/body_row_component.rb | 4 ++++ app/components/koi/tables/header_row_component.rb | 4 ++++ .../koi/admin_views/admin_views_generator.rb | 2 +- spec/components/koi/tables/body_spec.rb | 9 +++++++++ .../app/views/admin/banners/_banner.html+row.erb | 1 + 6 files changed, 34 insertions(+), 1 deletion(-) diff --git a/app/components/koi/tables/body.rb b/app/components/koi/tables/body.rb index f6eec9059..a33092c24 100644 --- a/app/components/koi/tables/body.rb +++ b/app/components/koi/tables/body.rb @@ -114,6 +114,21 @@ def rendered_value value.present? ? image_tag(value.variant(@variant)) : "" end end + + # Shows an attachment in a cell + # + # If it is representable, shows as a image tag(assumes a variant called :thumb is defined for the attachment) + # + # Otherwise shows as a link to download + class AttachmentComponent < BodyCellComponent + def rendered_value + if value.representable? + image_tag(value.variant(:thumb)) + else + link_to value.blob.filename, rails_blob_path(value, disposition: :attachment) + end + end + end end end end diff --git a/app/components/koi/tables/body_row_component.rb b/app/components/koi/tables/body_row_component.rb index 5c6e01353..ea2e7c1a6 100644 --- a/app/components/koi/tables/body_row_component.rb +++ b/app/components/koi/tables/body_row_component.rb @@ -39,6 +39,10 @@ def text(attribute, **options, &block) def image(attribute, variant: :thumb, **options, &block) with_column(Body::ImageComponent.new(@table, @record, attribute, variant:, **options), &block) end + + def attachment(attribute, **options, &block) + with_column(Body::AttachmentComponent.new(@table, @record, attribute, **options), &block) + end end end end diff --git a/app/components/koi/tables/header_row_component.rb b/app/components/koi/tables/header_row_component.rb index ceb635ed6..60f687524 100644 --- a/app/components/koi/tables/header_row_component.rb +++ b/app/components/koi/tables/header_row_component.rb @@ -33,6 +33,10 @@ def link(attribute, **attributes, &block) header_cell(attribute, **attributes, &block) end + def attachment(attribute, **attributes, &block) + header_cell(attribute, type: :link, **attributes, &block) + end + def text(attribute, **attributes, &block) header_cell(attribute, **attributes, &block) end diff --git a/lib/generators/koi/admin_views/admin_views_generator.rb b/lib/generators/koi/admin_views/admin_views_generator.rb index 1b817d3be..cb7de06d2 100644 --- a/lib/generators/koi/admin_views/admin_views_generator.rb +++ b/lib/generators/koi/admin_views/admin_views_generator.rb @@ -79,7 +79,7 @@ def index_attribute_for(attribute) when :rich_text %(<% row.rich_text :#{attribute.name} %>) when :attachment - %(<% row.image :#{attribute.name} %>) + %(<% row.attachment :#{attribute.name} %>) else %(<% row.text :#{attribute.name} %>) end diff --git a/spec/components/koi/tables/body_spec.rb b/spec/components/koi/tables/body_spec.rb index ea520296f..0f477afe1 100644 --- a/spec/components/koi/tables/body_spec.rb +++ b/spec/components/koi/tables/body_spec.rb @@ -124,4 +124,13 @@ HTML end end + + describe Koi::Tables::Body::AttachmentComponent do + it "renders column with a preview of the image" do + record = create(:banner, :with_image) + component = described_class.new(table, record, :image) + rendered = render_inline(component) + expect(rendered).to have_css("td > img[src*='dummy.png']") + end + end end diff --git a/spec/templates/app/views/admin/banners/_banner.html+row.erb b/spec/templates/app/views/admin/banners/_banner.html+row.erb index c8dda08dd..d6e0755e6 100644 --- a/spec/templates/app/views/admin/banners/_banner.html+row.erb +++ b/spec/templates/app/views/admin/banners/_banner.html+row.erb @@ -1,2 +1,3 @@ <% row.ordinal %> <% row.link :name %> +<% row.image :image %>