lib/avo/fields/belongs_to_field.rb in avo-2.4.1 vs lib/avo/fields/belongs_to_field.rb in avo-2.5.0
- old
+ new
@@ -60,10 +60,12 @@
class BelongsToField < BaseField
attr_reader :polymorphic_as
attr_reader :relation_method
attr_reader :types # for Polymorphic associations
attr_reader :allow_via_detaching
+ attr_reader :scope
+ attr_reader :polymorphic_help
def initialize(id, **args, &block)
args[:placeholder] ||= I18n.t("avo.choose_an_option")
super(id, **args, &block)
@@ -71,10 +73,12 @@
@searchable = args[:searchable] == true
@polymorphic_as = args[:polymorphic_as]
@types = args[:types]
@relation_method = id.to_s.parameterize.underscore
@allow_via_detaching = args[:allow_via_detaching] == true
+ @scope = args[:scope]
+ @polymorphic_help = args[:polymorphic_help]
end
def searchable
@searchable && ::Avo::App.license.has_with_trial(:searchable_associations)
end
@@ -109,10 +113,16 @@
def values_for_type(model = nil)
resource = target_resource
resource = App.get_resource_by_model_name model if model.present?
- ::Avo::Services::AuthorizationService.apply_policy(user, resource.class.query_scope).all.map do |model|
+ query = Avo::Services::AuthorizationService.apply_policy(user, resource.class.query_scope)
+
+ if scope.present?
+ query = Avo::Hosts::AssociationScopeHost.new(block: scope, query: query, parent: get_model).handle
+ end
+
+ query.all.map do |model|
[model.send(resource.class.title), model.id]
end
end
def database_value