Skip to content

Commit

Permalink
Add attachment component
Browse files Browse the repository at this point in the history
  • Loading branch information
hasarindaKI committed Feb 29, 2024
1 parent e04943a commit 2107be1
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 1 deletion.
15 changes: 15 additions & 0 deletions app/components/koi/tables/body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions app/components/koi/tables/body_row_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions app/components/koi/tables/header_row_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/koi/admin_views/admin_views_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions spec/components/koi/tables/body_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<% row.ordinal %>
<% row.link :name %>
<% row.image :image %>

0 comments on commit 2107be1

Please sign in to comment.