Class: Queries::CollectionObject::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/queries/collection_object/filter.rb

Overview

!! does not inherit from base query

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Filter) initialize(params)

Returns a new instance of Filter

Parameters:

  • args (Hash)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/queries/collection_object/filter.rb', line 21

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

  @recent = params[:recent].blank? ? false : true
  @keyword_ids = params[:keyword_ids].blank? ? [] : params[:keyword_ids]

  @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



18
19
20
# File 'lib/queries/collection_object/filter.rb', line 18

def end_date
  @end_date
end

- (Object) keyword_ids

Returns the value of attribute keyword_ids



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

def keyword_ids
  @keyword_ids
end

- (Object) query_date_partial_overlap

Returns the value of attribute query_date_partial_overlap



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

def query_date_partial_overlap
  @query_date_partial_overlap
end

- (Object) query_date_type_select

Returns the value of attribute query_date_type_select



13
14
15
# File 'lib/queries/collection_object/filter.rb', line 13

def query_date_type_select
  @query_date_type_select
end

- (Object) query_end_date

Returns the value of attribute query_end_date



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

def query_end_date
  @query_end_date
end

- (Object) query_geographic_area_ids

Query variables



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

def query_geographic_area_ids
  @query_geographic_area_ids
end

- (Object) query_id_namespace

Returns the value of attribute query_id_namespace



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

def query_id_namespace
  @query_id_namespace
end

- (Object) query_otu_descendants

Returns the value of attribute query_otu_descendants



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

def query_otu_descendants
  @query_otu_descendants
end

- (Object) query_otu_id

Returns the value of attribute query_otu_id



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

def query_otu_id
  @query_otu_id
end

- (Object) query_params

Returns the value of attribute query_params



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

def query_params
  @query_params
end

- (Object) query_range_start

Returns the value of attribute query_range_start



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

def query_range_start
  @query_range_start
end

- (Object) query_range_stop

Returns the value of attribute query_range_stop



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

def query_range_stop
  @query_range_stop
end

- (Object) query_shape

Query variables



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

def query_shape
  @query_shape
end

- (Object) query_start_date

Returns the value of attribute query_start_date



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

def query_start_date
  @query_start_date
end

- (Object) query_user

Returns the value of attribute query_user



13
14
15
# File 'lib/queries/collection_object/filter.rb', line 13

def query_user
  @query_user
end

- (Object) query_user_date_range_end

Returns the value of attribute query_user_date_range_end



13
14
15
# File 'lib/queries/collection_object/filter.rb', line 13

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



13
14
15
# File 'lib/queries/collection_object/filter.rb', line 13

def query_user_date_range_start
  @query_user_date_range_start
end

- (Object) recent

Returns the value of attribute recent



5
6
7
# File 'lib/queries/collection_object/filter.rb', line 5

def recent
  @recent
end

- (Object) start_date

Resolved/processed results



18
19
20
# File 'lib/queries/collection_object/filter.rb', line 18

def start_date
  @start_date
end

- (Object) user_date_end

Resolved/processed results



18
19
20
# File 'lib/queries/collection_object/filter.rb', line 18

def user_date_end
  @user_date_end
end

- (Object) user_date_start

Resolved/processed results



18
19
20
# File 'lib/queries/collection_object/filter.rb', line 18

def user_date_start
  @user_date_start
end

Instance Method Details

- (ActiveRecord::Relation) all

Returns:

  • (ActiveRecord::Relation)


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/queries/collection_object/filter.rb', line 116

def all
  a = and_clauses
  b = merge_clauses

  # q = nil
  if a && b
    q = b.where(a).distinct
  elsif a
    q = ::CollectionObject.where(a).distinct
  elsif b
    q = b.distinct
  else
    q = ::CollectionObject.all
  end

  q = q.order(updated_at: :desc) if recent
  q
end

- (ActiveRecord::Relation) and_clauses

Returns:

  • (ActiveRecord::Relation)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/queries/collection_object/filter.rb', line 83

def and_clauses
  clauses = []
  # clauses += attribute_clauses

  clauses += [
  ].compact

  return nil if clauses.empty?

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

- (Array) applied_scopes(scopes = [])

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

Parameters:

  • scopes (Array) (defaults to: [])

Returns:

  • (Array)

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



260
261
262
263
264
265
266
267
268
269
# File 'lib/queries/collection_object/filter.rb', line 260

def applied_scopes(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)


148
149
150
# File 'lib/queries/collection_object/filter.rb', line 148

def area_set?
  query_geographic_area_ids.present?
end

- (Scope) date_scope

Returns:

  • (Scope)


211
212
213
214
215
216
217
# File 'lib/queries/collection_object/filter.rb', line 211

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)


153
154
155
# File 'lib/queries/collection_object/filter.rb', line 153

def date_set?
  start_date.present?
end

- (Scope) geographic_area_scope

Returns:

  • (Scope)


192
193
194
195
196
197
198
199
200
201
202
# File 'lib/queries/collection_object/filter.rb', line 192

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)


220
221
222
223
224
225
# File 'lib/queries/collection_object/filter.rb', line 220

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

- (Boolean) identifier_set?

Returns:

  • (Boolean)


173
174
175
# File 'lib/queries/collection_object/filter.rb', line 173

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

- (Boolean) keyword_ids_set?

Returns:

  • (Boolean)


143
144
145
# File 'lib/queries/collection_object/filter.rb', line 143

def keyword_ids_set?
  keyword_ids.any?
end

- (Object) matching_keyword_ids

TODO: make generic



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/queries/collection_object/filter.rb', line 56

def matching_keyword_ids
  return nil unless keyword_ids_set?
  o = table
  t = ::Tag.arel_table

  a = o.alias("a_")
  b = o.project(a[Arel.star]).from(a)

  c = t.alias('t1')

  b = b.join(c, Arel::Nodes::OuterJoin)
        .on(
          a[:id].eq(c[:tag_object_id])
            .and(c[:tag_object_type].eq(table.name.classify))
        )

  e = c[:keyword_id].not_eq(nil)
  f = c[:keyword_id].eq_any(keyword_ids)

  b = b.where(e.and(f))
  b = b.group(a['id'])
  b = b.as('tz5_')

  _a = ::CollectionObject.joins(Arel::Nodes::InnerJoin.new(b, Arel::Nodes::On.new(b['id'].eq(o['id']))))
end

- (ActiveRecord::Relation) merge_clauses

Returns:

  • (ActiveRecord::Relation)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/queries/collection_object/filter.rb', line 100

def merge_clauses
  # from the simple filter
  clauses = [matching_keyword_ids]
  # from the complex query
  clauses = applied_scopes(clauses).compact

  return nil if clauses.empty?

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

- (Scope) otu_scope

Returns:

  • (Scope)


185
186
187
188
189
# File 'lib/queries/collection_object/filter.rb', line 185

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)


158
159
160
# File 'lib/queries/collection_object/filter.rb', line 158

def otu_set?
  query_otu_id.present?
end

- (Scope) result

Returns:

  • (Scope)


272
273
274
# File 'lib/queries/collection_object/filter.rb', line 272

def result
  return all
end

- (Object) set_and_order_dates

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



136
137
138
139
140
# File 'lib/queries/collection_object/filter.rb', line 136

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)


206
207
208
# File 'lib/queries/collection_object/filter.rb', line 206

def shape_scope
  ::GeographicItem.gather_map_data(query_shape, 'CollectionObject', Current.project_id)       # !!! ARG NO !!!
end

- (Boolean) shape_set?

Returns:

  • (Boolean)


163
164
165
# File 'lib/queries/collection_object/filter.rb', line 163

def shape_set?
  query_shape.present?
end

- (Arel::Table) table

Returns:

  • (Arel::Table)


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

def table
  ::CollectionObject.arel_table
end

- (Object) tag_table



51
52
53
# File 'lib/queries/collection_object/filter.rb', line 51

def tag_table
  ::Tag.arel_table
end

- (Scope) user_date_scope

noinspection RubyResolve

Returns:

  • (Scope)


229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/queries/collection_object/filter.rb', line 229

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)


178
179
180
# File 'lib/queries/collection_object/filter.rb', line 178

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)


168
169
170
# File 'lib/queries/collection_object/filter.rb', line 168

def with_descendants?
  query_otu_descendants == 'on'
end