Class: Queries::Query::Filter
- Inherits:
-
Queries::Query
- Object
- Queries::Query
- Queries::Query::Filter
- Includes:
- Concerns::Users
- Defined in:
- lib/queries/query/filter.rb
Overview
Overview
This class manages params and nesting of filter queries.
Each inheriting class defines a PARAMS variable. Each concern defines `self.params`. Together these two lists are used to compose a list of acceptable params, dynamically, based on the nature of the nested queries.
Test coverage is currently in /spec/lib/queries/otu/filter_spec.rb.
Direct Known Subclasses
AlternateValue::Filter, Attribution::Filter, BiologicalAssociation::Filter, BiologicalAssociationsGraph::Filter, Citation::Filter, CollectingEvent::Filter, CommonName::Filter, Confidence::Filter, Content::Filter, DataAttribute::Filter, Descriptor::Filter, Documentation::Filter, Download::Filter, DwcOccurrence::Filter, Georeference::Filter, Identifier::Filter, Loan::Filter, Namespace::Filter, Note::Filter, Observation::Filter, ObservationMatrix::Filter, ObservationMatrixRow::Filter, OriginRelationship::Filter, Otu::Filter, Person::Filter, ProtocolRelationship::Filter, Role::Filter, Serial::Filter, SqedDepiction::Filter, Tag::Filter, TaxonDetermination::Filter, TaxonNameClassification::Filter, TaxonNameRelationship::Filter
Constant Summary collapse
- SUBQUERIES =
!! SUBQUERIES is cross-referenced in app/views/javascript/vue/components/radials/filter/links/*.js models. !! When you add a reference here, ensure corresponding js model is aligned. There are tests that will catch if they are not.
For example: github.com/SpeciesFileGroup/taxonworks/app/javascript/vue/components/radials/filter/constants/queryParam.js github.com/SpeciesFileGroup/taxonworks/app/javascript/vue/components/radials/filter/constants/filterLinks.js github.com/SpeciesFileGroup/taxonworks/app/javascript/vue/components/radials/filter/links/CollectionObject.js
You may also need a reference in app/javascript/vue/routes/routes.js app/javascript/vue/components/radials/linker/links
This is read as :to <- [:from1, from2…] ].
{ asserted_distribution: [:source, :otu, :biological_association, :taxon_name], biological_association: [:source, :collecting_event, :otu, :collection_object, :taxon_name, :asserted_distribution], biological_associations_graph: [:biological_association, :source], collecting_event: [:source, :collection_object, :biological_association, :otu, :image, :taxon_name], collection_object: [:source, :loan, :otu, :taxon_name, :collecting_event, :biological_association, :extract, :image, :observation], content: [:source, :otu, :taxon_name, :image], descriptor: [:source, :observation, :otu], extract: [:source, :otu, :collection_object], image: [:content, :collection_object, :collecting_event, :otu, :observation, :source, :taxon_name ], loan: [:collection_object, :otu], observation: [:collection_object, :descriptor, :image, :otu, :source, :taxon_name], otu: [:asserted_distribution, :biological_association, :collection_object, :collecting_event, :content, :descriptor, :extract, :image, :loan, :observation, :source, :taxon_name ], person: [], source: [:asserted_distribution, :biological_association, :collecting_event, :collection_object, :content, :descriptor, :extract, :image, :observation, :otu, :taxon_name], taxon_name: [:asserted_distribution, :biological_association, :collection_object, :collecting_event, :image, :otu, :source ] }.freeze
- FILTER_QUERIES =
We could consider `.safe_constantize` to make this a f(n), but we'd have to have a list somewhere else anyways to further restrict allowed classes.
{ asserted_distribution_query: '::Queries::AssertedDistribution::Filter', biological_association_query: '::Queries::BiologicalAssociation::Filter', biological_associations_graph_query: '::Queries::BiologicalAssociationsGraph::Filter', collecting_event_query: '::Queries::CollectingEvent::Filter', collection_object_query: '::Queries::CollectionObject::Filter', content_query: '::Queries::Content::Filter', descriptor_query: '::Queries::Descriptor::Filter', extract_query: '::Queries::Extract::Filter', image_query: '::Queries::Image::Filter', loan_query: '::Queries::Loan::Filter', observation_query: '::Queries::Observation::Filter', otu_query: '::Queries::Otu::Filter', person_query: '::Queries::Person::Filter', source_query: '::Queries::Source::Filter', taxon_name_query: '::Queries::TaxonName::Filter', }.freeze
Instance Attribute Summary collapse
-
#api ⇒ Object
Boolean When true api_except_params is applied.
- #asserted_distribution_query ⇒ Query::AssertedDistributionn::Filter?
- #biological_association_query ⇒ Query::BiologicalAssociation::Filter?
- #biological_associations_graph_query ⇒ Query::BiologicalAssociationsGraph::Filter?
- #collecting_event_query ⇒ Query::CollectingEvent::Filter?
- #collection_object_query ⇒ Query::TaxonName::Filter?
- #content_query ⇒ Query::Content::Filter?
- #descriptor_query ⇒ Query::Descriptor::Filter?
- #extract_query ⇒ Query::Extract::Filter?
- #image_query ⇒ Query::Image::Filter?
- #loan_query ⇒ Query::Loan::Filter?
-
#object_global_id ⇒ Array
Locally these look like gid://taxon-works/Otu/1 Using a global id is equivalent to using <model>_id.
- #observation_query ⇒ Query::Observation::Filter?
- #otu_query ⇒ Query::Otu::Filter?
-
#params ⇒ Object
readonly
!! Using setters directly on query parameters will not alter this variable !! !! This is used strictly during the permission process of ActionController::Parameters !!.
- #person_query ⇒ Query::Person::Filter?
- #project_id ⇒ Array
-
#recent ⇒ Object
Boolean Applies an order on updated.
- #taxon_name_query ⇒ Query::TaxonName::Filter?
Attributes inherited from Queries::Query
Class Method Summary collapse
-
.annotator_params ⇒ Object
to be merged into included params.
-
.api_except_params ⇒ Object
Any params set here, and in corresponding subclasses will not be permitted when api: true is present.
- .api_excluded_params ⇒ Object
- .included_annotator_facets ⇒ Object
-
.inverted_subqueries ⇒ Hash
Only referenced in specs.
-
.params ⇒ Object
Array merges `[:a, []]` into [:a].
Instance Method Summary collapse
-
#all(nil_empty = false) ⇒ ActiveRecord::Relation
See /lib/queries/ARCHITECTURE.md for additional explanation.
- #all_and_clauses ⇒ ActiveRecord::Relation?
- #all_merge_clauses ⇒ Scope?
-
#and_clauses ⇒ Object
Defined in inheriting classes.
- #annotator_and_clauses ⇒ Object
- #annotator_merge_clauses ⇒ Object
-
#attribute_exact_facet(attribute = nil) ⇒ Object
params attribute [Symbol] a facet for use when params include `author`, and `exact_author` pattern combinations See queries/source/filter.rb for example use See /spec/lib/queries/source/filter_spec.rb !! Whitespace (e.g. tabs, newlines) is ignored when exact is used !!!.
-
#deep_permit(params) ⇒ Object
ActionController::Parameters.
-
#initialize(query_params) ⇒ Object
constructor
Hash.
-
#merge_clauses ⇒ Object
Defined in inheriting classes.
-
#model_id_facet ⇒ Object
Returns id= facet, automatically added to all queries.
- #object_global_id_facet ⇒ Object
-
#permitted_params(hsh) ⇒ Object
This method is a preprocessor that discovers, by finding the nested subqueries, which params should be permitted.
- #project_id_facet ⇒ Object
-
#set_nested_queries(params) ⇒ Object
True.
- #shared_and_clauses ⇒ Object
-
#subquery_vector(hsh) ⇒ Array of Symbol
Since queries nest linearly we don't need to recursion.
Methods inherited from Queries::Query
#alphabetic_strings, #alphanumeric_strings, base_name, #base_name, #base_query, #build_terms, #cached_facet, #end_wildcard, #levenshtein_distance, #match_ordered_wildcard_pieces_in_cached, #no_terms?, referenced_klass, #referenced_klass, #referenced_klass_except, #referenced_klass_union, #start_and_end_wildcard, #start_wildcard, #table, #wildcard_pieces
Constructor Details
#initialize(query_params) ⇒ Object
Returns Hash.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/queries/query/filter.rb', line 167 def initialize(query_params) # Reference to query_params, i.e. always permitted @api = boolean_param(query_params, :api) @recent = boolean_param(query_params, :recent) @object_global_id = query_params[:object_global_id] # !! This is the *only* place Current.project_id should be seen !! It's still not the best # way to implement this, but we use it to optimize the scope of sub/nested-queries efficiently. # Ideally we'd have a global class param that stores this that all Filters would have access to, # rather than an instance variable. @project_id = query_params[:project_id] || Current.project_id # After this point, if you started with ActionController::Parameters, # then all values have been explicitly permitted. if query_params.kind_of?(Hash) @params = query_params elsif query_params.kind_of?(ActionController::Parameters) @params = deep_permit(query_params).to_hash.deep_symbolize_keys elsif query_params.nil? @params = {} else raise TaxonWorks::Error, "can not initialize filter with #{query_params.class.name}" end set_identifier_params(params) set_nested_queries(params) set_user_dates(params) end |
Instance Attribute Details
#api ⇒ Object
Returns Boolean When true api_except_params is applied.
157 158 159 |
# File 'lib/queries/query/filter.rb', line 157 def api @api end |
#asserted_distribution_query ⇒ Query::AssertedDistributionn::Filter?
110 111 112 |
# File 'lib/queries/query/filter.rb', line 110 def asserted_distribution_query @asserted_distribution_query end |
#biological_association_query ⇒ Query::BiologicalAssociation::Filter?
113 114 115 |
# File 'lib/queries/query/filter.rb', line 113 def biological_association_query @biological_association_query end |
#biological_associations_graph_query ⇒ Query::BiologicalAssociationsGraph::Filter?
116 117 118 |
# File 'lib/queries/query/filter.rb', line 116 def biological_associations_graph_query @biological_associations_graph_query end |
#collecting_event_query ⇒ Query::CollectingEvent::Filter?
122 123 124 |
# File 'lib/queries/query/filter.rb', line 122 def collecting_event_query @collecting_event_query end |
#collection_object_query ⇒ Query::TaxonName::Filter?
119 120 121 |
# File 'lib/queries/query/filter.rb', line 119 def collection_object_query @collection_object_query end |
#content_query ⇒ Query::Content::Filter?
125 126 127 |
# File 'lib/queries/query/filter.rb', line 125 def content_query @content_query end |
#descriptor_query ⇒ Query::Descriptor::Filter?
128 129 130 |
# File 'lib/queries/query/filter.rb', line 128 def descriptor_query @descriptor_query end |
#extract_query ⇒ Query::Extract::Filter?
140 141 142 |
# File 'lib/queries/query/filter.rb', line 140 def extract_query @extract_query end |
#image_query ⇒ Query::Image::Filter?
131 132 133 |
# File 'lib/queries/query/filter.rb', line 131 def image_query @image_query end |
#loan_query ⇒ Query::Loan::Filter?
146 147 148 |
# File 'lib/queries/query/filter.rb', line 146 def loan_query @loan_query end |
#object_global_id ⇒ Array
Locally these look like gid://taxon-works/Otu/1 Using a global id is equivalent to using <model>_id. I.e. it simply restricts the filter to those matching Model#id.
!! If any global id model name does not match the current filter, then then facet is completely rejected.
105 106 107 |
# File 'lib/queries/query/filter.rb', line 105 def object_global_id @object_global_id end |
#observation_query ⇒ Query::Observation::Filter?
143 144 145 |
# File 'lib/queries/query/filter.rb', line 143 def observation_query @observation_query end |
#otu_query ⇒ Query::Otu::Filter?
137 138 139 |
# File 'lib/queries/query/filter.rb', line 137 def otu_query @otu_query end |
#params ⇒ Object (readonly)
!! Using setters directly on query parameters will not alter this variable !! !! This is used strictly during the permission process of ActionController::Parameters !!
164 165 166 |
# File 'lib/queries/query/filter.rb', line 164 def params @params end |
#person_query ⇒ Query::Person::Filter?
149 150 151 |
# File 'lib/queries/query/filter.rb', line 149 def person_query @person_query end |
#project_id ⇒ Array
92 93 94 |
# File 'lib/queries/query/filter.rb', line 92 def project_id @project_id end |
#recent ⇒ Object
Returns Boolean Applies an order on updated.
153 154 155 |
# File 'lib/queries/query/filter.rb', line 153 def recent @recent end |
#taxon_name_query ⇒ Query::TaxonName::Filter?
134 135 136 |
# File 'lib/queries/query/filter.rb', line 134 def taxon_name_query @taxon_name_query end |
Class Method Details
.annotator_params ⇒ Object
to be merged into included params
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/queries/query/filter.rb', line 246 def self.annotator_params h = nil if i = included_annotator_facets # Setup with the first a = i.shift h = a.params if !h.last.kind_of?(Hash) h << {} end c = h.last # Now do the rest i.each do |j| p = j.params if p.last.kind_of?(Hash) c.merge!(p.pop) end h = p + h end end h end |
.api_except_params ⇒ Object
Any params set here, and in corresponding subclasses will not be permitted when api: true is present
239 240 241 |
# File 'lib/queries/query/filter.rb', line 239 def self.api_except_params [] end |
.api_excluded_params ⇒ Object
273 274 275 276 277 |
# File 'lib/queries/query/filter.rb', line 273 def self.api_excluded_params [ # if there are things like created_by_id that we deem universally out they go here ] + api_except_params end |
.included_annotator_facets ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/queries/query/filter.rb', line 205 def self.included_annotator_facets f = [ ::Queries::Concerns::Users ] if referenced_klass.annotates? f.push ::Queries::Concerns::Polymorphic if self < ::Queries::Concerns::Polymorphic else # TODO There is room for an AlternateValue concern here f.push ::Queries::Concerns::Attributes if self < ::Queries::Concerns::Attributes f.push ::Queries::Concerns::Citations if self < ::Queries::Concerns::Citations f.push ::Queries::Concerns::Containable if self < ::Queries::Concerns::Containable f.push ::Queries::Concerns::DataAttributes if self < ::Queries::Concerns::DataAttributes f.push ::Queries::Concerns::DateRanges if self < ::Queries::Concerns::DateRanges f.push ::Queries::Concerns::Depictions if self < ::Queries::Concerns::Depictions f.push ::Queries::Concerns::Identifiers if self < ::Queries::Concerns::Identifiers f.push ::Queries::Concerns::Notes if self < ::Queries::Concerns::Notes f.push ::Queries::Concerns::Protocols if self < ::Queries::Concerns::Protocols f.push ::Queries::Concerns::Tags if self < ::Queries::Concerns::Tags end f end |
.inverted_subqueries ⇒ Hash
Returns only referenced in specs.
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/queries/query/filter.rb', line 55 def self.inverted_subqueries r = {} SUBQUERIES.each do |k,v| v.each do |m| if r[m] r[m].push k else r[m] = [k] end end end r end |
.params ⇒ Object
Returns Array merges `[:a, []]` into [:a].
231 232 233 234 235 |
# File 'lib/queries/query/filter.rb', line 231 def self.params a = self::PARAMS.dup b = a.pop.keys (a + b).uniq end |
Instance Method Details
#all(nil_empty = false) ⇒ ActiveRecord::Relation
See /lib/queries/ARCHITECTURE.md for additional explanation.
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 |
# File 'lib/queries/query/filter.rb', line 527 def all(nil_empty = false) # TODO: should turn off/on project_id here on nil empty? a = all_and_clauses b = all_merge_clauses return nil if nil_empty && a.nil? && b.nil? # !! Do not apply `.distinct here` q = nil if a && b q = b.where(a) elsif a q = referenced_klass.where(a) elsif b q = b else q = referenced_klass.all end if recent q = referenced_klass.from(q.all, table.name).order(updated_at: :desc) end q end |
#all_and_clauses ⇒ ActiveRecord::Relation?
492 493 494 495 496 497 498 499 500 501 502 |
# File 'lib/queries/query/filter.rb', line 492 def all_and_clauses clauses = and_clauses + annotator_and_clauses + shared_and_clauses clauses.compact! return nil if clauses.empty? a = clauses.shift clauses.each do |b| a = a.and(b) end a end |
#all_merge_clauses ⇒ Scope?
510 511 512 513 514 515 516 517 518 519 520 |
# File 'lib/queries/query/filter.rb', line 510 def all_merge_clauses clauses = merge_clauses + annotator_merge_clauses clauses.compact! return nil if clauses.empty? a = clauses.shift clauses.each do |b| a = a.merge(b) end a end |
#and_clauses ⇒ Object
Defined in inheriting classes
487 488 489 |
# File 'lib/queries/query/filter.rb', line 487 def and_clauses [] end |
#annotator_and_clauses ⇒ Object
464 465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/queries/query/filter.rb', line 464 def annotator_and_clauses a = [] self.class.included_annotator_facets.each do |c| if c.respond_to?(:and_clauses) c.and_clauses.each do |f| if v = send(f) a.push v end end end end a end |
#annotator_merge_clauses ⇒ Object
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 |
# File 'lib/queries/query/filter.rb', line 446 def annotator_merge_clauses a = [] # !! Interesting `.compact` removes #<ActiveRecord::Relation []>, # so patterns that us collect.flatten.compact return empty, # `.present?` fails as well, so verbose loops here self.class.included_annotator_facets.each do |c| if c.respond_to?(:merge_clauses) c.merge_clauses.each do |f| if v = send(f) a.push v end end end end a end |
#attribute_exact_facet(attribute = nil) ⇒ Object
params attribute [Symbol]
a facet for use when params include `author`, and `exact_author` pattern combinations
See queries/source/filter.rb for example use
See /spec/lib/queries/source/filter_spec.rb
!! Whitespace (e.g. tabs, newlines) is ignored when exact is used !!!
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
# File 'lib/queries/query/filter.rb', line 427 def attribute_exact_facet(attribute = nil) a = attribute.to_sym return nil if send(a).blank? if send("exact_#{a}".to_sym) # TODO: Think we need to handle ' and ' v = send(a) v.gsub!(/\s+/, ' ') v = ::Regexp.escape(v) v.gsub!(/\\\s+/, '\s*') # spaces are escaped, but we need to expand them in case we dont' get them caught v = '^\s*' + v + '\s*$' table[a].matches_regexp(v) else table[a].matches('%' + send(a).strip.gsub(/\s+/, '%') + '%') end end |
#deep_permit(params) ⇒ Object
Returns ActionController::Parameters.
369 370 371 |
# File 'lib/queries/query/filter.rb', line 369 def deep_permit(params) p = params.permit( permitted_params(params.to_unsafe_hash)) end |
#merge_clauses ⇒ Object
Defined in inheriting classes
505 506 507 |
# File 'lib/queries/query/filter.rb', line 505 def merge_clauses [] end |
#model_id_facet ⇒ Object
Returns id= facet, automatically added to all queries. Over-ridden in some base classes.
398 399 400 401 402 |
# File 'lib/queries/query/filter.rb', line 398 def model_id_facet m = (base_name + '_id').to_sym return nil if send(m).empty? table[:id].eq_any(send(m)) end |
#object_global_id_facet ⇒ Object
409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/queries/query/filter.rb', line 409 def object_global_id_facet return nil if object_global_id.empty? ids = [] object_global_id.each do |i| g = GlobalID.parse(i) # If any global_ids do not reference this Class, abort return nil unless g.model_class.base_class.name == referenced_klass.name ids.push g.model_id end table[:id].eq_any(ids) end |
#permitted_params(hsh) ⇒ Object
This method is a preprocessor that discovers, by finding the nested subqueries, which params should be permitted. It is used to build a permitable profile of parameters.
That profile is then used in the actual .permit() call.
An alternate solution, first tried, is to permit the params directly during inspection for subquries. This also would work, however there are some nice benefits to having a profile of the allowed params available as an Array, for example we can use it for API documentation a little easier(?!).
In essence what we needed was for ActionController::Parameters to be able to accumulate (remember) all permitted params (not just their actual data) over multiple .permit() calls. If we had that, then we could do something like params.permitted_params => Array after multiple calls like params.permit(:a), params.permit(:b).
any parameter set for the query.
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/queries/query/filter.rb', line 300 def permitted_params(hsh) h = self.class::PARAMS.deep_dup if !h.last.kind_of?(Hash) h << {} end c = h.last # a {} if n = self.class.annotator_params c.merge!(n.pop) h = n + h end b = subquery_vector(hsh) parent = self.class while !b.empty? a = b.shift next unless SUBQUERIES[parent.base_name.to_sym].include?( a.to_s.gsub('_query', '').to_sym ) q = FILTER_QUERIES[a].safe_constantize p = q::PARAMS.deep_dup if !p.last.kind_of?(Hash) p << {} end if n = q.annotator_params p.last.merge!(n.pop) p = n + p end c[a] = p c = p.last parent = q end if api self.class.api_excluded_params.each do |a| h.delete_if{|k,v| a == k} h.last.delete_if{|k,v| a == k } end end h end |
#project_id_facet ⇒ Object
404 405 406 407 |
# File 'lib/queries/query/filter.rb', line 404 def project_id_facet return nil if project_id.empty? table[:project_id].eq_any(project_id) end |
#set_nested_queries(params) ⇒ Object
Returns True.
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/queries/query/filter.rb', line 376 def set_nested_queries(params) if n = params.select{|k, p| k.to_s =~ /_query/ } return nil if n.keys.count != 1 # can't have multiple nested queries inside one level query_name = n.first.first return nil unless SUBQUERIES[base_name.to_sym].include?( query_name.to_s.gsub('_query', '').to_sym ) # must be registered query_params = n.first.last q = FILTER_QUERIES[query_name].safe_constantize.new(query_params) # assign to @<model>_query v = send("#{query_name}=".to_sym, q) end true end |
#shared_and_clauses ⇒ Object
478 479 480 481 482 483 484 |
# File 'lib/queries/query/filter.rb', line 478 def shared_and_clauses [ project_id_facet, model_id_facet, object_global_id_facet, ] end |
#subquery_vector(hsh) ⇒ Array of Symbol
Since queries nest linearly we don't need to recursion.
357 358 359 360 361 362 363 364 365 |
# File 'lib/queries/query/filter.rb', line 357 def subquery_vector(hsh) result = [] while !hsh.keys.select{|k| k =~ /_query/}.empty? a = hsh.keys.select{|k| k =~ /_query/} result += a hsh = hsh[a.first] end result.map(&:to_sym) end |