Class: Queries::Identifier::Filter

Inherits:
Query::Filter show all
Includes:
Concerns::Polymorphic
Defined in:
lib/queries/identifier/filter.rb

Constant Summary collapse

PARAMS =
[
  *::Identifier.related_foreign_keys.map(&:to_sym),
  :identifier,
  :identifier_id,
  :identifier_object_id,
  :identifier_object_type,
  :namespace_id,
  :namespace_name,
  :namespace_short_name,
  :query_string,
  :type,
  identifier: [],
  identifier_id: [],
  identifier_object_id: [],
  identifier_object_type: [],
  namespace_id: [],
].freeze

Constants inherited from Query::Filter

Query::Filter::FILTER_QUERIES, Query::Filter::SUBQUERIES

Instance Attribute Summary collapse

Attributes inherited from Query::Filter

#api, #asserted_distribution_query, #biological_association_query, #biological_associations_graph_query, #collecting_event_query, #collection_object_query, #content_query, #controlled_vocabulary_term_query, #data_attribute_query, #descriptor_query, #document_query, #dwc_occurrence_query, #extract_query, #field_occurrence_query, #image_query, #loan_query, #object_global_id, #observation_query, #order_by, #otu_query, #page, #paginate, #params, #per, #person_query, #project_id, #recent, #recent_target, #taxon_name_query, #venn, #venn_mode

Attributes inherited from Query

#terms

Instance Method Summary collapse

Methods inherited from Query::Filter

#all, #all_and_clauses, #all_merge_clauses, #annotator_and_clauses, #annotator_merge_clauses, annotator_params, api_except_params, api_excluded_params, #apply_venn, #attribute_exact_facet, base_filter, base_query_name, base_query_to_h, #deep_permit, included_annotator_facets, instantiated_base_filter, inverted_subqueries, #model_id_facet, #object_global_id_facet, #only_project?, params, #permitted_params, #process_url_into_params, query_name, #query_name, #set_nested_queries, #shared_and_clauses, #subquery_vector, #target_and_clauses, #venn_query

Methods inherited from Query

#alphabetic_strings, #alphanumeric_strings, base_name, #base_name, #base_query, #build_terms, #end_wildcard, #levenshtein_distance, #match_ordered_wildcard_pieces_in_cached, #no_terms?, referenced_klass, #referenced_klass, #referenced_klass_except, #referenced_klass_intersection, #referenced_klass_union, #start_and_end_wildcard, #start_wildcard, #table, #wildcard_pieces

Constructor Details

#initialize(query_params) ⇒ Filter

Returns a new instance of Filter.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/queries/identifier/filter.rb', line 57

def initialize(query_params)
  super

  @identifier = params[:identifier]
  @identifier_id = params[:identifier_id]
  @identifier_object_id = params[:identifier_object_id]
  @identifier_object_type = params[:identifier_object_type]
  @namespace_id = params[:namespace_id]
  @namespace_name = params[:namespace_name]
  @namespace_short_name = params[:namespace_short_name]
  @query_string = params[:query_string]
  @type = params[:type]

  set_polymorphic_params(params)
end

Instance Attribute Details

#identifierArray

Returns:

  • (Array)


35
36
37
# File 'lib/queries/identifier/filter.rb', line 35

def identifier
  @identifier
end

#identifier_idObject

Returns Array.

Returns:

  • Array



28
29
30
# File 'lib/queries/identifier/filter.rb', line 28

def identifier_id
  @identifier_id
end

#identifier_object_idObject

Returns Array.

Returns:

  • Array



48
49
50
# File 'lib/queries/identifier/filter.rb', line 48

def identifier_object_id
  @identifier_object_id
end

#identifier_object_typeObject

Parameters:

  • identifier_object_type (Array, String)

    like ‘Otu’

    or ['Otu', 'Specimen']
    


45
46
47
# File 'lib/queries/identifier/filter.rb', line 45

def identifier_object_type
  @identifier_object_type
end

#namespace_idArray

Returns:

  • (Array)


38
39
40
# File 'lib/queries/identifier/filter.rb', line 38

def namespace_id
  @namespace_id
end

#namespace_nameObject

Returns the value of attribute namespace_name.



40
41
42
# File 'lib/queries/identifier/filter.rb', line 40

def namespace_name
  @namespace_name
end

#namespace_short_nameObject

Returns the value of attribute namespace_short_name.



39
40
41
# File 'lib/queries/identifier/filter.rb', line 39

def namespace_short_name
  @namespace_short_name
end

#query_stringObject

A fully qualified identifier, matches cached !! This is the only wildcarded value !!



32
33
34
# File 'lib/queries/identifier/filter.rb', line 32

def query_string
  @query_string
end

#typeObject

Returns Array.

Returns:

  • Array



51
52
53
# File 'lib/queries/identifier/filter.rb', line 51

def type
  @type
end

Instance Method Details

#and_clausesActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


182
183
184
185
186
187
188
189
190
191
192
# File 'lib/queries/identifier/filter.rb', line 182

def and_clauses
  [
    cached_facet,
    matching_identifier_attribute(:identifier),
    matching_identifier_attribute(:namespace_id),
    matching_identifier_attribute(:type),
    identifier_object_id_facet,
    identifier_object_type_facet,
    community_project_id_facet,
  ]
end

#annotated_classObject



97
98
99
100
101
102
103
104
105
106
# File 'lib/queries/identifier/filter.rb', line 97

def annotated_class
  # !! May be some base-class requirements here?!
  if identifier_object_type.present?
    identifier_object_type
  elsif polymorphic_type.present?
    [polymorphic_type]
  else
    [::Queries::Annotator.annotated_class(params, ::Identifier)]
  end
end

#cached_facetObject



127
128
129
130
# File 'lib/queries/identifier/filter.rb', line 127

def cached_facet
  return nil if query_string.blank?
  table[:cached].matches('%' + query_string + '%')
end

#community_project_id_facetObject

Ommits non community klasses of identifiers



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/queries/identifier/filter.rb', line 114

def community_project_id_facet
  return nil if project_id.nil?
  if !ignores_project?
    # Not a community class
    return table[:project_id].in(project_id)
  else
    # Is a community class
    # Identifiers that are not local only
    return table[:type].matches('Identifier::Global%').or(table[:project_id].in(project_id))
  end
  nil
end

#identifier_object_id_facetObject



132
133
134
135
# File 'lib/queries/identifier/filter.rb', line 132

def identifier_object_id_facet
  return nil if identifier_object_id.empty?
  table[:identifier_object_id].in(identifier_object_id)
end

#identifier_object_type_facetObject



137
138
139
140
# File 'lib/queries/identifier/filter.rb', line 137

def identifier_object_type_facet
  return nil if identifier_object_type.empty?
   table[:identifier_object_type].in(identifier_object_type)
end

#ignores_project?Boolean

Returns:

  • (Boolean)


108
109
110
111
# File 'lib/queries/identifier/filter.rb', line 108

def ignores_project?
  # Use the same constant - TODO: move it dual annotator likely
  ::Identifier::ALWAYS_COMMUNITY & annotated_class == annotated_class
end

#matching_identifier_attribute(attribute) ⇒ Object



142
143
144
145
# File 'lib/queries/identifier/filter.rb', line 142

def matching_identifier_attribute(attribute)
  v = send(attribute)
  v.blank? ? nil : table[attribute].in(v)
end

#matching_namespace(attribute) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/queries/identifier/filter.rb', line 147

def matching_namespace(attribute)
  v = self.send("namespace_#{attribute}")
  return nil if v.blank?
  o = table
  n = ::Namespace.arel_table

  a = o.alias('a_')
  b = o.project(a[Arel.star]).from(a)

  c = n.alias('n1')

  b = b.join(c, Arel::Nodes::OuterJoin)
    .on(
      a[:namespace_id].eq(c[:id])
    )

  e = c[:id].not_eq(nil)
  f = c[attribute].eq(v)

  b = b.where(e.and(f))
  b = b.group(a['id'])
  b = b.as('z5_')

  ::Identifier.joins(Arel::Nodes::InnerJoin.new(b, Arel::Nodes::On.new(b['id'].eq(o['id']))))
end

#merge_clausesObject



194
195
196
197
198
199
# File 'lib/queries/identifier/filter.rb', line 194

def merge_clauses
  [
    matching_namespace(:short_name),
    matching_namespace(:name),
  ]
end

#project_id_facetObject



173
174
175
176
177
178
179
# File 'lib/queries/identifier/filter.rb', line 173

def project_id_facet
  if ignores_project?
    nil
 else
   super
 end
end