class RuboCop::Cop::Doctolib::OneOperationPerMigration

Flag ActiveRecord database migrations which contain more than one operation.

@example

# bad
def change
  add_column :foos, :bar, :text
  add_column :foos, :qux, :text
end

# good
def change
  add_column :foos, :bar, :text
end

Constants

MSG

Public Instance Methods

on_def(node) click to toggle source
# File lib/rubocop/cop/doctolib/one_operation_per_migration.rb, line 61
def on_def(node)
  migration_count = 0
  each_child_recursive(node) do |child|
    next unless migration_operation?(child)

    migration_count += 1
    add_offense(node) if migration_count > 1
  end
end

Private Instance Methods

each_child_recursive(node) { |child| ... } click to toggle source
# File lib/rubocop/cop/doctolib/one_operation_per_migration.rb, line 73
def each_child_recursive(node)
  children_stack = [node] # Initialize stack with root node.
  until children_stack.empty?
    # Iterate until all leaves have been found.
    child = children_stack.pop # Select one child from the stack.

    # Add all the next children to the stack if this node-type can have children.
    children_stack.push(*child.children) if child.respond_to?(:children)

    yield(child)
  end
end