Skip to content
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

Implement multiple option for select field. #3426

Closed
4 tasks
Paul-Bob opened this issue Nov 15, 2024 · 20 comments · Fixed by #3572
Closed
4 tasks

Implement multiple option for select field. #3426

Paul-Bob opened this issue Nov 15, 2024 · 20 comments · Fixed by #3572
Assignees
Labels
Enhancement Not necessarily a feature, but something has improved Stale exempt

Comments

@Paul-Bob
Copy link
Contributor

Paul-Bob commented Nov 15, 2024

Feature

Right now the select field do not allow multiple selections. Let's add the multiple option for the select field which will enable the multiple-select mode.

Approach

https://www.loom.com/share/7cdb77e591de4c27bb6115afab147b7f?sid=0748b730-efc4-45ad-b08e-8c5ad71a4dbc

  • Add multiple option on the select field that defaults to false
  • Pass the field.multiple option to the form helper on the view components
  • Ensure that is working as expected and add tests for the option
  • Document the multiple option including the version since its available

https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select_multiple

@Paul-Bob Paul-Bob added the Enhancement Not necessarily a feature, but something has improved label Nov 15, 2024
@Paul-Bob Paul-Bob moved this to To Do in Issues Nov 15, 2024
@zhephyn
Copy link
Contributor

zhephyn commented Nov 18, 2024

Hey @Paul-Bob. Could this be assigned to me. I presume this is the newly opened issue you had referenced earlier on.

@Paul-Bob
Copy link
Contributor Author

Yes @zhephyn sure! Thank you! Let me know how it goes and if you need any assistance!

@Paul-Bob
Copy link
Contributor Author

Hei @zhephyn I just added a loom on the description, hope it helps

@zhephyn
Copy link
Contributor

zhephyn commented Nov 20, 2024

Hey @Paul-Bob. Thanks for the added context. I've been working on the issue so far, and I think I'll have a PR ready shortly.

@zhephyn
Copy link
Contributor

zhephyn commented Nov 21, 2024

Hey @Paul-Bob. I've successfully made the required changes on the back end, however on testing out the feature, I get an "un permitted params" error. Which is most definitely because the stage field has not been updated to accept an array of values instead of a single value as part of the create action. I checked the projects controller and it turns out the code for specifying the params is not here. Also, given that the controller had no code, it seemed non reasonable to make the changes here. Could you direct me about where this controller functionality exists in the code base. Specifically where I can make the change to allow the stage params to accept an array instead of a single value. Thanks in advance

@ObiWanKeoni
Copy link
Contributor

Hi @zhephyn and @Paul-Bob,

I've been following Avo pretty closely for some time now and have been using it extensively in my day job. I want to thank you both, @adrianthedev, and the rest of the community for the work you're doing here.

Anyway, to answer your question @zhephyn, I think you'd want to override the to_permitted_param method on the select field to conditionally look like the override in the FilesField

I hope this helps!

@Paul-Bob
Copy link
Contributor Author

Hey @Paul-Bob. I've successfully made the required changes on the back end

Amazing news @zhephyn!

however on testing out the feature, I get an "un permitted params" error. Which is most definitely because the stage field has not been updated to accept an array of values instead of a single value as part of the create action. I checked the projects controller and it turns out the code for specifying the params is not here. Also, given that the controller had no code, it seemed non reasonable to make the changes here.

You took the right path and you're right, the projects controller is generated for the specific project model so it's not reasonable to make the changes there.

Could you direct me about where this controller functionality exists in the code base. Specifically where I can make the change to allow the stage params to accept an array instead of a single value. Thanks in advance

As @ObiWanKeoni mentioned, we need to override the to_permitted_param on the select field. Let's do a conditional there and when multiple is true the id of that field should accept array on params, otherwise we can call super. Let me know if you have any question

@Paul-Bob
Copy link
Contributor Author

Hi @zhephyn and @Paul-Bob,
I've been following Avo pretty closely for some time now and have been using it extensively in my day job. I want to thank you both, @adrianthedev, and the rest of the community for the work you're doing here.

Hi @ObiWanKeoni, thank you for the feedback, we truly appreciate it!

Anyway, to answer your question @zhephyn, I think you'd want to override the to_permitted_param method on the select field to conditionally look like the override in the FilesField
I hope this helps!

Also thank you for your contribution! Great guidance here =)

@zhephyn
Copy link
Contributor

zhephyn commented Nov 23, 2024

Hey @Paul-Bob and @ObiWanKeoni. Thanks for the guidance. Let me get on it ASAP.

@zhephyn
Copy link
Contributor

zhephyn commented Nov 26, 2024

Hey @Paul-Bob. Finally got the feature working. I realized though that I ought to write tests for the feature before submitting the PR. Could you please guide me on how to approach this? Are we testing that the feature works for both new and edit project forms? Or it's just for one of them? Thanks in advance

@Paul-Bob
Copy link
Contributor Author

Hey @Paul-Bob. Finally got the feature working.

Hi @zhephyn, amazing!

I realized though that I ought to write tests for the feature before submitting the PR. Could you please guide me on how to approach this?

Sure, we have some tests for the select field, I'll include the path here. Let me know if you can inspire yourself from those or you need a more detailed approach.

  • spec/features/avo/select_field_spec.rb
  • spec/features/avo/select_field/enum_array_display_value_false_spec.rb
  • spec/features/avo/select_field/enum_hash_display_value_true_spec.rb

Are we testing that the feature works for both new and edit project forms? Or it's just for one of them?

When multiple: true is configured on a field it should work on all form views (new and edit).

Thanks in advance

Anytime, thank you!

@zhephyn
Copy link
Contributor

zhephyn commented Dec 3, 2024

Hey @Paul-Bob . After banging my head against the wall trying to figure this out, i thought it would be better to seek out your guidance. The approach i had used, while it had worked had involved me deleting the enum declaration in the project model, which now coming to think of it, might not be the best approach for solving this. Is there a way i could make this work while at the same type keeping the enum declaration in the project.rb model file, because as much as my stage column has been modified to accept array values as per the schema.rb, i keep on running into the same "Argument error " when i submit the create form. Stating that "["idea", "drafting", "discovery"] is not a valid stage name.
SCHEMA.rb
t.string "stage", default: [], array: true
enum declaration in project model
if Gem::Version.new(Rails.version) >= Gem::Version.new("7.3.0") enum :stage, { Discovery: "discovery", Idea: "idea", Done: "done", "On hold": "on hold", Cancelled: "cancelled", Drafting: "drafting" }, multiple: true else enum stage: { Discovery: "discovery", Idea: "idea", Done: "done", "On Hold": "on hold", Cancelled: "cancelled", Drafting: "drafting" }, multiple: true end
Thanks so much for your assistance in advance.

@Paul-Bob
Copy link
Contributor Author

Paul-Bob commented Dec 4, 2024

Hello @zhephyn.

Thanks for reaching out.

Let's keep the current Project configuration and add another column, for example add sizes (where the options can be small, medium, large, etc.) to Products as an array and test the multiple select there

The current stage field is tested in several test cases, and any modifications to it could potentially disrupt those tests.

@zhephyn
Copy link
Contributor

zhephyn commented Dec 4, 2024

Hey @Paul-Bob. Yeah most of the tests I looked into were linked to the stage column, so any changes applied to it would certainly require modifications to those test cases. Anyway, about the new column, should I add a provision for it in the UI too, such that the user can select the options from there when creating the project as part of the form? Thanks again for the feedback 🙏

@Paul-Bob
Copy link
Contributor Author

Paul-Bob commented Dec 4, 2024

Anyway, about the new column, should I add a provision for it in the UI too, such that the user can select the options from there when creating the project as part of the form? Thanks again for the feedback 🙏

Yes something like field :sizes, as: :select, multiple: :true, ... on the resource. But let's add it to Product since Project already have a fair amount of attributes.

@zhephyn
Copy link
Contributor

zhephyn commented Dec 13, 2024

Hey @Paul-Bob. Is the approach for testing this implementation the same as that for the projects? Should the tests be only for the select_field_spec.rb file?

@Paul-Bob
Copy link
Contributor Author

Is the approach for testing this implementation the same as that for the projects? Should the tests be only for the select_field_spec.rb file?

Yes, since we're still testing the select field, it makes sense to keep the tests for multiple option on the same file

Copy link
Contributor

This issue has been marked as stale because there was no activity for the past 15 days.

@zhephyn
Copy link
Contributor

zhephyn commented Jan 3, 2025

Hey @Paul-Bob . Hope all is well on your end. I've submitted a PR for this issue. I would appreciate it if you looked at it.
#3572
Regarding the tests, while they currently pass, i think they could use a bit of refactoring though I'm not sure how to go about that. Kindly get back to me in case of any feedback you may have. I will be very happy to make changes where possible.
Finally, i wasn't sure whether to commit and push the migration and the schema.rb files or not, so i included them. Incase, they ought not to have been included, kindly let me know and I'll make the necessary changes.
PS: HAPPY NEW YEAR 2025

@Paul-Bob
Copy link
Contributor Author

Paul-Bob commented Jan 6, 2025

Thank you, happy new year!

Let's continue the discussion on the PR

@github-project-automation github-project-automation bot moved this from To Do to Done in Issues Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Not necessarily a feature, but something has improved Stale exempt
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants