Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable to pass html options to URL field #2139

Merged
merged 1 commit into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/views/fields/url/_index.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 %>
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
shouichi marked this conversation as resolved.
Show resolved Hide resolved

private

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

describe "fields/url/_index", 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 html options" do
url = instance_double(
"Administrate::Field::Url",
data: product.image_url,
html_options: { referrerpolicy: "no-referrer" },
)

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

expect(rendered).to have_css(
%{a[href="#{product.image_url}"][referrerpolicy="no-referrer"]},
text: product.image_url,
)
end
end
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 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