Class: Queries::CollectingEventLatLongExtractorQuery

Inherits:
Object
  • Object
show all
Includes:
Arel::Nodes
Defined in:
lib/queries/collecting_event_lat_long_extractor_query.rb

Overview

# TODO: perhaps a utility class of queries

For example:

start_id.gt(collecting_event_id)

becomes

start_id.gt(Arel::Nodes::Quoted.new(collecting_event_id))

Arel performing automatic type casting is deprecated, and will be removed in Arel 8.0. If you are seeing this, it is because you are manually passing a value to an Arel predicate, and the ‘Arel::Table` object was constructed manually. The easiest way to remove this warning is to use an `Arel::Table` object returned from calling `arel_table` on an ApplicationRecord subclass.

If you’re certain the value is already of the right type, change ‘attribute.eq(value)` to `attribute.eq (Arel::Nodes::Quoted.new(value))` (you will be able to remove that in Arel 8.0, it is only required to silence this deprecation warning).

You can also silence this warning globally by setting ‘$arel_silence_type_casting_deprecation` to `true`. (Do NOT do this if you are a library author)

If you are passing user input to a predicate, you must either give an appropriate type caster object to the ‘Arel::Table`, or manually cast the value before passing it to Arel. DEPRECATION WARNING: Passing a column to `quote` has been deprecated. It is only used for type casting, which should be handled elsewhere. See github.com/rails/arel/commit/6160bfbda1d1781c3b08a33ec4955f170e95be11 for more information. (called from where_sql at /Users/tuckerjd/src/taxonworks/lib/queries/collecting_event_lat_long_extractor_query.rb:41)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collecting_event_id: nil, project_id: nil, filters: []) ⇒ CollectingEventLatLongExtractorQuery

Returns a new instance of CollectingEventLatLongExtractorQuery.

Parameters:

  • collecting_event_id (Integer) (defaults to: nil)
  • project_id (Integer) (defaults to: nil)
  • Project_id (Integer)


42
43
44
45
46
47
48
49
# File 'lib/queries/collecting_event_lat_long_extractor_query.rb', line 42

def initialize(collecting_event_id: nil, project_id: nil, filters: [])

  collecting_event_id = 0 if collecting_event_id.nil?

  @collecting_event_id = collecting_event_id
  @filters = filters
  @project_id = project_id
end

Instance Attribute Details

#collecting_event_idObject

Returns the value of attribute collecting_event_id.



35
36
37
# File 'lib/queries/collecting_event_lat_long_extractor_query.rb', line 35

def collecting_event_id
  @collecting_event_id
end

#filtersObject

Returns the value of attribute filters.



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

def filters
  @filters
end

#project_idObject

Returns the value of attribute project_id.



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

def project_id
  @project_id
end

Instance Method Details

#allScope

Returns:

  • (Scope)


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

def all
  ::CollectingEvent.where(where_sql)
end

#filter_scopesString

Returns of all of the regexs available at this time.

Returns:

  • (String)

    of all of the regexs available at this time



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/queries/collecting_event_lat_long_extractor_query.rb', line 52

def filter_scopes
  if filters.blank?
    filter_keys = Utilities::Geo::REGEXP_COORD.keys.compact
  else
    filter_keys = filters
  end

  all_filters = filter_keys.collect do |kee|
    # attach 'verbatim_label ~ ' to each regex
    regex_function(kee)
  end.join(' OR ')
  # remove the names from the named groups: these don't work for sql regexs
  q1 = "(#{all_filters.gsub('?<lat>', '').gsub('?<long>', '')})"
  q2 = ActiveRecord::Base.send(:sanitize_sql_array, ['(?)', all_filters.gsub('?<lat>', '').gsub('?<long>', '')])
  Arel.sql(q1)
end

#regex_function(filter) ⇒ Scope

Parameters:

  • filter (String)

    key to FILTERS regex string

Returns:

  • (Scope)


107
108
109
110
# File 'lib/queries/collecting_event_lat_long_extractor_query.rb', line 107

def regex_function(filter)
  regex = Utilities::Geo::REGEXP_COORD[filter][:reg].to_s.gsub('(?i-mx:', '').chomp(')')
  "verbatim_label ~* '" + regex + "'"
end

#starting_afterArel::Attribute

Returns:

  • (Arel::Attribute)


100
101
102
103
# File 'lib/queries/collecting_event_lat_long_extractor_query.rb', line 100

def starting_after
  start_id = Arel::Attribute.new(Arel::Table.new(:collecting_events), :id)
  start_id.gt(Arel::Nodes::Quoted.new(collecting_event_id))
end

#tableArel::Table

Returns:

  • (Arel::Table)


78
79
80
# File 'lib/queries/collecting_event_lat_long_extractor_query.rb', line 78

def table
  ::CollectingEvent.arel_table
end

#verbatim_label_not_emptyArel::Nodes::NamedFunction

Returns:

  • (Arel::Nodes::NamedFunction)


88
89
90
91
92
# File 'lib/queries/collecting_event_lat_long_extractor_query.rb', line 88

def verbatim_label_not_empty
  vl = Arel::Attribute.new(Arel::Table.new(:collecting_events), :verbatim_label)
  Arel::Nodes::NamedFunction.new('length', [vl]).gt(0)
  # Arel::Nodes::NamedFunction.new('length', [vl]).gt(Arel::Nodes::Quoted.new(0))
end

#verbatim_lat_long_emptyString

Returns:

  • (String)


95
96
97
# File 'lib/queries/collecting_event_lat_long_extractor_query.rb', line 95

def verbatim_lat_long_empty
  Arel.sql('(verbatim_latitude is null or verbatim_longitude is null)')
end

#where_sqlString

Returns:

  • (String)


70
71
72
73
74
75
# File 'lib/queries/collecting_event_lat_long_extractor_query.rb', line 70

def where_sql
  # TODO: make sure you select the one of the following lines which suits your purpose: with or without
  # Verbatim_lat/long present (default: Verbatim_lat/long is empty)
  (verbatim_label_not_empty).and(verbatim_lat_long_empty).and(starting_after).and(filter_scopes).to_sql
  # (verbatim_label_not_empty).and(starting_after).and(filter_scopes).to_sql
end