diff --git a/app/helpers/rails_admin/application_helper.rb b/app/helpers/rails_admin/application_helper.rb index 147fd7ca3f..fbc8de8efa 100644 --- a/app/helpers/rails_admin/application_helper.rb +++ b/app/helpers/rails_admin/application_helper.rb @@ -7,6 +7,10 @@ def authorized?(action_name, abstract_model = nil, object = nil) action(action_name, abstract_model, object).try(:authorized?) end + def current_action + params[:action].in?(%w[create new]) ? 'create' : 'update' + end + def current_action?(action, abstract_model = @abstract_model, object = @object) @action.custom_key == action.custom_key && abstract_model.try(:to_param) == @abstract_model.try(:to_param) && diff --git a/app/views/rails_admin/main/_form_filtering_multiselect.html.erb b/app/views/rails_admin/main/_form_filtering_multiselect.html.erb index 08757b9ad5..5260087bd3 100644 --- a/app/views/rails_admin/main/_form_filtering_multiselect.html.erb +++ b/app/views/rails_admin/main/_form_filtering_multiselect.html.erb @@ -1,45 +1,14 @@ <% config = field.associated_model_config - source_abstract_model = RailsAdmin.config(form.object.class).abstract_model - - selected = form.object.send(field.name) - selected_ids = selected.map{|s| s.send(field.associated_primary_key).to_s} - - current_action = params[:action].in?(['create', 'new']) ? 'create' : 'update' - - xhr = !field.associated_collection_cache_all - - collection = if xhr - selected.map { |o| [o.send(field.associated_object_label_method), o.send(field.associated_primary_key)] } - else - i = 0 - 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).to_s] }.sort_by {|a| [selected_ids.index(a[1]) || selected_ids.size, i+=1] } - end - - js_data = { - xhr: xhr, - :'edit-url' => (field.inline_edit && authorized?(:edit, config.abstract_model) ? edit_path(model_name: config.abstract_model.to_param, id: '__ID__') : ''), - remote_source: index_path(config.abstract_model, source_object_id: form.object.id, source_abstract_model: source_abstract_model.to_param, associated_collection: field.name, current_action: current_action, compact: true), - scopeBy: field.dynamic_scope_relationships, - sortable: !!field.orderable, - removable: !!field.removable, - cacheAll: !!field.associated_collection_cache_all, - regional: { - add: t('admin.misc.add_new'), - chooseAll: t('admin.misc.chose_all'), - clearAll: t('admin.misc.clear_all'), - down: t('admin.misc.down'), - remove: t('admin.misc.remove'), - search: t('admin.misc.search'), - up: t('admin.misc.up') - } - } %> +
- <% selected_ids = (hdv = field.form_default_value).nil? ? selected_ids : hdv %> - <%= form.select field.method_name, collection, { selected: selected_ids, object: form.object }, field.html_attributes.reverse_merge({data: { filteringmultiselect: true, options: js_data.to_json }, multiple: true}) %> + <%= + form.select field.method_name, field.collection, { selected: field.form_value, object: form.object }, + field.html_attributes.reverse_merge({data: { filteringmultiselect: true, options: field.widget_options.to_json }, multiple: true}) + %>
<% if authorized?(:new, config.abstract_model) && field.inline_add %>