8000 Improve form f.inputs attributes rendering (#8439) · activeadmin/activeadmin@37f0545 · GitHub
[go: up one dir, main page]

Skip to content

Commit 37f0545

Browse files
authored
Improve form f.inputs attributes rendering (#8439)
* Improve rendering attributes for f.inputs * Support rails 6.1 but use tag.attributes if available * Use tag.legend instead of "<legend ..."
1 parent 69c2872 commit 37f0545

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/active_admin/views/components/active_admin_form.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,24 @@ def build(form_builder, *args, &block)
131131
html_options[:class] ||= "inputs"
132132
legend = args.shift if args.first.is_a?(::String)
133133
legend = html_options.delete(:name) if html_options.key?(:name)
134-
legend_tag = legend ? "<legend class=\"fieldset-title\">#{ERB::Util.html_escape(legend)}</legend>" : ""
135-
fieldset_attrs = html_options.map { |k, v| %Q{#{k}="#{v}"} }.join(" ")
134+
legend_tag = legend ? tag.legend(legend, class: "fieldset-title") : ""
135+
fieldset_attrs = tag_attributes html_options
136136
@opening_tag = "<fieldset #{fieldset_attrs}>#{legend_tag}<ol>"
137137
@closing_tag = "</ol></fieldset>"
138138
super(*(args << html_options), &block)
139139
end
140+
141+
private
142+
143+
def tag_attributes(html_options)
144+
if Rails::VERSION::MAJOR <= 6
145+
# Reimplement tag.attributes to backport support for Rails 6.1.
146+
# TODO: this can be removed when support for Rails 6.x is dropped
147+
tag.tag_options(html_options.to_h).to_s.strip.html_safe
148+
else
149+
tag.attributes html_options
150+
end
151+
end
140152
end
141153

142154
class SemanticActionsProxy < FormtasticProxy

spec/unit/form_builder_spec.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def build_form(options = {}, form_object = Post.new, &block)
6262
context "with custom settings" do
6363
let :body do
6464
build_form do |f|
65-
f.inputs class: "custom_class", name: "custom_name", custom_attr: "custom_attr" do
65+
f.inputs class: "custom_class", name: "custom_name", custom_attr: "custom_attr", data: { test: "custom" } do
6666
f.input :title
6767
f.input :body
6868
end
@@ -80,6 +80,10 @@ def build_form(options = {}, form_object = Post.new, &block)
8080
it "should generate a fieldset with a custom attributes" do
8181
expect(body).to have_css("fieldset[custom_attr='custom_attr']")
8282
end
83+
84+
it "should use the rails helper for rendering attributes" do
85+
expect(body).to have_css("fieldset[data-test='custom']")
86+
end
8387
end
8488

8589
context "with XSS payload as name" do

0 commit comments

Comments
 (0)
0