Skip to content

Commit

Permalink
Prevent setting null to not null foreign key
Browse files Browse the repository at this point in the history
  • Loading branch information
jibidus committed Oct 28, 2015
1 parent 9a09907 commit dd4ad2f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 19 deletions.
44 changes: 25 additions & 19 deletions app/assets/javascripts/rails_admin/ra.filtering-multiselect.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
return { query: query };
},
sortable: false,
removable: true,
regional: {
up: "Up",
down: "Down",
Expand Down Expand Up @@ -78,24 +79,27 @@


this.add = $('<a href="#" class="ui-icon ui-icon-circle-triangle-e ra-multiselect-item-add">' + this.options.regional.add + '</a>');
this.columns.center.append(this.add);

this.remove = $('<a href="#" class="ui-icon ui-icon-circle-triangle-w ra-multiselect-item-remove">' + this.options.regional.remove + '</a>');
if (this.options.removable) {
this.remove = $('<a href="#" class="ui-icon ui-icon-circle-triangle-w ra-multiselect-item-remove">' + this.options.regional.remove + '</a>');
this.columns.center.append(this.remove);
}

this.columns.center.append(this.add).append(this.remove)
if (this.options.sortable) {
this.up = $('<a href="#" class="ui-icon ui-icon-circle-triangle-n ra-multiselect-item-up">' + this.options.regional.up + '</a>');
this.down = $('<a href="#" class="ui-icon ui-icon-circle-triangle-s ra-multiselect-item-down">' + this.options.regional.down + '</a>');
this.columns.center.append(this.up).append(this.down);
}

this.selection = $('<select class="form-control ra-multiselect-selection" multiple="multiple"></select>');
this.columns.right.append(this.selection);



this.removeAll = $('<a href="#" class="ra-multiselect-item-remove-all"><span class="ui-icon ui-icon-circle-triangle-w"></span>' + this.options.regional.clearAll + '</a>');

this.columns.right.append(this.selection)
.append(this.removeAll);
if (this.options.removable) {
this.removeAll = $('<a href="#" class="ra-multiselect-item-remove-all"><span class="ui-icon ui-icon-circle-triangle-w"></span>' + this.options.regional.clearAll + '</a>');
this.columns.right.append(this.removeAll);
}

this.selection.wrap('<div class="wrapper"/>');

Expand Down Expand Up @@ -127,19 +131,21 @@
widget.selection.trigger('change');
});

/* Remove all from selection */
this.removeAll.click(function(e){
widget._deSelect($('option', widget.selection));
e.preventDefault();
widget.selection.trigger('change');
});
if (this.options.removable) {
/* Remove all from selection */
this.removeAll.click(function(e){
widget._deSelect($('option', widget.selection));
e.preventDefault();
widget.selection.trigger('change');
});

/* Remove from selection */
this.remove.click(function(e){
widget._deSelect($(':selected', widget.selection));
e.preventDefault();
widget.selection.trigger('change');
});
/* Remove from selection */
this.remove.click(function(e){
widget._deSelect($(':selected', widget.selection));
e.preventDefault();
widget.selection.trigger('change');
});
}

var timeout = null;
if(this.options.sortable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
:'edit-url' => (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),
sortable: !!field.orderable,
removable: !!field.removable,
cacheAll: !!field.associated_collection_cache_all,
regional: {
chooseAll: t("admin.misc.chose_all"),
Expand Down
5 changes: 5 additions & 0 deletions lib/rails_admin/config/fields/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ def association # rubocop:disable TrivialAccessors
@associated_collection_cache_all ||= (associated_model_config.abstract_model.count < 100)
end

# Reader whether association's elements can be removed
def removable
association.foreign_key_nullable?
end

# Reader for the association's child model's configuration
def associated_model_config
@associated_model_config ||= RailsAdmin.config(association.klass)
Expand Down
38 changes: 38 additions & 0 deletions spec/integration/config/edit/rails_admin_config_edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,44 @@ class HelpTest < Tableless
end
end

describe 'has_many', active_record: true do
context 'with not nullable foreign key' do
before do
RailsAdmin.config FieldTest do
edit do
field :nested_field_tests do
nested_form false
end
end
end
@field_test = FactoryGirl.create :field_test
end

it 'don\'t allow to remove element', js: true do
visit edit_path(model_name: 'FieldTest', id: @field_test.id)
is_expected.not_to have_selector('a.ra-multiselect-item-remove')
is_expected.not_to have_selector('a.ra-multiselect-item-remove-all')
end
end

context 'with nullable foreign key' do
before do
RailsAdmin.config Team do
edit do
field :players
end
end
@team = FactoryGirl.create :team
end

it 'allow to remove element', js: true do
visit edit_path(model_name: 'Team', id: @team.id)
is_expected.to have_selector('a.ra-multiselect-item-remove')
is_expected.to have_selector('a.ra-multiselect-item-remove-all')
end
end
end

describe 'fields which are nullable and have AR validations', active_record: true do
it 'is required' do
# draft.notes is nullable and has no validation
Expand Down

0 comments on commit dd4ad2f

Please sign in to comment.