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

fix: belongs_to field should use association primary_key #3665

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/avo/fields/belongs_to_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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
[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
Expand Down Expand Up @@ -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)

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
Expand Down
11 changes: 11 additions & 0 deletions spec/features/avo/belongs_to_polymorphic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@
expect(page.body).to have_text "Commentable"
expect(field_element_by_resource_id("commentable", comment.id)).to have_link project.name, href: "/admin/resources/projects/#{project.id}"
end

context "custom primary key (UUID) handling" do
let!(:uuid_project) { create :project, id: SecureRandom.uuid, name: "UUID Project" }
let!(:comment) { create :comment, commentable: uuid_project }

it "assigns the correct uuid to the polymorphic association" do
expect(comment.commentable_id).to eq(uuid_project.id)
end

# ToDo spec with relation belongs_to with primary_key
end
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions spec/features/avo/belongs_to_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@
end
end

context "custom primary key (UUID) handling" do
let!(:uuid_user) { create :user, id: SecureRandom.uuid }
let!(:post) { create :post, user: uuid_user }

it "displays the user link using the UUID and stores the correct foreign key" do
visit "/admin/resources/posts/#{post.slug}"

expect(page).to have_link uuid_user.name, href: "/admin/resources/users/#{uuid_user.slug}?via_record_id=#{post.slug}&via_resource_class=Avo%3A%3AResources%3A%3APost"

post.reload

expect(post.user_id).to eq(uuid_user.id)
end
end

describe "with user attached" do
let!(:post) { create :post, user: admin }
let!(:second_user) { create :user }
Expand Down
Loading