Class: Queries::Note::Filter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Filter

Returns a new instance of Filter.



22
23
24
25
26
27
28
# File 'lib/queries/note/filter.rb', line 22

def initialize(params)
  @text = params[:text]
  @note_object_type = params[:note_object_type]
  @note_object_id = params[:note_object_id]
  @object_global_id = params[:object_global_id]
  @project_id = params[:project_id]
end

Instance Attribute Details

#note_object_idArray

Returns:

  • (Array)


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

def note_object_id
  @note_object_id
end

#note_object_typeArray

Returns:

  • (Array)


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

def note_object_type
  @note_object_type
end

#object_global_idObject

Returns the value of attribute object_global_id.



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

def object_global_id
  @object_global_id
end

#project_idObject

Returns the value of attribute project_id.



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

def project_id
  @project_id
end

#textObject

Parameters:

  • text (String, nil)

    wildcard wrapped, always, to match against `text`



8
9
10
# File 'lib/queries/note/filter.rb', line 8

def text
  @text
end

Instance Method Details

#allActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


82
83
84
85
86
87
88
89
90
91
92
# File 'lib/queries/note/filter.rb', line 82

def all 
  q = nil
  if a = and_clauses
    q = ::Note.where(and_clauses)
  else
    q = ::Note.all
  end

  q = q.where(project_id: project_id) if !project_id.blank?
  q
end

#and_clausesActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/queries/note/filter.rb', line 66

def and_clauses
  clauses = [
    text_facet,
    note_object_id_facet,
    note_object_type_facet,
    object_global_id_facet,
  ].compact

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

#note_object_id_facetObject



52
53
54
55
# File 'lib/queries/note/filter.rb', line 52

def note_object_id_facet
  return nil if note_object_id.empty?
  table[:note_object_id].eq_any(note_object_id)
end

#note_object_type_facetObject



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

def note_object_type_facet
  return nil if note_object_type.empty?
  table[:note_object_type].eq_any(note_object_type)
end

#object_global_id_facetObject



57
58
59
60
61
62
63
# File 'lib/queries/note/filter.rb', line 57

def object_global_id_facet
  return nil if object_global_id.nil?
  o = GlobalID::Locator.locate(object_global_id)
  k = o.class.base_class.name
  id = o.id 
  table[:note_object_id].eq(o.id).and(table[:note_object_type].eq(k)) 
end

#tableObject



38
39
40
# File 'lib/queries/note/filter.rb', line 38

def table
  ::Note.arel_table
end

#text_facetObject



42
43
44
45
# File 'lib/queries/note/filter.rb', line 42

def text_facet
  return nil if text.blank?
  table[:text].matches('%' + text + '%')
end