Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Parent readonly in multiselects #1388

Closed
wants to merge 2 commits into from

2 participants

@pehlert

This adds a parent_readonly option to has_many associations which can be used to hide/show the editing and adding features of multiselects.

I needed this because my parent association was excluded from rails_admin's models which caused an error when someone clicked the "Add another" button or double clicked an item in the select box.

I am willing to give this some more love (testing + documentation) if someone confirms that it would be useful. Just wanted to make sure in advance as it is my first contribution to rails_admin.

@mshibuya
Collaborator

Sorry for not responding so long...
This could be useful, I think:+1:
Would you mind rebasing to latest master and adding some specs?

@pehlert

Hi Mitsuhiro, I would very much like to see this in the official repo! I'm a little busy right now, but will take care of this as soon as I can!

@mshibuya
Collaborator

Please reopen on update.

@mshibuya mshibuya closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
4 app/views/rails_admin/main/_form_filtering_multiselect.html.haml
@@ -23,7 +23,7 @@
js_data = {
:xhr => xhr,
- :'edit-url' => (authorized?(:edit, config.abstract_model) ? edit_path(:model_name => config.abstract_model.to_param, :id => '__ID__') : ''),
+ :'edit-url' => (authorized?(:edit, config.abstract_model) && !field.parent_readonly ? 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),
:sortable => !!field.orderable,
:cacheAll => !!field.associated_collection_cache_all,
@@ -41,7 +41,7 @@
- selected_ids = (hdv = field.html_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})
-- if authorized? :new, config.abstract_model
+- if authorized?(:new, config.abstract_model) && !field.parent_readonly
- 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 => "create btn btn-info", :style => 'margin-left:10px'
View
5 lib/rails_admin/config/fields/types/has_many_association.rb
@@ -17,6 +17,11 @@ class HasManyAssociation < RailsAdmin::Config::Fields::Association
false
end
+ # read-only parent objects
+ register_instance_option :parent_readonly do
+ self.associated_model_config.excluded?
+ end
+
def method_name
nested_form ? "#{super}_attributes".to_sym : "#{super.to_s.singularize}_ids".to_sym # name_ids
end
Something went wrong with that request. Please try again.