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

#build_terms, #cached, #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_like, #with_project_id, #years

Constructor Details

- (CollectionObjectFilterQuery) initialize(params)

Returns a new instance of CollectionObjectFilterQuery



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

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]

  set_and_order_dates
end

Instance Attribute Details

- (Object) end_date

Reolved/processed results



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

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_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_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



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

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



86
87
88
89
90
91
92
93
# File 'lib/queries/collection_object_filter_query.rb', line 86

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
end

- (Boolean) area_set?

Returns:

  • (Boolean)


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

def area_set?
  !query_geographic_area_ids.nil?
end

- (Scope) date_scope

Returns:

  • (Scope)


79
80
81
82
# File 'lib/queries/collection_object_filter_query.rb', line 79

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)


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

def date_set?
  !start_date.nil? 
end

- (Scope) geographic_area_scope

Returns:

  • (Scope)


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

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

- (Scope) otu_scope

Returns:

  • (Scope)


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

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)


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

def otu_set?
  !query_otu_id.nil?
end

- (Scope) result

Returns:

  • (Scope)


96
97
98
99
100
101
102
103
# File 'lib/queries/collection_object_filter_query.rb', line 96

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!



28
29
30
31
32
# File 'lib/queries/collection_object_filter_query.rb', line 28

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)


74
75
76
# File 'lib/queries/collection_object_filter_query.rb', line 74

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

- (Boolean) shape_set?

Returns:

  • (Boolean)


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

def shape_set?
  !query_shape.nil?
end

- (Boolean) with_descendants?

Returns:

  • (Boolean)


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

def with_descendants?
  query_otu_descendants == 'on'
end