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 committed Feb 29, 2024
1 parent 12718ac commit e04943a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
16 changes: 10 additions & 6 deletions app/components/koi/tables/body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,23 @@ def default_html_attributes
end

# 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]
#
# Link options can be passed in to customise the link,
# the link text is the value of the attribute
#
# @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, link: {}, **html_attributes)
super(table, record, attribute, **html_attributes)

@url_builder = url
@url_builder = link.delete(:url) || ->(object) { [:admin, object] }
@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

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
2 changes: 1 addition & 1 deletion app/views/admin/admin_users/_admin.html+row.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<%= row.link :name, url: :admin_admin_user_path %>
<%= row.link :name, link: { url: :admin_admin_user_path } %>
<%= row.text :email %>
14 changes: 12 additions & 2 deletions spec/components/koi/tables/body_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
end

it "supports path helpers" do
component = described_class.new(table, record, :name, url: :edit_admin_post_path)
component = described_class.new(table, record, :name, link: { url: :edit_admin_post_path })
rendered = render_inline(component)
expect(rendered).to match_html(<<~HTML)
<td>
Expand All @@ -35,7 +35,7 @@

it "supports procs" do
record = create(:url_rewrite, to: "www.example.com")
component = described_class.new(table, record, :from, url: ->(object) { object.to })
component = described_class.new(table, record, :from, link: { url: ->(object) { object.to } })
rendered = render_inline(component)
expect(rendered).to match_html(<<~HTML)
<td>
Expand All @@ -53,6 +53,16 @@
</td>
HTML
end

it "supports attributes for anchor tag" do
component = described_class.new(table, record, :name, 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 e04943a

Please sign in to comment.