Class: Telm::Core::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/telm/core/query.rb

Overview

Safe relation builder. Security invariant: sort/dir are validated against the introspected schema before touching the relation — an unknown value raises InvalidQuery (rendered as a 400), never reaching SQL. Everything is built from an allowlist of relation calls; params are never interpolated. Results are always LIMITed.

Defined Under Namespace

Classes: InvalidQuery, Result

Constant Summary collapse

PER_PAGE =
25
DIRECTIONS =
%w[asc desc].freeze

Instance Method Summary collapse

Constructor Details

#initialize(resource, options = {}) ⇒ Query

Accepts :page, :sort, :dir, :fk, :fk_id as a plain hash.



37
38
39
40
41
42
43
# File 'lib/telm/core/query.rb', line 37

def initialize(resource, options = {})
  @resource = resource
  @page = [options[:page].to_i, 1].max
  @sort = validate_sort(options[:sort])
  @dir = validate_dir(options[:dir])
  @fk, @fk_id = validate_fk(options[:fk], options[:fk_id])
end

Instance Method Details

#resultObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/telm/core/query.rb', line 45

def result
  Result.new(
    records: scope.to_a,
    total: filtered.count,
    page: @page,
    per_page: PER_PAGE,
    sort: @sort,
    dir: @dir
  )
end