Class: Parse::Constraint::PointerEqualsLinkedPointerConstraint

Inherits:
Parse::Constraint show all
Defined in:
lib/parse/query/constraints.rb

Overview

A constraint for comparing pointer fields through linked objects using MongoDB aggregation. This allows comparing ObjectA.field1 with ObjectA.linkedObject.field2 where both are pointers.

# Find ObjectA where ObjectA.author equals ObjectA.project.owner ObjectA.where(:author.equals_linked_pointer => { through: :project, field: :owner })

# This generates a MongoDB aggregation pipeline with $lookup and $expr # to compare pointer fields across linked documents

Instance Attribute Summary

Attributes inherited from Parse::Constraint

#operand, #operation, #operator, #value

Instance Method Summary collapse

Methods inherited from Parse::Constraint

#as_json, constraint_keyword, create, formatted_value, #formatted_value, #initialize, #key, #precedence, #regex_unicode_option, register, #to_s

Constructor Details

This class inherits a constructor from Parse::Constraint

Instance Method Details

#buildHash

Returns the compiled constraint.

Returns:

  • (Hash)

    the compiled constraint.



3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
# File 'lib/parse/query/constraints.rb', line 3023

def build
  unless @value.is_a?(Hash) && @value[:through] && @value[:field]
    raise ArgumentError, "equals_linked_pointer requires: { through: :linked_field, field: :target_field }"
  end

  through_field = @value[:through]
  target_field = @value[:field]
  local_field = @operation.operand

  # Format field names according to Parse conventions
  # Pointer fields in MongoDB are stored with _p_ prefix
  formatted_through = "_p_" + Parse::Query.format_field(through_field)
  formatted_target = "_p_" + Parse::Query.format_field(target_field)
  formatted_local = "_p_" + Parse::Query.format_field(local_field)

  # Determine the target collection name from the through field
  # Use classify to convert field name to class name (e.g., :project -> "Project")
  target_collection = through_field.to_s.classify

  # Build the aggregation pipeline
  # Use clean alias name without _p_ prefix for readability
  lookup_alias = "#{through_field.to_s.camelize(:lower)}_data"

  # Parse stores pointers as "ClassName$objectId" strings
  # We need to extract just the objectId part after the $
  pipeline = [
    {
      "$addFields" => {
        "#{formatted_through}_id" => {
          "$substr" => [
            "$#{formatted_through}",
            target_collection.length + 1,  # Skip "ClassName$"
            -1,  # Rest of string
          ],
        },
      },
    },
    {
      "$lookup" => {
        "from" => target_collection,
        "localField" => formatted_through,
        "foreignField" => "_id",
        "as" => lookup_alias,
      },
    },
    {
      "$match" => {
        "$expr" => {
          "$eq" => [
            { "$arrayElemAt" => ["$#{lookup_alias}.#{formatted_target}", 0] },
            "$#{formatted_local}",
          ],
        },
      },
    },
  ]

  # Return a special marker that indicates this needs aggregation pipeline processing
  { "__aggregation_pipeline" => pipeline }
end

#equals_linked_pointerPointerEqualsLinkedPointerConstraint

A registered method on a symbol to create the constraint.

Examples:

q.where :field.equals_linked_pointer => { through: :linked_field, field: :target_field }

Returns:



3020
# File 'lib/parse/query/constraints.rb', line 3020

register :equals_linked_pointer