Skip to content

Commit

Permalink
fix: belongs_to broken for some model names (#203)
Browse files Browse the repository at this point in the history
* fix: belongs_to broken for some model names

* refactor: use the klass first if available
  • Loading branch information
adrianthedev authored Dec 17, 2020
1 parent ad8945c commit b973d6c
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/avo/app/fields/belongs_to.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def hydrate_field(fields, model, resource, view)
fields[:searchable] = @searchable
fields[:is_relation] = true
fields[:database_id] = foreign_key model
target_resource = App.get_resources.find { |r| r.class == "Avo::Resources::#{name}".safe_constantize }

target_resource = get_target_resource model

relation_model = model.public_send(@relation_method)

Expand Down Expand Up @@ -61,6 +62,16 @@ def foreign_key(model)
model.class.reflections[@relation_method].foreign_key
end
end

def get_target_resource(model)
if model._reflections[id.to_s].klass.present?
App.get_resource_by_model_name model._reflections[id.to_s].klass.to_s
elsif model._reflections[id.to_s].options[:class_name].present?
App.get_resource_by_model_name model._reflections[id.to_s].options[:class_name]
else
App.get_resource_by_name class_name.to_s
end
end
end
end
end

0 comments on commit b973d6c

Please sign in to comment.