Skip to content
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

fix: actual_value_for(validate) returns false instead of true when there is no validate option #1378

Merged
merged 1 commit into from Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -6,6 +6,11 @@ module AssociationMatchers
class OptionVerifier
delegate :reflection, to: :reflector

DEFAULT_VALUE_OF_OPTIONS = {
has_many: {
validate: true,
},
}.freeze
RELATION_OPTIONS = [:conditions, :order].freeze

def initialize(reflector)
Expand Down Expand Up @@ -55,7 +60,7 @@ def actual_value_for(name)
if respond_to?(method_name, true)
__send__(method_name)
else
reflection.options[name]
actual_value_for_option(name)
end
end
end
Expand Down Expand Up @@ -116,6 +121,16 @@ def actual_value_for_relation_clause(name)
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
Expand Down
Expand Up @@ -1177,20 +1177,40 @@ def belonging_to_non_existent_class(model_name, assoc_name, options = {})
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

Expand Down