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

Support composite primary keys on show, edit, update and destroy actions #2645

Merged
merged 2 commits into from Apr 9, 2024

Conversation

filipegiusti
Copy link
Contributor

Impressively, this seems to be all that's required to support composite primary keys in Rails 7.1

Description

Support composite primary keys on show, edit, update and destroy actions on Rails 7.1.

params.extract_values was introduced in Rails 7.1.0.beta1.

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works

Screenshots & recording

Not really.

Manual review steps

We do have a model that uses 3 enums as primary keys and I was able to view it's show page, edit and update it after the change. To implement the change in our codebase we have overwriting set_record on each resource that has a composite primary key.

Copy link

codeclimate bot commented Mar 28, 2024

Code Climate has analyzed commit e1428c9 and detected 0 issues on this pull request.

View more on Code Climate.

@filipegiusti
Copy link
Contributor Author

filipegiusti commented Mar 28, 2024

This PR probably needs a test and a check to only use params.extract_values on Rails 7.1 and above. I decided to create it right away in case others want to simple copy and paste the method in their base controller like

module Avo
  class BaseResourcesController < Avo::ResourcesController
    def set_record
      id = @resource.model_class.primary_key.is_a?(Array) ? params.extract_value(:id) : params[:id]
      @record = @resource.find_record(id, query: model_scope, params:)
      @resource.hydrate(record: @record)
    end
  end
end

Copy link
Contributor

@Paul-Bob Paul-Bob left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution @filipegiusti

I let a comment with a change suggestion to avoid breaks on Rails versions that doesn't have the extract_value method.

app/controllers/avo/application_controller.rb Outdated Show resolved Hide resolved
filipegiusti and others added 2 commits April 9, 2024 10:21
Impressively, this seems to be all that's required to support composite primary keys.
… keys


`params.extract_values` was [introduced in Rails 7.1.0.beta1](rails/rails@da7a6da).

Co-authored-by: Paul Bob <69730720+Paul-Bob@users.noreply.github.com>
@Paul-Bob Paul-Bob merged commit 52bec77 into avo-hq:main Apr 9, 2024
19 checks passed
@filipegiusti filipegiusti deleted the patch-1 branch April 9, 2024 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants