Class: Georeference::Filter

Inherits:
Queries::Query show all
Defined in:
lib/queries/georeference/filter.rb

Overview

TODO: Unify all and filter

Instance Attribute Summary collapse

Attributes inherited from Queries::Query

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

Instance Method Summary collapse

Methods inherited from Queries::Query

#alphabetic_strings, #autocomplete, #autocomplete_cached, #autocomplete_cached_wildcard_anywhere, #autocomplete_exact_id, #autocomplete_ordered_wildcard_pieces_in_cached, #autocomplete_start_date, #build_terms, #cached, #combine_or_clauses, #end_wildcard, #exactly_named, #fragments, #integers, #match_ordered_wildcard_pieces_in_cached, #match_wildcard_end_in_cached, #match_wildcard_in_cached, #named, #no_digits, #only_ids, #only_integers?, #parent, #parent_child_join, #parent_child_where, #pieces, #result, #scope, #simple_date, #start_and_end_wildcard, #start_wildcard, #wildcard_pieces, #wildcard_wrapped_integers, #wildcard_wrapped_years, #with_cached, #with_cached_like, #with_id, #with_project_id, #with_start_date, #year_letter, #years

Constructor Details

#initialize(params) ⇒ Filter

Returns a new instance of Filter

Parameters:

  • params (Hash)


10
11
12
13
14
15
# File 'lib/queries/georeference/filter.rb', line 10

def initialize(params)
  params.reject! { |_k, v| v.blank? }

  @collecting_event_id = params[:collecting_event_id]
  @collecting_event_ids = [params[:collecting_event_ids]].flatten
end

Instance Attribute Details

#collecting_event_idObject

Query variables



7
8
9
# File 'lib/queries/georeference/filter.rb', line 7

def collecting_event_id
  @collecting_event_id
end

#collecting_event_idsObject

Query variables



7
8
9
# File 'lib/queries/georeference/filter.rb', line 7

def collecting_event_ids
  @collecting_event_ids
end

Instance Method Details

#allActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/queries/georeference/filter.rb', line 73

def all
  a = and_clauses
  b = merge_clauses
  if a && b
    b.where(a).distinct
  elsif a
    ::Georeference.where(a).distinct
  elsif b
    b.distinct
  else
    ::Georeference.all
  end
end

#and_clausesActiveRecord::Relation?

Returns:

  • (ActiveRecord::Relation, nil)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/queries/georeference/filter.rb', line 42

def and_clauses
  clauses = [
    matching_collecting_event_id,
    matching_collecting_event_ids,
  # Queries::Annotator.annotator_params(options, ::Citation),
  ].compact

  return nil if clauses.empty?

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

#collecting_event_tableObject



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

def collecting_event_table 
  ::CollectingEvent.arel_table
end

#ids_for_collecting_eventsArray

Returns of otu_id

Returns:

  • (Array)

    of otu_id



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

def ids_for_collecting_events
  ([collecting_event_id] + collecting_event_ids).compact.uniq
end

#matching_collecting_event_idObject



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

def matching_collecting_event_id
  a = collecting_event_id
  a.nil? ? nil : table[:collecting_event_id].eq(a)
end

#matching_collecting_event_idsObject



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

def matching_collecting_event_ids
  a = ids_for_collecting_events
  a.empty? ? nil : table[:collecting_event_id].eq_any(a)
end

#merge_clausesObject



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/queries/georeference/filter.rb', line 58

def merge_clauses
  clauses = [
  # matching_verbatim_author
  ].compact

  return nil if clauses.empty?

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

#tableObject



17
18
19
# File 'lib/queries/georeference/filter.rb', line 17

def table
  ::Georeference.arel_table
end