Use association name in selects#462
Merged
Merged
Conversation
This allows use of association name instead of id in inputs
ldlsegovia
approved these changes
Apr 24, 2023
| require_relative "input_helpers/input_html_helpers" | ||
|
|
||
| module ActiveAdminAddons | ||
| class SelectInputBase < Formtastic::Inputs::SelectInput |
Contributor
There was a problem hiding this comment.
You did this to inherit from Formtastic::Inputs::SelectInput right?
Contributor
Author
There was a problem hiding this comment.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation / Background
Search and nested selects have to use the name of the id in order to work. This has the following drawbacks:
f.input :userinstead off.input :user_idbelongs_to, would not show, because the name of the input, i.e.user_id, would not match the name of the validated attribute, i.e.userCloses #406
Detail
This Pull Request makes both selects work with the name of the association instead of the id. This is a breaking change.
With these changes, this:
becomes this:
To achieve this, these changes were done:
SelectInputBasethat inherits fromFormtastic::Inputs::SelectInput, and use it innested_level_inputandsearch_select_input(previously a base input was being used that inherited fromFormtastic::Inputs::StringInput). This is becauseFormtastic::Inputs::SelectInputhas the logic to define the name in input_html_options from the association keyinput_basetoinput_html_helpersSearchSelectInputto workSearchSelectFilterInput, that inherits fromSearchSelectInput, required a few more changes:input_methodwas modified to useinput_name(the id), becauseeq_input_nameusesvalid_method, which would be the name of the associationinput_html_options_namechanged to used theinput_methodmentioned in the previous pointinput_valuewas overriden for the same reason asinput_method: to useinput_nameinstead ofvalid_methodnested_select_input.rb: now it sets a virtual_attr and it's value for both the association and the id. The association is needed to avoid errors likecity has no attribute :country, and the id is needed for thenested_level_inputnested_level_input.rb: Inherits now fromSelectInputBase, and gets the parent_id attribute fromparent_id_attributeinstead ofparent_attributeslim-select-nested.js: adjust places where checking or using parentExtra
Had to fix an error in an association in the dummy app
Additional information
Examples of inputs now showing validation errors
Given the search select input for required
user:Given the nested input for required
item:Checklist
Before submitting the PR make sure the following are checked: