-
-
Notifications
You must be signed in to change notification settings - Fork 277
fix: belongs_to field should use association primary_key #3665
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?
Changes from 31 commits
d84d813
6694428
0eea6e0
3e4c15c
14ec146
7c46787
5840ba5
048f19d
fc2342e
e580342
22dcaf0
ec5253a
6b3d1cd
2795894
7f9840e
44ec64b
ab78711
ed2870b
c75c9ec
5ed4c6d
9f636f3
09d3d89
b9643e3
e0808bc
dd17f6b
74370e7
6504ea9
aff6f43
a1b9396
700db5b
b4d8dbe
7ef911e
912c9fd
d7663e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -115,6 +115,13 @@ def options | |||||
values_for_type | ||||||
end | ||||||
|
||||||
def primary_key | ||||||
@primary_key ||= reflection.association_primary_key | ||||||
# Quick fix for "Polymorphic associations do not support computing the class." | ||||||
rescue | ||||||
nil | ||||||
end | ||||||
|
||||||
def values_for_type(model = nil) | ||||||
resource = target_resource | ||||||
resource = Avo.resource_manager.get_resource_by_model_class model if model.present? | ||||||
|
@@ -126,6 +133,7 @@ def values_for_type(model = nil) | |||||
end | ||||||
|
||||||
query.all.limit(Avo.configuration.associations_lookup_list_limit).map do |record| | ||||||
# to_param uses slug so checking primary_key is unnecessary | ||||||
Nevelito marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
[resource.new(record: record).record_title, record.to_param] | ||||||
end.tap do |options| | ||||||
options << t("avo.more_records_available") if options.size == Avo.configuration.associations_lookup_list_limit | ||||||
|
@@ -212,14 +220,16 @@ def fill_field(record, key, value, params) | |||||
if valid_model_class.blank? || id_from_param.blank? | ||||||
record.send(:"#{polymorphic_as}_id=", nil) | ||||||
else | ||||||
record_id = target_resource(record:, polymorphic_model_class: value.safe_constantize).find_record(id_from_param).id | ||||||
found_record = target_resource(record:, polymorphic_model_class: value.safe_constantize).find_record(id_from_param) | ||||||
|
||||||
record_id = found_record&.send(primary_key.presence || :id) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This line must necessarily have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed here 912c9fd |
||||||
|
||||||
record.send(:"#{polymorphic_as}_id=", record_id) | ||||||
end | ||||||
else | ||||||
record_id = value.blank? ? value : target_resource(record:).find_record(value).id | ||||||
found_record = value.present? ? target_resource(record:).find_record(value) : nil | ||||||
|
||||||
record.send(:"#{key}=", record_id) | ||||||
record.send(:"#{key}=", found_record&.send(primary_key.presence || :id)) | ||||||
end | ||||||
|
||||||
record | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Avo::Resources::Volunteer < Avo::BaseResource | ||
# self.includes = [] | ||
# self.attachments = [] | ||
# self.search = { | ||
# query: -> { query.ransack(id_eq: params[:q], m: "or").result(distinct: false) } | ||
# } | ||
Nevelito marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def fields | ||
field :id, as: :id | ||
field :name, as: :text | ||
field :role, as: :text | ||
field :event_uuid, as: :text | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# This controller has been generated to enable Rails' resource routes. | ||
# More information on https://docs.avohq.io/3.0/controllers.html | ||
class Avo::VolunteersController < Avo::ResourcesController | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Volunteer < ApplicationRecord | ||
belongs_to :event, foreign_key: "event_uuid", primary_key: "uuid" | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddUuidToEvents < ActiveRecord::Migration[6.1] | ||
def change | ||
add_column :events, :uuid, :uuid | ||
add_index :events, :uuid, unique: true | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CreateVolunteers < ActiveRecord::Migration[6.1] | ||
def change | ||
create_table :volunteers do |t| | ||
t.string :name | ||
t.string :role | ||
t.uuid :event_uuid, null: false | ||
|
||
t.timestamps | ||
end | ||
add_index :volunteers, :event_uuid | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -159,6 +159,16 @@ | |
end | ||
end | ||
|
||
describe "with custom primary key set" do | ||
let(:event) { create(:event, name: "Sample Event") } | ||
|
||
let!(:volunteer) { create(:volunteer, event: event) } | ||
|
||
it "displays the event link using the UUID and stores the correct foreign key" do | ||
expect(volunteer.event_uuid).to eq(event.uuid) | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is not effectively validating the changes. If we use the same setup on Let's correctly set the volunteer with |
||
|
||
describe "hidden columns if current polymorphic association" do | ||
let!(:user) { create :user } | ||
let!(:project) { create :project, name: "Haha project" } | ||
|
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.
This was a temporary fix for an error encountered during development. Could you remove this rescue and verify if the error still occurs? If it does, let's investigate the root cause and implement a more robust solution if possible. Thanks!