Skip to content

Commit

Permalink
Refactor implementation and add application level tests
Browse files Browse the repository at this point in the history
Co-authored-by: Rémy Hannequin <remy@thoughtbot.com>
  • Loading branch information
laicuRoot and rhannequin committed Feb 16, 2024
1 parent 5bdbb23 commit dedadd9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
Expand Up @@ -15,10 +15,8 @@ def initialize(reflection)
def associated_class
associated_class = reflection.klass

if subject.strict_loading_by_default
unless reflection.options[:strict_loading] == false
reflection.options[:strict_loading] = true
end
if subject.strict_loading_by_default && !(reflection.options[:strict_loading] == false)
reflection.options[:strict_loading] = true
end

associated_class
Expand Down
Expand Up @@ -1200,6 +1200,41 @@ def belonging_to_non_existent_class(model_name, assoc_name, options = {})
end

context 'when the application is configured with strict_loading disabled by default' do
it 'accepts an association with a matching :strict_loading option' do
with_strict_loading_by_default_disabled do
expect(having_many_children).
to have_many(:children).strict_loading(false)
end
end

it 'rejects an association with a non-matching :strict_loading option without explicit value with the correct message' do
with_strict_loading_by_default_disabled do
message = [
'Expected Parent to have a has_many association called children ',
'(children should have strict_loading set to true)',
].join

expect {
expect(having_many_children).
to have_many(:children).strict_loading
}.to fail_with_message(message)
end
end

it 'rejects an association with a non-matching :strict_loading option with the correct message' do
with_strict_loading_by_default_disabled do
message = [
'Expected Parent to have a has_many association called children ',
'(children should have strict_loading set to true)',
].join

expect {
expect(having_many_children).
to have_many(:children).strict_loading(true)
}.to fail_with_message(message)
end
end

context 'when the association is configured with a strict_loading constraint' do
context 'when qualified with strict_loading(true)' do
it 'accepts an association with a matching :strict_loading option' do
Expand Down Expand Up @@ -1239,7 +1274,7 @@ def belonging_to_non_existent_class(model_name, assoc_name, options = {})
end
end

it 'rejects an association with a matching :strict_loading option without explicit value with the correct message' do
it 'rejects an association with a non-matching :strict_loading option without explicit value with the correct message' do
with_strict_loading_by_default_disabled do
message = [
'Expected Parent to have a has_many association called children ',
Expand Down Expand Up @@ -1370,6 +1405,34 @@ def belonging_to_non_existent_class(model_name, assoc_name, options = {})
end

context 'when the application is configured with strict_loading enabled by default' do
it 'accepts an association with a matching :strict_loading option' do
with_strict_loading_by_default_enabled do
expect(having_many_children).
to have_many(:children).strict_loading(true)
end
end

it 'accepts an association with a matching :strict_loading option without explicit value' do
with_strict_loading_by_default_enabled do
expect(having_many_children).
to have_many(:children).strict_loading
end
end

it 'rejects an association with a non-matching :strict_loading option with the correct message' do
with_strict_loading_by_default_enabled do
message = [
'Expected Parent to have a has_many association called children ',
'(children should have strict_loading set to false)',
].join

expect {
expect(having_many_children).
to have_many(:children).strict_loading(false)
}.to fail_with_message(message)
end
end

context 'when the association is configured with a strict_loading constraint' do
context 'when qualified with strict_loading(true)' do
it 'accepts an association with a matching :strict_loading option' do
Expand Down Expand Up @@ -1409,7 +1472,7 @@ def belonging_to_non_existent_class(model_name, assoc_name, options = {})
end
end

it 'rejects an association with a matching :strict_loading option without explicit value with the correct message' do
it 'rejects an association with a non-matching :strict_loading option without explicit value with the correct message' do
with_strict_loading_by_default_enabled do
message = [
'Expected Parent to have a has_many association called children ',
Expand Down

0 comments on commit dedadd9

Please sign in to comment.