Skip to content

Commit

Permalink
Add attachment columns
Browse files Browse the repository at this point in the history
* Rename existing image column to attachment to match ActiveStorage
* Add representation? based rendering for images and files
* Move App::PERMITTED_IMAGE_SIZE to Koi.config.image_size_limit
  • Loading branch information
hasarindaKI authored and sfnelson committed Mar 1, 2024
1 parent 6d67dbe commit 248ca7a
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 25 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ group :development, :test do
gem "erb_lint", require: false
gem "factory_bot_rails"
gem "faker"
gem "image_processing" # only required for projects using active storage
gem "katalyst-basic-auth"
gem "katalyst-healthcheck"
gem "propshaft"
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ GEM
concurrent-ruby (~> 1.1)
webrick (~> 1.7)
websocket-driver (>= 0.6, < 0.8)
ffi (1.16.3)
globalid (1.2.1)
activesupport (>= 6.1)
google-protobuf (3.25.3-aarch64-linux)
Expand All @@ -187,6 +188,9 @@ GEM
activesupport (>= 6.1.4.4)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
image_processing (1.12.2)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
importmap-rails (2.0.1)
actionpack (>= 6.0.0)
activesupport (>= 6.0.0)
Expand Down Expand Up @@ -238,6 +242,7 @@ GEM
marcel (1.0.2)
matrix (0.4.2)
method_source (1.0.0)
mini_magick (4.12.0)
mini_mime (1.1.5)
minitest (5.22.2)
mutex_m (0.2.0)
Expand Down Expand Up @@ -389,6 +394,8 @@ GEM
rubocop-capybara (~> 2.17)
rubocop-factory_bot (~> 2.22)
ruby-progressbar (1.13.0)
ruby-vips (2.2.0)
ffi (~> 1.12)
ruby2_keywords (0.0.5)
safety_net_attestation (0.4.0)
jwt (~> 2.0)
Expand Down Expand Up @@ -467,6 +474,7 @@ DEPENDENCIES
erb_lint
factory_bot_rails
faker
image_processing
katalyst-basic-auth
katalyst-healthcheck
katalyst-koi!
Expand Down
12 changes: 11 additions & 1 deletion app/components/koi/summary_list/attachment_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@
module Koi
module SummaryList
class AttachmentComponent < Base
def initialize(model, attribute, variant: :thumb, **attributes)
super(model, attribute, **attributes)

@variant = variant
end

def attribute_value
link_to raw_value.blob.filename, rails_blob_path(raw_value, disposition: :attachment)
if raw_value.try(:representable?)
image_tag(@variant.nil? ? raw_value : raw_value.variant(@variant))
elsif raw_value.try(:attached?)
link_to raw_value.blob.filename, rails_blob_path(raw_value, disposition: :attachment)
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/components/koi/summary_list_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def text(attribute, label: nil, skip_blank: @skip_blank, &)
with_definition_text(@model, attribute, label:, skip_blank:, &)
end

def attachment(attribute, label: nil, skip_blank: @skip_blank, &)
with_definition_attachment(@model, attribute, label:, skip_blank:, &)
def attachment(attribute, label: nil, skip_blank: @skip_blank, variant: :thumb, &)
with_definition_attachment(@model, attribute, label:, skip_blank:, variant:, &)
end

# @deprecated legacy interface
Expand Down
17 changes: 13 additions & 4 deletions app/components/koi/tables/body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,26 @@ def url
end
end

# Shows a thumbnail image
# The value is expected to be an ActiveStorage attachment with a default variant named :thumb
class ImageComponent < BodyCellComponent
# Shows an attachment in a cell
#
# The value is expected to be an ActiveStorage attachment
#
# If it is representable, shows as a image tag using a default variant named :thumb.
#
# Otherwise shows as a link to download.
class AttachmentComponent < BodyCellComponent
def initialize(table, record, attribute, variant: :thumb, **options)
super(table, record, attribute, **options)

@variant = variant
end

def rendered_value
value.present? ? image_tag(value.variant(@variant)) : ""
if value.try(:representable?)
image_tag(@variant.nil? ? value : value.variant(@variant))
elsif value.try(:attached?)
link_to value.blob.filename, rails_blob_path(value, disposition: :attachment)
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/components/koi/tables/body_row_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def text(attribute, **options, &block)
with_column(BodyCellComponent.new(@table, @record, attribute, **options), &block)
end

def image(attribute, variant: :thumb, **options, &block)
with_column(Body::ImageComponent.new(@table, @record, attribute, variant:, **options), &block)
def attachment(attribute, variant: :thumb, **options, &block)
with_column(Body::AttachmentComponent.new(@table, @record, attribute, variant:, **options), &block)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/components/koi/tables/header_row_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def text(attribute, **attributes, &block)
header_cell(attribute, **attributes, &block)
end

def image(attribute, **attributes, &block)
def attachment(attribute, **attributes, &block)
header_cell(attribute, **attributes, &block)
end

Expand Down
10 changes: 6 additions & 4 deletions lib/generators/koi/admin_views/admin_views_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ def govuk_input_for(attribute)
%(<%= form.govuk_check_box_field :#{attribute.name} %>)
when :date
%(<%= form.govuk_date_field :#{attribute.name}, legend: { size: "s" } %>)
when :text
%(<%= form.govuk_rich_text_area :#{attribute.name} %>)
when :rich_text
when :rich_text, :text
%(<%= form.govuk_rich_text_area :#{attribute.name} %>)
when :attachment
%(<%= form.govuk_image_field :#{attribute.name} %>)
else
""
end
Expand All @@ -61,6 +61,8 @@ def summary_attribute_for(attribute)
%(<% dl.datetime :#{attribute.name} %>)
when :rich_text
%(<% dl.rich_text :#{attribute.name} %>)
when :attachment
%(<% dl.attachment :#{attribute.name} %>)
else
%(<% dl.text :#{attribute.name} %>)
end
Expand All @@ -79,7 +81,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
3 changes: 3 additions & 0 deletions lib/koi/config.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# frozen_string_literal: true

require "active_support/configurable"
require "active_support/core_ext/numeric"

module Koi
class Config
include ActiveSupport::Configurable

config_accessor(:resource_name_candidates) { %i[title name] }

config_accessor(:image_size_limit) { 10.megabytes }
end
end
4 changes: 2 additions & 2 deletions lib/koi/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def admin_discard(text = "Discard", url: :back, **kwargs)
# @api internal
# @see GOVUKDesignSystemFormBuilder::Builder#govuk_document_field
def govuk_document_field(attribute_name, hint: {}, **kwargs, &block)
max_size = hint[:max_size] || App::PERMITTED_IMAGE_SIZE
max_size = hint[:max_size] || Koi.config.image_size_limit
super(attribute_name, hint:, **kwargs) do
if block
concat(yield)
Expand All @@ -50,7 +50,7 @@ def govuk_document_field(attribute_name, hint: {}, **kwargs, &block)
# @api internal
# @see GOVUKDesignSystemFormBuilder::Builder#govuk_image_field
def govuk_image_field(attribute_name, hint: {}, **kwargs, &block)
max_size = hint[:max_size] || App::PERMITTED_IMAGE_SIZE
max_size = hint[:max_size] || Koi.config.image_size_limit
super(attribute_name, hint:, **kwargs) do
if block
concat(yield)
Expand Down
18 changes: 9 additions & 9 deletions spec/components/koi/tables/body_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,6 @@
end
end

describe Koi::Tables::Body::ImageComponent do
it "renders column" 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

describe Koi::Tables::Body::RichTextComponent do
it "renders column" do
component = described_class.new(table, record, :content)
Expand Down Expand Up @@ -125,4 +116,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

0 comments on commit 248ca7a

Please sign in to comment.