Module: Queries::Concerns::Identifiers

Extended by:
ActiveSupport::Concern
Included in:
Queries::CollectionObject::Filter, Image::Filter, Query
Defined in:
lib/queries/concerns/identifiers.rb

Overview

Helpers for queries that reference Identifier

For filter queries: !! requires a `query_base` method !! requires `set_identifiers` be called in initialize()

See spec/lib/queries/collection_object/filter_spec.rb for existing spec tests

Instance Method Summary collapse

Instance Method Details

#autocomplete_identifier_cached_exactObject

Autocomplete for tables referencing identifiers See lib/queries/identifiers/autocomplete for autocomplete for identifiers

May need to alter base query here



152
153
154
# File 'lib/queries/concerns/identifiers.rb', line 152

def autocomplete_identifier_cached_exact
  query_base.joins(:identifiers).where(with_identifier_cached.to_sql)
end

#autocomplete_identifier_cached_likeObject



160
161
162
# File 'lib/queries/concerns/identifiers.rb', line 160

def autocomplete_identifier_cached_like
  query_base.joins(:identifiers).where(with_identifier_cached_wildcarded.to_sql)
end

#autocomplete_identifier_identifier_exactObject



156
157
158
# File 'lib/queries/concerns/identifiers.rb', line 156

def autocomplete_identifier_identifier_exact
  query_base.joins(:identifiers).where(with_identifier_identifier.to_sql)
end

#autocomplete_identifier_matching_cached_anywhereObject



164
165
166
# File 'lib/queries/concerns/identifiers.rb', line 164

def autocomplete_identifier_matching_cached_anywhere
  query_base.joins(:identifiers).where(with_identifier_cached_wildcarded.to_sql)
end

#autocomplete_identifier_matching_cached_fragments_anywhereObject



168
169
170
# File 'lib/queries/concerns/identifiers.rb', line 168

def autocomplete_identifier_matching_cached_fragments_anywhere
  query_base.joins(:identifiers).where(with_identifier_cached_like_fragments.to_sql)
end

#betweenObject



67
68
69
70
71
72
73
74
75
# File 'lib/queries/concerns/identifiers.rb', line 67

def between
  Arel::Nodes::Between.new(
    cast,
    Arel::Nodes::And.new(
      [ Arel::Nodes::SqlLiteral.new(identifier_start),
        Arel::Nodes::SqlLiteral.new(identifier_end) ]
    )
  )
end

#castObject



59
60
61
# File 'lib/queries/concerns/identifiers.rb', line 59

def cast
  Arel::Nodes::NamedFunction.new('CAST', [substring])
end

#identifier_between_facetObject



102
103
104
105
106
107
108
109
110
# File 'lib/queries/concerns/identifiers.rb', line 102

def identifier_between_facet
  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

  query_base.joins(:identifiers).where(w)
end

#identifier_facetObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/queries/concerns/identifiers.rb', line 77

def identifier_facet
  return nil if identifier.blank?

  q = query_base.joins(:identifiers)
  w = identifier_exact ?
    identifier_table[:cached].eq(identifier) :
    identifier_table[:cached].matches('%' + identifier + '%')

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

#identifier_namespace_facetObject



96
97
98
99
100
# File 'lib/queries/concerns/identifiers.rb', line 96

def identifier_namespace_facet
  return nil if namespace_id.blank?
  q = query_base.joins(:identifiers)
  q.where(identifier_table[:namespace_id].eq(namespace_id))
end

#identifier_tableArel::Table

Returns:

  • (Arel::Table)


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

def identifier_table
  ::Identifier.arel_table
end

#identifier_type_facetObject



89
90
91
92
93
94
# File 'lib/queries/concerns/identifiers.rb', line 89

def identifier_type_facet
  return nil if identifier_type.empty?
  q = query_base.joins(:identifiers)
  w = identifier_table[:type].eq_any(identifier_type)
  q.where(w) 
end

#query_baseObject (private)



174
175
176
# File 'lib/queries/concerns/identifiers.rb', line 174

def query_base
  table.name.classify.safe_constantize
end

#set_identifier(params) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/queries/concerns/identifiers.rb', line 44

def set_identifier(params)
  @namespace_id = params[:namespace_id]
  @identifier_start = params[:identifier_start]
  @identifier_end = params[:identifier_end]
  @identifier = params[:identifier]

  @identifier_exact = (params[:identifier_exact]&.downcase == 'true' ? true : false) if !params[:identifier_exact].nil?
  @identifier_type = params[:identifier_type] || [] 
end

#substringObject



63
64
65
# File 'lib/queries/concerns/identifiers.rb', line 63

def substring
  Arel::Nodes::NamedFunction.new('SUBSTRING', [ identifier_table[:identifier], Arel::Nodes::SqlLiteral.new("'([\\d]{1,9})$'") ]).as('integer')
end

#with_identifier_cachedArel::Nodes::Equality

Returns:

  • (Arel::Nodes::Equality)


118
119
120
# File 'lib/queries/concerns/identifiers.rb', line 118

def with_identifier_cached
  identifier_table[:cached].eq(query_string)
end

#with_identifier_cached_like_fragmentsArel::Nodes::Grouping

Returns:

  • (Arel::Nodes::Grouping)


139
140
141
142
143
# File 'lib/queries/concerns/identifiers.rb', line 139

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_endArel::Nodes::Equality

Returns:

  • (Arel::Nodes::Equality)


127
128
129
# File 'lib/queries/concerns/identifiers.rb', line 127

def with_identifier_cached_wildcard_end
  identifier_table[:cached].matches(end_wildcard)
end

#with_identifier_cached_wildcardedArel::Nodes::Equality

Returns:

  • (Arel::Nodes::Equality)


132
133
134
# File 'lib/queries/concerns/identifiers.rb', line 132

def with_identifier_cached_wildcarded
  identifier_table[:cached].matches(start_and_end_wildcard)
end

#with_identifier_identifierArel::Nodes::Equality

Returns:

  • (Arel::Nodes::Equality)


113
114
115
# File 'lib/queries/concerns/identifiers.rb', line 113

def with_identifier_identifier
  identifier_table[:identifier].eq(query_string)
end

#with_identifier_wildcard_endObject



122
123
124
# File 'lib/queries/concerns/identifiers.rb', line 122

def with_identifier_wildcard_end
  identifier_table[:identifier].matches(end_wildcard)
end