Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Bob committed Sep 10, 2024
1 parent b4dd715 commit 7c498e8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
10 changes: 7 additions & 3 deletions app/controllers/avo/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,16 @@ def resource
Avo.resource_manager.get_resource_by_controller_name @resource_name
end

def association_from_params
params[:for_attribute] || params[:related_name]
end

def related_resource
field = find_association_field(resource: @resource, association: params[:related_name])
reflection = @record.class.reflect_on_association(association_from_params)

return field.use_resource if field&.use_resource.present?
field = find_association_field(resource: @resource, association: association_from_params, type: reflection.macro)

reflection = @record.class.reflect_on_association(field&.for_attribute || params[:related_name])
return field.use_resource if field&.use_resource.present?

reflected_model = reflection.klass

Expand Down
8 changes: 2 additions & 6 deletions app/controllers/avo/associations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class AssociationsController < BaseController
before_action :set_related_resource_name
before_action :set_related_resource, only: [:show, :index, :new, :create, :destroy]
before_action :set_related_authorization
before_action :set_reflection
before_action :set_reflection_field
before_action :set_related_record, only: [:show]
before_action :set_reflection
before_action :set_attachment_class, only: [:show, :index, :new, :create, :destroy]
before_action :set_attachment_resource, only: [:show, :index, :new, :create, :destroy]
before_action :set_attachment_record, only: [:create, :destroy]
Expand Down Expand Up @@ -126,7 +126,7 @@ def set_attachment_record
end

def set_reflection_field
@field = find_association_field(resource: @resource, association: @related_resource_name.to_sym)
@field = find_association_field(resource: @resource, association: @related_resource_name.to_sym, type: @reflection.macro)
@field.hydrate(resource: @resource, record: @record, view: Avo::ViewInquirer.new(:new))
rescue
end
Expand Down Expand Up @@ -171,10 +171,6 @@ def set_related_authorization
end
end

def association_from_params
@field&.for_attribute || params[:related_name]
end

def source_foreign_key
@reflection.source_reflection.foreign_key
end
Expand Down
8 changes: 4 additions & 4 deletions lib/avo/concerns/find_association_field.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module Avo
module Concerns
module FindAssociationField
def find_association_field(resource:, association:)
if params[:turbo_frame]&.starts_with?("has_")
type = params[:turbo_frame][/.*(?=_field)/]
def find_association_field(resource:, association: nil, type: nil)
if params[:turbo_frame]&.starts_with?("has_") || type.present?
type ||= params[:turbo_frame][/.*(?=_field)/]

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data High

This
regular expression
that depends on a
user-provided value
may run slow on strings with many repetitions of 'a'.

resource.get_field_definitions.find do |field|
(field.id == association.to_sym) && (field.type == type)
(field.id == association.to_sym) && (field.type == type.to_s)
end
else
resource.get_field association
Expand Down

0 comments on commit 7c498e8

Please sign in to comment.