Class: Queries::Query::Filter

Inherits:
Queries::Query show all
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.

!! When adding a new query tests do some linting of parameters, constants etc. Run them early and often !!

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…] ].

!! If you add a ‘def <model>_query_facet“ to a filter you will get warnings if that !! model is not referencened in this constant.

{
  asserted_distribution: [:source, :otu, :biological_association, :taxon_name, :dwc_occurrence],
  biological_association: [:source, :collecting_event, :otu, :collection_object, :field_occurrence, :taxon_name, :asserted_distribution], # :field_occurrence
  biological_associations_graph: [:biological_association, :source],
  collecting_event: [:source, :collection_object, :field_occurrence, :biological_association, :otu, :image, :taxon_name, :dwc_occurrence],
  collection_object: [:source, :loan, :otu, :taxon_name, :collecting_event, :biological_association, :extract, :image, :observation, :dwc_occurrence],
  content: [:source, :otu, :taxon_name, :image],
  controlled_vocabulary_term: [:data_attribute],
  data_attribute: [:collection_object, :collecting_event, :field_occurrence, :taxon_name, :otu],
  dwc_occurrence: [:asserted_distribution, :collection_object, :collecting_event, :field_occurrence],
  depiction: [:image],
  descriptor: [:source, :observation, :otu],
  extract: [:source, :otu, :collection_object, :observation],
  field_occurrence: [:collecting_event, :otu, :biological_association, :dwc_occurrence, :taxon_name], # [:source, :otu, :collecting_event, :biological_association, :observation, :taxon_name, :extract],
  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, :dwc_occurrence, :field_occurrence, :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],
  sound: [],
  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',
  controlled_vocabulary_term_query: '::Queries::ControlledVocabularyTerm::Filter',
  data_attribute_query: '::Queries::DataAttribute::Filter',
  depiction_query: '::Queries::Depiction::Filter',
  descriptor_query: '::Queries::Descriptor::Filter',
  document_query: '::Queries::Document::Filter',
  dwc_occurrence_query: '::Queries::DwcOccurrence::Filter',
  extract_query: '::Queries::Extract::Filter',
  field_occurrence_query: '::Queries::FieldOccurrence::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',
  sound_query: '::Queries::Sound::Filter',
  source_query: '::Queries::Source::Filter',
  taxon_name_query: '::Queries::TaxonName::Filter',
}.freeze

Instance Attribute Summary collapse

Attributes inherited from Queries::Query

#query_string, #terms

Class Method Summary collapse

Instance Method Summary collapse

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_intersection, #referenced_klass_union, #start_and_end_wildcard, #start_wildcard, #table, #wildcard_pieces

Constructor Details

#initialize(query_params) ⇒ Filter

Returns a new instance of Filter.



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/queries/query/filter.rb', line 262

def initialize(query_params)

  # Reference to query_params, i.e. always permitted
  @api = boolean_param(query_params, :api)

  @recent = boolean_param(query_params, :recent)
  @recent_target = query_params[:recent_target]

  @object_global_id = query_params[:object_global_id]

  @venn = query_params[:venn]
  @venn_mode = query_params[:venn_mode]

  # !! 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 = case
                when query_params[:project_id] == false # !! Only internal should pass this, therefor no type conversions
                  nil
                when query_params[:project_id].blank?
                  Current.project_id
                else
                  query_params[:project_id]
                end

  @paginate = boolean_param(query_params, :paginate)
  @per = query_params[:per]
  @page = query_params[:page]

  @order_by = query_params[:order_by]

  # 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

#apiObject

Returns Boolean When true api_except_params is applied and other restrictions are placed:

- :venn param is ignored
- is_public=true is applied.

Returns:

  • Boolean When true api_except_params is applied and other restrictions are placed:

    - :venn param is ignored
    - is_public=true is applied
    


228
229
230
# File 'lib/queries/query/filter.rb', line 228

def api
  @api
end

#asserted_distribution_queryQuery::AssertedDistributionn::Filter?

Returns:

  • (Query::AssertedDistributionn::Filter, nil)


153
154
155
# File 'lib/queries/query/filter.rb', line 153

def asserted_distribution_query
  @asserted_distribution_query
end

#biological_association_queryQuery::BiologicalAssociation::Filter?

Returns:

  • (Query::BiologicalAssociation::Filter, nil)


156
157
158
# File 'lib/queries/query/filter.rb', line 156

def biological_association_query
  @biological_association_query
end

#biological_associations_graph_queryQuery::BiologicalAssociationsGraph::Filter?

Returns:

  • (Query::BiologicalAssociationsGraph::Filter, nil)


159
160
161
# File 'lib/queries/query/filter.rb', line 159

def biological_associations_graph_query
  @biological_associations_graph_query
end

#collecting_event_queryQuery::CollectingEvent::Filter?

Returns:

  • (Query::CollectingEvent::Filter, nil)


168
169
170
# File 'lib/queries/query/filter.rb', line 168

def collecting_event_query
  @collecting_event_query
end

#collection_object_queryQuery::TaxonName::Filter?

Returns:

  • (Query::TaxonName::Filter, nil)


165
166
167
# File 'lib/queries/query/filter.rb', line 165

def collection_object_query
  @collection_object_query
end

#content_queryQuery::Content::Filter?

Returns:

  • (Query::Content::Filter, nil)


171
172
173
# File 'lib/queries/query/filter.rb', line 171

def content_query
  @content_query
end

#controlled_vocabulary_term_queryQuery::ControlledVocabularyTerm::Filter?

Returns:

  • (Query::ControlledVocabularyTerm::Filter, nil)


162
163
164
# File 'lib/queries/query/filter.rb', line 162

def controlled_vocabulary_term_query
  @controlled_vocabulary_term_query
end

#data_attribute_queryQuery::DataAttribute::Filter?

Returns:

  • (Query::DataAttribute::Filter, nil)


174
175
176
# File 'lib/queries/query/filter.rb', line 174

def data_attribute_query
  @data_attribute_query
end

#depiction_queryQuery::Depiction::Filter?

Returns:

  • (Query::Depiction::Filter, nil)


180
181
182
# File 'lib/queries/query/filter.rb', line 180

def depiction_query
  @depiction_query
end

#descriptor_queryQuery::Descriptor::Filter?

Returns:

  • (Query::Descriptor::Filter, nil)


177
178
179
# File 'lib/queries/query/filter.rb', line 177

def descriptor_query
  @descriptor_query
end

#document_queryQuery::Document::Filter?

Returns:

  • (Query::Document::Filter, nil)


183
184
185
# File 'lib/queries/query/filter.rb', line 183

def document_query
  @document_query
end

#dwc_occurrence_queryQuery::DwcOccurrence::Filter?

Returns:

  • (Query::DwcOccurrence::Filter, nil)


186
187
188
# File 'lib/queries/query/filter.rb', line 186

def dwc_occurrence_query
  @dwc_occurrence_query
end

#extract_queryQuery::Extract::Filter?

Returns:

  • (Query::Extract::Filter, nil)


201
202
203
# File 'lib/queries/query/filter.rb', line 201

def extract_query
  @extract_query
end

#field_occurrence_queryQuery::TaxonName::Filter?

Returns:

  • (Query::TaxonName::Filter, nil)


189
190
191
# File 'lib/queries/query/filter.rb', line 189

def field_occurrence_query
  @field_occurrence_query
end

#image_queryQuery::Image::Filter?

Returns:

  • (Query::Image::Filter, nil)


192
193
194
# File 'lib/queries/query/filter.rb', line 192

def image_query
  @image_query
end

#loan_queryQuery::Loan::Filter?

Returns:

  • (Query::Loan::Filter, nil)


207
208
209
# File 'lib/queries/query/filter.rb', line 207

def loan_query
  @loan_query
end

#object_global_idArray

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.

Returns:

  • (Array)


148
149
150
# File 'lib/queries/query/filter.rb', line 148

def object_global_id
  @object_global_id
end

#observation_queryQuery::Observation::Filter?

Returns:

  • (Query::Observation::Filter, nil)


204
205
206
# File 'lib/queries/query/filter.rb', line 204

def observation_query
  @observation_query
end

#order_byObject

Supported values:

* `match_identifiers` - sort by the order of the identifiers in provided

!! Does not sub-sort

Parameters:

  • order_by (String, Symbol)

    the kind of sort to apply.

Returns:

  • symbol must match a existing parameter name (used to check if values provided)



251
252
253
# File 'lib/queries/query/filter.rb', line 251

def order_by
  @order_by
end

#otu_queryQuery::Otu::Filter?

Returns:

  • (Query::Otu::Filter, nil)


198
199
200
# File 'lib/queries/query/filter.rb', line 198

def otu_query
  @otu_query
end

#pageObject

Returns Integer, nil required if paginate == true.

Returns:

  • Integer, nil required if paginate == true



130
131
132
# File 'lib/queries/query/filter.rb', line 130

def page
  @page
end

#paginateObject

Apply pagination within Filter scope

true - apply per and page
false, nil - ignored


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

def paginate
  @paginate
end

#paramsObject (readonly)

!! Using setters directly on query parameters will not alter this variable !! !! This is used strictly during the permission process of ActionController::Parameters !!

Returns:

  • Hash the parsed/permitted params

    that were used to on initialize() only!!
    


258
259
260
# File 'lib/queries/query/filter.rb', line 258

def params
  @params
end

#perObject

Returns Integer, nil paginate must equal true page must be !nil?.

Returns:

  • Integer, nil paginate must equal true page must be !nil?



135
136
137
# File 'lib/queries/query/filter.rb', line 135

def per
  @per
end

#person_queryQuery::Person::Filter?

Returns:

  • (Query::Person::Filter, nil)


210
211
212
# File 'lib/queries/query/filter.rb', line 210

def person_query
  @person_query
end

#project_idArray

Parameters:

  • project_id (Array, Integer, false)

    !! when passed false then Current.project_id is not applied, i.e. the result will be [] !! use the false pattern only for internal calls (e.g. rewriting

Returns:

  • (Array)


121
122
123
# File 'lib/queries/query/filter.rb', line 121

def project_id
  @project_id
end

#recentObject

Returns Boolean Applies an order on updated.

Returns:

  • Boolean Applies an order on updated.



217
218
219
# File 'lib/queries/query/filter.rb', line 217

def recent
  @recent
end

#recent_targetObject

Returns symbol :created_at, :updated_at defaults to :updated_at if blank.

Returns:

  • symbol :created_at, :updated_at defaults to :updated_at if blank



221
222
223
# File 'lib/queries/query/filter.rb', line 221

def recent_target
  @recent_target
end

#sound_queryQuery::Sound::Filter?

Returns:

  • (Query::Sound::Filter, nil)


213
214
215
# File 'lib/queries/query/filter.rb', line 213

def sound_query
  @sound_query
end

#taxon_name_queryQuery::TaxonName::Filter?

Returns:

  • (Query::TaxonName::Filter, nil)


195
196
197
# File 'lib/queries/query/filter.rb', line 195

def taxon_name_query
  @taxon_name_query
end

#vennObject

Returns String A JSON full URL containing the base string for a query.

Returns:

  • String A JSON full URL containing the base string for a query



232
233
234
# File 'lib/queries/query/filter.rb', line 232

def venn
  @venn
end

#venn_modeObject

Returns Symbol one of :a, :ab, :b defaults to :ab

:a :ab  :b

( A ( B ) C ).

Returns:

  • Symbol one of :a, :ab, :b defaults to :ab

    :a :ab  :b
    

    ( A ( B ) C )



238
239
240
# File 'lib/queries/query/filter.rb', line 238

def venn_mode
  @venn_mode
end

Class Method Details

.annotator_paramsObject

to be merged into included params

Returns:

  • Array, nil a [:a, :b, []] formatted Array



425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/queries/query/filter.rb', line 425

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_paramsObject

Any params set here, and in corresponding subclasses will not be permitted when api: true is present



418
419
420
# File 'lib/queries/query/filter.rb', line 418

def self.api_except_params
  [:venn, :venn_mode]
end

.api_excluded_paramsObject



452
453
454
455
456
# File 'lib/queries/query/filter.rb', line 452

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

.base_filter(params) ⇒ Filter?

Returns the class of filter that is referenced at the base of this parameter set.

Returns:

  • (Filter, nil)

    the class of filter that is referenced at the base of this parameter set



347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/queries/query/filter.rb', line 347

def self.base_filter(params)
  if s = base_query_name(params)
    t = s.gsub('_query', '').to_sym

    if SUBQUERIES.include?(t)
      k = t.to_s.camelcase
      return "Queries::#{k}::Filter".constantize
    else
      return nil
    end
  else
    nil
  end
end

.base_query_name(params) ⇒ Object



371
372
373
# File 'lib/queries/query/filter.rb', line 371

def self.base_query_name(params)
  params.keys.select{|s| s =~ /\A.+_query\z/}.first
end

.base_query_to_h(params) ⇒ Object

base_query, as params, like ‘{}` This sanitizes params.

Returns:

  • the params use to instantiate the full



378
379
380
# File 'lib/queries/query/filter.rb', line 378

def self.base_query_to_h(params)
  return { base_query_name(params) => instantiated_base_filter(params).params }
end

.included_annotator_facetsObject



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/queries/query/filter.rb', line 382

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::Confidences if self < ::Queries::Concerns::Confidences
    f.push ::Queries::Concerns::Containable if self < ::Queries::Concerns::Containable
    f.push ::Queries::Concerns::Conveyances if self < ::Queries::Concerns::Conveyances
    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

.instantiated_base_filter(params) ⇒ Object

An instiatied filter, with params set, for params with patterns like ‘otu_query={}`



363
364
365
366
367
368
369
# File 'lib/queries/query/filter.rb', line 363

def self.instantiated_base_filter(params)
  if s = base_filter(params)
    s.new(params[base_query_name(params)])
  else
    nil
  end
end

.inverted_subqueriesHash

Returns only referenced in specs.

Returns:

  • (Hash)

    only referenced in specs



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/queries/query/filter.rb', line 75

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

.paramsObject

Returns Array merges ‘[:a, []]` into [:a].

Returns:

  • Array merges ‘[:a, []]` into [:a]



410
411
412
413
414
# File 'lib/queries/query/filter.rb', line 410

def self.params
  a = self::PARAMS.dup
  b = a.pop.keys
  (a + b).uniq
end

.query_nameObject



65
66
67
# File 'lib/queries/query/filter.rb', line 65

def self.query_name
  base_name + '_query'
end

Instance Method Details

#all(nil_empty = false) ⇒ ActiveRecord::Relation

See /lib/queries/ARCHITECTURE.md for additional explanation.

TODO: consider “true” for default, changing to false on controller Filter calls

Parameters:

  • nil_empty (Boolean) (defaults to: false)

    If true then if there are no clauses return nil not .all

Returns:

  • (ActiveRecord::Relation)


764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
# File 'lib/queries/query/filter.rb', line 764

def all(nil_empty = false)

  # TODO: should turn off/on project_id here on nil empty?

  a = all_and_clauses
  b = all_merge_clauses

  # TODO: do not consider project_id alone a parameter on nil_empty

  # Limited use within the UI because project_id is set
  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 venn_mode && venn && !api
    q = apply_venn(q)
  end

  # TODO: collides with recent, and needs isolation/generic application
  # probably through native .order() use.
  # Order in general likely belongs outside the scope of filters, but
  # see this param use, where we depend on the incoming values
  #
  # See spec/lib/queries/otu/filter_spec.rb for tests

  # See lib/queries/concerns/identifiers.rb
  if order_by
    q = match_identifier_order_by(q)
  end

  if recent
    q = referenced_klass.from(q.all, table.name).order(recent_target => :desc)
  end

  if api
    if referenced_klass.column_names.include?('is_public')
      q = q.where(is_public: [nil, true])
    end
  end

  if paginate
    if order_by
      q = q.page(page).per(per)
    else
      q = q.order(:id).page(page).per(per)
    end
  end

  # TODO: canonically address whether or not to use `.distinct` at this point, we should be able to, however
  # some incoming queries may have joins/group/etc. alone?! I.e. why can't we?

  q
end

#all_and_clausesActiveRecord::Relation?

Returns:

  • (ActiveRecord::Relation, nil)


680
681
682
683
684
685
686
687
688
689
# File 'lib/queries/query/filter.rb', line 680

def all_and_clauses
  clauses = target_and_clauses
  return nil if clauses.empty?

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

#all_merge_clausesScope?

Of interest, the previous native ‘merge()` (and `and()“) make things complicated:

For example a.merge(b) != b.merge(a) in some cases. This is because certain clauses are tossed without warning (notably ‘.from()`). See:

We presently use SQL with INTERSECTION to combine facets.

Returns:

  • (Scope, nil)


711
712
713
714
715
716
717
718
719
720
721
722
723
724
# File 'lib/queries/query/filter.rb', line 711

def all_merge_clauses
  clauses = merge_clauses + annotator_merge_clauses
  clauses.compact!

  return nil if clauses.empty?

  # TODO: consider whether to implement this.
  # It should be safe, except, possibly for aggregate based queries
  # that include custom attributes, would these get cleared.
  # We could requier that at this level they are wrapped in a From etc.
  # a = clauses.collect{|q| q.unscope(:select).select(:id) }

  referenced_klass_intersection(clauses)
end

#and_clausesObject

Defined in inheriting classes



675
676
677
# File 'lib/queries/query/filter.rb', line 675

def and_clauses
  []
end

#annotator_and_clausesObject



652
653
654
655
656
657
658
659
660
661
662
663
664
# File 'lib/queries/query/filter.rb', line 652

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_clausesObject



633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
# File 'lib/queries/query/filter.rb', line 633

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)
      next if c.name == 'Queries::Concerns::Identifiers' && no_identifier_clauses
      c.merge_clauses.each do |f|
        if v = send(f)
          a.push v
        end
      end
    end
  end
  a
end

#apply_venn(query) ⇒ Object



726
727
728
# File 'lib/queries/query/filter.rb', line 726

def apply_venn(query)
  Queries.venn(query, venn_query.all, venn_mode)
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 !!!


614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# File 'lib/queries/query/filter.rb', line 614

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.

Returns:

  • ActionController::Parameters



554
555
556
# File 'lib/queries/query/filter.rb', line 554

def deep_permit(params)
  p = params.permit( permitted_params(params.to_unsafe_hash))
end

#merge_clausesObject

Defined in inheriting classes



696
697
698
# File 'lib/queries/query/filter.rb', line 696

def merge_clauses
  []
end

#model_id_facetObject

Returns id= facet, automatically added to all queries. Over-ridden in some base classes.



585
586
587
588
589
# File 'lib/queries/query/filter.rb', line 585

def model_id_facet
  m = (base_name + '_id').to_sym
  return nil if send(m).empty?
  table[:id].in(send(m))
end

#object_global_id_facetObject



596
597
598
599
600
601
602
603
604
605
606
607
# File 'lib/queries/query/filter.rb', line 596

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].in(ids)
end

#only_project?Boolean

Returns Boolean true - the only param pasted is ‘project_id` !! Note that this is the default for all queries, it is set on initialize false - there are no params at ALL or at least one that is not `project_id`, and project_id != false.

Returns:

  • (Boolean)

    Boolean true - the only param pasted is ‘project_id` !! Note that this is the default for all queries, it is set on initialize false - there are no params at ALL or at least one that is not `project_id`, and project_id != false



752
753
754
# File 'lib/queries/query/filter.rb', line 752

def only_project?
  (project_id_facet && target_and_clauses.size == 1 && all_merge_clauses.nil?) ? true : false
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 subqueries. 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.

Returns:

  • Array like [:a,:b, :c, []]



479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'lib/queries/query/filter.rb', line 479

def permitted_params(hsh)
  h = self.class::PARAMS.deep_dup
  h.unshift(:per)
  h.unshift(:page)
  h.unshift(:paginate)

  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

    p.unshift(:per)
    p.unshift(:page)
    p.unshift(:paginate)

    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

#process_url_into_params(url) ⇒ Object



340
341
342
# File 'lib/queries/query/filter.rb', line 340

def process_url_into_params(url)
  Rack::Utils.parse_nested_query(url)
end

#project_id_facetObject



591
592
593
594
# File 'lib/queries/query/filter.rb', line 591

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

#query_nameObject



69
70
71
# File 'lib/queries/query/filter.rb', line 69

def query_name
  self.class.query_name
end

#set_nested_queries(params) ⇒ Object

TODO: when a nesting problem is found we need to flag the query as invalid

Returns:

  • True



562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# File 'lib/queries/query/filter.rb', line 562

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 !!! This lets us eliminate infinite loops at the cost of expressiveness?!

    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_clausesObject



666
667
668
669
670
671
672
# File 'lib/queries/query/filter.rb', line 666

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.

Returns:

  • (Array of Symbol)

    all queries, in nested order



542
543
544
545
546
547
548
549
550
# File 'lib/queries/query/filter.rb', line 542

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

#target_and_clausesObject



691
692
693
# File 'lib/queries/query/filter.rb', line 691

def target_and_clauses
  [and_clauses + annotator_and_clauses + shared_and_clauses].flatten.compact
end

#venn_queryObject



730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
# File 'lib/queries/query/filter.rb', line 730

def venn_query
  u = ::Addressable::URI.parse(venn).query
  # Brackets may be multi-encoded
  t = nil
  i = 0
  max = 10
  while t != u && i < max
    t = u
    u = Addressable::URI.unencode(t)
    i += 1
  end

  p = ::Rack::Utils.parse_nested_query(u) # nested supports brackets

  a = ActionController::Parameters.new(p)

  self.class.new(a)
end