Skip to content

Add belongs_to optional/required support to required? #3717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/rails_admin/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ def parse_id(id)
end
end

def belongs_to_required_by_default
model.belongs_to_required_by_default
end

private

def primary_key_scope(scope, id)
Expand Down
4 changes: 4 additions & 0 deletions lib/rails_admin/adapters/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ def adapter_supports_joins?
false
end

def belongs_to_required_by_default
::Mongoid.belongs_to_required_by_default
end

private

def build_statement(column, type, value, operator)
Expand Down
9 changes: 8 additions & 1 deletion lib/rails_admin/config/fields/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,18 @@ def filter_options
end

(@required ||= {})[context] ||= !!([name] + children_fields).uniq.detect do |column_name|
abstract_model.model.validators_on(column_name).detect do |v|
model = abstract_model.model
model.validators_on(column_name).detect do |v|
!(v.options[:allow_nil] || v.options[:allow_blank]) &&
%i[presence numericality attachment_presence].include?(v.kind) &&
(v.options[:on] == context || v.options[:on].blank?) &&
(v.options[:if].blank? && v.options[:unless].blank?)
end || model.reflect_on_all_associations(:belongs_to).detect do |a|
next unless a.name == column_name

required = a.options[:required] if a.options.key?(:required)
required = !a.options[:optional] if a.options.key?(:optional) && required.nil?
required.nil? ? abstract_model.belongs_to_required_by_default : required
end
end
end
Expand Down
24 changes: 24 additions & 0 deletions spec/rails_admin/config/fields/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,15 @@ class RelTest < Tableless
column :league_id, :integer
column :division_id, :integer, nil, false
column :player_id, :integer
column :team_id, :integer
column :draft_id, :integer
column :image_id, :integer
belongs_to :league, optional: true
belongs_to :division, optional: true
belongs_to :player, optional: true
belongs_to :team, optional: false
belongs_to :draft, required: true
belongs_to :image, required: false
validates_numericality_of(:player_id, only_integer: true)
end
@fields = RailsAdmin.config(RelTest).create.fields
Expand All @@ -111,6 +117,24 @@ class RelTest < Tableless
expect(@fields.detect { |f| f.name == :player }.required?).to be_truthy
end
end

describe 'for belongs_to association with optional: false' do
it 'is required' do
expect(@fields.detect { |f| f.name == :team }.required?).to be_truthy
end
end

describe 'for belongs_to association with required: true' do
it 'is required' do
expect(@fields.detect { |f| f.name == :draft }.required?).to be_truthy
end
end

describe 'for belongs_to association with required: false' do
it 'is optional' do
expect(@fields.detect { |f| f.name == :image }.required?).to be_falsey
end
end
end
end

Expand Down
Loading