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)

TODO: identifiers



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

def initialize(params)
  @recent = params[:recent].blank? ? false : true
  @keyword_ids = params[:keyword_ids].blank? ? [] : params[:keyword_ids]
end

Instance Attribute Details

- (Object) keyword_ids

Returns the value of attribute keyword_ids



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

def keyword_ids
  @keyword_ids
end

- (Object) recent

Boolean (TODO: reconcile with other use)



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

def recent
  @recent
end

Instance Method Details

- (ActiveRecord::Relation) all

Returns:

  • (ActiveRecord::Relation)


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/queries/collection_object/filter.rb', line 109

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)


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/queries/collection_object/filter.rb', line 77

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

- (Object) matching_keyword_ids



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/queries/collection_object/filter.rb', line 28

def matching_keyword_ids
  return nil if keyword_ids.empty?
  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

- (Object) matching_shape



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/queries/collection_object/filter.rb', line 54

def matching_shape
  return nil if shape.nil?

  geometry = shape.geometry
  this_type = geometry.geometry_type.to_s.downcase
  geometry = geometry.as_text
  radius = shape['radius'] || 100

  case this_type
  when 'point'
    ::CollectionObject
      .joins(:geographic_items)
      .where(::GeographicItem.within_radius_of_wkt_sql(geometry, radius ))
  when 'polygon'
    ::CollectionObject
      .joins(:geographic_items)
      .where(::GeographicItem.contained_by_wkt_sql(geometry))
  else
    nil
  end
end

- (Object) merge_clauses



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/queries/collection_object/filter.rb', line 93

def merge_clauses
  clauses = [
    matching_keyword_ids,
    # matching_shape,
  ].compact

  return nil if clauses.empty?

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

- (Arel::Table) table

Returns:

  • (Arel::Table)


20
21
22
# File 'lib/queries/collection_object/filter.rb', line 20

def table
  ::CollectionObject.arel_table
end

- (Object) tag_table



24
25
26
# File 'lib/queries/collection_object/filter.rb', line 24

def tag_table
  ::Tag.arel_table
end