Module: ActiveRecordExtended::Ltree

Defined in:
lib/active_record_extended/ltree_query_methods.rb

Overview

Arel helpers for ltree functions - can be used in complex queries

Class Method Summary collapse

Class Method Details

.index(column, search) ⇒ Object

index(ltree, ltree) - Returns position of first occurrence of second argument.
search is properly quoted via the AR connection so callers can pass
untrusted strings safely — Brakeman flags raw Arel.sql("'#{x}'") as
SQL-injection (correctly).



47
48
49
50
# File 'lib/active_record_extended/ltree_query_methods.rb', line 47

def index(column, search)
  quoted_search = ActiveRecord::Base.lease_connection.quote(search.to_s)
  Arel::Nodes::NamedFunction.new('index', [Arel.sql(column.to_s), Arel.sql(quoted_search)])
end

.lca(*columns) ⇒ Object

lca(ltree, ltree, ...) - Returns longest common ancestor of paths



38
39
40
41
# File 'lib/active_record_extended/ltree_query_methods.rb', line 38

def lca(*columns)
  args = columns.map { |c| Arel.sql(c.to_s) }
  Arel::Nodes::NamedFunction.new('lca', args)
end

.ltree2text(column) ⇒ Object

ltree2text(ltree) - Converts ltree to text



60
61
62
# File 'lib/active_record_extended/ltree_query_methods.rb', line 60

def ltree2text(column)
  Arel::Nodes::NamedFunction.new('ltree2text', [Arel.sql(column.to_s)])
end

.nlevel(column) ⇒ Object

nlevel(ltree) - Returns the number of labels in the path



26
27
28
# File 'lib/active_record_extended/ltree_query_methods.rb', line 26

def nlevel(column)
  Arel::Nodes::NamedFunction.new('nlevel', [Arel.sql(column.to_s)])
end

.subpath(column, offset, len = nil) ⇒ Object

subpath(ltree, offset, len) - Extracts subpath starting at offset for len labels



31
32
33
34
35
# File 'lib/active_record_extended/ltree_query_methods.rb', line 31

def subpath(column, offset, len = nil)
  args = [Arel.sql(column.to_s), Arel.sql(offset.to_s)]
  args << Arel.sql(len.to_s) if len
  Arel::Nodes::NamedFunction.new('subpath', args)
end

.text2ltree(value) ⇒ Object

text2ltree(text) - Converts text to ltree. value is properly quoted
so untrusted strings can be passed through safely.



54
55
56
57
# File 'lib/active_record_extended/ltree_query_methods.rb', line 54

def text2ltree(value)
  quoted_value = ActiveRecord::Base.lease_connection.quote(value.to_s)
  Arel::Nodes::NamedFunction.new('text2ltree', [Arel.sql(quoted_value)])
end