Class: Queries::Person::Filter

Inherits:
Query
  • Object
show all
Includes:
Concerns::Tags, Concerns::Users
Defined in:
lib/queries/person/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.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/queries/person/filter.rb', line 53

def initialize(params)
  @role = [params[:role]].flatten.compact

  @first_name = params[:first_name]
  @last_name = params[:last_name]

  @name = params[:name]

  @last_name_starts_with = params[:last_name_starts_with]

  @person_wildcard = [params[:person_wildcard]].flatten.compact

  @used_in_project_id = params[:used_in_project_id]

  @levenshtein_cuttoff = params[:levenshtein_cuttoff]

  @born_after_year = params[:born_after_year]
  @born_before_year = params[:born_before_year]

  @active_after_year = params[:active_after_year]
  @active_before_year = params[:active_before_year]

  @died_after_year = params[:died_after_year]
  @died_before_year = params[:died_before_year]

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

Instance Attribute Details

#active_after_yearObject

Returns the value of attribute active_after_year.



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

def active_after_year
  @active_after_year
end

#active_before_yearObject

Returns the value of attribute active_before_year.



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

def active_before_year
  @active_before_year
end

#born_after_yearObject

Returns the value of attribute born_after_year.



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

def born_after_year
  @born_after_year
end

#born_before_yearObject

Returns the value of attribute born_before_year.



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

def born_before_year
  @born_before_year
end

#died_after_yearObject

Returns the value of attribute died_after_year.



42
43
44
# File 'lib/queries/person/filter.rb', line 42

def died_after_year
  @died_after_year
end

#died_before_yearObject

Returns the value of attribute died_before_year.



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

def died_before_year
  @died_before_year
end

#first_nameString?

Returns also matches any AlternateValue.

Returns:

  • (String, nil)

    also matches any AlternateValue



10
11
12
# File 'lib/queries/person/filter.rb', line 10

def first_name
  @first_name
end

#last_nameString?

Returns also matches any AlternateValue.

Returns:

  • (String, nil)

    also matches any AlternateValue



14
15
16
# File 'lib/queries/person/filter.rb', line 14

def last_name
  @last_name
end

#last_name_starts_withObject

Returns the value of attribute last_name_starts_with.



45
46
47
# File 'lib/queries/person/filter.rb', line 45

def last_name_starts_with
  @last_name_starts_with
end

#levenshtein_cuttoffInteger?

Returns matches cached, records less than this edit distance are returned !! requires `name` or is ignored !! if provided then last/first_name are ignored.

Returns:

  • (Integer, nil)

    matches cached, records less than this edit distance are returned !! requires `name` or is ignored !! if provided then last/first_name are ignored



29
30
31
# File 'lib/queries/person/filter.rb', line 29

def levenshtein_cuttoff
  @levenshtein_cuttoff
end

#nameString?

Returns where against `cached`.

Returns:

  • (String, nil)

    where against `cached`



23
24
25
# File 'lib/queries/person/filter.rb', line 23

def name
  @name
end

#person_wildcardArray

Returns values are attributes that should be wildcarded:

last_name, first_name, name.

Returns:

  • (Array)

    values are attributes that should be wildcarded:

    last_name, first_name, name
    


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

def person_wildcard
  @person_wildcard
end

#roleArray

Parameters:

  • role (Array, String)

    A valid role name like 'Author' or array like ['TaxonDeterminer', 'SourceEditor']. See Role descendants.

Returns:

  • (Array)


19
20
21
# File 'lib/queries/person/filter.rb', line 19

def role
  @role
end

#used_in_project_idArray

Returns only return people with roles in this project, named to clarify this has slightly different meaning for shared people than straight project_id.

Returns:

  • (Array)

    only return people with roles in this project, named to clarify this has slightly different meaning for shared people than straight project_id



50
51
52
# File 'lib/queries/person/filter.rb', line 50

def used_in_project_id
  @used_in_project_id
end

Instance Method Details

#active_after_year_facetObject



121
122
123
124
125
# File 'lib/queries/person/filter.rb', line 121

def active_after_year_facet
  return nil if active_after_year.nil?
  table[:year_active_start].gt(active_after_year)
    .or(table[:year_active_end].gt(active_after_year))
end

#active_before_year_facetObject



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

def active_before_year_facet
  return nil if active_before_year.nil?
  table[:year_active_start].lt(active_before_year)
    .or(table[:year_active_end].lt(active_before_year))
end

#allActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/queries/person/filter.rb', line 248

def all
  a = and_clauses
  b = merge_clauses

  if a && b
    b.where(a).distinct
  elsif a
    ::Person.where(a).distinct
  elsif b
    b.distinct
  else
    ::Person.all
  end
end

#and_clausesActiveRecord::Relation?

Returns:

  • (ActiveRecord::Relation, nil)


194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/queries/person/filter.rb', line 194

def and_clauses
  clauses = [
    name_facet,
    born_after_year_facet,
    born_before_year_facet,

    died_after_year_facet,
    died_before_year_facet,

    active_after_year_facet,
    active_before_year_facet,

    last_name_starts_with_facet,
  ].compact

  return nil if clauses.empty?

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

#base_queryObject



93
94
95
# File 'lib/queries/person/filter.rb', line 93

def base_query
  ::Person.select('people.*')
end

#born_after_year_facetObject



101
102
103
104
# File 'lib/queries/person/filter.rb', line 101

def born_after_year_facet
  return nil if born_after_year.nil?
  table[:year_born].gt(born_after_year)
end

#born_before_year_facetObject



106
107
108
109
# File 'lib/queries/person/filter.rb', line 106

def born_before_year_facet
  return nil if born_before_year.nil?
  table[:year_born].lt(born_before_year)
end

#died_after_year_facetObject



111
112
113
114
# File 'lib/queries/person/filter.rb', line 111

def died_after_year_facet
  return nil if died_after_year.nil?
  table[:year_died].gt(died_after_year)
end

#died_before_year_facetObject



116
117
118
119
# File 'lib/queries/person/filter.rb', line 116

def died_before_year_facet
  return nil if died_before_year.nil?
  table[:year_died].lt(died_before_year)
end

#last_name_starts_with_facetObject



164
165
166
167
# File 'lib/queries/person/filter.rb', line 164

def last_name_starts_with_facet
  return nil if last_name_starts_with.blank? || !levenshtein_cuttoff.blank?
  table[:last_name].matches(last_name_starts_with + '%')
end

#levenshtein_facetObject



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

def levenshtein_facet
  return nil unless levenshtein_cuttoff && (!name.blank?)
  ::Person.where(
    levenshtein_distance(:cached, name).lt(levenshtein_cuttoff).to_sql
  )
end

#merge_clausesObject



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/queries/person/filter.rb', line 218

def merge_clauses
  clauses = [
    name_part_facet(:last_name),
    name_part_facet(:first_name),

    role_facet,
    levenshtein_facet,

    identifier_between_facet,
    identifier_facet,
    identifier_namespace_facet,

    keyword_id_facet,
    tag_facet,

    used_in_project_id_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

#name_facetObject



155
156
157
158
159
160
161
162
# File 'lib/queries/person/filter.rb', line 155

def name_facet
  return nil if name.nil? || !levenshtein_cuttoff.blank?
  if person_wildcard.include?('name')
    table[:cached].matches('%' + name + '%')
  else
    table[:cached].eq(name)
  end
end

#name_part_facet(part = :last_name) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/queries/person/filter.rb', line 133

def name_part_facet(part = :last_name)
  v = send(part)

  return nil if v.nil?

  q = ::Person.left_outer_joins(:alternate_values)
  a = ::AlternateValue.arel_table

  w1, w2 = nil, nil

  if person_wildcard.include?(part.to_s)
    w = '%' + v + '%'
    w1 = table[part].matches(w)
    w2 = a[:value].matches(w)
  else
    w1 = table[part].eq(v)
    w2 = a[:value].eq(v)
  end

  q.where( w1.or(w2).to_sql )
end

#role_facetObject



169
170
171
172
# File 'lib/queries/person/filter.rb', line 169

def role_facet
  return nil if role.empty?
  ::Person.joins(:roles).where( role_table[:type].eq_any(role) )
end

#role_tableArel::Table

Returns:

  • (Arel::Table)


89
90
91
# File 'lib/queries/person/filter.rb', line 89

def role_table
  ::Role.arel_table
end

#tableArel::Table

Returns:

  • (Arel::Table)


84
85
86
# File 'lib/queries/person/filter.rb', line 84

def table
  ::Person.arel_table
end

#used_in_project_id_facetObject



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/queries/person/filter.rb', line 181

def used_in_project_id_facet
  return nil unless !used_in_project_id.empty?

  w1 = role_table[:project_id].eq_any(used_in_project_id)
  w2 = ::ProjectSource.arel_table[:project_id].eq_any(used_in_project_id)

  a = ::Person.joins(:roles).where(w1.to_sql)
  b = ::Person.joins(sources: [:project_sources]).where( w2.to_sql)

  ::Person.from("((#{a.to_sql}) UNION (#{b.to_sql})) as people")
end