Class: ActiveRecord::VirtualAttributes::VirtualAttribute

Inherits:
Arel::Nodes::Grouping
  • Object
show all
Defined in:
lib/active_record/virtual_attributes/virtual_arel.rb

Overview

VirtualArel associates arel with an attribute

Model.virtual_attribute :field, :string, :arel => ->(t) { t.grouping(t) } } Model.select(:field)

is equivalent to:

Model.select(Model.arel_table.grouping(Model.arel_table).as(:field)) Model.attribute_supported_by_sql?(:field) # => true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arel, name = nil, relation = nil) ⇒ VirtualAttribute

Returns a new instance of VirtualAttribute.



14
15
16
17
18
# File 'lib/active_record/virtual_attributes/virtual_arel.rb', line 14

def initialize(arel, name = nil, relation = nil)
  super(arel)
  @name = name
  @relation = relation
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



20
21
22
# File 'lib/active_record/virtual_attributes/virtual_arel.rb', line 20

def name
  @name
end

#relationObject

Returns the value of attribute relation.



20
21
22
# File 'lib/active_record/virtual_attributes/virtual_arel.rb', line 20

def relation
  @relation
end

Instance Method Details

#able_to_type_cast?Boolean

rubocop:disable Rails/Delegate

Returns:

  • (Boolean)


37
38
39
# File 'lib/active_record/virtual_attributes/virtual_arel.rb', line 37

def able_to_type_cast?
  relation.able_to_type_cast?
end

#lowerObject

Create a node for lowering this attribute



28
29
30
# File 'lib/active_record/virtual_attributes/virtual_arel.rb', line 28

def lower
  relation.lower(self)
end

#type_cast_for_database(value) ⇒ Object



32
33
34
# File 'lib/active_record/virtual_attributes/virtual_arel.rb', line 32

def type_cast_for_database(value)
  relation.type_cast_for_database(name, value)
end

#type_casterObject

methods from Arel::Nodes::Attribute



23
24
25
# File 'lib/active_record/virtual_attributes/virtual_arel.rb', line 23

def type_caster
  relation.type_for_attribute(name)
end