Skip to content

Commit

Permalink
Support strict_loading in model and application
Browse files Browse the repository at this point in the history
This PR adds the tests and logic for support at
model level for strict_loading.
  • Loading branch information
laicuRoot committed Feb 16, 2024
1 parent 56df66c commit 5bdbb23
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ def initialize(reflection)
end

def associated_class
reflection.klass
associated_class = reflection.klass

if subject.strict_loading_by_default
unless reflection.options[:strict_loading] == false
reflection.options[:strict_loading] = true
end
end

associated_class
end

def polymorphic?
Expand Down
175 changes: 170 additions & 5 deletions spec/unit/shoulda/matchers/active_record/association_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,7 @@ def belonging_to_non_existent_class(model_name, assoc_name, options = {})
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)',
'(children should have strict_loading set to false)',
].join

define_model :child, parent_id: :integer
Expand Down Expand Up @@ -1332,7 +1332,7 @@ def belonging_to_non_existent_class(model_name, assoc_name, options = {})
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 false)',
'(children should have strict_loading set to true)',
].join

define_model :child, parent_id: :integer
Expand All @@ -1351,7 +1351,7 @@ def belonging_to_non_existent_class(model_name, assoc_name, options = {})
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 false)',
'(children should have strict_loading set to true)',
].join

define_model :child, parent_id: :integer
Expand All @@ -1367,11 +1367,176 @@ def belonging_to_non_existent_class(model_name, assoc_name, options = {})
end
end
end

# TODO: tests with self.strict_loading_by_default = true on model
end

context 'when the application is configured with strict_loading enabled by default' do
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
with_strict_loading_by_default_enabled do
expect(having_many_children(strict_loading: true)).
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(strict_loading: true)).
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(strict_loading: true)).
to have_many(:children).strict_loading(false)
}.to fail_with_message(message)
end
end
end

context 'when qualified with strict_loading(false)' do
it 'accepts an association with a matching :strict_loading option' do
with_strict_loading_by_default_enabled do
expect(having_many_children(strict_loading: false)).
to have_many(:children).strict_loading(false)
end
end

it 'rejects an association with a 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 ',
'(children should have strict_loading set to true)',
].join

expect {
expect(having_many_children(strict_loading: false)).
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_enabled 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(strict_loading: false)).
to have_many(:children).strict_loading(true)
}.to fail_with_message(message)
end
end
end
end

context 'when strict_loading is defined on the model level' do
context 'when it is set to true' do
it 'accepts an association with a matching :strict_loading option' do
with_strict_loading_by_default_enabled do
define_model :child, parent_id: :integer
parent = define_model(:parent) do |model|
model.strict_loading_by_default = true
model.has_many :children
end.new

expect(parent).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
define_model :child, parent_id: :integer
parent = define_model(:parent) do |model|
model.strict_loading_by_default = true
model.has_many :children
end.new

expect(parent).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

define_model :child, parent_id: :integer
parent = define_model(:parent) do |model|
model.strict_loading_by_default = true
model.has_many :children
end.new

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

context 'when it is set to false' do
it 'accepts an association with a matching :strict_loading option' do
with_strict_loading_by_default_enabled do
define_model :child, parent_id: :integer
parent = define_model(:parent) do |model|
model.strict_loading_by_default = false
model.has_many :children
end.new

expect(parent).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_enabled do
message = [
'Expected Parent to have a has_many association called children ',
'(children should have strict_loading set to true)',
].join

define_model :child, parent_id: :integer
parent = define_model(:parent) do |model|
model.strict_loading_by_default = false
model.has_many :children
end.new

expect {
expect(parent).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_enabled do
message = [
'Expected Parent to have a has_many association called children ',
'(children should have strict_loading set to true)',
].join

define_model :child, parent_id: :integer
parent = define_model(:parent) do |model|
model.strict_loading_by_default = false
model.has_many :children
end.new

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

def having_many_children(options = {})
Expand Down

0 comments on commit 5bdbb23

Please sign in to comment.