Search and nested selects have to use the name of the id in order to work. This has the following drawbacks:
It moves us away from activeadmin's normal syntax. With a normal select, you would do f.input :user instead of f.input :user_id
Validation errors for the association, like the presence validation that comes by default when using belongs_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. user
Translation for the association wouldn't work automatically for the id attribute
Closes #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:
Added a new SelectInputBase that inherits from Formtastic::Inputs::SelectInput, and use it in nested_level_input and search_select_input (previously a base input was being used that inherited from Formtastic::Inputs::StringInput). This is because Formtastic::Inputs::SelectInput has the logic to define the name in input_html_options from the association key
To do this I had to move some methods from input_base to input_html_helpers
This change was enough for the SearchSelectInput to work
The SearchSelectFilterInput, that inherits from SearchSelectInput, required a few more changes:
input_method was modified to use input_name (the id), because eq_input_name uses valid_method, which would be the name of the association
input_html_options_name changed to used the input_method mentioned in the previous point
input_value was overriden for the same reason as input_method: to use input_name instead of valid_method
To change the behavior in the nested input, changes to various files were needed:
nested_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 like city has no attribute :country, and the id is needed for the nested_level_input
nested_level_input.rb: Inherits now from SelectInputBase, and gets the parent_id attribute from parent_id_attribute instead of parent_attribute
slim-select-nested.js: adjust places where checking or using parent
Changed docs and added entry to changelog
Extra
Had to fix an error in an association in the dummy app
Before submitting the PR make sure the following are checked:
[x] This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
[x] Commit message has a concise description of what changed and why.
[x] Tests are added or updated if you fix a bug or add a feature.
[x] Documentation has been added or updated if you add a feature or modify an existing one.
[x] CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature (under the "Unreleased" heading if this is not a version change).
[x] My changes don't introduce any linter rule violations.
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 :user
instead off.input :user_id
belongs_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.user
Closes #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:
SelectInputBase
that inherits fromFormtastic::Inputs::SelectInput
, and use it innested_level_input
andsearch_select_input
(previously a base input was being used that inherited fromFormtastic::Inputs::StringInput
). This is becauseFormtastic::Inputs::SelectInput
has the logic to define the name in input_html_options from the association keyinput_base
toinput_html_helpers
SearchSelectInput
to workSearchSelectFilterInput
, that inherits fromSearchSelectInput
, required a few more changes:input_method
was modified to useinput_name
(the id), becauseeq_input_name
usesvalid_method
, which would be the name of the associationinput_html_options_name
changed to used theinput_method
mentioned in the previous pointinput_value
was overriden for the same reason asinput_method
: to useinput_name
instead ofvalid_method
nested_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_input
nested_level_input.rb
: Inherits now fromSelectInputBase
, and gets the parent_id attribute fromparent_id_attribute
instead ofparent_attribute
slim-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: