Skip to content

Commit 2e7db29

Browse files
committedOct 9, 2018
Use require instead of require_dependency
refs #45, where ActiveStorage has a circular dependency which fails to load in one ordering (but not in the other). As far as I can tell, `require_dependency` is mostly useful for reloading, and since the entire use case in `lib/consistency_fail/models.rb` is about *preloading*, this doesn't seem necessary. Maybe this was necessary back when I was using `ObjectSpace` as a terrible hack? Anyway, `require` it is. This also sorts dependencies in any given `LOAD_PATH` directory before loading them in sequence, just so whatever issues happen along these lines in the future are more easily repeatable.
1 parent fc976ba commit 2e7db29

File tree

8 files changed

+46
-21
lines changed

8 files changed

+46
-21
lines changed
 

‎Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ source "https://rubygems.org"
44
gemspec
55

66
gem 'pry'
7+
8+
group :test do
9+
gem 'activesupport', '~> 5.0'
10+
end

‎lib/consistency_fail/models.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ def dirs
1717

1818
def preload_all
1919
self.dirs.each do |d|
20-
Dir.glob(File.join(d, "**", "*.rb")).each do |model_filename|
21-
Kernel.require_dependency model_filename
20+
ruby_files = Dir.glob(File.join(d, "**", "*.rb")).sort
21+
ruby_files.each do |model_filename|
22+
Kernel.require model_filename
2223
end
2324
end
2425
end

‎spec/index_spec.rb

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
require_relative 'support/models/correct_address'
2+
require_relative 'support/models/wrong_address'
3+
14
describe ConsistencyFail::Index do
2-
5+
36
let(:index) do
47
ConsistencyFail::Index.new(
5-
CorrectAddress,
6-
CorrectAddress.table_name,
8+
CorrectAddress,
9+
CorrectAddress.table_name,
710
["city", "state"]
811
)
912
end
10-
13+
1114
describe "value objectiness" do
1215
it "holds onto model, table name, and columns" do
1316
expect(index.model).to eq(CorrectAddress)
@@ -24,12 +27,12 @@
2427
end
2528
end
2629

27-
describe "equality test" do
30+
describe "equality test" do
2831
it "passes when everything matches" do
2932
expect(index).to eq(
3033
ConsistencyFail::Index.new(
31-
"CorrectAddress".constantize,
32-
"correct_addresses",
34+
"CorrectAddress".constantize,
35+
"correct_addresses",
3336
["city", "state"]
3437
)
3538
)
@@ -38,8 +41,8 @@
3841
it "fails when tables are different" do
3942
expect(index).not_to eq(
4043
ConsistencyFail::Index.new(
41-
CorrectAttachment,
42-
CorrectAttachment.table_name,
44+
CorrectAttachment,
45+
CorrectAttachment.table_name,
4346
["attachable_id", "attachable_type"]
4447
)
4548
)
@@ -48,12 +51,12 @@
4851
it "fails when columns are different" do
4952
expect(index).not_to eq(
5053
ConsistencyFail::Index.new(
51-
CorrectAddress,
52-
CorrectAddress.table_name,
54+
CorrectAddress,
55+
CorrectAddress.table_name,
5356
["correct_user_id"]
5457
)
5558
)
5659
end
5760
end
58-
61+
5962
end

‎spec/models_spec.rb

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
describe ConsistencyFail::Models do
2-
2+
33
def models(load_path)
44
ConsistencyFail::Models.new(load_path)
55
end
@@ -19,7 +19,7 @@ def models(load_path)
1919
expect(models.dirs).to eq([Pathname.new("app/models")])
2020
end
2121

22-
it "preloads models by calling require_dependency" do
22+
it "preloads models" do
2323
models = models(["foo/bar/baz", "app/models", "some/other/models"])
2424
allow(Dir).to receive(:glob).
2525
with(File.join("app/models", "**", "*.rb")).
@@ -28,9 +28,9 @@ def models(load_path)
2828
with(File.join("some/other/models", "**", "*.rb")).
2929
and_return(["some/other/models/foo.rb"])
3030

31-
expect(Kernel).to receive(:require_dependency).with("app/models/user.rb")
32-
expect(Kernel).to receive(:require_dependency).with("app/models/address.rb")
33-
expect(Kernel).to receive(:require_dependency).with("some/other/models/foo.rb")
31+
expect(Kernel).to receive(:require).with("app/models/user.rb")
32+
expect(Kernel).to receive(:require).with("app/models/address.rb")
33+
expect(Kernel).to receive(:require).with("some/other/models/foo.rb")
3434

3535
models.preload_all
3636
end
@@ -44,5 +44,9 @@ def models(load_path)
4444

4545
expect(models([]).all).to eq([model_a, model_c, model_b])
4646
end
47-
47+
48+
it "preloads models successfully" do
49+
models = models([File.join(File.dirname(__FILE__), "support/models")])
50+
expect {models.preload_all}.not_to raise_error
51+
end
4852
end

‎spec/spec_helper.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
Bundler.require(:default, :test)
44

5-
Dir['./spec/support/**/*.rb'].sort.each { |file| require file }
5+
require_relative 'support/active_record'
6+
require_relative 'support/schema'
7+
require 'active_support/dependencies'
8+
ActiveSupport::Dependencies.autoload_paths << './spec/support/models'
69

710
RSpec.configure do |config|
811
config.around do |example|

‎spec/support/models/blob.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Blob < ActiveRecord::Base
2+
require_dependency 'blob/edible'
3+
include Edible
4+
end

‎spec/support/models/blob/edible.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module Blob::Edible
2+
end

‎spec/support/schema.rb

+4
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,8 @@
118118
execute 'CREATE VIEW new_correct_people AS '\
119119
'SELECT * FROM correct_people '\
120120
'WHERE created_at = updated_at'
121+
122+
create_table :blob do |t|
123+
t.timestamps
124+
end
121125
end

0 commit comments

Comments
 (0)
Please sign in to comment.