Module: Parse::QueryFieldAliasScope

Defined in:
lib/parse/query.rb

Overview

Wraps the public compile and pipeline entry points of Parse::Query and the aggregation helpers so Parse::Query.format_field honors the model's explicit field: names (for example account_id or authId_sub) instead of camel-casing them. Without this, where(account_id: ...) compiled to accountId and silently matched nothing.

Defined Under Namespace

Modules: DirectMethods

Constant Summary collapse

QUERY_METHODS =
%i[
  add_constraint keys exclude_keys order includes pluck count_distinct
  compile compile_where prepared pipeline count results distinct first first_direct
  build_direct_mongodb_pipeline build_query_aggregate_pipeline build_aggregation_pipeline
  aggregate group_by group_by_date explain sum average min max
].freeze
HELPER_METHODS =

Aggregation helper methods that format field names while running.

%i[pipeline results count execute! sum average min max list raw].freeze

Class Method Summary collapse

Class Method Details

.wrap(klass, methods, table_of) ⇒ Object

Parameters:

  • klass (Class) —

    the class whose methods are wrapped.

  • methods (Array<Symbol>)
  • table_of (Proc) —

    ->(receiver) { parse_class_name }.



9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
# File 'lib/parse/query.rb', line 9708

def self.wrap(klass, methods, table_of)
  private_methods = methods.select { |m| klass.private_method_defined?(m) }
  protected_methods = methods.select { |m| klass.protected_method_defined?(m) }
  mod = Module.new
  methods.each do |m|
    next unless klass.method_defined?(m) || klass.private_method_defined?(m)
    # A call for the table already in scope goes straight to the
    # original, so nested query building pays only the table lookup.
    mod.send(:define_method, m) do |*args, **kwargs, &blk|
      table = table_of.call(self)
      frame = Fiber[Parse::Query::FIELD_ALIAS_SCOPE_KEY]
      return super(*args, **kwargs, &blk) if frame && frame.open && table == frame.scope.table
      blk = Parse::Query.block_outside_field_aliases(blk, table) if blk
      Parse::Query.with_field_aliases(table) { super(*args, **kwargs, &blk) }
    end
  end
  # Keep each wrapped method's original visibility.
  mod.send(:private, *private_methods) unless private_methods.empty?
  mod.send(:protected, *protected_methods) unless protected_methods.empty?
  klass.prepend(mod)
end