Module: ConcernsOnRails::Support::ColumnGuard

Included in:
Models::Stateable::ClassMethods
Defined in:
lib/concerns_on_rails/support/column_guard.rb

Overview

Shared schema-validation helper mixed into a concern’s ClassMethods. Runs in class context, so ‘column_names` / `table_name` resolve against the including model. Centralizes the column-existence check that every model concern used to re-implement, and keeps the error wording uniform.

class_methods do
  include ConcernsOnRails::Support::ColumnGuard

  def activatable_by(field = :active)
    self.activatable_field = field.to_sym
    ensure_columns!("ConcernsOnRails::Models::Activatable", activatable_field)
  end
end

The phrase “does not exist” is preserved so existing specs that match /does not exist/ keep passing.

Instance Method Summary collapse

Instance Method Details

#ensure_columns!(concern, *fields) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/concerns_on_rails/support/column_guard.rb', line 22

def ensure_columns!(concern, *fields)
  fields.flatten.compact.each do |field|
    next if column_names.include?(field.to_s)

    raise ArgumentError,
          "#{concern}: '#{field}' does not exist in the database (table: #{table_name})"
  end
end