Class: Queries::Content::Filter
- Inherits:
-
Object
- Object
- Queries::Content::Filter
- Includes:
- Queries::Concerns::Users
- Defined in:
- lib/queries/content/filter.rb
Instance Attribute Summary collapse
-
#citations ⇒ Boolean?
True - only content with citations.
-
#depictions ⇒ Boolean?
True - only content with depictions.
- #exact ⇒ Boolean?
- #otu_id ⇒ Array
- #project_id ⇒ Array
-
#text ⇒ Object
was query_string.
- #topic_id ⇒ Array
Instance Method Summary collapse
- #all ⇒ ActiveRecord::Relation
- #and_clauses ⇒ ActiveRecord::Relation
- #citations_facet ⇒ Arel::Node?
- #depictions_facet ⇒ Arel::Node?
-
#initialize(params) ⇒ Filter
constructor
A new instance of Filter.
- #merge_clauses ⇒ Object
- #otu_id_facet ⇒ Arel::Node?
- #project_id_facet ⇒ Arel::Node?
- #table ⇒ Arel::Table
- #text_facet ⇒ Arel::Node?
- #topic_id_facet ⇒ Arel::Node?
Constructor Details
#initialize(params) ⇒ Filter
Returns a new instance of Filter.
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
#citations ⇒ Boolean?
Returns true - only content with citations.
30 31 32 |
# File 'lib/queries/content/filter.rb', line 30 def citations @citations end |
#depictions ⇒ Boolean?
Returns true - only content with depictions.
26 27 28 |
# File 'lib/queries/content/filter.rb', line 26 def depictions @depictions end |
#exact ⇒ Boolean?
20 21 22 |
# File 'lib/queries/content/filter.rb', line 20 def exact @exact end |
#otu_id ⇒ Array
13 14 15 |
# File 'lib/queries/content/filter.rb', line 13 def otu_id @otu_id end |
#project_id ⇒ Array
17 18 19 |
# File 'lib/queries/content/filter.rb', line 17 def project_id @project_id end |
#text ⇒ Object
was query_string
22 23 24 |
# File 'lib/queries/content/filter.rb', line 22 def text @text end |
#topic_id ⇒ Array
9 10 11 |
# File 'lib/queries/content/filter.rb', line 9 def topic_id @topic_id end |
Instance Method Details
#all ⇒ 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_clauses ⇒ 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_facet ⇒ Arel::Node?
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_facet ⇒ Arel::Node?
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_clauses ⇒ Object
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_facet ⇒ Arel::Node?
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_facet ⇒ Arel::Node?
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 |
#table ⇒ Arel::Table
60 61 62 |
# File 'lib/queries/content/filter.rb', line 60 def table ::Content.arel_table end |
#text_facet ⇒ Arel::Node?
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_facet ⇒ Arel::Node?
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 |