Class: JSONAPI::ActiveRelation::JoinManager

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi/active_relation/join_manager.rb

Overview

Stores relationship paths starting from the resource_klass, consolidating duplicate paths from relationships, filters and sorts. When joins are made the table aliases are tracked in join_details

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_klass:, source_relationship: nil, relationships: nil, filters: nil, sort_criteria: nil) ⇒ JoinManager

Returns a new instance of JoinManager.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jsonapi/active_relation/join_manager.rb', line 12

def initialize(resource_klass:,
               source_relationship: nil,
               relationships: nil,
               filters: nil,
               sort_criteria: nil)

  @resource_klass = resource_klass
  @join_details = nil
  @collected_aliases = Set.new

  @resource_join_tree = {
      root: {
          join_type: :root,
          resource_klasses: {
              resource_klass => {
                  relationships: {}
              }
          }
      }
  }
  add_source_relationship(source_relationship)
  add_sort_criteria(sort_criteria)
  add_filters(filters)
  add_relationships(relationships)
end

Instance Attribute Details

#join_detailsObject (readonly)

Returns the value of attribute join_details.



7
8
9
# File 'lib/jsonapi/active_relation/join_manager.rb', line 7

def join_details
  @join_details
end

#resource_join_treeObject (readonly)

Returns the value of attribute resource_join_tree.



7
8
9
# File 'lib/jsonapi/active_relation/join_manager.rb', line 7

def resource_join_tree
  @resource_join_tree
end

#resource_klassObject (readonly)

Returns the value of attribute resource_klass.



7
8
9
# File 'lib/jsonapi/active_relation/join_manager.rb', line 7

def resource_klass
  @resource_klass
end

#source_relationshipObject (readonly)

Returns the value of attribute source_relationship.



7
8
9
# File 'lib/jsonapi/active_relation/join_manager.rb', line 7

def source_relationship
  @source_relationship
end

Class Method Details

.alias_from_arel_node(node) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/jsonapi/active_relation/join_manager.rb', line 92

def self.alias_from_arel_node(node)
  case node.left
  when Arel::Table
    node.left.name
  when Arel::Nodes::TableAlias
    node.left.right
  when Arel::Nodes::StringJoin
    # :nocov:
    warn "alias_from_arel_node: Unsupported join type - use custom filtering and sorting"
    nil
    # :nocov:
  end
end

.get_join_arel_node(records, options = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/jsonapi/active_relation/join_manager.rb', line 73

def self.get_join_arel_node(records, options = {})
  init_join_sources = records.arel.join_sources
  init_join_sources_length = init_join_sources.length

  records = yield(records, options)

  join_sources = records.arel.join_sources
  if join_sources.length > init_join_sources_length
    last_join = (join_sources - init_join_sources).last
  else
    # :nocov:
    warn "get_join_arel_node: No join added"
    last_join = nil
    # :nocov:
  end

  return records, last_join
end

Instance Method Details

#join(records, options) ⇒ Object



38
39
40
41
42
# File 'lib/jsonapi/active_relation/join_manager.rb', line 38

def join(records, options)
  fail "can't be joined again" if @join_details
  @join_details = {}
  perform_joins(records, options)
end

#join_details_by_polymorphic_relationship(relationship, type) ⇒ Object



63
64
65
66
# File 'lib/jsonapi/active_relation/join_manager.rb', line 63

def join_details_by_polymorphic_relationship(relationship, type)
  segment = PathSegment::Relationship.new(relationship: relationship, resource_klass: resource_klass.resource_klass_for(type))
  @join_details[segment]
end

#join_details_by_relationship(relationship) ⇒ Object



68
69
70
71
# File 'lib/jsonapi/active_relation/join_manager.rb', line 68

def join_details_by_relationship(relationship)
  segment = PathSegment::Relationship.new(relationship: relationship, resource_klass: relationship.resource_klass)
  @join_details[segment]
end

#source_join_details(type = nil) ⇒ Object

source details will only be on a relationship if the source_relationship is set this method gets the join details whether they are on a relationship or are just pseudo details for the base resource. Specify the resource type for polymorphic relationships



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/jsonapi/active_relation/join_manager.rb', line 48

def source_join_details(type=nil)
  if source_relationship
    related_resource_klass = type ? resource_klass.resource_klass_for(type) : source_relationship.resource_klass
    segment = PathSegment::Relationship.new(relationship: source_relationship, resource_klass: related_resource_klass)
    details = @join_details[segment]
  else
    if type
      details = @join_details["##{type}"]
    else
      details = @join_details['']
    end
  end
  details
end