|
1 | 1 | <% |
2 | 2 | config = field.associated_model_config |
3 | | - related_id = params[:associations] && params[:associations][field.name.to_s] |
4 | 3 | source_abstract_model = RailsAdmin.config(form.object.class).abstract_model |
5 | 4 |
|
6 | | - if form.object.new_record? && related_id.present? && related_id != 'new' |
7 | | - selected = config.abstract_model.get(related_id) |
8 | | - selected_id = selected.send(field.associated_primary_key) |
9 | | - selected_name = selected.send(field.associated_object_label_method) |
10 | | - else |
11 | | - selected_id = field.selected_id |
12 | | - selected_name = field.formatted_value |
13 | | - end |
14 | | - |
15 | 5 | current_action = params[:action].in?(['create', 'new']) ? 'create' : 'update' |
16 | 6 |
|
17 | 7 | edit_url = authorized?(:edit, config.abstract_model) ? edit_path(model_name: config.abstract_model.to_param, modal: true, id: '__ID__') : '' |
18 | 8 |
|
19 | 9 | xhr = !field.associated_collection_cache_all |
20 | 10 |
|
21 | | - 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)] } |
| 11 | + collection = xhr ? [[field.formatted_value, field.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)] } |
22 | 12 |
|
23 | 13 | js_data = { |
24 | 14 | xhr: xhr, |
25 | 15 | 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) |
26 | 16 | } |
27 | 17 | %> |
28 | 18 |
|
29 | | -<% selected_id = (hdv = field.form_default_value).nil? ? selected_id : hdv %> |
| 19 | +<% selected_id = (hdv = field.form_default_value).nil? ? field.selected_id : hdv %> |
30 | 20 | <%= 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'), style: "float: left" }) %> |
31 | 21 | <% if authorized?(:new, config.abstract_model) && field.inline_add %> |
32 | | - <% path_hash = { model_name: config.abstract_model.to_param, modal: true } %> |
33 | | - <% path_hash.merge!({ associations: { field.inverse_of => (form.object.persisted? ? form.object.id : 'new') } }) if field.inverse_of %> |
| 22 | + <% path_hash = { model_name: config.abstract_model.to_param, modal: true }.merge!(field.associated_prepopulate_params) %> |
34 | 23 | <%= link_to "<i class=\"fas fa-plus\"></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' %> |
35 | 24 | <% end %> |
36 | 25 | <% if edit_url.present? && field.inline_edit %> |
|
0 commit comments