Sha256: bb378b6a2dba646f4fc378172206a04dc784015cbd84749a009d658e9f3ee094

Contents?: true

Size: 1.34 KB

Versions: 4

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Ezcater
      # Use `execute` instead of `ActiveRecord::Base.connection.execute` in migrations. The latter is redundant and
      # can bypass migration safety checks.
      #
      # @example
      #
      #   # good
      #   execute("...")
      #
      #   # bad
      #   ActiveRecord::Base.connection.execute("...")
      #
      class RailsTopLevelSqlExecute < Base
        extend RuboCop::Cop::AutoCorrector

        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

        def_node_matcher "ar_connection_execute", <<-PATTERN
          (send (send (const (const _ :ActiveRecord) :Base) :connection) :execute _)
        PATTERN

        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
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ezcater_rubocop-8.0.0 lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb
ezcater_rubocop-7.1.2 lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb
ezcater_rubocop-7.1.1 lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb
ezcater_rubocop-7.1.0 lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb