Skip to content

Commit

Permalink
Allow one to set strict_loading_mode globally
Browse files Browse the repository at this point in the history
  • Loading branch information
gjtorikian committed Mar 18, 2024
1 parent 5411787 commit 6d47dc2
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"[ruby]": {
"editor.defaultFormatter": "Shopify.ruby-lsp"
}
}
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Allow one to define `strict_loading_mode`, either globally or within a model

*Garen Torikian*

* Add dirties option to uncached

This adds a `dirties` option to `ActiveRecord::Base.uncached` and
Expand Down
8 changes: 7 additions & 1 deletion activerecord/lib/active_record/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def self.configurations
class_attribute :belongs_to_required_by_default, instance_accessor: false

class_attribute :strict_loading_by_default, instance_accessor: false, default: false
class_attribute :strict_loading_mode, instance_accessor: false, default: :all

class_attribute :has_many_inversing, instance_accessor: false, default: false

Expand Down Expand Up @@ -699,6 +700,11 @@ def strict_loading!(value = true, mode: :all)

attr_reader :strict_loading_mode

# Returns +true+ if the record uses strict_loading with +:all+ mode enabled.
def strict_loading_all?
@strict_loading_mode == :all
end

# Returns +true+ if the record uses strict_loading with +:n_plus_one_only+ mode enabled.
def strict_loading_n_plus_one_only?
@strict_loading_mode == :n_plus_one_only
Expand Down Expand Up @@ -778,7 +784,7 @@ def init_internals

@primary_key = klass.primary_key
@strict_loading = klass.strict_loading_by_default
@strict_loading_mode = :all
@strict_loading_mode = klass.strict_loading_mode

klass.define_attribute_methods
end
Expand Down
14 changes: 14 additions & 0 deletions activerecord/test/cases/strict_loading_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ def test_strict_loading_n_plus_one_only_mode_with_belongs_to
end
end

def test_default_mode_is_all
developer = Developer.first
assert_predicate(developer, :strict_loading_all?)
end

def test_default_mode_can_be_changed_globally
developer = Class.new(ActiveRecord::Base) do
self.strict_loading_mode = :n_plus_one_only
self.table_name = "developers"
end.new

assert_predicate(developer, :strict_loading_n_plus_one_only?)
end

def test_strict_loading
Developer.all.each { |d| assert_not d.strict_loading? }
Developer.strict_loading.each { |d| assert_predicate d, :strict_loading? }
Expand Down
4 changes: 4 additions & 0 deletions guides/source/configuring.md
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,10 @@ changed to `:log` to send violations to the logger instead of raising.
Is a boolean value that either enables or disables strict_loading mode by
default. Defaults to `false`.

#### `config.active_record.strict_loading_mode`

Sets the mode in which strict loading is reported. Deaults to `:all`.

#### `config.active_record.warn_on_records_fetched_greater_than`

Allows setting a warning threshold for query result size. If the number of
Expand Down

0 comments on commit 6d47dc2

Please sign in to comment.