Skip to content

Commit

Permalink
Move ActiveStorage fixture hooks to on_load
Browse files Browse the repository at this point in the history
In a test environment, rely on the loading of
`:active_support_test_case`.

Introduce the `:active_record_fixture_set` hook for the Active Storage
engine to listen for during the load process in a development
environment (like when running `db:fixtures:load`).

Since this commit moves the task-aware path resolution out of the block
that provided local variables, it recreates part of the fixture
directory path resolution logic.
  • Loading branch information
seanpdoyle committed Jan 25, 2021
1 parent 948e9d7 commit 48e601c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
2 changes: 2 additions & 0 deletions activerecord/lib/active_record/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -782,3 +782,5 @@ def find
end
end
end

ActiveSupport.run_load_hooks :active_record_fixture_set, ActiveRecord::FixtureSet
4 changes: 0 additions & 4 deletions activerecord/lib/active_record/railties/databases.rake
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,6 @@ db_namespace = namespace :db do
Dir["#{fixtures_dir}/**/*.yml"].map { |f| f[(fixtures_dir.size + 1)..-5] }
end

if defined? ActiveStorage::FixtureSet
ActiveStorage::FixtureSet.file_fixture_path = File.join fixtures_dir, "files"
end

ActiveRecord::FixtureSet.create_fixtures(fixtures_dir, fixture_files)
end

Expand Down
14 changes: 14 additions & 0 deletions activestorage/lib/active_storage/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,19 @@ class Engine < Rails::Engine # :nodoc:
ActiveRecord::Reflection.singleton_class.prepend(Reflection::ReflectionExtension)
end
end

initializer "active_storage.fixture_set" do
ActiveSupport.on_load(:active_record_fixture_set) do
ActiveStorage::FixtureSet.file_fixture_path ||= Rails.root.join(*[
ENV.fetch("FIXTURES_PATH") { File.join("test", "fixtures") },
ENV["FIXTURES_DIR"],
"files"
].compact_blank)
end

ActiveSupport.on_load(:active_support_test_case) do
ActiveStorage::FixtureSet.file_fixture_path = ActiveSupport::TestCase.file_fixture_path
end
end
end
end
4 changes: 0 additions & 4 deletions railties/lib/rails/test_help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
ActiveSupport.on_load(:action_dispatch_integration_test) do
self.fixture_path = ActiveSupport::TestCase.fixture_path
end

ActiveSupport.on_load(:active_storage_record) do
ActiveStorage::FixtureSet.file_fixture_path = ActiveSupport::TestCase.file_fixture_path
end
end

# :enddoc:
Expand Down

0 comments on commit 48e601c

Please sign in to comment.