8000 Improve form f.inputs attributes rendering by amiel · Pull Request #8439 · activeadmin/activeadmin · GitHub
[go: up one dir, main page]

Skip to content

Improve form f.inputs attributes rendering #8439

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

Merged
merged 3 commits into from
Aug 23, 2024

Conversation

amiel
Copy link
Contributor
@amiel amiel commented Aug 21, 2024

This resolves #8436 by using standard rails helpers to build html tags and attributes.

Note that this would be easy to backport to v3 and I'm happy to open another PR if a backported release would be considered.

@amiel amiel force-pushed the improve-f-inputs-attributes branch from 86aa14a to 95996d4 Compare August 21, 2024 16:46
Copy link
Contributor
@mgrunberg mgrunberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amiel thanks for this! Looks good to me

Copy link
codecov bot commented Aug 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.11%. Comparing base (69c2872) to head (4312e2b).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #8439   +/-   ##
=======================================
  Coverage   99.11%   99.11%           
=======================================
  Files         141      141           
  Lines        4055     4060    +5     
=======================================
+ Hits         4019     4024    +5     
  Misses         36       36           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines 136 to 137
@opening_tag = "<fieldset #{fieldset_attrs}>#{legend_tag}<ol>"
@closing_tag = "</ol></fieldset>"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that it's not so trivial to use rails helpers to build the fieldset and ol since they are fragments.

private

def tag_attributes(html_options)
# When support for Rails 6.x is dropped, use tag.attributes instead.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a standard way to mark code to be reconsidered after rails upgrades?

@amiel amiel marked this pull request as ready for review August 21, 2024 21:51
Comment on lines 143 to 151
def tag_attributes(html_options)
# When support for Rails 6.x is dropped, use tag.attributes instead.
#
# tag.attributes html_options
#
tag.tag_options(html_options.to_h).to_s.strip.html_safe
end
Copy link
Member
@javierjulio javierjulio Aug 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that this is done to support both Rails 6.1 and 7+ but we want to use a conditional, so if it's Rails 6.1 we use the tag.tag_options method, otherwise always use the current tag.attributes method. I would expect the former to be removed from Rails at some point in the future so that's why I want to make sure we isolate it to only those still running Rails 6.1 which eventually we'll drop support for.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find any examples of checking the rails version in the ActiveAdmin codebase. Would you prefer feature detection or a version check?

For example, something like this?

# Rails.version is a String, and both "6.1" <=> "7" and "7.1" <=> "7" work as expected
if Rails.version < "7"

or this?

if tag.respond_to?(:attributes)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@javierjulio I went with the latter approach, but am happy to revisit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use if Rails::VERSION::MAJOR <= 6 to isolate the old method and that way the else condition is for the current approach. I would be ok with feature detection but favor the version check in this case because otherwise we'd be falling back to the old approach.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Feature detection didn't work anyway 🤦🏼‍♂️

@amiel amiel force-pushed the improve-f-inputs-attributes branch 3 times, most recently from dfd45c3 to c5b1536 Compare August 22, 2024 17:01
10000
@amiel
Copy link
Contributor Author
amiel commented Aug 22, 2024

Is it possible to re-run CI?

This looks like a transient error:

test (3.1, rails_72)
GitHub Actions has encountered an internal error when running your job

@javierjulio
Copy link
Member

@amiel GitHub Actions has been reporting issues for the past hour and was just resolved a few minutes ago. Either way looks like latest build is green so it's ok.

@amiel
Copy link
Contributor Author
amiel commented Aug 22, 2024

Thanks @javierjulio. I think this PR is ready to go.

I appreciate you taking the time to help me through this change. It will clean up a bunch of code at Bento.

Would you consider another PR based on the 3-0-stable branch? We're not quite ready to upgrade to v4.

@javierjulio javierjulio force-pushed the improve-f-inputs-attributes branch from c5b1536 to 4312e2b Compare August 23, 2024 03:37
Copy link
Member
@javierjulio javierjulio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@javierjulio javierjulio changed the title Improve rendering attributes for f.inputs Improve form f.inputs attributes rendering Aug 23, 2024
@javierjulio javierjulio merged commit 37f0545 into activeadmin:master Aug 23, 2024
24 checks passed
@javierjulio
Copy link
Member

@amiel you're welcome. I will not do a backport release but @mgrunberg has done those previously. Since he engaged I believe he would consider a release if you open a PR based on the 3-0-stable branch. I'll let him follow up. Thanks again.

amiel added a commit to amiel/active_admin that referenced this pull request Aug 23, 2024
* Improve rendering attributes for f.inputs

* Support rails 6.1

but use tag.attributes if available

* Use tag.legend instead of "<legend ..."
@mgrunberg
Copy link
Contributor

@javierjulio, @amiel I have some free time today. I'm going to backport this and release it. @amiel, don't worry about the backport PR.

mgrunberg pushed a commit that referenced this pull request Aug 23, 2024
* Improve rendering attributes for f.inputs

* Support rails 6.1

but use tag.attributes if available

* Use tag.legend instead of "<legend ..."
@amiel amiel deleted the improve-f-inputs-attributes branch August 23, 2024 16:08
mgrunberg added a commit that referenced this pull request Aug 23, 2024
#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`.
mgrunberg added a commit that referenced this pull request Aug 23, 2024
#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`.
mgrunberg added a commit that referenced this pull request Aug 23, 2024
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`.
mgrunberg added a commit that referenced this pull request Aug 23, 2024
* 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>
@mgrunberg mgrunberg mentioned this pull request Sep 3, 2024
< 7090 div class="flex-shrink-0 my-1 my-md-0 ml-md-3"> Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0