Skip to content

Use association name in selects#462

Merged
difernandez merged 8 commits into
masterfrom
use-association-name-in-selects
Apr 24, 2023
Merged

Use association name in selects#462
difernandez merged 8 commits into
masterfrom
use-association-name-in-selects

Conversation

@difernandez

@difernandez difernandez commented Apr 19, 2023

Copy link
Copy Markdown
Contributor

Motivation / Background

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:

f.input :city_id, as: :nested_select,
                  level_1: { attribute: :country_id },
                  level_2: { attribute: :region_id },
                  level_3: { attribute: :city_id }
f.input :user_id, as: :search_select, display_name: :email, url: admin_users_path, fields: [:email]

becomes this:

f.input :city, as: :nested_select,
               level_1: { attribute: :country },
               level_2: { attribute: :region },
               level_3: { attribute: :city }
f.input :user, as: :search_select, display_name: :email, url: admin_users_path, fields: [:email]

To achieve this, these changes were done:

  • 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

Additional information

Examples of inputs now showing validation errors

Given the search select input for required user:

f.input :user, as: :search_select, display_name: :email, url: admin_users_path, fields: [:email]

image

Given the nested input for required item:

f.input :item, as: :nested_select,
               level_1: { attribute: :brand },
               level_2: { attribute: :item }

image

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a concise description of what changed and why.
  • Tests are added or updated if you fix a bug or add a feature.
  • Documentation has been added or updated if you add a feature or modify an existing one.
  • 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).
  • My changes don't introduce any linter rule violations.

@difernandez difernandez requested a review from ldlsegovia April 19, 2023 19:45

@ldlsegovia ldlsegovia left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👏🏻

require_relative "input_helpers/input_html_helpers"

module ActiveAdminAddons
class SelectInputBase < Formtastic::Inputs::SelectInput

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You did this to inherit from Formtastic::Inputs::SelectInput right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@difernandez difernandez merged commit 458f22c into master Apr 24, 2023
@difernandez difernandez deleted the use-association-name-in-selects branch April 24, 2023 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Ajax Select2 doesn't show validation errors

2 participants