Module: Queries::Concerns::Identifiers

Extended by:
ActiveSupport::Concern
Includes:
Helpers
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

Methods included from Helpers

#boolean_param

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



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

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

#autocomplete_identifier_cached_likeObject



182
183
184
# File 'lib/queries/concerns/identifiers.rb', line 182

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

#autocomplete_identifier_identifier_exactObject



178
179
180
# File 'lib/queries/concerns/identifiers.rb', line 178

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

#autocomplete_identifier_matching_cached_anywhereObject



186
187
188
# File 'lib/queries/concerns/identifiers.rb', line 186

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

#autocomplete_identifier_matching_cached_fragments_anywhereObject



190
191
192
# File 'lib/queries/concerns/identifiers.rb', line 190

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

#betweenObject



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

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



70
71
72
# File 'lib/queries/concerns/identifiers.rb', line 70

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

#identifier_between_facetObject



124
125
126
127
128
129
130
131
132
# File 'lib/queries/concerns/identifiers.rb', line 124

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



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

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



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

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)


66
67
68
# File 'lib/queries/concerns/identifiers.rb', line 66

def identifier_table
  ::Identifier.arel_table
end

#identifier_type_facetObject



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

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

#identifiers_facetObject



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

def identifiers_facet
  return nil if identifiers.nil?
  if identifiers
    query_base.joins(:identifiers).distinct
  else
    query_base.left_outer_joins(:identifiers)
      .where(identifiers: {id: nil})
      .distinct
  end
end

#query_baseObject (private)



196
197
198
# File 'lib/queries/concerns/identifiers.rb', line 196

def query_base
  table.name.classify.safe_constantize
end

#set_identifier(params) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/queries/concerns/identifiers.rb', line 54

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

  @identifier_exact = boolean_param(params, :identifier_exact)
  @identifier_type = params[:identifier_type] || []
end

#substringObject



74
75
76
# File 'lib/queries/concerns/identifiers.rb', line 74

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)


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

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

#with_identifier_cached_like_fragmentsArel::Nodes::Grouping

Returns:

  • (Arel::Nodes::Grouping)


161
162
163
164
165
# File 'lib/queries/concerns/identifiers.rb', line 161

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)


149
150
151
# File 'lib/queries/concerns/identifiers.rb', line 149

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

#with_identifier_cached_wildcardedArel::Nodes::Equality

Returns:

  • (Arel::Nodes::Equality)


154
155
156
# File 'lib/queries/concerns/identifiers.rb', line 154

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

#with_identifier_identifierArel::Nodes::Equality

Returns:

  • (Arel::Nodes::Equality)


135
136
137
# File 'lib/queries/concerns/identifiers.rb', line 135

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

#with_identifier_wildcard_endObject



144
145
146
# File 'lib/queries/concerns/identifiers.rb', line 144

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