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

Inherits:
RuboCop::Cop
  • Object
show all
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 =
<<~END_MESSAGE.split("\n").join(" ")
  Use `execute` instead of `ActiveRecord::Base.connection.execute` in migrations. The latter is
  redundant and can bypass safety checks.
END_MESSAGE

Instance Method Summary collapse

Instance Method Details

#autocorrect(node) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb', line 33

def autocorrect(node)
  lambda 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

#on_send(node) ⇒ Object



27
28
29
30
31
# File 'lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb', line 27

def on_send(node)
  ar_connection_execute(node) do
    add_offense(node, location: :expression, message: MSG)
  end
end