Class: Queries::CollectionObjectFilterQuery

Inherits:
Query
  • Object
show all
Defined in:
lib/queries/collection_object_filter_query.rb

Instance Attribute Summary (collapse)

Attributes inherited from Query

#dynamic_limit, #project_id, #query_string, #terms

Instance Method Summary (collapse)

Methods inherited from Query

#autocomplete, #build_terms, #cached, #combine_or_clauses, #end_wildcard, #exactly_named, #fragments, #identifier_table, #integers, #named, #no_digits, #only_ids, #only_integers?, #parent, #parent_child_join, #parent_child_where, #scope, #start_and_end_wildcard, #start_wildcard, #strings, #wildcard_wrapped_integers, #with_id, #with_identifier, #with_identifier_like, #with_project_id, #years

Constructor Details

- (CollectionObjectFilterQuery) initialize(params)

Returns a new instance of CollectionObjectFilterQuery



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/queries/collection_object_filter_query.rb', line 14

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

  @query_geographic_area_ids = params[:geographic_area_ids]
  @query_shape = params[:drawn_area_shape]
  @query_start_date = params[:search_start_date] # TODO: sync key names
  @query_end_date = params[:search_end_date]
  @query_otu_id = params[:otu_id]
  @query_otu_descendants = params[:descendants] # .downcase if params[:descendants] # TODO: remove downcase requirement
  @query_date_partial_overlap = params[:partial_overlap]
  @query_id_namespace = params[:id_namespace]
  @query_range_start = params[:id_range_start]
  @query_range_stop = params[:id_range_stop]

  set_and_order_dates
end

Instance Attribute Details

- (Object) end_date

Reolved/processed results



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

def end_date
  @end_date
end

- (Object) query_date_partial_overlap

Returns the value of attribute query_date_partial_overlap



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

def query_date_partial_overlap
  @query_date_partial_overlap
end

- (Object) query_end_date

Returns the value of attribute query_end_date



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

def query_end_date
  @query_end_date
end

- (Object) query_geographic_area_ids

Query variables



6
7
8
# File 'lib/queries/collection_object_filter_query.rb', line 6

def query_geographic_area_ids
  @query_geographic_area_ids
end

- (Object) query_id_namespace

Returns the value of attribute query_id_namespace



9
10
11
# File 'lib/queries/collection_object_filter_query.rb', line 9

def query_id_namespace
  @query_id_namespace
end

- (Object) query_otu_descendants

Returns the value of attribute query_otu_descendants



8
9
10
# File 'lib/queries/collection_object_filter_query.rb', line 8

def query_otu_descendants
  @query_otu_descendants
end

- (Object) query_otu_id

Returns the value of attribute query_otu_id



8
9
10
# File 'lib/queries/collection_object_filter_query.rb', line 8

def query_otu_id
  @query_otu_id
end

- (Object) query_range_start

Returns the value of attribute query_range_start



9
10
11
# File 'lib/queries/collection_object_filter_query.rb', line 9

def query_range_start
  @query_range_start
end

- (Object) query_range_stop

Returns the value of attribute query_range_stop



9
10
11
# File 'lib/queries/collection_object_filter_query.rb', line 9

def query_range_stop
  @query_range_stop
end

- (Object) query_shape

Query variables



6
7
8
# File 'lib/queries/collection_object_filter_query.rb', line 6

def query_shape
  @query_shape
end

- (Object) query_start_date

Returns the value of attribute query_start_date



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

def query_start_date
  @query_start_date
end

- (Object) start_date

Reolved/processed results



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

def start_date
  @start_date
end

Instance Method Details

- (Array) applied_scopes

Returns determine which scopes to apply based on parameters provided

Returns:

  • (Array)

    determine which scopes to apply based on parameters provided



102
103
104
105
106
107
108
109
110
# File 'lib/queries/collection_object_filter_query.rb', line 102

def applied_scopes
  scopes = []
  scopes.push :otu_scope if otu_set?
  scopes.push :geographic_area_scope if area_set?
  scopes.push :shape_scope if shape_set?
  scopes.push :date_scope if date_set?
  scopes.push :identifier_scope if identifier_set?
  scopes
end

- (Boolean) area_set?

Returns:

  • (Boolean)


38
39
40
# File 'lib/queries/collection_object_filter_query.rb', line 38

def area_set?
  !query_geographic_area_ids.nil?
end

- (Scope) date_scope

Returns:

  • (Scope)


87
88
89
90
# File 'lib/queries/collection_object_filter_query.rb', line 87

def date_scope
  CollectionObject.joins(:collecting_event).where(CollectingEvent.date_sql_from_dates(start_date, end_date, query_date_partial_overlap))
  #date_sql_from_dates(start_date, end_date, query_date_partial_overlap ))
end

- (Boolean) date_set?

Returns:

  • (Boolean)


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

def date_set?
  !start_date.nil?
end

- (Scope) geographic_area_scope

Returns:

  • (Scope)


72
73
74
75
76
77
78
79
# File 'lib/queries/collection_object_filter_query.rb', line 72

def geographic_area_scope
  # This could be simplified if the AJAX selector returned a geographic_item_id rather than a GeographicAreaId
  target_geographic_item_ids = []
  query_geographic_area_ids.each do |gaid|
    target_geographic_item_ids.push(GeographicArea.joins(:geographic_items).find(gaid).default_geographic_item.id)
  end
  CollectionObject.joins(:geographic_items).where(GeographicItem.contained_by_where_sql(target_geographic_item_ids))
end

- (Object) identifier_scope



92
93
94
95
96
97
98
# File 'lib/queries/collection_object_filter_query.rb', line 92

def identifier_scope
  ns = nil
  ns = Namespace.where(short_name: query_id_namespace).first if query_id_namespace.present?
  CollectionObject.with_identifier_type_and_namespace('Identifier::Local::CatalogNumber', ns, true)
      .where("CAST(identifiers.identifier AS integer) between ? and ?",
             query_range_start.to_i, query_range_stop.to_i)
end

- (Boolean) identifier_set?

Returns:

  • (Boolean)


58
59
60
# File 'lib/queries/collection_object_filter_query.rb', line 58

def identifier_set?
  query_range_start.present? || query_range_stop.present?
end

- (Scope) otu_scope

Returns:

  • (Scope)


65
66
67
68
69
# File 'lib/queries/collection_object_filter_query.rb', line 65

def otu_scope
  # Challenge: Refactor to use a join pattern instead of SELECT IN
  innerscope = with_descendants? ? Otu.self_and_descendants_of(query_otu_id) : Otu.where(id: query_otu_id)
  CollectionObject.joins(:otus).where(otus: {id: innerscope} )
end

- (Boolean) otu_set?

Returns:

  • (Boolean)


46
47
48
# File 'lib/queries/collection_object_filter_query.rb', line 46

def otu_set?
  !query_otu_id.nil?
end

- (Scope) result

Returns:

  • (Scope)


113
114
115
116
117
118
119
120
# File 'lib/queries/collection_object_filter_query.rb', line 113

def result
  return CollectionObject.none if applied_scopes.empty?
  a = CollectionObject.all
  applied_scopes.each do |scope|
    a = a.merge(self.send(scope))
  end
  a
end

- (Object) set_and_order_dates

Only set (and therefor ultimately use) dates if they were provided!



32
33
34
35
36
# File 'lib/queries/collection_object_filter_query.rb', line 32

def set_and_order_dates
  if query_start_date || query_end_date
    @start_date, @end_date = Utilities::Dates.normalize_and_order_dates(query_start_date, query_end_date)
  end
end

- (Scope) shape_scope

Returns:

  • (Scope)


82
83
84
# File 'lib/queries/collection_object_filter_query.rb', line 82

def shape_scope
  GeographicItem.gather_map_data(query_shape, 'CollectionObject')
end

- (Boolean) shape_set?

Returns:

  • (Boolean)


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

def shape_set?
  !query_shape.nil?
end

- (Boolean) with_descendants?

Returns:

  • (Boolean)


54
55
56
# File 'lib/queries/collection_object_filter_query.rb', line 54

def with_descendants?
  query_otu_descendants == 'on'
end