Module: ActiveRecord::VirtualAttributes::ClassMethods
- Defined in:
- lib/active_record/virtual_attributes.rb
Instance Method Summary collapse
- #attribute_types ⇒ Object
- #define_virtual_attribute(name, cast_type) ⇒ Object
- #virtual_attribute(name, type, uses: nil, arel: nil, **options) ⇒ Object
-
#virtual_attribute?(name) ⇒ Boolean
Introspection.
- #virtual_attribute_names ⇒ Object
-
#virtual_column(name, type:, **options) ⇒ Object
Compatibility method: ‘virtual_attribute` is a more accurate name.
Instance Method Details
#attribute_types ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/active_record/virtual_attributes.rb', line 90 def attribute_types @attribute_types || super.tap do |hash| virtual_attributes_to_define.each do |name, (type, )| type = type.call if type.respond_to?(:call) type = ActiveRecord::Type.lookup(type, **) if type.kind_of?(Symbol) hash[name] = type end end end |
#define_virtual_attribute(name, cast_type) ⇒ Object
100 101 102 |
# File 'lib/active_record/virtual_attributes.rb', line 100 def define_virtual_attribute(name, cast_type) attribute_types[name.to_s] = cast_type end |
#virtual_attribute(name, type, uses: nil, arel: nil, **options) ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/active_record/virtual_attributes.rb', line 59 def virtual_attribute(name, type, uses: nil, arel: nil, **) name = name.to_s reload_schema_from_cache self.virtual_attributes_to_define = virtual_attributes_to_define.merge(name => [type, ]) define_virtual_include(name, uses) define_virtual_arel(name, arel) end |
#virtual_attribute?(name) ⇒ Boolean
Introspection
74 75 76 77 78 79 80 |
# File 'lib/active_record/virtual_attributes.rb', line 74 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_names ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/active_record/virtual_attributes.rb', line 82 def virtual_attribute_names if respond_to?(:column_names) attribute_names - column_names else attribute_names end end |
#virtual_column(name, type:, **options) ⇒ Object
Compatibility method: ‘virtual_attribute` is a more accurate name
55 56 57 |
# File 'lib/active_record/virtual_attributes.rb', line 55 def virtual_column(name, type:, **) virtual_attribute(name, type, **) end |