Skip to content

Commit

Permalink
Enable to pass html options to URL field
Browse files Browse the repository at this point in the history
  • Loading branch information
shouichi committed Feb 8, 2022
1 parent 0b4b7b7 commit de79b76
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/views/fields/url/_show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ By default, the value is rendered as an `a` element.
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/Url
%>

<%= content_tag :a, href: field.data do %>
<%= content_tag :a, href: field.data, **field.html_options do %>
<%= field.data %>
<% end %>
3 changes: 3 additions & 0 deletions docs/customizing_dashboards.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ Default is `true`.
`:truncate` - Set the number of characters to display in the index view.
Defaults to `50`.

`:html_options` - Specify anchor tag attributes (e.g., `target="_blank"`).
Defaults is `{}`.

**Field::Password**

`:searchable` - Specify if the attribute should be considered when searching.
Expand Down
4 changes: 4 additions & 0 deletions lib/administrate/field/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def truncate
data.to_s[0...truncation_length]
end

def html_options
@options[:html_options] || {}
end

private

def truncation_length
Expand Down
25 changes: 23 additions & 2 deletions spec/administrate/views/fields/url/_show_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
require "rails_helper"

describe "fields/url/_show", type: :view do
let(:product) do
build(:product, image_url: "https://thoughtbot.com/image.jpg")
end

it "renders url" do
product = create(:product, image_url: "https://thoughtbot.com/image.jpg")
url = instance_double(
"Administrate::Field::Url",
data: product.image_url,
attribute: :image_url,
html_options: {},
)

render(
Expand All @@ -19,4 +22,22 @@
text: product.image_url,
)
end

it "renders extra html options" do
url = instance_double(
"Administrate::Field::Url",
data: product.image_url,
html_options: { target: :_blank },
)

render(
partial: "fields/url/show",
locals: { field: url, namespace: :admin },
)

expect(rendered).to have_css(
%{a[href="#{product.image_url}"][target="_blank"]},
text: product.image_url,
)
end
end

0 comments on commit de79b76

Please sign in to comment.