fix: has_many without validate option returns false instead of true (#1378)

This commit is contained in:
Pedro Paiva 2020-11-19 07:43:58 -03:00 committed by GitHub
parent bf41204490
commit 8772cd41a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 5 deletions

View File

@ -6,6 +6,11 @@ module Shoulda
class OptionVerifier
delegate :reflection, to: :reflector
DEFAULT_VALUE_OF_OPTIONS = {
has_many: {
validate: true,
},
}.freeze
RELATION_OPTIONS = [:conditions, :order].freeze
def initialize(reflector)
@ -55,7 +60,7 @@ module Shoulda
if respond_to?(method_name, true)
__send__(method_name)
else
reflection.options[name]
actual_value_for_option(name)
end
end
end
@ -116,6 +121,16 @@ module Shoulda
def actual_value_for_class_name
reflector.associated_class
end
def actual_value_for_option(name)
option_value = reflection.options[name]
if option_value.nil?
DEFAULT_VALUE_OF_OPTIONS.dig(reflection.macro, name)
else
option_value
end
end
end
end
end

View File

@ -1177,20 +1177,40 @@ Expected Parent to have a has_many association called children through conceptio
end
context 'validate' do
it 'accepts when the :validate option matches' do
it 'accepts validate(false) when the :validate option is false' do
expect(having_many_children(validate: false)).to have_many(:children).validate(false)
end
it 'rejects when the :validate option does not match' do
it 'accepts validate(true) when the :validate option is true' do
expect(having_many_children(validate: true)).to have_many(:children).validate(true)
end
it 'rejects validate(false) when the :validate option is true' do
expect(having_many_children(validate: true)).not_to have_many(:children).validate(false)
end
it 'rejects validate(true) when the :validate option is false' do
expect(having_many_children(validate: false)).not_to have_many(:children).validate(true)
end
it 'assumes validate() means validate(true)' do
expect(having_many_children(validate: true)).to have_many(:children).validate
end
it 'rejects validate() when :validate option is false' do
expect(having_many_children(validate: false)).not_to have_many(:children).validate
end
it 'matches validate(false) to having no validate option specified' do
expect(having_many_children).to have_many(:children).validate(false)
it 'rejects validate(false) when no :validate option was specified' do
expect(having_many_children).not_to have_many(:children).validate(false)
end
it 'accepts validate(true) when no :validate option was specified' do
expect(having_many_children).to have_many(:children).validate(true)
end
it 'accepts validate() when no :validate option was specified' do
expect(having_many_children).to have_many(:children).validate
end
end