From b1ee993f348c3ec5c5c6f09588f017d95eb8d9dc Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Wed, 9 Dec 2015 12:59:48 -0500 Subject: [PATCH] Make BelongsTo fields nullable Prior to this commit, there was no way to set a `belongs_to` relationship to `nil` through the UI. --- CHANGELOG.md | 1 + lib/administrate/fields/belongs_to.rb | 2 +- spec/lib/fields/belongs_to_spec.rb | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b5e94fc34..2483245bc5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ * [#251] [FEATURE] Raise a helpful error when an attribute is missing from `ATTRIBUTE_TYPES` * [#298] [FEATURE] Support ActiveRecord model I18n translations +* [#312] [FEATURE] Add a `nil` option to `belongs_to` form fields * [#231] [UI] Fix layout issue on show page where a long label next to an empty value would cause following fields on the page to be mis-aligned. * [#309] [UI] Fix layout issue in datetime pickers where months and years diff --git a/lib/administrate/fields/belongs_to.rb b/lib/administrate/fields/belongs_to.rb index c753975781..befe5b6893 100644 --- a/lib/administrate/fields/belongs_to.rb +++ b/lib/administrate/fields/belongs_to.rb @@ -12,7 +12,7 @@ def permitted_attribute end def associated_resource_options - candidate_resources.map do |resource| + [nil] + candidate_resources.map do |resource| [display_candidate_resource(resource), resource.id] end end diff --git a/spec/lib/fields/belongs_to_spec.rb b/spec/lib/fields/belongs_to_spec.rb index 8f2993f36f..5bffc73489 100644 --- a/spec/lib/fields/belongs_to_spec.rb +++ b/spec/lib/fields/belongs_to_spec.rb @@ -31,7 +31,7 @@ candidates = field.associated_resource_options expect(Foo).to have_received(:all) - expect(candidates).to eq([]) + expect(candidates).to eq([nil]) ensure remove_constants :Foo end