Skip to content

Commit 9bfea79

Browse files
committed
Fix belongs_to_required_by_default call for Mongoid
1 parent 5c5853e commit 9bfea79

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

lib/rails_admin/adapters/active_record.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ def parse_id(id)
135135
end
136136
end
137137

138+
def belongs_to_required_by_default
139+
model.belongs_to_required_by_default
140+
end
141+
138142
private
139143

140144
def primary_key_scope(scope, id)

lib/rails_admin/adapters/mongoid.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ def adapter_supports_joins?
112112
false
113113
end
114114

115+
def belongs_to_required_by_default
116+
Mongoid.belongs_to_required_by_default
117+
end
118+
115119
private
116120

117121
def build_statement(column, type, value, operator)

lib/rails_admin/config/fields/base.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,9 @@ def filter_options
225225
end || model.reflect_on_all_associations(:belongs_to).detect do |a|
226226
next unless a.name == column_name
227227

228-
if a.options.key?(:required)
229-
!!a.options[:required]
230-
elsif a.options[:optional].nil?
231-
model.belongs_to_required_by_default
232-
else
233-
!a.options[:optional]
234-
end
228+
required = a.options[:required] if a.options.key?(:required)
229+
required = !a.options[:optional] if a.options.key?(:optional) && required.nil?
230+
required.nil? ? abstract_model.belongs_to_required_by_default : required
235231
end
236232
end
237233
end

0 commit comments

Comments
 (0)