-
Notifications
You must be signed in to change notification settings - Fork 16
Add drag-and-drop positioning for categories scoped by category type #712
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
base: main
Are you sure you want to change the base?
Conversation
|
@jmilljr24 lgtm, but you have more context on gem implementation. main thing is scoping the position uniqueness to within the CategoryType (via metadatum_id, until that table gets renamed and it can be category_type_id) i've got a separate pr to fix the allow_nil in the category model. |
Awesome! Yes, scoping is a great feature!! |
|
Check out the stimulus controller we have |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@maebeale Love this feature. Can you update the stimulus sortable controller to send the param as position: instead of ordering: I missed it when we changed everything over to the same column name. Faq is broken at the moment.
body: JSON.stringify({ position: newIndex + 1 })
Once merged we can just run Category.heal_position_column! in production to dedup positions and remove the default value.
| if params[:ordering].present? | ||
| ordering = params[:ordering] | ||
| # Validate ordering is a valid positive integer | ||
| if ordering.to_s.match?(/^\d+$/) && (ordering = ordering.to_i) > 0 | ||
| if @category.update(position: ordering) | ||
| head :ok | ||
| else | ||
| head :unprocessable_entity | ||
| end | ||
| else | ||
| head :bad_request | ||
| end | ||
| return | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't need this. @category.update(category_params) should do the trick and the position gem will handle validation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jmilljr24 i removed it and the UI stopped working and my tests were failing, so i'm going to keep it for now and figure out how to remove it later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nvm. i see now it's bc of the stimulus controller sending ordering param instead of position!
Co-authored-by: maebeale <7607813+maebeale@users.noreply.github.com>
Co-authored-by: maebeale <7607813+maebeale@users.noreply.github.com>
…coverage Co-authored-by: maebeale <7607813+maebeale@users.noreply.github.com>
…rmatting Co-authored-by: maebeale <7607813+maebeale@users.noreply.github.com>
* note * Small fixes
… as the column did
…me (tho no positions should match using position gem)
7e07d0e to
f6ce54f
Compare
| const url = this.urlValue.replace(":id", id); | ||
| put(url, { | ||
| body: JSON.stringify({ ordering: newIndex + 1 }), // add 1 to change sortablejs 0-based index to 1-based for positioning gem | ||
| body: JSON.stringify({ position: newIndex + 1 }), // add 1 to change sortablejs 0-based index to 1-based for positioning gem |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!!!
What is the goal of this PR and why is this important?
Implements drag-and-drop reordering for categories on the categories index page, matching the existing FAQ implementation. Positions are scoped to
metadatum_id(category_type) so categories of different types maintain independent position sequences.How did you approach the change?
Model (
category.rb)positioned scope: :metadatum_idto enable position management per category typeController (
categories_controller.rb)orderingparameter inupdateaction for drag-and-drop position updatesView (
categories/index.html.erb)data-sortable-handleicondata-controller="sortable"anddata-sortable-url-valueto tbodydata-sortable-idto each rowTests
Reuses existing
sortable_controller.jsStimulus controller andpositioninggem (v0.4.7).Anything else to add?
Position updates work across paginated results since the positioning gem handles server-side state. Categories can share position values across different category types due to the metadatum_id scope.
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.