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

- (Filter) initialize(params)

Returns a new instance of Filter



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

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

- (Object) identifier

Returns the value of attribute identifier



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

def identifier
  @identifier
end

- (Object) identifier_object_id

Returns the value of attribute identifier_object_id



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

def identifier_object_id
  @identifier_object_id
end

- (Object) identifier_object_ids

Returns the value of attribute identifier_object_ids



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

def identifier_object_ids
  @identifier_object_ids
end

- (Object) identifier_object_type

Returns the value of attribute identifier_object_type



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

def identifier_object_type
  @identifier_object_type
end

- (Object) identifier_object_types

Returns the value of attribute identifier_object_types



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

def identifier_object_types
  @identifier_object_types
end

- (Object) namespace_id

Returns the value of attribute namespace_id



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

def namespace_id
  @namespace_id
end

- (Object) namespace_name

Returns the value of attribute namespace_name



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

def namespace_name
  @namespace_name
end

- (Object) namespace_short_name

Returns the value of attribute namespace_short_name



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

def namespace_short_name
  @namespace_short_name
end

- (Object) object_global_id

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



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

def object_global_id
  @object_global_id
end

- (Object) query_string

A fully qualified identifier, matches cached



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

def query_string
  @query_string
end

- (Object) type

Returns the value of attribute type



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

def type
  @type
end

Instance Method Details

- (ActiveRecord::Relation) all

Returns:

  • (ActiveRecord::Relation)


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

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

- (ActiveRecord::Relation) and_clauses

Returns:

  • (ActiveRecord::Relation)


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

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

- (Arel::Node?) matching_cached

Returns:

  • (Arel::Node, nil)


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

def matching_cached
  query_string.blank? ? nil : table[:cached].eq(query_string)
end

- (Arel::Node?) matching_identifier_attribute(attribute)

Returns:

  • (Arel::Node, nil)


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

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

- (Arel::Node?) matching_identifier_object_ids

Returns:

  • (Arel::Node, nil)


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

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

- (Arel::Node?) matching_identifier_object_types

Returns:

  • (Arel::Node, nil)


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

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

- (Object) matching_namespace(attribute)



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

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

- (Object) merge_clauses



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

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

- (Arel::Table) table

Returns:

  • (Arel::Table)


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

def table
  ::Identifier.arel_table
end