Class: RuboCop::Cop::Rails::RakeEnvironment

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rails/rake_environment.rb

Overview

Checks for Rake tasks without the ‘:environment` task dependency. The `:environment` task loads application code for other Rake tasks. Without it, tasks cannot make use of application code like models.

You can ignore the offense if the task satisfies at least one of the following conditions:

  • The task does not need application code.

  • The task invokes the ‘:environment` task.

Examples:

# bad
task :foo do
  do_something
end

# good
task foo: :environment do
  do_something
end

Constant Summary collapse

MSG =
'Include `:environment` task as a dependency for all Rake tasks.'

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ Object

rubocop:disable InternalAffairs/NumblockHandler



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rubocop/cop/rails/rake_environment.rb', line 42

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
  task_definition?(node) do |task_method|
    return if task_name(task_method) == :default
    return if with_dependencies?(task_method)

    add_offense(task_method) do |corrector|
      if with_arguments?(task_method)
        new_task_dependency = correct_task_arguments_dependency(task_method)
        corrector.replace(task_arguments(task_method), new_task_dependency)
      else
        task_name = task_method.first_argument
        new_task_dependency = correct_task_dependency(task_name)
        corrector.replace(task_name, new_task_dependency)
      end
    end
  end
end