class Babik::QuerySet::OrderField
Each one of the fields that appear in the order statement
Attributes
direction[R]
model[R]
selection[R]
Public Class Methods
new(model, field_path, direction)
click to toggle source
Construct the OrderField
@param model [ActiveRecord::Base] base model. @param field_path [String, Symbol, Selection] field path. If local, it will be one of the attributes,
otherwise will be an association path.
@param direction [String, Symbol] :ASC or :DESC (a string will be converted to symbol).
# File lib/babik/queryset/components/order.rb, line 121 def initialize(model, field_path, direction) direction_sym = direction.to_sym unless %i[ASC DESC].include?(direction_sym) raise "Invalid order type #{direction} in #{field_path}: Expecting :ASC or :DESC" end @model = model if [String, Symbol].include?(field_path.class) @selection = Babik::Selection::Path::Factory.build(@model, field_path) elsif [Babik::Selection::Path::LocalPath, Babik::Selection::Path::ForeignPath].include?(field_path.class) @selection = field_path else raise "field_path of class #{field_path.class} not valid. A Symbol/String/Babik::Selection::Base expected" end @direction = direction_sym end
Public Instance Methods
invert()
click to toggle source
Return a new OrderField
with the direction inverted @return [OrderField] Order
field with inverted direction.
# File lib/babik/queryset/components/order.rb, line 139 def invert inverted_direction = if @direction.to_sym == :ASC :DESC else :ASC end OrderField.new(@model, @selection, inverted_direction) end
sql()
click to toggle source
Return sql of the field to order. i.e. something like this:
<table_alias>.<field> ASC <table_alias>.<field> DESC
e.g.
users_0.first_name ASC posts_0.title DESC
@return [SQL] SQL code for field to order.
# File lib/babik/queryset/components/order.rb, line 156 def sql "#{@selection.target_alias}.#{@selection.selected_field} #{@direction}" end