Module: Queries::Concerns::Identifiers
- Extended by:
- ActiveSupport::Concern
- Includes:
- Helpers
- Included in:
- Query, Query::Autocomplete, Repository::Autocomplete
- Defined in:
- lib/queries/concerns/identifiers.rb
Overview
Helpers for queries that reference Identifier
TODO: Some of this is only for autocomplete !! See anything refencing terms, query_string
These queries are impacted by Shared::Containable. When Containable identifiers have to match, potentially targets directly, and indirectly through virtual containers.
Class Method Summary collapse
Instance Method Summary collapse
-
#autocomplete_identifier_cached_exact ⇒ Object
Autocomplete for tables referencing identifiers See lib/queries/identifiers/autocomplete for autocomplete for identifiers.
- #autocomplete_identifier_cached_like ⇒ Object
- #autocomplete_identifier_identifier_exact ⇒ Object
- #autocomplete_identifier_matching_cached_anywhere ⇒ Object
- #autocomplete_identifier_matching_cached_fragments_anywhere ⇒ Object
- #between ⇒ Object
-
#global_identifiers_facet ⇒ Object
TODO: Simplify local/global copy-pasta.
- #identifier_between_facet ⇒ Object
- #identifier_facet ⇒ Object
- #identifier_namespace_facet ⇒ Object
- #identifier_table ⇒ Arel::Table
- #identifier_type_facet ⇒ Object
- #identifiers_facet ⇒ Object
-
#identifiers_to_match ⇒ Object
Array.
- #local_identifiers_facet ⇒ Object
- #match_identifier_order_by(target_query) ⇒ Object
- #match_identifiers_facet ⇒ Object
- #set_identifier_params(params) ⇒ Object
- #with_identifier_cached ⇒ Arel::Nodes::Equality
- #with_identifier_cached_like_fragments ⇒ Arel::Nodes::Grouping
- #with_identifier_cached_wildcard_end ⇒ Arel::Nodes::Equality
- #with_identifier_cached_wildcarded ⇒ Arel::Nodes::Equality
- #with_identifier_identifier ⇒ Arel::Nodes::Equality
- #with_identifier_wildcard_end ⇒ Object
Methods included from Helpers
#boolean_param, #integer_param
Class Method Details
.merge_clauses ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/queries/concerns/identifiers.rb', line 164 def self.merge_clauses [ :global_identifiers_facet, :identifier_between_facet, :identifier_facet, :identifier_namespace_facet, :identifier_type_facet, :identifiers_facet, :local_identifiers_facet, :match_identifiers_facet, ] end |
.params ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/queries/concerns/identifiers.rb', line 15 def self.params [ :identifier, :identifier_end, :identifier_exact, :identifier_start, :identifier_type, :identifiers, :local_identifiers, :global_identifiers, :match_identifiers, :match_identifiers_caseless, :match_identifiers_delimiter, :match_identifiers_type, :namespace_id, identifier_type: [], ] end |
Instance Method Details
#autocomplete_identifier_cached_exact ⇒ Object
Autocomplete for tables referencing identifiers See lib/queries/identifiers/autocomplete for autocomplete for identifiers
May need to alter base query here
425 426 427 |
# File 'lib/queries/concerns/identifiers.rb', line 425 def autocomplete_identifier_cached_exact referenced_klass.joins(:identifiers).where(with_identifier_cached.to_sql) end |
#autocomplete_identifier_cached_like ⇒ Object
433 434 435 |
# File 'lib/queries/concerns/identifiers.rb', line 433 def autocomplete_identifier_cached_like referenced_klass.joins(:identifiers).where(with_identifier_cached_wildcarded.to_sql) end |
#autocomplete_identifier_identifier_exact ⇒ Object
429 430 431 |
# File 'lib/queries/concerns/identifiers.rb', line 429 def autocomplete_identifier_identifier_exact referenced_klass.joins(:identifiers).where(with_identifier_identifier.to_sql) end |
#autocomplete_identifier_matching_cached_anywhere ⇒ Object
437 438 439 |
# File 'lib/queries/concerns/identifiers.rb', line 437 def autocomplete_identifier_matching_cached_anywhere referenced_klass.joins(:identifiers).where(with_identifier_cached_wildcarded.to_sql) end |
#autocomplete_identifier_matching_cached_fragments_anywhere ⇒ Object
441 442 443 |
# File 'lib/queries/concerns/identifiers.rb', line 441 def autocomplete_identifier_matching_cached_fragments_anywhere referenced_klass.joins(:identifiers).where(with_identifier_cached_like_fragments.to_sql) end |
#between ⇒ Object
177 178 179 180 181 182 183 184 185 |
# File 'lib/queries/concerns/identifiers.rb', line 177 def between Arel::Nodes::Between.new( identifier_table[:cached_numeric_identifier], Arel::Nodes::And.new( [ Arel::Nodes::SqlLiteral.new(identifier_start), Arel::Nodes::SqlLiteral.new(identifier_end) ] ) ) end |
#global_identifiers_facet ⇒ Object
TODO: Simplify local/global copy-pasta
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/queries/concerns/identifiers.rb', line 307 def global_identifiers_facet return nil if global_identifiers.nil? a = nil if global_identifiers a = referenced_klass.joins(:identifiers).where("identifiers.type ILIKE 'Identifier::Global%'").distinct else i = ::Identifier.arel_table[:identifier_object_id].eq(table[:id]).and( ::Identifier.arel_table[:identifier_object_type].eq( table.name.classify.to_s ) ) a = referenced_klass.where.not(::Identifier::Local.where(i).arel.exists) end # TODO: this logic can almost certainly be simplified. Test coverage is decent for attempts to do so. if referenced_klass.is_containable? c = referenced_klass_union([a, global_identifiers_container_match ].compact) @global_identifiers = !local_identifiers except = global_identifiers_container_match @global_identifiers = !local_identifiers if except return referenced_klass.from('(' + c.to_sql + " EXCEPT #{except.to_sql}) as #{table.name}") else return referenced_klass.from('(' + c.to_sql + ") as #{table.name}") end end a end |
#identifier_between_facet ⇒ Object
373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/queries/concerns/identifiers.rb', line 373 def identifier_between_facet return nil if identifier_start.blank? @identifier_end = identifier_start if identifier_end.blank? w = between w = w.and(identifier_table[:namespace_id].eq(namespace_id)) if namespace_id # TODO: redundant with namespace facet likely a = referenced_klass.joins(:identifiers).where(w) a = referenced_klass_union([a, identifier_between_container_match]) if referenced_klass.is_containable? a end |
#identifier_facet ⇒ Object
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/queries/concerns/identifiers.rb', line 338 def identifier_facet return nil if identifier.blank? q = referenced_klass.joins(:identifiers) 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 a = q.where(w) a = referenced_klass_union([a, identifier_container_match]) if referenced_klass.is_containable? a end |
#identifier_namespace_facet ⇒ Object
364 365 366 367 368 369 370 371 |
# File 'lib/queries/concerns/identifiers.rb', line 364 def identifier_namespace_facet return nil if namespace_id.blank? q = referenced_klass.joins(:identifiers) a = q.where(identifier_table[:namespace_id].eq(namespace_id)) a = referenced_klass_union([a, identifier_namespace_container_match ]) if referenced_klass.is_containable? a end |
#identifier_table ⇒ Arel::Table
160 161 162 |
# File 'lib/queries/concerns/identifiers.rb', line 160 def identifier_table ::Identifier.arel_table end |
#identifier_type_facet ⇒ Object
354 355 356 357 358 359 360 361 362 |
# File 'lib/queries/concerns/identifiers.rb', line 354 def identifier_type_facet return nil if identifier_type.empty? q = referenced_klass.joins(:identifiers) w = identifier_table[:type].in(identifier_type) a = q.where(w) a = referenced_klass_union([a, identifier_type_container_match ]) if referenced_klass.is_containable? a end |
#identifiers_facet ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/queries/concerns/identifiers.rb', line 223 def identifiers_facet return nil if identifiers.nil? a = nil if identifiers a = referenced_klass.joins(:identifiers).distinct else a = referenced_klass.where.missing(:identifiers).distinct end if referenced_klass.is_containable? c = referenced_klass_union([a, identifiers_container_match ]) @identifiers = !identifiers except = identifiers_container_match @identifiers = !identifiers return referenced_klass.from('(' + c.to_sql + " EXCEPT #{except.to_sql}) as #{table.name}") end a end |
#identifiers_to_match ⇒ Object
Returns Array.
188 189 190 191 192 193 194 |
# File 'lib/queries/concerns/identifiers.rb', line 188 def identifiers_to_match ids = match_identifiers.strip.split(match_identifiers_delimiter).flatten.map(&:strip).uniq if match_identifiers_type == 'internal' ids = ids.select{|i| i =~ /^\d+$/} end ids end |
#local_identifiers_facet ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/queries/concerns/identifiers.rb', line 275 def local_identifiers_facet return nil if local_identifiers.nil? a = nil if local_identifiers a = referenced_klass.joins(:identifiers).where("identifiers.type ILIKE 'Identifier::Local%'").distinct else i = ::Identifier.arel_table[:identifier_object_id].eq(table[:id]).and( ::Identifier.arel_table[:identifier_object_type].eq( table.name.classify.to_s ) ) a = referenced_klass.where.not(::Identifier::Local.where(i).arel.exists) end # TODO: this logic can almost certainly be simplified. Test coverage is decent for attempts to do so. if referenced_klass.is_containable? c = referenced_klass_union([a, local_identifiers_container_match ].compact) @local_identifiers = !local_identifiers except = local_identifiers_container_match @local_identifiers = !local_identifiers if except return referenced_klass.from('(' + c.to_sql + " EXCEPT #{except.to_sql}) as #{table.name}") else return referenced_klass.from('(' + c.to_sql + ") as #{table.name}") end end a end |
#match_identifier_order_by(target_query) ⇒ Object
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'lib/queries/concerns/identifiers.rb', line 445 def match_identifier_order_by(target_query) case order_by when :match_identifiers if match_identifiers.present? ids = if match_identifiers_caseless != false # nil or true identifiers_to_match.map(&:downcase) else identifiers_to_match end case match_identifiers_type # rubocop:disable Metrics/BlockNesting when 'internal' o = "array_position(ARRAY[#{ids.join(',')}], #{table.name}.id)" target_query = target_query.order(Arel.sql(o)) else if match_identifiers_caseless != false # nil or true # caseless # TODO: optimize, this was done hastily o = "array_position(ARRAY[#{ids.collect{|i| "'#{i}'"}.join(',')}], LOWER(sid.cached)) s" i = ::Identifier .from('identifiers sid') .where('LOWER(sid.cached) IN (?)', ids) # This is required to de-duplicate for some reason ?! .select("sid.*, #{o}") target_query = target_query.with(sid: i) .joins("JOIN sid on sid.identifier_object_id = #{table.name}.id AND sid.identifier_object_type = '#{referenced_klass.base_class.name}'") .select("#{table.name}.*, sid.s") .order('sid.s') else # TODO: optimize, this was done hastily o = "array_position(ARRAY[#{ids.collect{|i| "'#{i}'"}.join(',')}], sid.cached) s" i = ::Identifier .from('identifiers sid') .where(sid: {cached: ids}) # This is required to de-duplicate for some reason ?! .select("sid.*, #{o}") target_query = target_query.with(sid: i) .joins("JOIN sid on sid.identifier_object_id = #{table.name}.id AND sid.identifier_object_type = '#{referenced_klass.base_class.name}'") .select("#{table.name}.*, sid.s") .order('sid.s') end end end end target_query end |
#match_identifiers_facet ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/queries/concerns/identifiers.rb', line 196 def match_identifiers_facet return nil if match_identifiers.blank? ids = identifiers_to_match return nil if ids.empty? a = nil case match_identifiers_type when 'internal' a = referenced_klass.where(id: ids) when 'identifier' if match_identifiers_caseless != false # nil or true a = referenced_klass.joins(:identifiers).where('LOWER(identifiers.cached) IN (?)', ids.map(&:downcase)).distinct else a = referenced_klass.joins(:identifiers).where(identifiers: {cached: ids}).distinct end when 'dwc_occurrence_id' a = referenced_klass.joins(:identifiers).where(identifiers: {cached: ids, type: 'Identifier::Global::Uuid::TaxonworksDwcOccurrence' }).distinct else return nil end a = referenced_klass_union([a, match_identifiers_container_match ]) if referenced_klass.is_containable? a end |
#set_identifier_params(params) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/queries/concerns/identifiers.rb', line 143 def set_identifier_params(params) @global_identifiers = boolean_param(params, :global_identifiers) @identifier = params[:identifier] @identifier_end = integer_param(params, :identifier_end) @identifier_exact = boolean_param(params, :identifier_exact) @identifier_start = integer_param(params, :identifier_start) @identifier_type = params[:identifier_type] @identifiers = boolean_param(params, :identifiers) @local_identifiers = boolean_param(params, :local_identifiers) @match_identifiers = params[:match_identifiers] @match_identifiers_delimiter = params[:match_identifiers_delimiter] @match_identifiers_type = params[:match_identifiers_type] @match_identifiers_caseless = boolean_param(params, :match_identifiers_caseless) @namespace_id = params[:namespace_id] end |
#with_identifier_cached ⇒ Arel::Nodes::Equality
391 392 393 |
# File 'lib/queries/concerns/identifiers.rb', line 391 def with_identifier_cached identifier_table[:cached].eq(query_string) end |
#with_identifier_cached_like_fragments ⇒ Arel::Nodes::Grouping
412 413 414 415 416 |
# File 'lib/queries/concerns/identifiers.rb', line 412 def with_identifier_cached_like_fragments a = [ start_and_end_wildcard ] a = a + wildcard_wrapped_integers identifier_table[:cached].matches_any(a) end |
#with_identifier_cached_wildcard_end ⇒ Arel::Nodes::Equality
400 401 402 |
# File 'lib/queries/concerns/identifiers.rb', line 400 def with_identifier_cached_wildcard_end identifier_table[:cached].matches(end_wildcard) end |
#with_identifier_cached_wildcarded ⇒ Arel::Nodes::Equality
405 406 407 |
# File 'lib/queries/concerns/identifiers.rb', line 405 def with_identifier_cached_wildcarded identifier_table[:cached].matches(start_and_end_wildcard) end |
#with_identifier_identifier ⇒ Arel::Nodes::Equality
386 387 388 |
# File 'lib/queries/concerns/identifiers.rb', line 386 def with_identifier_identifier identifier_table[:identifier].eq(query_string) end |
#with_identifier_wildcard_end ⇒ Object
395 396 397 |
# File 'lib/queries/concerns/identifiers.rb', line 395 def with_identifier_wildcard_end identifier_table[:identifier].matches(end_wildcard) end |