:ruby
  config = field.associated_model_config
  related_id = params[:associations] && params[:associations][field.name.to_s]
  source_abstract_model = RailsAdmin.config(form.object.class).abstract_model

  if form.object.new_record? && related_id.present? && related_id != 'new'
    selected = config.abstract_model.get(related_id)
    selected_id = selected.send(field.associated_primary_key)
    selected_name = selected.send(field.associated_object_label_method)
  else
    selected_id = field.selected_id
    selected_name = field.formatted_value
  end

  current_action = params[:action].in?(['create', 'new']) ? 'create' : 'update'

  edit_url = authorized?(:edit, config.abstract_model) ? edit_path(model_name: config.abstract_model.to_param, modal: true, id: '__ID__') : ''

  xhr = !field.associated_collection_cache_all

  collection = xhr ? [[selected_name, selected_id]] : controller.list_entries(config, :index, field.associated_collection_scope, false).map { |o| [o.send(field.associated_object_label_method), o.send(field.associated_primary_key)] }

  js_data = {
    xhr: xhr,
    remote_source: index_path(config.abstract_model.to_param, source_object_id: form.object.id, source_abstract_model: source_abstract_model.to_param, associated_collection: field.name, current_action: current_action, compact: true)
  }

- selected_id = (hdv = field.form_default_value).nil? ? selected_id : hdv
= form.select field.method_name, collection, { selected: selected_id, include_blank: true }, field.html_attributes.reverse_merge({ data: { filteringselect: true, options: js_data.to_json }, placeholder: t('admin.misc.search') })

- if authorized?(:new, config.abstract_model) && field.inline_add
  - path_hash = { model_name: config.abstract_model.to_param, modal: true }
  - path_hash.merge!({ associations: { field.inverse_of => (form.object.persisted? ? form.object.id : 'new') } }) if field.inverse_of
  = link_to "<i class=\"icon-plus icon-white\"></i> ".html_safe + wording_for(:link, :new, config.abstract_model), '#', data: { link: new_path(path_hash) }, class: "btn btn-info create", style: 'margin-left:10px'

- if edit_url.present? && field.inline_edit
  = link_to "<i class=\"icon-pencil icon-white\"></i> ".html_safe + wording_for(:link, :edit, config.abstract_model), '#', data: { link: edit_url }, class: "btn btn-info update #{field.value.nil? && 'disabled'}", style: 'margin-left:10px'
