-
Notifications
You must be signed in to change notification settings - Fork 25
/
filter.rb
103 lines (86 loc) · 2.71 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
module Queries
module BiologicalAssociation
# !! 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 AlternateValue
attr_accessor :subject_global_id, :object_global_id, :biological_relationship_id, :any_global_id
def initialize(params)
@subject_global_id = params[:subject_global_id]
@object_global_id = params[:object_global_id]
@biological_relationship_id = params[:biological_relationship_id]
@any_global_id = params[:any_global_id]
@options = params
end
# @return [ActiveRecord::Relation]
def and_clauses
clauses = [
matching_subject_global_id,
matching_object_global_id,
matching_any_global_id,
matching_biological_relationship_id,
].compact
return nil if clauses.empty?
a = clauses.shift
clauses.each do |b|
a = a.and(b)
end
a
end
# @return [ActiveRecord object, nil]
# TODO: DRY
def object_for(global_id)
if o = GlobalID::Locator.locate(global_id)
o
else
nil
end
end
def subject_matches(object)
table["biological_association_subject_id"].eq(object.id).and(
table["biological_association_subject_type"].eq(object.metamorphosize.class.name)
)
end
def object_matches(object)
table["biological_association_object_id"].eq(object.id).and(
table["biological_association_object_type"].eq(object.metamorphosize.class.name)
)
end
def matching_subject_global_id
t = object_for(subject_global_id)
return nil if t.nil?
subject_matches(t)
end
def matching_object_global_id
t = object_for(object_global_id)
return nil if t.nil?
object_matches(t)
end
def matching_any_global_id
t = object_for(any_global_id)
return nil if t.nil?
subject_matches(t).or(object_matches(t))
end
# @return [Arel::Node, nil]
def matching_biological_relationship_id
biological_relationship_id.blank? ? nil : table[:biological_relationship_id].eq(biological_relationship_id)
end
# @return [ActiveRecord::Relation]
def all
if a = and_clauses
::BiologicalAssociation.where(and_clauses)
else
::BiologicalAssociation.all
end
end
# @return [Arel::Table]
def table
::BiologicalAssociation.arel_table
end
end
end
end