Sha256: c7e7049800ca651410c986c3fb0cff982552f9d858770e1deb0b207961811d3c

Contents?: true

Size: 1.35 KB

Versions: 28

Compression:

Stored size: 1.35 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 < Cop
        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, location: :expression, message: MSG)
          end
        end

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

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
ezcater_rubocop-7.0.0 lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb
ezcater_rubocop-6.1.1 lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb
ezcater_rubocop-6.1.0 lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb
ezcater_rubocop-6.0.3 lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb
ezcater_rubocop-6.0.2 lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb
ezcater_rubocop-6.0.1 lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb
ezcater_rubocop-6.0.0 lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb
ezcater_rubocop-5.2.1 lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb
ezcater_rubocop-5.2.0 lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb
ezcater_rubocop-2.5.0 lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb
ezcater_rubocop-5.1.0 lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb
ezcater_rubocop-5.0.0 lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb
ezcater_rubocop-4.0.0 lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb
ezcater_rubocop-3.0.2 lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb
ezcater_rubocop-3.0.1 lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb
ezcater_rubocop-3.0.0 lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb
ezcater_rubocop-2.4.0 lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb
ezcater_rubocop-2.3.0 lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb
ezcater_rubocop-2.2.0 lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb
ezcater_rubocop-2.1.0 lib/rubocop/cop/ezcater/rails_top_level_sql_execute.rb