Class: RuboCop::Cop::Ezcater::RailsTopLevelSqlExecute

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

Overview

Use ‘execute` instead of `ActiveRecord::Base.connection.execute` in migrations. The latter is redundant and can bypass migration safety checks.

Examples:


# good
execute("...")

# bad
ActiveRecord::Base.connection.execute("...")

Constant Summary collapse

MSG =
"Use `execute` instead of `ActiveRecord::Base.connection.execute` in migrations. The latter is\nredundant and can bypass safety checks.\n".split("\n").join(" ")

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb', line 29

def on_send(node)
  ar_connection_execute(node) do
    add_offense(node.loc.expression, message: MSG) do |corrector|
      range = Parser::Source::Range.new(
        node.source_range.source_buffer,
        node.source_range.begin_pos,
        node.source_range.end_pos
      )

      corrector.replace(range, "execute(#{node.last_argument.source})")
    end
  end
end