Skip to content

Commit

Permalink
Pass options to link element
Browse files Browse the repository at this point in the history
  • Loading branch information
hasarindaKI authored and sfnelson committed Mar 1, 2024
1 parent 12718ac commit 6d67dbe
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
25 changes: 15 additions & 10 deletions app/components/koi/tables/body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

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 @@ -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)
Expand Down
15 changes: 13 additions & 2 deletions spec/components/koi/tables/body_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
<td>
Expand Down Expand Up @@ -45,14 +45,25 @@
end

it "supports content" do
component = described_class.new(table, record, :name).with_content("Redirect from #{record.name}")
component = described_class.new(table, record, :name, url: [:admin, record])
.with_content("Redirect from #{record.name}")
rendered = render_inline(component)
expect(rendered).to match_html(<<~HTML)
<td>
<a href="#{polymorphic_path([:admin, record])}">Redirect from #{record.name}</a>
</td>
HTML
end

it "supports attributes for anchor tag" do
component = described_class.new(table, record, :name, url: [:admin, record], link: { class: "custom" })
rendered = render_inline(component)
expect(rendered).to match_html(<<~HTML)
<td>
<a class="custom" href="#{polymorphic_path([:admin, record])}">#{record.name}</a>
</td>
HTML
end
end

describe Koi::Tables::Body::ImageComponent do
Expand Down

0 comments on commit 6d67dbe

Please sign in to comment.