class TheSchemaIs::Presence

Constants

MSG_NO_DB_SCHEMA
MSG_NO_MODEL_SCHEMA

Private Instance Methods

register_offense(node) click to toggle source
# File lib/the_schema_is/cops.rb, line 86
def register_offense(node)
  schema.nil? and
    add_offense(model.source, message: MSG_NO_DB_SCHEMA % [model.table_name, schema_path])

  model.schema.nil? and add_offense(model.source, message: MSG_NO_MODEL_SCHEMA) do |corrector|
    indent = node.loc.expression.column + 2
    code = [
      "the_schema_is #{model.table_name.to_s.inspect} do |t|",
      *schema_columns.map { |_, col| "  #{col.source.loc.expression.source}" },
      'end'
    ].map { |s| ' ' * indent + s }.join("\n").then { |s| "\n#{s}\n" }

    # in "class User < ActiveRecord::Base" -- second child is "ActiveRecord::Base"
    corrector.insert_after(node.children[1].loc.expression, code)
  end
end