Class: Parse::Constraint::PointerEqualsLinkedPointerConstraint
- Inherits:
-
Constraint
- Object
- Constraint
- Parse::Constraint::PointerEqualsLinkedPointerConstraint
- 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 Method Summary collapse
-
#build ⇒ Hash
The compiled constraint.
-
#equals_linked_pointer ⇒ PointerEqualsLinkedPointerConstraint
A registered method on a symbol to create the constraint.
Instance Method Details
#build ⇒ Hash
Returns the compiled constraint.
3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 |
# File 'lib/parse/query/constraints.rb', line 3812 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 # 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 # Format field names according to Parse conventions # Pointer fields in MongoDB are stored with _p_ prefix. The local and # through fields are columns of the queried class; the target field is # a column of the linked class, so it uses that class's names. formatted_through = "_p_" + Parse::Query.format_field(through_field) formatted_target = "_p_" + Parse::Query.with_field_aliases(target_collection) do Parse::Query.format_field(target_field) end formatted_local = "_p_" + Parse::Query.format_field(local_field) # 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_pointer ⇒ PointerEqualsLinkedPointerConstraint
A registered method on a symbol to create the constraint.
3809 |
# File 'lib/parse/query/constraints.rb', line 3809 register :equals_linked_pointer |