Module: Queries::Concerns::Protocols

Extended by:
ActiveSupport::Concern
Included in:
Queries::CollectingEvent::Filter, Queries::CollectionObject::Filter, Extract::Filter, FieldOccurrence::Filter, Observation::Filter
Defined in:
lib/queries/concerns/protocols.rb

Overview

Helpers and facets for queries that reference Protocols.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.merge_clausesObject



84
85
86
87
88
89
# File 'lib/queries/concerns/protocols.rb', line 84

def self.merge_clauses
  [
    :protocol_id_facet,
    :protocols_facet,
  ]
end

.paramsObject



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/queries/concerns/protocols.rb', line 7

def self.params
  [
    :protocols,
    :protocol_id_or,
    :protocol_id_and,
    :protocol_id,        # TODO: unused? or maybe in polymorphics setters!!
    protocol_id: [],     # TODO: unused? or maybe in polymorphics setters!!
    protocol_id_or: [],
    protocol_id_and: [],
  ]
end

Instance Method Details

#matching_protocol_id_andObject (private)

merge



106
107
108
109
110
111
112
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/concerns/protocols.rb', line 106

def matching_protocol_id_and
  return nil if protocol_id_and.empty?
  l = table.name
  k = l.classify.safe_constantize
  t = ::ProtocolRelationship.arel_table

  a = table.alias("k_#{l}")

  b = table.project(a[Arel.star]).from(a)
    .join(t)
    .on(
      t[:protocol_relationship_object_id].eq(a[:id]),
      t[:protocol_relationship_object_type].eq(k.name)
    )

  i = 0

  protocol_id_and.each do |j|
    t_a = t.alias("tk_#{l[0..5]}_#{i}")
    b = b.join(t_a).on(
      t_a['protocol_relationship_object_id'].eq(a['id']),
      t_a[:protocol_relationship_object_type].eq(k),
      t_a[:protocol_id].eq(j)
    )

    i += 1
  end

  b = b.group(a[:id]).having(t[:protocol_id].count.gteq(protocol_id_and.count))
  b = b.as("#{l}_ai")

  k.joins(Arel::Nodes::InnerJoin.new(b, Arel::Nodes::On.new(b[:id].eq(table[:id]))))
end

#matching_protocol_id_orObject (private)

merge



94
95
96
97
98
99
100
101
102
103
# File 'lib/queries/concerns/protocols.rb', line 94

def matching_protocol_id_or
  return nil if protocol_id_or.empty?
  k = table.name.classify.safe_constantize
  t = ::ProtocolRelationship.arel_table

  w = t[:protocol_relationship_object_id].eq(table[:id]).and( t[:protocol_relationship_object_type].eq(table.name.classify))
  w = w.and( t[:protocol_id].in(protocol_id_or) ) if protocol_id_or.any?

  k.where( ::ProtocolRelationship.where(w).arel.exists )
end

#protocol_id_facetObject

Returns all sources that match all _and ids OR any OR id.

Returns:

  • all sources that match all _and ids OR any OR id



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/queries/concerns/protocols.rb', line 60

def protocol_id_facet
  return nil if protocol_id_or.empty? && protocol_id_and.empty?

  a = matching_protocol_id_or
  b = matching_protocol_id_and

  if a.nil?
    b
  elsif b.nil?
    a
  else
    referenced_klass_union([a,b]) # k.from("( (#{a.to_sql}) UNION (#{b.to_sql})) as #{table.name}")
  end
end

#protocols_facetObject



75
76
77
78
79
80
81
82
# File 'lib/queries/concerns/protocols.rb', line 75

def protocols_facet
  return nil if protocols.nil?
  if protocols
    referenced_klass.joins(:protocols).distinct
  else
    referenced_klass.where.missing(:protocols)
  end
end