Module: ActiveRecord::VirtualAttributes::VirtualArel::ClassMethods

Defined in:
lib/active_record/virtual_attributes/virtual_arel.rb

Instance Method Summary collapse

Instance Method Details

#arel_for_virtual_attribute(column_name, table) ⇒ Nil|Arel::Nodes::Grouping

private api

Returns:

  • for virtual attributes: returns the arel for the column for non sql friendly virtual attributes: returns nil



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/active_record/virtual_attributes/virtual_arel.rb', line 97

def arel_for_virtual_attribute(column_name, table) # :nodoc:
  arel_lambda = _virtual_arel[column_name.to_s]
  return unless arel_lambda

  arel = arel_lambda.call(table)
  # By convention, all attributes are defined with a grouping.
  # Since we're adding a VirtualAttribute node, which is essentially a
  # grouping, there is no need to keep both and end up with double parens
  arel = arel.expr if arel.kind_of?(Arel::Nodes::Grouping)

  VirtualAttribute.new(arel, column_name, table)
end

#arel_tableObject

ActiveRecord::Core 6.1



77
78
79
# File 'lib/active_record/virtual_attributes/virtual_arel.rb', line 77

def arel_table
  @arel_table ||= ArelTableProxy.new(table_name, :klass => self)
end

#attribute_supported_by_sql?(name) ⇒ Boolean

supported by sql if any are true:

  • it is an attribute alias
  • it is an attribute that is non virtual
  • it is an attribute that is virtual and has arel defined

Returns:



85
86
87
88
# File 'lib/active_record/virtual_attributes/virtual_arel.rb', line 85

def attribute_supported_by_sql?(name)
  try(:attribute_alias?, name) ||
    (has_attribute?(name) && (!virtual_attribute?(name) || !!_virtual_arel[name.to_s]))
end