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, #depiction_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.



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

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)


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

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



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

def identifier_object_id
  @identifier_object_id
end

#identifier_object_typeObject

Parameters:

  • identifier_object_type (Array, String)

    like ‘Otu’

    or ['Otu', 'Specimen']
    


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

def identifier_object_type
  @identifier_object_type
end

#namespace_idArray

Returns:

  • (Array)


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

def namespace_id
  @namespace_id
end

#namespace_nameObject

Returns the value of attribute namespace_name.



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

def namespace_name
  @namespace_name
end

#namespace_short_nameObject

Returns the value of attribute namespace_short_name.



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

def namespace_short_name
  @namespace_short_name
end

#query_stringObject

TODO: Renamed ‘cached` A fully qualified identifier, matches cached !! This is the only wildcarded value !!



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

def query_string
  @query_string
end

#typeObject

Returns Array.

Returns:

  • Array



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

def type
  @type
end

Instance Method Details

#and_clausesActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


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

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



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

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



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

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



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

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



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

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



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

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)


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

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



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

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

#matching_namespace(attribute) ⇒ Object



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

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



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

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

#project_id_facetObject



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

def project_id_facet
  if ignores_project?
    nil
 else
   super
 end
end