Module: DynamicActiveModel::DangerousAttributesPatch

Defined in:
lib/dynamic-active-model/dangerous_attributes_patch.rb

Overview

The DangerousAttributesPatch module is a safety feature that prevents conflicts between database column names and Ruby reserved words or ActiveRecord methods. It automatically detects and ignores columns that could cause conflicts, particularly focusing on boolean columns that might conflict with Ruby’s question mark methods.

Examples:

Basic Usage

class User < ActiveRecord::Base
  include DynamicActiveModel::DangerousAttributesPatch
end

With Boolean Column

# If a table has a boolean column named 'class',
# it will be automatically ignored to prevent conflicts
# with Ruby's Object#class method

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Extends the including class with dangerous attribute protection This method:

  1. Checks if the class has any attributes

  2. Identifies columns that could cause conflicts

  3. Adds those columns to the ignored_columns list

Parameters:

  • base (Class)

    The ActiveRecord model class



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dynamic-active-model/dangerous_attributes_patch.rb', line 27

def self.included(base)
  return unless base.attribute_names

  columns_to_ignore = base.columns.select do |column|
    if column.type == :boolean
      base.dangerous_attribute_method?(column.name) ||
        base.dangerous_attribute_method?("#{column.name}?")
    else
      base.dangerous_attribute_method?(column.name)
    end
  end
  base.ignored_columns = columns_to_ignore.map(&:name)
end