Module: ActiveRecordExtended::LtreeSelectMethods

Extended by:
ActiveSupport::Concern
Defined in:
lib/active_record_extended/ltree_query_methods.rb

Overview

SELECT clause helpers - mixed into ActiveRecord::Base

Class Method Summary collapse

Class Method Details

.order_by_ltree_depth(column, direction = :asc) ⇒ Object



168
169
170
# File 'lib/active_record_extended/ltree_query_methods.rb', line 168

def order_by_ltree_depth(column, direction = :asc)
  order(Arel.sql("nlevel(#{table_name}.#{column}) #{direction}"))
end

.select_ltree_depth(column, alias_name = nil) ⇒ Object



139
140
141
142
# File 'lib/active_record_extended/ltree_query_methods.rb', line 139

def select_ltree_depth(column, alias_name = nil)
  alias_name ||= "#{column}_depth"
  select("#{table_name}.*, nlevel(#{table_name}.#{column}) as #{alias_name}")
end

.select_ltree_parent(column, alias_name = nil) ⇒ Object



149
150
151
152
# File 'lib/active_record_extended/ltree_query_methods.rb', line 149

def select_ltree_parent(column, alias_name = nil)
  alias_name ||= "#{column}_parent"
  select("#{table_name}.*, subpath(#{table_name}.#{column}, 0, nlevel(#{table_name}.#{column}) - 1) as #{alias_name}")
end

.select_ltree_root(column, alias_name = nil) ⇒ Object



144
145
146
147
# File 'lib/active_record_extended/ltree_query_methods.rb', line 144

def select_ltree_root(column, alias_name = nil)
  alias_name ||= "#{column}_root"
  select("#{table_name}.*, subpath(#{table_name}.#{column}, 0, 1) as #{alias_name}")
end

.select_ltree_subpath(column, offset, len = nil, alias_name: nil) ⇒ Object

Parameters:

  • alias_name (Object, nil) (defaults to: nil)
  • column (Object)
  • offset (Object)
  • len (Object, nil) (defaults to: nil)


158
159
160
161
162
163
164
165
166
# File 'lib/active_record_extended/ltree_query_methods.rb', line 158

def select_ltree_subpath(column, offset, len = nil, alias_name: nil)
  alias_name ||= "#{column}_subpath"
  subpath_sql = if len
                  "subpath(#{table_name}.#{column}, #{offset}, #{len})"
                else
                  "subpath(#{table_name}.#{column}, #{offset})"
                end
  select("#{table_name}.*, #{subpath_sql} as #{alias_name}")
end