Class: Queries::Source::Filter

Inherits:
Query
  • Object
show all
Includes:
Concerns::Tags, Concerns::Users
Defined in:
lib/queries/source/filter.rb

Instance Attribute Summary collapse

Attributes inherited from Query

#dynamic_limit, #options, #project_id, #query_string, #terms

Instance Method Summary collapse

Methods inherited from Query

#alphabetic_strings, #attribute_exact_facet, #autocomplete, #autocomplete_cached, #autocomplete_cached_wildcard_anywhere, #autocomplete_common_name_exact, #autocomplete_common_name_like, #autocomplete_exact_id, #autocomplete_exactly_named, #autocomplete_named, #autocomplete_ordered_wildcard_pieces_in_cached, #build_terms, #cached, #combine_or_clauses, #common_name_name, #common_name_table, #common_name_wild_pieces, #end_wildcard, #exactly_named, #fragments, #integers, #levenshtein_distance, #match_ordered_wildcard_pieces_in_cached, #match_wildcard_end_in_cached, #match_wildcard_in_cached, #named, #no_terms?, #only_ids, #only_integers?, #parent, #parent_child_join, #parent_child_where, #pieces, #result, #scope, #start_and_end_wildcard, #start_wildcard, #wildcard_pieces, #wildcard_wrapped_integers, #wildcard_wrapped_years, #with_cached, #with_cached_like, #with_id, #with_project_id, #year_letter, #years

Constructor Details

#initialize(params) ⇒ Filter

Returns a new instance of Filter.

Parameters:

  • params (Hash)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/queries/source/filter.rb', line 93

def initialize(params)
  @query_string = params[:query_term]
  
  @author = params[:author]
  @author_ids = params[:author_ids] || []

  @author_ids_or = (params[:author_ids_or]&.downcase == 'true' ? true : false) if !params[:author_ids_or].nil?

  @ids = params[:ids] || []
  @topic_ids = params[:topic_ids] || []
  @serial_ids = params[:serial_ids] || []
  @citation_object_type = params[:citation_object_type] || []
  @citations = (params[:citations]&.downcase == 'true' ? true : false) if !params[:citations].nil?
  @documents = (params[:documents]&.downcase == 'true' ? true : false) if !params[:documents].nil?
  @exact_author = (params[:exact_author]&.downcase == 'true' ? true : false) if !params[:exact_author].nil?
  @exact_title = (params[:exact_title]&.downcase == 'true' ? true : false) if !params[:exact_title].nil?
  @in_project = (params[:in_project]&.downcase == 'true' ? true : false) if !params[:in_project].nil?
  @nomenclature = (params[:nomenclature]&.downcase == 'true' ? true : false) if !params[:nomenclature].nil?
  @notes = (params[:notes]&.downcase == 'true' ? true : false) if !params[:notes].nil?
  @project_id = params[:project_id] # TODO: also in Queries::Query
  @roles = (params[:roles]&.downcase == 'true' ? true : false) if !params[:roles].nil?
  @source_type = params[:source_type]
  @title = params[:title]
  @with_doi = (params[:with_doi]&.downcase == 'true' ? true : false) if !params[:with_doi].nil?
  @year_end = params[:year_end]
  @year_start = params[:year_start]
  @recent = (params[:recent]&.downcase == 'true' ? true : false) if !params[:recent].nil?

  build_terms
  set_identifier(params)
  set_tags_params(params)
  set_user_dates(params)
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



18
19
20
# File 'lib/queries/source/filter.rb', line 18

def author
  @author
end

#author_idsObject

Returns the value of attribute author_ids.



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

def author_ids
  @author_ids
end

#author_ids_orObject

Returns the value of attribute author_ids_or.



31
32
33
# File 'lib/queries/source/filter.rb', line 31

def author_ids_or
  @author_ids_or
end

#citation_object_typeArray?

TODO: move tc citations concern

Returns:

  • (Array, nil)


76
77
78
# File 'lib/queries/source/filter.rb', line 76

def citation_object_type
  @citation_object_type
end

#citationsBoolean?

Returns:

  • (Boolean, nil)


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

def citations
  @citations
end

#documentsBoolean?

Returns:

  • (Boolean, nil)


63
64
65
# File 'lib/queries/source/filter.rb', line 63

def documents
  @documents
end

#exact_authorBoolean?

Returns:

  • (Boolean, nil)


25
26
27
# File 'lib/queries/source/filter.rb', line 25

def exact_author
  @exact_author
end

#exact_titleBoolean?

Returns:

  • (Boolean, nil)


47
48
49
# File 'lib/queries/source/filter.rb', line 47

def exact_title
  @exact_title
end

#idsObject

Returns the value of attribute ids.



21
22
23
# File 'lib/queries/source/filter.rb', line 21

def ids
  @ids
end

#in_projectBoolean?

! requires `project_id`

Returns:

  • (Boolean, nil)


15
16
17
# File 'lib/queries/source/filter.rb', line 15

def in_project
  @in_project
end

#nomenclatureBoolean?

Returns:

  • (Boolean, nil)


67
68
69
# File 'lib/queries/source/filter.rb', line 67

def nomenclature
  @nomenclature
end

#notesBoolean?

Returns:

  • (Boolean, nil)


83
84
85
# File 'lib/queries/source/filter.rb', line 83

def notes
  @notes
end

#recentBoolean?

Returns:

  • (Boolean, nil)


55
56
57
# File 'lib/queries/source/filter.rb', line 55

def recent
  @recent
end

#rolesBoolean?

Returns:

  • (Boolean, nil)


59
60
61
# File 'lib/queries/source/filter.rb', line 59

def roles
  @roles
end

#serial_idsObject

Returns the value of attribute serial_ids.



90
91
92
# File 'lib/queries/source/filter.rb', line 90

def serial_ids
  @serial_ids
end

#source_typeString?

Returns:

  • (String, nil)


87
88
89
# File 'lib/queries/source/filter.rb', line 87

def source_type
  @source_type
end

#titleObject

Returns the value of attribute title.



43
44
45
# File 'lib/queries/source/filter.rb', line 43

def title
  @title
end

#topic_idsObject

Returns the value of attribute topic_ids.



34
35
36
# File 'lib/queries/source/filter.rb', line 34

def topic_ids
  @topic_ids
end

#with_doiBoolean?

Returns:

  • (Boolean, nil)


71
72
73
# File 'lib/queries/source/filter.rb', line 71

def with_doi
  @with_doi
end

#year_endObject

Returns the value of attribute year_end.



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

def year_end
  @year_end
end

#year_startObject

Returns the value of attribute year_start.



37
38
39
# File 'lib/queries/source/filter.rb', line 37

def year_start
  @year_start
end

Instance Method Details

#allActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/queries/source/filter.rb', line 361

def all
  a = and_clauses
  b = merge_clauses

  q = nil
  if a && b
    q = b.where(a)
  elsif a
    q = ::Source.where(a)
  elsif b
    q = b
  else
    q = ::Source.all
  end

  q = q.order(updated_at: :desc) if recent
  q
end

#and_clausesActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/queries/source/filter.rb', line 338

def and_clauses
  clauses = []

  clauses += [
    cached,
    source_ids_facet,
    serial_ids_facet,
    attribute_exact_facet(:author),
    attribute_exact_facet(:title),
    source_type_facet,
    year_facet
  ].compact

  return nil if clauses.empty?

  a = clauses.shift
  clauses.each do |b|
    a = a.and(b)
  end
  a
end

#author_ids_facetObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/queries/source/filter.rb', line 176

def author_ids_facet
  return nil if author_ids.empty?
  o = table
  r = ::Role.arel_table

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

  c = r.alias('r1')

  b = b.join(c, Arel::Nodes::OuterJoin)
    .on(
      a[:id].eq(c[:role_object_id])
    .and(c[:role_object_type].eq('Source'))
    .and(c[:type].eq('SourceAuthor'))
  )

  e = c[:id].not_eq(nil)
  f = c[:person_id].eq_any(author_ids)

  b = b.where(e.and(f))
  b = b.group(a['id'])
  b = b.having(a['id'].count.eq(author_ids.length)) unless author_ids_or
  b = b.as('z1_')

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

#base_queryObject



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

def base_query
  ::Source.select('sources.*')
end

#citation_facetObject

TODO: move to a concern



222
223
224
225
226
227
228
229
230
231
232
# File 'lib/queries/source/filter.rb', line 222

def citation_facet
  return nil if citations.nil?

  if citations
    ::Source.joins(:citations).distinct
  else
    ::Source.left_outer_joins(:citations)
      .where(citations: {id: nil})
      .distinct
  end
end

#citation_object_type_facetObject

TODO: move to citation concern



288
289
290
291
292
# File 'lib/queries/source/filter.rb', line 288

def citation_object_type_facet
  return nil if citation_object_type.empty?
  ::Source.joins(:citations)
    .where(citations: {citation_object_type: citation_object_type}).distinct
end

#document_facetObject

TODO: move to a concern



275
276
277
278
279
280
281
282
283
284
285
# File 'lib/queries/source/filter.rb', line 275

def document_facet
  return nil if documents.nil?

  if documents
    ::Source.joins(:documents).distinct
  else
    ::Source.left_outer_joins(:documents)
      .where(documents: {id: nil})
      .distinct
  end
end

#fragment_year_matchesActiveRecord::Relation?

Returns if user provides 5 or fewer strings and any number of years look for any string && year.

Returns:

  • (ActiveRecord::Relation, nil)

    if user provides 5 or fewer strings and any number of years look for any string && year



143
144
145
146
147
148
149
150
151
# File 'lib/queries/source/filter.rb', line 143

def fragment_year_matches
  if fragments.any?
    s = table[:cached].matches_any(fragments)
    s = s.and(table[:year].eq_any(years)) if !years.empty?
    s
  else
    nil
  end
end

#in_project_facetObject



208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/queries/source/filter.rb', line 208

def in_project_facet
  return nil if project_id.nil? || in_project.nil?

  if in_project
    ::Source.joins(:project_sources)
      .where(project_sources: {project_id: project_id})
  else
    ::Source.left_outer_joins(:project_sources)
      .where("project_sources.project_id != ? OR project_sources.id IS NULL", Current.project_id)
      .distinct
  end
end

#merge_clausesObject



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
# File 'lib/queries/source/filter.rb', line 308

def merge_clauses
  clauses = [
    author_ids_facet,
    topic_ids_facet,
    citation_facet,
    citation_object_type_facet,
    document_facet,
    in_project_facet,
    nomenclature_facet,
    role_facet,
    with_doi_facet,
    matching_keyword_ids,
    tag_facet,
    note_facet,
    identifier_between_facet,
    identifier_facet,
    identifier_namespace_facet,
    created_updated_facet, # See Queries::Concerns::Users
  ].compact

  return nil if clauses.empty?

  a = clauses.shift
  clauses.each do |b|
    a = a.merge(b)
  end
  a
end

#nomenclature_facetObject



294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/queries/source/filter.rb', line 294

def nomenclature_facet
  return nil if nomenclature.nil?

  if nomenclature 
    ::Source.joins(:citations)
      .where(citations: {citation_object_type: ['TaxonName', 'TaxonNameRelationship', 'TaxonNameClassification', 'TypeMaterial']})
      .distinct
  else
    ::Source.left_outer_joins(:citations)
      .where("(citations.citation_object_type NOT IN ('TaxonName','TaxonNameRelationship','TaxonNameClassification','TypeMaterial')) OR (citations.id is null)")
      .distinct
  end
end

#note_facetObject

TODO: move to a concern



263
264
265
266
267
268
269
270
271
272
# File 'lib/queries/source/filter.rb', line 263

def note_facet
  return nil if notes.nil?

  if notes 
    ::Source.joins(:notes).distinct
  else
    ::Source.left_outer_joins(:notes)
      .where(notes: {id: nil})
  end
end

#project_sources_tableArel::Table

Returns:

  • (Arel::Table)


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

def project_sources_table
  ::ProjectSource.arel_table
end

#role_facetObject

TODO: move to a concern



251
252
253
254
255
256
257
258
259
260
# File 'lib/queries/source/filter.rb', line 251

def role_facet
  return nil if roles.nil?

  if roles
    ::Source.joins(:roles).distinct
  else
    ::Source.left_outer_joins(:roles)
      .where(roles: {id: nil})
  end
end

#serial_ids_facetObject



172
173
174
# File 'lib/queries/source/filter.rb', line 172

def serial_ids_facet
  serial_ids.empty? ? nil : table[:serial_id].eq_any(serial_ids)
end

#source_ids_facetObject



168
169
170
# File 'lib/queries/source/filter.rb', line 168

def source_ids_facet
  ids.empty? ? nil : table[:id].eq_any(ids)
end

#source_type_facetObject



153
154
155
156
# File 'lib/queries/source/filter.rb', line 153

def source_type_facet
  return nil if source_type.blank?
  table[:type].eq(source_type)
end

#tableArel::Table

Returns:

  • (Arel::Table)


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

def table
  ::Source.arel_table
end

#topic_ids_facetObject



204
205
206
# File 'lib/queries/source/filter.rb', line 204

def topic_ids_facet
  ::Source.joins(:citation_topics).where(citation_topics: { topic_id: topic_ids }).distinct unless topic_ids.empty?
end

#with_doi_facetObject

TODO: move to generalized code in identifiers concern



235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/queries/source/filter.rb', line 235

def with_doi_facet
  return nil if with_doi.nil?

  # See lib/queries/concerns/identifiers.rb
  @identifier_type.push 'Identifier::Global::Doi'
  @identifier_type.uniq!

  if with_doi
    identifier_type_facet
  else
    ::Source.left_outer_joins(:identifiers)
      .where("(identifiers.type != 'Identifier::Global::Doi') OR (identifiers.identifier_object_id is null)")
  end
end

#year_facetObject



158
159
160
161
162
163
164
165
166
# File 'lib/queries/source/filter.rb', line 158

def year_facet
  return nil if year_start.blank?
  if year_start && !year_end.blank?
    table[:year].gteq(year_start)
      .and(table[:year].lteq(year_end))
  else # only start
    table[:year].eq(year_start)
  end
end