Skip to content

Commit

Permalink
refactor: Prefer Model.reflections over private Model#_reflections. (#…
Browse files Browse the repository at this point in the history
…2927)

* Prefer Model.reflections over private Model#_reflections.

- `_reflections` returns different key type per Rails version

* Move to reflect_on_association.
  • Loading branch information
simi authored Aug 2, 2024
1 parent 15032c2 commit c08e7d9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/avo/base_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def order_actions
end

def get_record_associations(record)
record._reflections
record.class.reflections
end

def valid_association_name(record, association_name)
Expand Down Expand Up @@ -382,7 +382,7 @@ def hydrate_model_with_default_values

if field.type == "belongs_to"

reflection = @model._reflections[@params[:via_relation]]
reflection = @model.class.reflections[@params[:via_relation]]

if field.polymorphic_as.present? && field.types.map(&:to_s).include?(@params[:via_relation_class])
# set the value to the actual record
Expand Down
8 changes: 4 additions & 4 deletions lib/avo/fields/belongs_to_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@ def target_resource

reflection_key = polymorphic_as || id

if @model._reflections[reflection_key.to_s].klass.present?
App.get_resource_by_model_name @model._reflections[reflection_key.to_s].klass.to_s
elsif @model._reflections[reflection_key.to_s].options[:class_name].present?
App.get_resource_by_model_name @model._reflections[reflection_key.to_s].options[:class_name]
if @model.class.reflect_on_association(reflection_key).klass.present?
App.get_resource_by_model_name @model.class.reflect_on_association(reflection_key).klass.to_s
elsif @model.class.reflect_on_association(reflection_key).options[:class_name].present?
App.get_resource_by_model_name @model.class.reflect_on_association(reflection_key).options[:class_name]
else
App.get_resource_by_name reflection_key.to_s
end
Expand Down
8 changes: 4 additions & 4 deletions lib/avo/fields/has_base_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ def field_label
end

def target_resource
if @model._reflections[id.to_s].klass.present?
Avo::App.get_resource_by_model_name @model._reflections[id.to_s].klass.to_s
elsif @model._reflections[id.to_s].options[:class_name].present?
Avo::App.get_resource_by_model_name @model._reflections[id.to_s].options[:class_name]
if @model.class.reflect_on_association(id).klass.present?
Avo::App.get_resource_by_model_name @model.class.reflect_on_association(id).klass.to_s
elsif @model.class.reflect_on_association(id).options[:class_name].present?
Avo::App.get_resource_by_model_name @model.class.reflect_on_association(id).options[:class_name]
else
Avo::App.get_resource_by_name id.to_s
end
Expand Down

0 comments on commit c08e7d9

Please sign in to comment.