require 'administrate/field/belongs_to' require 'rails' module Administrate module Field class BelongsToSearch < Administrate::Field::BelongsTo class Engine < ::Rails::Engine initializer 'administrate-field-belongs_to_search.add_assets' do |app| app.config.assets.precompile << 'belongs_to_search.js' if app.config.respond_to? :assets Administrate::Engine.add_javascript 'belongs_to_search.js' if defined?(Administrate::Engine) end end def associated_class super end def belong_to_scope_package(current_user, model) # model is Package # create a room need package reference model_variable = model.to_s.underscore.pluralize #=> packages # so we scope the current user packages # methode send(...) t.ly/56cI scoped_model = current_user.send(model_variable).pluck(:id) candidates = Package.find(*scoped_model) # => array of Packages # but we need an activeRecordRelation and no an array # let transform that array to an activeRecordRelation here t.ly/2D0A candidates_activeRecord = Package.where(id: candidates.map(&:id)) # => Package::ActiveRecord_Relation [nil] + candidates_activeRecord.map do |resource| [display_candidate_resource(resource), resource.send(primary_key)] end end def associated_resource_options # candidate_resources is Package::ActiveRecord_Relation [nil] + candidate_resources.map do |resource| # resource is #=> Package [display_candidate_resource(resource), resource.send(primary_key)] end end end end end