8000 Backport Improve form f.inputs attributes rendering (#8446) · activeadmin/activeadmin@55d88a6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 55d88a6

Browse files
mgrunbergamiel
andauthored
Backport Improve form f.inputs attributes rendering (#8446)
* 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 ..." * use helpers to prevent calling resource methods (tag admin) * be able to customize Tag resource form #8439 improved form attributes rendering but introduced an issue if host app has an admin for Tag resource. The problem is that `tag` (`tag.attributes`) refences the resource and not the `TagHelper`. Let's prefix the call with `helpers`. --------- Co-authored-by: Amiel Martin <amiel.martin@gmail.com>
1 parent 0e02889 commit 55d88a6

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

features/edit_page.feature

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@ Feature: Edit Page
66
Given a category named "Music" exists
77
And a user named "John Doe" exists
88
And a post with the title "Hello World" written by "John Doe" exists
9+
And a tag named "Bugs" exists
910
And I am logged in
10-
And a configuration of:
11+
12+
Scenario: Default form with no config
13+
Given a configuration of:
1114
"""
1215
ActiveAdmin.register Post do
1316
permit_params :custom_category_id, :author_id, :title,
1417
:body, :position, :published_date, :starred
1518
end
1619
"""
1720
When I am on the index page for posts
18-
19-
Scenario: Default form with no config
20-
Given I follow "Edit"
21+
And I follow "Edit"
2122
Then the "Title" field should contain "Hello World"
2223
And the "Body" field should contain ""
2324
And the "Category" field should contain ""
@@ -47,6 +48,7 @@ Feature: Edit Page
4748
end
4849
end
4950
"""
51+
When I am on the index page for posts
5052
And I follow "Edit"
5153
Then I should see a fieldset titled "Your Post"
5254
And I should see a fieldset titled "Publishing"
@@ -76,6 +78,7 @@ Feature: Edit Page
7678
end
7779
end
7880
"""
81+
When I am on the index page for posts
7982
And I follow "New"
8083
Then I follow "Posts"
8184
And I follow "Edit"
@@ -107,6 +110,7 @@ Feature: Edit Page
107110
form partial: "form"
108111
end
109112
"""
113+
When I am on the index page for posts
110114
And I follow "Edit"
111115
Then the "Title" field should contain "Hello World"
112116
And the "Body" field should contain ""
@@ -115,3 +119,20 @@ Feature: Edit Page
115119
Then I should see "Post was successfully updated."
116120
And I should see the attribute "Title" with "Hello World from update"
117121
And I should see the attribute "Author" with "John Doe"
122+
123+
Scenario: Generating a custom form for Tag resource
124+
Given a configuration of:
125+
"""
126+
ActiveAdmin.register Tag do
127+
form do |f|
128+
f.inputs "Details" do
129+
f.input :name
130+
end
131+
f.actions
132+
end
133+
end
134+
"""
135+
When I am on the index page for tags
136+
And I follow "Edit"
137+
Then I should see a fieldset titled "Details"
138+
And the "Name" field should contain "Bugs"

features/step_definitions/factory_steps.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ def create_user(name, type = "User")
2828
Store.create! name: name
2929
end
3030

31+
Given /^a tag named "([^"]*)" exists$/ do |name|
32+
Tag.create! name: name
33+
end
34+
3135
Given /^I create a new post with the title "([^"]*)"$/ do |title|
3236
first(:link, "Posts").click
3337
click_link "New Post"

lib/active_admin/views/components/active_admin_form.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,24 @@ def build(form_builder, *args, &block)
127127
html_options[:class] ||= "inputs"
128128
legend = args.shift if args.first.is_a?(::String)
129129
legend = html_options.delete(:name) if html_options.key?(:name)
130-
legend_tag = legend ? "<legend><span>#{ERB::Util.html_escape(legend)}</span></legend>" : ""
131-
fieldset_attrs = html_options.map { |k, v| %Q{#{k}="#{v}"} }.join(" ")
130+
legend_tag = legend ? helpers.tag.legend(legend, class: "fieldset-title") : ""
131+
fieldset_attrs = tag_attributes html_options
132132
@opening_tag = "<fieldset #{fieldset_attrs}>#{legend_tag}<ol>"
133133
@closing_tag = "</ol></fieldset>"
134134
super(*(args << html_options), &block)
135135
end
136+
137+
private
138+
139+
def tag_attributes(html_options)
140+
if Rails::VERSION::MAJOR <= 6
141+
# Reimplement tag.attributes to backport support for Rails 6.1.
142+
# TODO: this can be removed when support for Rails 6.x is dropped
143+
helpers.tag.tag_options(html_options.to_h).to_s.strip.html_safe
144+
else
145+
helpers.tag.attributes html_options
146+
end
147+
end
136148
end
137149

138150
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_selector("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