Class: Queries::DwcOccurrence::Filter

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

Overview

Keep this minimal, in pricinple filtering should be done on the base objects, not the core here.

Instance Attribute Summary collapse

Attributes inherited from Query

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

Instance Method Summary collapse

Methods included from Helpers

#boolean_param

Methods inherited from Query

#alphabetic_strings, #alphanumeric_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, #string_fragments, #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.



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

def initialize(params)
  set_user_dates(params)
end

Instance Attribute Details

#dwc_occurrence_object_idObject

Returns the value of attribute dwc_occurrence_object_id.



11
12
13
# File 'lib/queries/dwc_occurrence/filter.rb', line 11

def dwc_occurrence_object_id
  @dwc_occurrence_object_id
end

#dwc_occurrence_object_typeObject

Returns the value of attribute dwc_occurrence_object_type.



12
13
14
# File 'lib/queries/dwc_occurrence/filter.rb', line 12

def dwc_occurrence_object_type
  @dwc_occurrence_object_type
end

Instance Method Details

#allActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/queries/dwc_occurrence/filter.rb', line 94

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

#and_clausesActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/queries/dwc_occurrence/filter.rb', line 46

def and_clauses
  clauses = base_and_clauses

  return nil if clauses.empty?

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

#base_and_clausesArray

Returns:

  • (Array)


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

def base_and_clauses
  clauses = []

  clauses += [
    dwc_occurrence_object_id_facet,
    dwc_occurrence_object_type_facet
  ]

  clauses.compact!
  clauses
end

#base_merge_clausesObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/queries/dwc_occurrence/filter.rb', line 71

def base_merge_clauses
  clauses = []

  clauses += [
    created_updated_facet,  # See Queries::Concerns::Users
  ]

  clauses.compact!
  clauses
end

#base_queryObject



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

def base_query
  ::DwcOccurrence.select('dwc_occurrences.*')
end

#dwc_occurrence_object_id_facetObject



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

def dwc_occurrence_object_id_facet
  return nil if dwc_occurrence_object_id.empty?
  table[:dwc_occurrence_object_id].eq_any(dwc_occurrence_object_id)
end

#dwc_occurrence_object_type_facetObject



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

def dwc_occurrence_object_type_facet
  return nil if dwc_occurrence_object_type.empty?
  table[:dwc_occurrence_object_type].eq_any(dwc_occurrence_object_type)
end

#merge_clausesActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


83
84
85
86
87
88
89
90
91
# File 'lib/queries/dwc_occurrence/filter.rb', line 83

def merge_clauses
  clauses = base_merge_clauses
  return nil if clauses.empty?
  a = clauses.shift
  clauses.each do |b|
    a = a.merge(b)
  end
  a
end

#tableArel::Table

Returns:

  • (Arel::Table)


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

def table
  ::DwcOccurrence.arel_table
end