Class: Queries::Identifier::Filter

Inherits:
Object
  • Object
show all
Includes:
Concerns::Polymorphic
Defined in:
lib/queries/identifier/filter.rb

Overview

!! does not inherit from Queries::Query

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Filter

Returns a new instance of Filter



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

def initialize(params)
  @query_string = params[:query_string]
  @namespace_id = params[:namespace_id]
  @identifier = params[:identifier]
  @namespace_short_name = params[:namespace_short_name]
  @namespace_name = params[:namespace_name]

  @identifier_object_type = params[:identifier_object_type] 
  @identifier_object_id = params[:identifier_object_id] 

  @identifier_object_ids = params[:identifier_object_ids] || []
  @identifier_object_types = params[:identifier_object_types] || []

  @type = params[:type]

  self.object_global_id = params[:object_global_id]

  set_polymorphic_ids(params)
  # need 'set annotator params'
end

Instance Attribute Details

#identifierObject

Returns the value of attribute identifier



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

def identifier
  @identifier
end

#identifier_object_idObject

Returns the value of attribute identifier_object_id



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

def identifier_object_id
  @identifier_object_id
end

#identifier_object_idsObject

Returns the value of attribute identifier_object_ids



21
22
23
# File 'lib/queries/identifier/filter.rb', line 21

def identifier_object_ids
  @identifier_object_ids
end

#identifier_object_typeObject

Returns the value of attribute identifier_object_type



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

def identifier_object_type
  @identifier_object_type
end

#identifier_object_typesObject

Returns the value of attribute identifier_object_types



22
23
24
# File 'lib/queries/identifier/filter.rb', line 22

def identifier_object_types
  @identifier_object_types
end

#namespace_idObject

Returns the value of attribute namespace_id



16
17
18
# File 'lib/queries/identifier/filter.rb', line 16

def namespace_id
  @namespace_id
end

#namespace_nameObject

Returns the value of attribute namespace_name



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

def namespace_name
  @namespace_name
end

#namespace_short_nameObject

Returns the value of attribute namespace_short_name



17
18
19
# File 'lib/queries/identifier/filter.rb', line 17

def namespace_short_name
  @namespace_short_name
end

#object_global_idObject

TODO -> attribute does nothing yet Probably turn it into component parts



28
29
30
# File 'lib/queries/identifier/filter.rb', line 28

def object_global_id
  @object_global_id
end

#query_stringObject

A fully qualified identifier, matches cached !! This is the only wildcarded value !!



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

def query_string
  @query_string
end

#typeObject

Returns the value of attribute type



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

def type
  @type
end

Instance Method Details

#allActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/queries/identifier/filter.rb', line 136

def all
  a = and_clauses
  b = merge_clauses
  if a && b
    b.where(a).distinct
  elsif a
    ::Identifier.where(a).distinct
  elsif b
    b.distinct
  else
    ::Identifier.all
  end
end

#and_clausesActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


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

def and_clauses
  clauses = [
    # Queries::Annotator.annotator_params(options, ::Identifier),
    matching_cached,
    matching_identifier_attribute(:identifier),
    matching_identifier_attribute(:namespace_id),
    matching_identifier_attribute(:identifier_object_type),
    matching_identifier_attribute(:identifier_object_id),
    matching_identifier_attribute(:type),
    matching_identifier_object_types,
    matching_polymorphic_ids
  ].compact

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

#matching_cachedArel::Node?

Returns:

  • (Arel::Node, nil)


89
90
91
# File 'lib/queries/identifier/filter.rb', line 89

def matching_cached
  query_string.blank? ? nil : table[:cached].matches('%' + query_string + '%')
end

#matching_identifier_attribute(attribute) ⇒ Arel::Node?

Returns:

  • (Arel::Node, nil)


104
105
106
107
# File 'lib/queries/identifier/filter.rb', line 104

def matching_identifier_attribute(attribute)
  v = send(attribute)
  v.blank? ? nil : table[attribute].eq(v)
end

#matching_identifier_object_idsArel::Node?

Returns:

  • (Arel::Node, nil)


94
95
96
# File 'lib/queries/identifier/filter.rb', line 94

def matching_identifier_object_ids
  identifier_object_ids.empty? ? nil : table[:identifier_object_id].eq_any(matching_identifier_object_ids)
end

#matching_identifier_object_typesArel::Node?

Returns:

  • (Arel::Node, nil)


99
100
101
# File 'lib/queries/identifier/filter.rb', line 99

def matching_identifier_object_types
  identifier_object_types.empty? ? nil : table[:identifier_object_type].eq_any(identifier_object_types)
end

#matching_namespace(attribute) ⇒ Object



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
# File 'lib/queries/identifier/filter.rb', line 109

def matching_namespace(attribute)
  v = self.send("namespace_#{attribute}") 
  return nil if v.blank?
  o = table
  n = ::Namespace.arel_table

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

  c = n.alias('n1')

  b = b.join(c, Arel::Nodes::OuterJoin)
    .on(
      a[:namespace_id].eq(c[:id])
  )

  e = c[:id].not_eq(nil)
  f = c[attribute].eq(v)

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

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

#merge_clausesObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/queries/identifier/filter.rb', line 73

def merge_clauses
  clauses = [
    matching_namespace(:short_name),
    matching_namespace(:name),
  ].compact

  return nil if clauses.empty?

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

#tableArel::Table

Returns:

  • (Arel::Table)


151
152
153
# File 'lib/queries/identifier/filter.rb', line 151

def table
  ::Identifier.arel_table
end