Module: Queries::Concerns::Containable

Extended by:
ActiveSupport::Concern
Includes:
Helpers
Included in:
Queries::CollectionObject::Filter, Extract::Filter
Defined in:
lib/queries/concerns/containable.rb

Overview

TODO: !? Requires that Query includes Queries::Concerns::Identifiers

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#boolean_param, #integer_param

Class Method Details

.merge_clausesObject



24
25
26
# File 'lib/queries/concerns/containable.rb', line 24

def self.merge_clauses
  [ ]
end

.paramsObject



10
11
12
# File 'lib/queries/concerns/containable.rb', line 10

def self.params
  [ ] # params come from Identifiers so far
end

Instance Method Details

#container_tableObject



20
21
22
# File 'lib/queries/concerns/containable.rb', line 20

def container_table
  ::Container.arel_table
end

#global_identifiers_container_matchObject

Objects with/out global identifiers by proxy of of their containment.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/queries/concerns/containable.rb', line 103

def global_identifiers_container_match
  return nil if global_identifiers.nil?

  s = "WITH identified_containers AS (#{identified_containers.where("identifiers.type ILIKE 'Identifier::Global%'").to_sql}) "
  s << referenced_klass_with_container_items
    .joins("JOIN identified_containers AS identified_containers1 on identified_containers1.id = ci2.contained_object_id and ci2.contained_object_type = 'Container'").to_sql

  q = referenced_klass.from( "(#{s}) as #{table.name}")

  if global_identifiers
    q
  else
    # If we don't need to exclude objects from containers that don't have (global) identifiers.
    nil
  end
end

#identified_containersObject



28
29
30
31
# File 'lib/queries/concerns/containable.rb', line 28

def identified_containers
  # TODO: Ultimately replace with Containers filter
  ::Container.joins(:identifiers).where(project_id:)
end

#identifier_between_container_matchObject



72
73
74
75
76
77
78
79
80
# File 'lib/queries/concerns/containable.rb', line 72

def identifier_between_container_match
  return nil if @identifier_start.nil?
  @identifier_end = @identifier_start if @identifier_end.nil?

  w = between
  w = w.and(identifier_table[:namespace_id].eq(namespace_id)) if namespace_id # TODO: redundant with namespace facet likely

  referenced_klass_container_identifiers.where(w)
end

#identifier_container_matchObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/queries/concerns/containable.rb', line 60

def identifier_container_match
  return nil if identifier.blank?

  w = identifier_exact ?
    identifier_table[:cached].eq(identifier) :
    identifier_table[:cached].matches('%' + identifier.to_s + '%')

  w = w.and(identifier_table[:namespace_id].eq(namespace_id)) if namespace_id

  referenced_klass_container_identifiers.where(w)
end

#identifier_namespace_container_matchObject



50
51
52
53
# File 'lib/queries/concerns/containable.rb', line 50

def identifier_namespace_container_match
  return nil if namespace_id.blank?
  referenced_klass_container_identifiers.where(identifiers: {namespace_id:})
end

#identifier_type_container_matchObject



55
56
57
58
# File 'lib/queries/concerns/containable.rb', line 55

def identifier_type_container_match
  return nil if identifier_type.empty?
  referenced_klass_container_identifiers.where(identifiers: {type: identifier_type})
end

#identifiers_container_matchObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/queries/concerns/containable.rb', line 120

def identifiers_container_match
  return nil if identifiers.nil?

  s = "WITH identified_containers AS (#{identified_containers.to_sql}) "
  s << referenced_klass_with_container_items
    .joins("JOIN identified_containers AS identified_containers1 on identified_containers1.id = ci2.contained_object_id and ci2.contained_object_type = 'Container'").to_sql

  q = referenced_klass.from( "(#{s}) as #{table.name}")

  if identifiers
    q
  else
    s = "WITH co_with_identifier_containers as (#{q.to_sql}) " +
    referenced_klass_with_container_items.joins("LEFT JOIN co_with_identifier_containers AS co_with_identifier_containers1 on co_with_identifier_containers1.id = #{table.name}.id")
        .where('co_with_identifier_containers1.id IS NULL').to_sql

    referenced_klass.from( "(#{s}) as #{table.name}")
  end
end

#local_identifiers_container_matchObject

Objects with/out local identifiers by proxy of of their containment.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/queries/concerns/containable.rb', line 84

def local_identifiers_container_match
  return nil if local_identifiers.nil?

  s = "WITH identified_containers AS (#{identified_containers.where("identifiers.type ILIKE 'Identifier::Local%'").to_sql}) "
  s << referenced_klass_with_container_items
    .joins("JOIN identified_containers AS identified_containers1 on identified_containers1.id = ci2.contained_object_id and ci2.contained_object_type = 'Container'").to_sql

  q = referenced_klass.from( "(#{s}) as #{table.name}")

  if local_identifiers
    q
  else
    # If we don't need to exclude objects from containers that don't have (local) identifiers.
    nil
  end
end

#match_identifiers_container_matchObject



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/queries/concerns/containable.rb', line 140

def match_identifiers_container_match
  return nil if match_identifiers.blank?
  ids = identifiers_to_match
  return nil if ids.empty?

  if match_identifiers_type&.downcase == 'identifier'
    referenced_klass_container_identifiers.where(identifiers: {cached: ids})
  elsif match_identifiers_type&.downcase == 'dwc_occurrence_id'
    referenced_klass_container_identifiers.where(identifiers: {cached: ids, type: 'Identifier::Global::Uuid::TaxonworksDwcOccurrence' })
  else
    nil # don't match on internal IDs of containers, it doesn't make sense
  end
end

#referenced_klass_container_identifiersObject



45
46
47
48
# File 'lib/queries/concerns/containable.rb', line 45

def referenced_klass_container_identifiers
  referenced_klass_containers
    .joins("JOIN identifiers on identifiers.identifier_object_id = containers.id and identifiers.identifier_object_type = 'Container'")
end

#referenced_klass_containersObject



40
41
42
43
# File 'lib/queries/concerns/containable.rb', line 40

def referenced_klass_containers
  referenced_klass_with_container_items
    .joins("JOIN containers on containers.id = ci2.contained_object_id and ci2.contained_object_type = 'Container'")
end

#referenced_klass_with_container_itemsObject

referenced_klass referenceable in any ContainerItem



34
35
36
37
38
# File 'lib/queries/concerns/containable.rb', line 34

def referenced_klass_with_container_items
  referenced_klass.joins(:container_item)
    .joins('JOIN container_item_hierarchies cih on cih.descendant_id = container_items.id')
    .joins('JOIN container_items ci2 on ci2.id = cih.ancestor_id')
end

#set_containable_params(params) ⇒ Object



17
18
# File 'lib/queries/concerns/containable.rb', line 17

def set_containable_params(params)
end