Class: Queries::CollectingEvent::Filter

Inherits:
Object
  • Object
show all
Includes:
Queries::Concerns::DateRanges
Defined in:
lib/queries/collecting_event/filter.rb

Overview

!! does not inherit from base query

Constant Summary

ATTRIBUTES =
(::CollectingEvent.column_names - %w{project_id created_by_id updated_by_id created_at updated_at})

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Filter) initialize(params)

Returns a new instance of Filter



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/queries/collecting_event/filter.rb', line 36

def initialize(params)
  @in_labels = params[:in_labels]
  @in_verbatim_locality = params[:in_verbatim_locality]
  @recent = params[:recent].blank? ? nil : params[:recent].to_i
  self.shape = params[:shape]

  @keyword_ids = params[:keyword_ids].blank? ? [] : params[:keyword_ids]
  @spatial_geographic_area_ids = params[:spatial_geographic_area_ids].blank? ? [] : params[:spatial_geographic_area_ids]

  set_attributes(params)
  set_dates(params)
end

Instance Attribute Details

- (Object) in_labels

Wildcard wrapped matching any label



16
17
18
# File 'lib/queries/collecting_event/filter.rb', line 16

def in_labels
  @in_labels
end

- (Object) in_verbatim_locality

Wildcard wrapped matching verbatim_locality



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

def in_verbatim_locality
  @in_verbatim_locality
end

- (Object) keyword_ids

Returns the value of attribute keyword_ids



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

def keyword_ids
  @keyword_ids
end

- (Object) recent

TODO: factor to include An integer, order result and return the last :recent records



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

def recent
  @recent
end

- (Object) shape

An RGeo::GeoJSON feature



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

def shape
  @shape
end

- (Object) spatial_geographic_area_ids

Reference geographic areas to do a spatial query



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

def spatial_geographic_area_ids
  @spatial_geographic_area_ids
end

Instance Method Details

- (ActiveRecord::Relation) all

Returns:

  • (ActiveRecord::Relation)


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/queries/collecting_event/filter.rb', line 185

def all
  a = and_clauses
  b = merge_clauses

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

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

- (ActiveRecord::Relation) and_clauses

Returns:

  • (ActiveRecord::Relation)


147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/queries/collecting_event/filter.rb', line 147

def and_clauses
  clauses = []
  clauses += attribute_clauses
 
  clauses += [
    between_date_range,
    matching_any_label,
    matching_verbatim_locality,
  ].compact
  
  return nil if clauses.empty?

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

- (Object) attribute_clauses



69
70
71
72
73
74
75
76
77
# File 'lib/queries/collecting_event/filter.rb', line 69

def attribute_clauses
  c = []
  ATTRIBUTES.each do |a|
    if v = send(a)
      c.push table[a.to_sym].eq(v) if !v.blank?
    end
  end
  c
end

- (Object) matching_any_label



134
135
136
137
138
# File 'lib/queries/collecting_event/filter.rb', line 134

def matching_any_label
  return nil if in_labels.blank?
  t = "%#{in_labels}%"
  table[:verbatim_label].matches(t).or(table[:print_label].matches(t)).or(table[:document_label].matches(t))
end

- (Object) matching_keyword_ids



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/queries/collecting_event/filter.rb', line 79

def matching_keyword_ids
  return nil if keyword_ids.empty?
  o = table
  t = ::Tag.arel_table

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

  c = t.alias('t1')

  b = b.join(c, Arel::Nodes::OuterJoin)
    .on(
      a[:id].eq(c[:tag_object_id])
    .and(c[:tag_object_type].eq(table.name.classify))
  )

  e = c[:keyword_id].not_eq(nil)
  f = c[:keyword_id].eq_any(keyword_ids)

  b = b.where(e.and(f))
  b = b.group(a['id'])
  b = b.as('tz5_')

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

- (Object) matching_shape



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/queries/collecting_event/filter.rb', line 105

def matching_shape
  return nil if shape.nil?

  geometry = shape.geometry
  this_type = geometry.geometry_type.to_s.downcase
  geometry = geometry.as_text
  radius = shape['radius'] || 100

  case this_type
  when 'point'
    ::CollectingEvent
      .joins(:geographic_items)
      .where(::GeographicItem.within_radius_of_wkt_sql(geometry, radius ))
  when 'polygon'
    ::CollectingEvent
      .joins(:geographic_items)
      .where(::GeographicItem.contained_by_wkt_sql(geometry))
  else
    nil
  end
end

- (Object) matching_spatial_via_geographic_area_ids

TODO: throttle by size?



128
129
130
131
132
# File 'lib/queries/collecting_event/filter.rb', line 128

def matching_spatial_via_geographic_area_ids
  return nil if spatial_geographic_area_ids.empty? 
  a = ::GeographicItem.default_by_geographic_area_ids(spatial_geographic_area_ids).ids 
  ::CollectingEvent.joins(:geographic_items).where( ::GeographicItem.contained_by_where_sql( a ) )
end

- (Object) matching_verbatim_locality



140
141
142
143
144
# File 'lib/queries/collecting_event/filter.rb', line 140

def matching_verbatim_locality
  return nil if in_verbatim_locality.blank?
  t = "%#{in_verbatim_locality}%"
  table[:verbatim_locality].matches(t)
end

- (Object) merge_clauses



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/queries/collecting_event/filter.rb', line 166

def merge_clauses
  clauses = [
    matching_keyword_ids,
    matching_shape,
    matching_spatial_via_geographic_area_ids

    # matching_verbatim_author
  ].compact

  return nil if clauses.empty?

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

- (Object) set_attributes(params)



54
55
56
57
58
# File 'lib/queries/collecting_event/filter.rb', line 54

def set_attributes(params)
  ATTRIBUTES.each do |a|
    send("#{a}=", params[a.to_sym]) 
  end
end

- (Arel::Table) table

Returns:

  • (Arel::Table)


61
62
63
# File 'lib/queries/collecting_event/filter.rb', line 61

def table
  ::CollectingEvent.arel_table
end

- (Object) tag_table



65
66
67
# File 'lib/queries/collecting_event/filter.rb', line 65

def tag_table
  ::Tag.arel_table
end