|
| 1 | +module JSONAPI |
| 2 | + module ActiveRelationResourceFinder |
| 3 | + class JoinTree |
| 4 | + # Stores relationship paths starting from the resource_klass. This allows consolidation of duplicate paths from |
| 5 | + # relationships, filters and sorts. This enables the determination of table aliases as they are joined. |
| 6 | + |
| 7 | + attr_reader :resource_klass, :options, :source_relationship |
| 8 | + |
| 9 | + def initialize(resource_klass:, options: {}, source_relationship: nil, filters: nil, sort_criteria: nil) |
| 10 | + @resource_klass = resource_klass |
| 11 | + @options = options |
| 12 | + @source_relationship = source_relationship |
| 13 | + |
| 14 | + @join_relationships = {} |
| 15 | + |
| 16 | + add_sort_criteria(sort_criteria) |
| 17 | + add_filters(filters) |
| 18 | + end |
| 19 | + |
| 20 | + # A hash of joins that can be used to create the required joins |
| 21 | + def get_joins |
| 22 | + walk_relation_node(@join_relationships) |
| 23 | + end |
| 24 | + |
| 25 | + def add_filters(filters) |
| 26 | + return if filters.blank? |
| 27 | + filters.each_key do |filter| |
| 28 | + # Do not add joins for filters with an apply callable. This can be overridden by setting perform_joins to true |
| 29 | + next if resource_klass._allowed_filters[filter].try(:[], :apply) && |
| 30 | + !resource_klass._allowed_filters[filter].try(:[], :perform_joins) |
| 31 | + |
| 32 | + add_join(filter) |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + def add_sort_criteria(sort_criteria) |
| 37 | + return if sort_criteria.blank? |
| 38 | + |
| 39 | + sort_criteria.each do |sort| |
| 40 | + add_join(sort[:field], :left) |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + private |
| 45 | + |
| 46 | + def add_join_relationship(parent_joins, join_name, relation_name, type) |
| 47 | + parent_joins[join_name] ||= {relation_name: relation_name, relationship: {}, type: type} |
| 48 | + if parent_joins[join_name][:type] == :left && type == :inner |
| 49 | + parent_joins[join_name][:type] = :inner |
| 50 | + end |
| 51 | + parent_joins[join_name][:relationship] |
| 52 | + end |
| 53 | + |
| 54 | + def add_join(path, default_type = :inner) |
| 55 | + relationships, _field = resource_klass.parse_relationship_path(path) |
| 56 | + |
| 57 | + current_joins = @join_relationships |
| 58 | + |
| 59 | + terminated = false |
| 60 | + |
| 61 | + relationships.each do |relationship| |
| 62 | + if terminated |
| 63 | + # ToDo: Relax this, if possible |
| 64 | + # :nocov: |
| 65 | + warn "Can not nest joins under polymorphic join" |
| 66 | + # :nocov: |
| 67 | + end |
| 68 | + |
| 69 | + if relationship.polymorphic? |
| 70 | + relation_names = relationship.polymorphic_relations |
| 71 | + relation_names.each do |relation_name| |
| 72 | + join_name = "#{relationship.name}[#{relation_name}]" |
| 73 | + add_join_relationship(current_joins, join_name, relation_name, :left) |
| 74 | + end |
| 75 | + terminated = true |
| 76 | + else |
| 77 | + join_name = relationship.name |
| 78 | + current_joins = add_join_relationship(current_joins, join_name, relationship.relation_name(options), default_type) |
| 79 | + end |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + # Create a nested set of hashes from an array of path components. This will be used by the `join` methods. |
| 84 | + # [post, comments] => { post: { comments: {} } |
| 85 | + def relation_join_hash(path, path_hash = {}) |
| 86 | + relation = path.shift |
| 87 | + if relation |
| 88 | + path_hash[relation] = {} |
| 89 | + relation_join_hash(path, path_hash[relation]) |
| 90 | + end |
| 91 | + path_hash |
| 92 | + end |
| 93 | + |
| 94 | + # Returns the paths from shortest to longest, allowing the capture of the table alias for earlier paths. For |
| 95 | + # example posts, posts.comments and then posts.comments.author joined in that order will alow each |
| 96 | + # alias to be determined whereas just joining posts.comments.author will only record the author alias. |
| 97 | + # ToDo: Dependence on this specialized logic should be removed in the future, if possible. |
| 98 | + def walk_relation_node(node, paths = {}, current_relation_path = [], current_relationship_path = []) |
| 99 | + node.each do |key, value| |
| 100 | + if current_relation_path.empty? && source_relationship |
| 101 | + current_relation_path << source_relationship.relation_name(options) |
| 102 | + end |
| 103 | + |
| 104 | + current_relation_path << value[:relation_name].to_s |
| 105 | + current_relationship_path << key.to_s |
| 106 | + |
| 107 | + rel_path = current_relationship_path.join('.') |
| 108 | + paths[rel_path] ||= { |
| 109 | + alias: nil, |
| 110 | + join_type: value[:type], |
| 111 | + relation_join_hash: relation_join_hash(current_relation_path.dup) |
| 112 | + } |
| 113 | + |
| 114 | + walk_relation_node(value[:relationship], |
| 115 | + paths, |
| 116 | + current_relation_path, |
| 117 | + current_relationship_path) |
| 118 | + |
| 119 | + current_relation_path.pop |
| 120 | + current_relationship_path.pop |
| 121 | + end |
| 122 | + paths |
| 123 | + end |
| 124 | + end |
| 125 | + end |
| 126 | +end |
0 commit comments