class Babik::Table::Field

Field module abstracts the concept of table field according to some useful conversions

Public Class Methods

new(model, field) click to toggle source

Create an actual field for a model. @param model [ActiveRecord::Base] model this field belongs to. @param field [String] field model that could need the conversion.

# File lib/babik/queryset/lib/field.rb, line 13
def initialize(model, field)
  @model = model
  @field = field
end

Public Instance Methods

real_field() click to toggle source

Check if the field requires some conversion and if that's the case, return the converted final field If the field is a name of an association, it will be converted to the foreign entity id @return [String] Actual name of the field that will be used in the SQL.

# File lib/babik/queryset/lib/field.rb, line 21
def real_field
  # If the selected field is a local attribute return the condition as-is (that's the most usual case)
  is_local_attribute = @model.column_names.include?(@field.to_s)
  return @field if is_local_attribute
  # If the selected field is the name of an association, convert it to be a right condition
  association = @model.reflect_on_association(@field.to_sym)
  # Only if the association is belongs to, the other associations will be checked by foreign filter method
  return association.foreign_key if association && association.belongs_to?
  # Field that is not present in the model
  raise "Unrecognized field #{@field} for model #{@model} in filter/exclude"
end