diff --git a/app/components/koi/tables/body.rb b/app/components/koi/tables/body.rb index 1cdccb8e2..bfcbcf639 100644 --- a/app/components/koi/tables/body.rb +++ b/app/components/koi/tables/body.rb @@ -73,27 +73,32 @@ def default_html_attributes # Displays a link to the record # The link text is the value of the attribute - # @param url [Proc] a proc that takes the record and returns the url, defaults to [:admin, record] + # @see Koi::Tables::BodyRowComponent#link class LinkComponent < BodyCellComponent - def initialize(table, record, attribute, url: ->(object) { [:admin, object] }, **attributes) - super(table, record, attribute, **attributes) + def initialize(table, record, attribute, url:, link: {}, **options) + super(table, record, attribute, **options) - @url_builder = url + @url = url + @link_options = link end def call content # ensure content is set before rendering options - link = content.present? && url.present? ? link_to(content, url) : content.to_s + link = content.present? && url.present? ? link_to(content, url, @link_options) : content.to_s content_tag(@type, link, **html_attributes) end def url - @url ||= if @url_builder.is_a?(Symbol) - helpers.public_send(@url_builder, record) - else - @url_builder.call(record) - end + case @url + when Symbol + # helpers are not available until the component is rendered + @url = helpers.public_send(@url, record) + when Proc + @url = @url.call(record) + else + @url + end end end diff --git a/app/components/koi/tables/body_row_component.rb b/app/components/koi/tables/body_row_component.rb index f45c5204e..5c6e01353 100644 --- a/app/components/koi/tables/body_row_component.rb +++ b/app/components/koi/tables/body_row_component.rb @@ -28,8 +28,8 @@ def rich_text(attribute, **options, &block) with_column(Body::RichTextComponent.new(@table, @record, attribute, **options), &block) end - def link(attribute, **options, &block) - with_column(Body::LinkComponent.new(@table, @record, attribute, **options), &block) + def link(attribute, url: [:admin, @record], link: {}, **attributes, &block) + with_column(Body::LinkComponent.new(@table, @record, attribute, url:, link:, **attributes), &block) end def text(attribute, **options, &block) diff --git a/spec/components/koi/tables/body_spec.rb b/spec/components/koi/tables/body_spec.rb index ff561f622..a63be6aba 100644 --- a/spec/components/koi/tables/body_spec.rb +++ b/spec/components/koi/tables/body_spec.rb @@ -14,7 +14,7 @@ describe Koi::Tables::Body::LinkComponent do it "renders column" do - component = described_class.new(table, record, :name) + component = described_class.new(table, record, :name, url: [:admin, record]) rendered = render_inline(component) expect(rendered).to match_html(<<~HTML)