Module: ActiveRecord::VirtualAttributes::ClassMethods

Defined in:
lib/active_record/virtual_attributes.rb

Instance Method Summary collapse

Instance Method Details

#virtual_attribute(name, type, **options) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/active_record/virtual_attributes.rb', line 58

def virtual_attribute(name, type, **options)
  name = name.to_s
  reload_schema_from_cache

  self.virtual_attributes_to_define =
    virtual_attributes_to_define.merge(name => [type, options])
end

#virtual_attribute?(name) ⇒ Boolean

Introspection

Returns:

  • (Boolean)


70
71
72
73
74
75
76
# File 'lib/active_record/virtual_attributes.rb', line 70

def virtual_attribute?(name)
  load_schema
  has_attribute?(name) && (
    !respond_to?(:column_for_attribute) ||
    column_for_attribute(name).kind_of?(ActiveRecord::ConnectionAdapters::NullColumn)
  )
end

#virtual_attribute_namesObject



78
79
80
81
82
83
84
# File 'lib/active_record/virtual_attributes.rb', line 78

def virtual_attribute_names
  if respond_to?(:column_names)
    attribute_names - column_names
  else
    attribute_names
  end
end

#virtual_column(name, **options) ⇒ Object

Compatibility method: ‘virtual_attribute` is a more accurate name

Raises:

  • (ArgumentError)


51
52
53
54
55
56
# File 'lib/active_record/virtual_attributes.rb', line 51

def virtual_column(name, **options)
  type = options.delete(:type)
  raise ArgumentError, "missing :type attribute" unless type

  virtual_attribute(name, type, **options)
end