strict-loading 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +2 -0
- data/Gemfile +22 -0
- data/README.md +41 -0
- data/Rakefile +12 -0
- data/lib/strict-loading/abstract_reflection.rb +9 -0
- data/lib/strict-loading/association.rb +20 -0
- data/lib/strict-loading/core.rb +43 -0
- data/lib/strict-loading/query_methods.rb +34 -0
- data/lib/strict-loading/railtie.rb +21 -0
- data/lib/strict-loading/relation.rb +17 -0
- data/lib/strict-loading/version.rb +5 -0
- data/lib/strict-loading.rb +25 -0
- data/lib/strict_loading.rb +1 -0
- data/spec/internal/app/models/todo_item.rb +5 -0
- data/spec/internal/app/models/todo_list.rb +5 -0
- data/spec/internal/config/database.yml +6 -0
- data/spec/internal/db/combustion_test.sqlite +0 -0
- data/spec/internal/db/schema.rb +12 -0
- data/spec/internal/log/test.log +931 -0
- data/spec/spec_helper.rb +38 -0
- data/spec/strict_loading_spec.rb +60 -0
- data/strict-loading.gemspec +19 -0
- metadata +101 -0
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
ENV["RAILS_ENV"] ||= "test"
|
|
4
|
+
|
|
5
|
+
require "combustion"
|
|
6
|
+
|
|
7
|
+
Combustion.initialize! :active_record do
|
|
8
|
+
if config.active_record.sqlite3
|
|
9
|
+
config.active_record.sqlite3.represent_boolean_as_integer = true
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
require "strict-loading"
|
|
14
|
+
require "rspec/rails"
|
|
15
|
+
require "database_cleaner"
|
|
16
|
+
|
|
17
|
+
module StrictLoading
|
|
18
|
+
module SpecHelpers
|
|
19
|
+
def with_violation_action(action)
|
|
20
|
+
old_action = ActiveRecord::Base.action_on_strict_loading_violation
|
|
21
|
+
ActiveRecord::Base.action_on_strict_loading_violation = action
|
|
22
|
+
yield
|
|
23
|
+
ensure
|
|
24
|
+
ActiveRecord::Base.action_on_strict_loading_violation = old_action
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
RSpec.configure do |config|
|
|
30
|
+
config.include(StrictLoading::SpecHelpers)
|
|
31
|
+
|
|
32
|
+
DatabaseCleaner.strategy = :transaction
|
|
33
|
+
|
|
34
|
+
config.before(:suite) { DatabaseCleaner.clean_with :truncation }
|
|
35
|
+
|
|
36
|
+
config.before { DatabaseCleaner.start }
|
|
37
|
+
config.after { DatabaseCleaner.clean }
|
|
38
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
require "pry-byebug"
|
|
5
|
+
|
|
6
|
+
describe StrictLoading do
|
|
7
|
+
before(:each) do
|
|
8
|
+
list = TodoList.create(name: "Groceries")
|
|
9
|
+
list.todo_items.create(name: "Celery")
|
|
10
|
+
list.todo_items.create(name: "Carrots")
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
describe "#strict_loading" do
|
|
14
|
+
it "raises an error when attempting to access an unloaded relation" do
|
|
15
|
+
list = TodoList.first
|
|
16
|
+
list.strict_loading!
|
|
17
|
+
|
|
18
|
+
expect { list.todo_items.to_a }.to raise_error(ActiveRecord::StrictLoadingViolationError) do |err|
|
|
19
|
+
expect(err.message).to eq(
|
|
20
|
+
"`TodoList` is marked for strict_loading. The TodoItem association named `:todo_items` cannot be lazily loaded."
|
|
21
|
+
)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "logs violations instead of raising" do
|
|
26
|
+
list = TodoList.first
|
|
27
|
+
list.strict_loading!
|
|
28
|
+
|
|
29
|
+
with_violation_action(:log) do
|
|
30
|
+
events = []
|
|
31
|
+
|
|
32
|
+
callback = lambda do |_name, _start, _finish, _id, payload|
|
|
33
|
+
events << payload
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
ActiveSupport::Notifications.subscribed(callback, "strict_loading_violation.active_record") do
|
|
37
|
+
list.todo_items.to_a
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
expect(events.size).to be > 0
|
|
41
|
+
|
|
42
|
+
event = events[0]
|
|
43
|
+
expect(event[:owner]).to eq(TodoList)
|
|
44
|
+
expect(event[:reflection]).to eq(TodoList.reflect_on_association(:todo_items))
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
describe ".strict_loading" do
|
|
50
|
+
it "raises an error when attempting to access an unloaded relation" do
|
|
51
|
+
list = TodoList.strict_loading.first
|
|
52
|
+
|
|
53
|
+
expect { list.todo_items.to_a }.to raise_error(ActiveRecord::StrictLoadingViolationError) do |err|
|
|
54
|
+
expect(err.message).to eq(
|
|
55
|
+
"`TodoList` is marked for strict_loading. The TodoItem association named `:todo_items` cannot be lazily loaded."
|
|
56
|
+
)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), "lib")
|
|
2
|
+
require "strict-loading/version"
|
|
3
|
+
|
|
4
|
+
Gem::Specification.new do |s|
|
|
5
|
+
s.name = "strict-loading"
|
|
6
|
+
s.version = ::StrictLoading::VERSION
|
|
7
|
+
s.authors = ["Cameron Dutro"]
|
|
8
|
+
s.email = ["camertron@gmail.com"]
|
|
9
|
+
s.homepage = "http://github.com/camertron"
|
|
10
|
+
|
|
11
|
+
s.description = s.summary = "A backport of ActiveRecord strict loading feature for Rails < 6.1."
|
|
12
|
+
s.platform = Gem::Platform::RUBY
|
|
13
|
+
|
|
14
|
+
s.add_dependency "railties", ">= 4.2.0", "< 6.1"
|
|
15
|
+
s.add_dependency "activerecord", ">= 4.2.0", "< 6.1"
|
|
16
|
+
|
|
17
|
+
s.require_path = "lib"
|
|
18
|
+
s.files = Dir["{lib,spec}/**/*", "Gemfile", "CHANGELOG.md", "README.md", "Rakefile", "strict-loading.gemspec"]
|
|
19
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: strict-loading
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Cameron Dutro
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: railties
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 4.2.0
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '6.1'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: 4.2.0
|
|
29
|
+
- - "<"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '6.1'
|
|
32
|
+
- !ruby/object:Gem::Dependency
|
|
33
|
+
name: activerecord
|
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - ">="
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: 4.2.0
|
|
39
|
+
- - "<"
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '6.1'
|
|
42
|
+
type: :runtime
|
|
43
|
+
prerelease: false
|
|
44
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ">="
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: 4.2.0
|
|
49
|
+
- - "<"
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: '6.1'
|
|
52
|
+
description: A backport of ActiveRecord strict loading feature for Rails < 6.1.
|
|
53
|
+
email:
|
|
54
|
+
- camertron@gmail.com
|
|
55
|
+
executables: []
|
|
56
|
+
extensions: []
|
|
57
|
+
extra_rdoc_files: []
|
|
58
|
+
files:
|
|
59
|
+
- CHANGELOG.md
|
|
60
|
+
- Gemfile
|
|
61
|
+
- README.md
|
|
62
|
+
- Rakefile
|
|
63
|
+
- lib/strict-loading.rb
|
|
64
|
+
- lib/strict-loading/abstract_reflection.rb
|
|
65
|
+
- lib/strict-loading/association.rb
|
|
66
|
+
- lib/strict-loading/core.rb
|
|
67
|
+
- lib/strict-loading/query_methods.rb
|
|
68
|
+
- lib/strict-loading/railtie.rb
|
|
69
|
+
- lib/strict-loading/relation.rb
|
|
70
|
+
- lib/strict-loading/version.rb
|
|
71
|
+
- lib/strict_loading.rb
|
|
72
|
+
- spec/internal/app/models/todo_item.rb
|
|
73
|
+
- spec/internal/app/models/todo_list.rb
|
|
74
|
+
- spec/internal/config/database.yml
|
|
75
|
+
- spec/internal/db/combustion_test.sqlite
|
|
76
|
+
- spec/internal/db/schema.rb
|
|
77
|
+
- spec/internal/log/test.log
|
|
78
|
+
- spec/spec_helper.rb
|
|
79
|
+
- spec/strict_loading_spec.rb
|
|
80
|
+
- strict-loading.gemspec
|
|
81
|
+
homepage: http://github.com/camertron
|
|
82
|
+
licenses: []
|
|
83
|
+
metadata: {}
|
|
84
|
+
rdoc_options: []
|
|
85
|
+
require_paths:
|
|
86
|
+
- lib
|
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
88
|
+
requirements:
|
|
89
|
+
- - ">="
|
|
90
|
+
- !ruby/object:Gem::Version
|
|
91
|
+
version: '0'
|
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - ">="
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '0'
|
|
97
|
+
requirements: []
|
|
98
|
+
rubygems_version: 3.6.9
|
|
99
|
+
specification_version: 4
|
|
100
|
+
summary: A backport of ActiveRecord strict loading feature for Rails < 6.1.
|
|
101
|
+
test_files: []
|