Class: Queries::CollectionObjectFilterQuery

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

Overview

TODO: deprecate/move into lib/queries/collection_object/filter

Instance Attribute Summary (collapse)

Attributes inherited from Query

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

Instance Method Summary (collapse)

Methods inherited from 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, #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

- (CollectionObjectFilterQuery) initialize(params)

Returns a new instance of CollectionObjectFilterQuery

Parameters:

  • args (Hash)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/queries/collection_object_filter_query.rb', line 18

def initialize(params)
  params.reject! { |_k, v| v.blank? } # dump all entries with empty values

  @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]
  @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]
  @query_user                  = params[:user]
  @query_date_type_select      = params[:date_type_select]
  @query_user_date_range_start = params[:user_date_range_start]
  @query_user_date_range_end   = params[:user_date_range_end]

  set_and_order_dates
end

Instance Attribute Details

- (Object) end_date

Resolved/processed results



15
16
17
# File 'lib/queries/collection_object_filter_query.rb', line 15

def end_date
  @end_date
end

- (Object) query_date_partial_overlap

Returns the value of attribute query_date_partial_overlap



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

def query_date_partial_overlap
  @query_date_partial_overlap
end

- (Object) query_date_type_select

Returns the value of attribute query_date_type_select



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

def query_date_type_select
  @query_date_type_select
end

- (Object) query_end_date

Returns the value of attribute query_end_date



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

def query_end_date
  @query_end_date
end

- (Object) query_geographic_area_ids

Query variables



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

def query_geographic_area_ids
  @query_geographic_area_ids
end

- (Object) query_id_namespace

Returns the value of attribute query_id_namespace



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

def query_id_namespace
  @query_id_namespace
end

- (Object) query_otu_descendants

Returns the value of attribute query_otu_descendants



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

def query_otu_descendants
  @query_otu_descendants
end

- (Object) query_otu_id

Returns the value of attribute query_otu_id



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

def query_otu_id
  @query_otu_id
end

- (Object) query_range_start

Returns the value of attribute query_range_start



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

def query_range_start
  @query_range_start
end

- (Object) query_range_stop

Returns the value of attribute query_range_stop



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

def query_range_stop
  @query_range_stop
end

- (Object) query_shape

Query variables



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

def query_shape
  @query_shape
end

- (Object) query_start_date

Returns the value of attribute query_start_date



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

def query_start_date
  @query_start_date
end

- (Object) query_user

Returns the value of attribute query_user



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

def query_user
  @query_user
end

- (Object) query_user_date_range_end

Returns the value of attribute query_user_date_range_end



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

def query_user_date_range_end
  @query_user_date_range_end
end

- (Object) query_user_date_range_start

Returns the value of attribute query_user_date_range_start



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

def query_user_date_range_start
  @query_user_date_range_start
end

- (Object) start_date

Resolved/processed results



15
16
17
# File 'lib/queries/collection_object_filter_query.rb', line 15

def start_date
  @start_date
end

- (Object) user_date_end

Resolved/processed results



15
16
17
# File 'lib/queries/collection_object_filter_query.rb', line 15

def user_date_end
  @user_date_end
end

- (Object) user_date_start

Resolved/processed results



15
16
17
# File 'lib/queries/collection_object_filter_query.rb', line 15

def user_date_start
  @user_date_start
end

Instance Method Details

- (Array) applied_scopes

Returns of symbols refering to methods determine which scopes to apply based on parameters provided

Returns:

  • (Array)

    of symbols refering to methods determine which scopes to apply based on parameters provided



154
155
156
157
158
159
160
161
162
163
# File 'lib/queries/collection_object_filter_query.rb', line 154

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.push :user_date_scope if user_date_set?
  scopes
end

- (Boolean) area_set?

Returns:

  • (Boolean)


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

def area_set?
  !query_geographic_area_ids.nil?
end

- (Scope) date_scope

Returns:

  • (Scope)


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

def date_scope
  sql = Queries::CollectingEvent::Filter.new(start_date: query_start_date, end_date: query_end_date, partial_overlap_dates: query_date_partial_overlap).between_date_range.to_sql
  ::CollectionObject.joins(:collecting_event)
    .where(sql)
end

- (Boolean) date_set?

Returns:

  • (Boolean)


52
53
54
# File 'lib/queries/collection_object_filter_query.rb', line 52

def date_set?
  !start_date.nil?
end

- (Scope) geographic_area_scope

Returns:

  • (Scope)


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

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 |ga_id|
    target_geographic_item_ids.push(::GeographicArea.joins(:geographic_items).find(ga_id).default_geographic_item.id)
  end
  ::CollectionObject.joins(:geographic_items)
      .where(GeographicItem.contained_by_where_sql(target_geographic_item_ids))
end

- (Scope) identifier_scope

Returns:

  • (Scope)


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

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)


72
73
74
# File 'lib/queries/collection_object_filter_query.rb', line 72

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

- (Scope) otu_scope

Returns:

  • (Scope)


84
85
86
87
88
# File 'lib/queries/collection_object_filter_query.rb', line 84

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

- (Boolean) otu_set?

Returns:

  • (Boolean)


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

def otu_set?
  !query_otu_id.nil?
end

- (Scope) result

Returns:

  • (Scope)


166
167
168
169
170
171
172
173
# File 'lib/queries/collection_object_filter_query.rb', line 166

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!



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

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)


102
103
104
# File 'lib/queries/collection_object_filter_query.rb', line 102

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

- (Boolean) shape_set?

Returns:

  • (Boolean)


62
63
64
# File 'lib/queries/collection_object_filter_query.rb', line 62

def shape_set?
  !query_shape.nil?
end

- (Scope) user_date_scope

noinspection RubyResolve

Returns:

  • (Scope)


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/queries/collection_object_filter_query.rb', line 124

def user_date_scope
  @user_date_start, @user_date_end = Utilities::Dates.normalize_and_order_dates(query_user_date_range_start,
                                                                                query_user_date_range_end)
  @user_date_start                 += ' 00:00:00' # adjust dates to beginning
  @user_date_end                   += ' 23:59:59' # and end of date days

  scope = case query_date_type_select
          when 'created_at', nil
            ::CollectionObject.created_in_date_range(@user_date_start, @user_date_end)
          when 'updated_at'
            ::CollectionObject.updated_in_date_range(@user_date_start, @user_date_end)
          else
            ::CollectionObject.all
          end

  unless query_user == 'All users' || query_user == 0
    user_id = User.get_user_id(query_user)
    scope   = case query_date_type_select
              when 'created_at'
                # noinspection RubyResolve
                scope.created_by_user(user_id)
              when 'updated_at'
                scope.updated_by_user(user_id)
              end
  end
  scope
end

- (Boolean) user_date_set?

Returns:

  • (Boolean)


77
78
79
# File 'lib/queries/collection_object_filter_query.rb', line 77

def user_date_set?
  query_user.present? or (query_user_date_range_start.present? or query_user_date_range_end.present?)
end

- (Boolean) with_descendants?

Returns:

  • (Boolean)


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

def with_descendants?
  query_otu_descendants == 'on'
end