Skip to content

Commit

Permalink
Load configuration from .active_record_doctor.rb
Browse files Browse the repository at this point in the history
This commit changes the default configuration file, so that text editors
recognize it as a Ruby file. The old file is still supported but a
warning is shown whenever it's used.

Co-authored-by: Jon Dufresne <jon.dufresne@gmail.com>
  • Loading branch information
gregnavis and jdufresne committed Dec 8, 2023
1 parent dc6c2f2 commit 4476ac0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ ActiveRecordDoctor::Rake::Task.new do |task|
task.deps = []

# A path to your active_record_doctor configuration file.
task.config_path = ::Rails.root.join(".active_record_doctor")
task.config_path = ::Rails.root.join(".active_record_doctor.rb")

# A Proc called right before running detectors that should ensure your Active
# Record models are preloaded and a database connection is ready.
Expand Down Expand Up @@ -111,7 +111,7 @@ If you want to use the default configuration then you don't have to do anything.
Just run `active_record_doctor` in your project directory.

If you want to customize the tool you should create a file named
`.active_record_doctor` in your project root directory with content like:
`.active_record_doctor.rb` in your project root directory with content like:

```ruby
ActiveRecordDoctor.configure do
Expand Down
38 changes: 35 additions & 3 deletions lib/active_record_doctor/rake/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,41 @@ def runner
end

def config
@config ||= begin
path = config_path && File.exist?(config_path) ? config_path : nil
ActiveRecordDoctor.load_config_with_defaults(path)
@config ||=
ActiveRecordDoctor.load_config_with_defaults(effective_config_path)
end

def effective_config_path
if config_path.nil?
# No explicit config_path was set, so we're trying to use defaults.
legacy_default_path = Rails.root.join(".active_record_doctor")
new_default_path = Rails.root.join(".active_record_doctor.rb")

# First, if the legacy file exists we'll use it but show a warning.
if legacy_default_path.exist?
warn(<<~WARN.squish)
DEPRECATION WARNING: active_record_doctor is using the default
configuration file located in #{legacy_default_path.basename}. However,
that default will change to #{new_default_path.basename} in the future.
In order to avoid errors, please rename the file from
#{legacy_default_path.basename} to #{new_default_path.basename}.
WARN

return legacy_default_path
end

# Second, if the legacy file does NOT exist, but the new one does then
# we'll use that.
if new_default_path.exist?
return new_default_path
end

# Otherwise, there's no configuration file in use.
nil
else
# If an explicit configuration file was set then we use it as is.
config_path
end
end

Expand Down
1 change: 0 additions & 1 deletion lib/tasks/active_record_doctor.rake
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ ActiveRecordDoctor::Rake::Task.new do |task|
# This file is imported when active_record_doctor is being used as part of a
# Rails app so it's the right place for all Rails-specific settings.
task.deps = [:environment]
task.config_path = Rails.root.join(".active_record_doctor")
task.setup = -> { Rails.application.eager_load! }
end
2 changes: 1 addition & 1 deletion test/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def config_file(content)
@previous_dir = Dir.pwd

directory = Dir.mktmpdir("active_record_doctor")
@config_path = File.join(directory, ".active_record_doctor")
@config_path = File.join(directory, ".active_record_doctor.rb")
File.write(@config_path, content)
Dir.chdir(directory)

Expand Down

0 comments on commit 4476ac0

Please sign in to comment.