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

#alphabetic_strings, #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, #pieces, #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



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

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]
  @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

Reolved/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



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_date_type_select

Returns the value of attribute query_date_type_select



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

def query_date_type_select
  @query_date_type_select
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) query_user

Returns the value of attribute query_user



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

def query_user
  @query_user
end

- (Object) query_user_date_range_end

Returns the value of attribute query_user_date_range_end



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

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



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

def query_user_date_range_start
  @query_user_date_range_start
end

- (Object) start_date

Reolved/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

Reolved/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

Reolved/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 determine which scopes to apply based on parameters provided

Returns:

  • (Array)

    determine which scopes to apply based on parameters provided



142
143
144
145
146
147
148
149
150
151
# File 'lib/queries/collection_object_filter_query.rb', line 142

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)


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

def area_set?
  !query_geographic_area_ids.nil?
end

- (Scope) date_scope

Returns:

  • (Scope)


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

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)


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

def date_set?
  !start_date.nil?
end

- (Scope) geographic_area_scope

Returns:

  • (Scope)


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

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



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

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)


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

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

- (Scope) otu_scope

Returns:

  • (Scope)


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

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)


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

def otu_set?
  !query_otu_id.nil?
end

- (Scope) result

Returns:

  • (Scope)


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

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!



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

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)


95
96
97
# File 'lib/queries/collection_object_filter_query.rb', line 95

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

- (Boolean) shape_set?

Returns:

  • (Boolean)


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

def shape_set?
  !query_shape.nil?
end

- (Object) user_date_scope



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/queries/collection_object_filter_query.rb', line 113

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'
                  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)


69
70
71
72
73
# File 'lib/queries/collection_object_filter_query.rb', line 69

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

- (Boolean) with_descendants?

Returns:

  • (Boolean)


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

def with_descendants?
  query_otu_descendants == 'on'
end