/
filter.rb
55 lines (46 loc) · 1.11 KB
/
filter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
53
54
55
module Queries
module Note
# !! does not inherit from base query
class Filter
# General annotator options handling
# happens directly on the params as passed
# through to the controller, keep them
# together here
attr_accessor :options
# Params specific to Note
attr_accessor :text
def initialize(params)
@text = params[:text]
@options = params
end
# @return [ActiveRecord::Relation]
def and_clauses
clauses = [
Queries::Annotator.annotator_params(options, ::Note),
matching_text,
].compact
a = clauses.shift
clauses.each do |b|
a = a.and(b)
end
a
end
# @return [Arel::Node, nil]
def matching_text
text.blank? ? nil : table[:text].eq(text)
end
# @return [ActiveRecord::Relation]
def all
if a = and_clauses
::Note.where(and_clauses)
else
::Note.none
end
end
# @return [Arel::Table]
def table
::Note.arel_table
end
end
end
end