From ebf5a861e3a8e132f87518733a510fcaad248c0a Mon Sep 17 00:00:00 2001 From: Shouichi Kamiya Date: Tue, 8 Feb 2022 12:17:33 +0900 Subject: [PATCH] Enable to pass html options to URL field --- app/views/fields/url/_index.html.erb | 2 +- app/views/fields/url/_show.html.erb | 2 +- docs/customizing_dashboards.md | 3 +++ lib/administrate/field/url.rb | 4 +++ .../views/fields/url/_index_spec.rb | 25 +++++++++++++++++-- .../views/fields/url/_show_spec.rb | 25 +++++++++++++++++-- 6 files changed, 55 insertions(+), 6 deletions(-) diff --git a/app/views/fields/url/_index.html.erb b/app/views/fields/url/_index.html.erb index 6d661ce19c..32f3641382 100644 --- a/app/views/fields/url/_index.html.erb +++ b/app/views/fields/url/_index.html.erb @@ -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 %> diff --git a/app/views/fields/url/_show.html.erb b/app/views/fields/url/_show.html.erb index 9131576c31..a85a30483e 100644 --- a/app/views/fields/url/_show.html.erb +++ b/app/views/fields/url/_show.html.erb @@ -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 %> diff --git a/docs/customizing_dashboards.md b/docs/customizing_dashboards.md index aa1948b917..a79b2d2061 100644 --- a/docs/customizing_dashboards.md +++ b/docs/customizing_dashboards.md @@ -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. diff --git a/lib/administrate/field/url.rb b/lib/administrate/field/url.rb index 334d8cb9be..b5f1dad2a5 100644 --- a/lib/administrate/field/url.rb +++ b/lib/administrate/field/url.rb @@ -11,6 +11,10 @@ def truncate data.to_s[0...truncation_length] end + def html_options + @options[:html_options] || {} + end + private def truncation_length diff --git a/spec/administrate/views/fields/url/_index_spec.rb b/spec/administrate/views/fields/url/_index_spec.rb index eec86516ce..e5719ae215 100644 --- a/spec/administrate/views/fields/url/_index_spec.rb +++ b/spec/administrate/views/fields/url/_index_spec.rb @@ -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( @@ -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 diff --git a/spec/administrate/views/fields/url/_show_spec.rb b/spec/administrate/views/fields/url/_show_spec.rb index 5cf320939f..e4d0c16d6f 100644 --- a/spec/administrate/views/fields/url/_show_spec.rb +++ b/spec/administrate/views/fields/url/_show_spec.rb @@ -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( @@ -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