Class: Queries::Content::Filter

Inherits:
Object
  • Object
show all
Includes:
Queries::Concerns::Users
Defined in:
lib/queries/content/filter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Filter

Returns a new instance of Filter.

Parameters:

  • args (Hash)


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/queries/content/filter.rb', line 33

def initialize(params)

  @topic_id = params[:topic_id]
  @otu_id = params[:otu_id]
  @project_id = params[:project_id]
  @text = params[:text]
  @exact = (params[:exact]&.downcase == 'true' ? true : false) if !params[:exact].nil?
  @depictions = (params[:depictions]&.downcase == 'true' ? true : false) if !params[:depictions].nil?

  @citations = (params[:citations]&.downcase == 'true' ? true : false) if !params[:citations].nil?

  set_user_dates(params)
end

Instance Attribute Details

#citationsBoolean?

Returns true - only content with citations.

Returns:

  • (Boolean, nil)

    true - only content with citations



30
31
32
# File 'lib/queries/content/filter.rb', line 30

def citations
  @citations
end

#depictionsBoolean?

Returns true - only content with depictions.

Returns:

  • (Boolean, nil)

    true - only content with depictions



26
27
28
# File 'lib/queries/content/filter.rb', line 26

def depictions
  @depictions
end

#exactBoolean?

Returns:

  • (Boolean, nil)


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

def exact
  @exact
end

#otu_idArray

Parameters:

  • otu_id (Array, Integer, String, nil)

Returns:

  • (Array)


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

def otu_id
  @otu_id
end

#project_idArray

Parameters:

  • project_id (Array, Integer, String, nil)

Returns:

  • (Array)


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

def project_id
  @project_id
end

#textObject

was query_string



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

def text
  @text
end

#topic_idArray

Parameters:

  • topic_id (Array, Integer, String, nil)

Returns:

  • (Array)


9
10
11
# File 'lib/queries/content/filter.rb', line 9

def topic_id
  @topic_id
end

Instance Method Details

#allActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/queries/content/filter.rb', line 147

def all
  a = and_clauses
  b = merge_clauses

  q = nil
  if a && b
    q = b.where(a)
  elsif a
    q = ::Content.includes(:otu, :topic).where(a)
  elsif b
    q = b
  else
    q = ::Content.includes(:otu, :topic).all
  end

  q
end

#and_clausesActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


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

def and_clauses
  clauses = [
    text_facet,
    topic_id_facet,
    otu_id_facet,
    project_id_facet,
  ].compact

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

#citations_facetArel::Node?

Returns:

  • (Arel::Node, nil)


115
116
117
118
119
120
121
122
# File 'lib/queries/content/filter.rb', line 115

def citations_facet
  return nil if citations.nil?
  if citations
    ::Content.joins(:citations)
  else
    ::Content.left_joins(:citations).where(citations: {id: nil})
  end
end

#depictions_facetArel::Node?

Returns:

  • (Arel::Node, nil)


105
106
107
108
109
110
111
112
# File 'lib/queries/content/filter.rb', line 105

def depictions_facet
  return nil if depictions.nil?
  if depictions
    ::Content.joins(:depictions)
  else
    ::Content.left_joins(:depictions).where(depictions: {id: nil})
  end
end

#merge_clausesObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/queries/content/filter.rb', line 130

def merge_clauses
  clauses = [
    depictions_facet, 
    citations_facet,
    created_updated_facet, # See Queries::Concerns::Users
  ].compact

  return nil if clauses.empty?

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

#otu_id_facetArel::Node?

Returns:

  • (Arel::Node, nil)


91
92
93
94
# File 'lib/queries/content/filter.rb', line 91

def otu_id_facet
  return nil if otu_id.empty?
  table[:otu_id].eq_any(otu_id) 
end

#project_id_facetArel::Node?

Returns:

  • (Arel::Node, nil)


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

def project_id_facet
  return nil if project_id.empty?
  table[:project_id].eq_any(project_id) 
end

#tableArel::Table

Returns:

  • (Arel::Table)


60
61
62
# File 'lib/queries/content/filter.rb', line 60

def table
  ::Content.arel_table
end

#text_facetArel::Node?

Returns:

  • (Arel::Node, nil)


81
82
83
84
85
86
87
88
# File 'lib/queries/content/filter.rb', line 81

def text_facet
  return nil if text.blank?
  if exact
    table[:text].eq(text.strip)
  else
    table[:text].matches('%' + text.strip.gsub(/\s+/, '%') + '%')
  end
end

#topic_id_facetArel::Node?

Returns:

  • (Arel::Node, nil)


125
126
127
128
# File 'lib/queries/content/filter.rb', line 125

def topic_id_facet
  return nil if topic_id.empty?
  table[:topic_id].eq_any(topic_id) 
end