Class: Queries::TaxonNameRelationship::Filter

Inherits:
Query
  • Object
show all
Defined in:
lib/queries/taxon_name_relationship/filter.rb

Instance Attribute Summary collapse

Attributes inherited from Query

#dynamic_limit, #options, #project_id, #query_string, #terms

Instance Method Summary collapse

Methods inherited from Query

#alphabetic_strings, #attribute_exact_facet, #autocomplete, #autocomplete_cached, #autocomplete_cached_wildcard_anywhere, #autocomplete_common_name_exact, #autocomplete_common_name_like, #autocomplete_exact_id, #autocomplete_exactly_named, #autocomplete_named, #autocomplete_ordered_wildcard_pieces_in_cached, #build_terms, #cached, #combine_or_clauses, #common_name_name, #common_name_table, #common_name_wild_pieces, #end_wildcard, #exactly_named, #fragments, #integers, #levenshtein_distance, #match_ordered_wildcard_pieces_in_cached, #match_wildcard_end_in_cached, #match_wildcard_in_cached, #named, #no_terms?, #only_ids, #only_integers?, #parent, #parent_child_join, #parent_child_where, #pieces, #result, #scope, #start_and_end_wildcard, #start_wildcard, #wildcard_pieces, #wildcard_wrapped_integers, #wildcard_wrapped_years, #with_cached, #with_cached_like, #with_id, #with_project_id, #year_letter, #years

Constructor Details

#initialize(params) ⇒ Filter

Returns a new instance of Filter.

Parameters:

  • params (Params)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/queries/taxon_name_relationship/filter.rb', line 40

def initialize(params)
  @taxon_name_id = params[:taxon_name_id]

  @subject_taxon_name_id = params[:subject_taxon_name_id]
  @object_taxon_name_id = params[:object_taxon_name_id]

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

  @taxon_name_relationship_type = [params[:taxon_name_relationship_type]].flatten.compact
  @taxon_name_relationship_set = [params[:taxon_name_relationship_set]].flatten.compact

  @project_id = params[:project_id]
end

Instance Attribute Details

#as_objectObject

Parameters:

  • as_object (Boolean, nil)

    if taxon_name_id and true then treat as subject



11
12
13
# File 'lib/queries/taxon_name_relationship/filter.rb', line 11

def as_object
  @as_object
end

#as_subjectObject

Parameters:

  • as_subject (Boolean, nil)

    if taxon_name_id and true then treat as subject



15
16
17
# File 'lib/queries/taxon_name_relationship/filter.rb', line 15

def as_subject
  @as_subject
end

#object_taxon_name_idObject

Match all relationships where object is this ID



29
30
31
# File 'lib/queries/taxon_name_relationship/filter.rb', line 29

def object_taxon_name_id
  @object_taxon_name_id
end

#subject_taxon_name_idObject

Match all relationships where subject is this ID



24
25
26
# File 'lib/queries/taxon_name_relationship/filter.rb', line 24

def subject_taxon_name_id
  @subject_taxon_name_id
end

#taxon_name_idObject

Parameters:

  • taxon_name_id (String, Array, nil)

    Match all relationships where either subject OR object is taxon_name_id(s)



7
8
9
# File 'lib/queries/taxon_name_relationship/filter.rb', line 7

def taxon_name_id
  @taxon_name_id
end

#taxon_name_relationship_setObject

See corresponding constants in config/intialize/constants/taxon_name_relationships.rb

Parameters:

  • taxon_name_relationship_set (String, Array, nil)

    one or more of:

    'status',
    'synonym',
    'classification'
    


37
38
39
# File 'lib/queries/taxon_name_relationship/filter.rb', line 37

def taxon_name_relationship_set
  @taxon_name_relationship_set
end

#taxon_name_relationship_typeObject

Parameters:

  • taxon_name_relationship_type (String, Array, nil)

    the full class name like 'TaxonNameRelationship::..etc.', or an Array of them



19
20
21
# File 'lib/queries/taxon_name_relationship/filter.rb', line 19

def taxon_name_relationship_type
  @taxon_name_relationship_type
end

Instance Method Details

#allActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/queries/taxon_name_relationship/filter.rb', line 136

def all
  a = and_clauses
  # b = merge_clauses

  q = nil 
  if a
    q = ::TaxonNameRelationship.where(a)
  else
    q = ::TaxonNameRelationship.all
  end

  q = q.where(project_id: project_id) if project_id
  q
end

#and_clausesObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/queries/taxon_name_relationship/filter.rb', line 115

def and_clauses
  clauses = []

  clauses += [
    taxon_name_relationship_type_facet,
    taxon_name_relationship_set_facet,
    taxon_name_id_facet,
    as_subject_facet,
    as_object_facet,
  ].compact

  return nil if clauses.empty?

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

#as_object_facetActiveRecord::Relation?

Returns:

  • (ActiveRecord::Relation, nil)


110
111
112
113
# File 'lib/queries/taxon_name_relationship/filter.rb', line 110

def as_object_facet
  return nil if object_taxon_name_id.empty?
  table[:object_taxon_name_id].eq_any(object_taxon_name_id)
end

#as_subject_facetActiveRecord::Relation?

Returns:

  • (ActiveRecord::Relation, nil)


104
105
106
107
# File 'lib/queries/taxon_name_relationship/filter.rb', line 104

def as_subject_facet
  return nil if subject_taxon_name_id.empty?
  table[:subject_taxon_name_id].eq_any(subject_taxon_name_id)
end

#relationship_typesArray

Returns:

  • (Array)


73
74
75
76
77
78
79
80
81
82
83
# File 'lib/queries/taxon_name_relationship/filter.rb', line 73

def relationship_types
  return [] unless taxon_name_relationship_set && taxon_name_relationship_set.any?
  t = []
  # TODO of_types =>
  taxon_name_relationship_set.each do |i|
    t += STATUS_TAXON_NAME_RELATIONSHIP_NAMES if i == 'status'
    t += TAXON_NAME_RELATIONSHIP_NAMES_SYNONYM if i == 'synonym'
    t += TAXON_NAME_RELATIONSHIP_NAMES_CLASSIFICATION if i == 'classification'
  end
  t
end

#tableArel::Table

Returns:

  • (Arel::Table)


56
57
58
# File 'lib/queries/taxon_name_relationship/filter.rb', line 56

def table
  ::TaxonNameRelationship.arel_table
end

#taxon_name_id_facetObject



95
96
97
98
99
100
101
# File 'lib/queries/taxon_name_relationship/filter.rb', line 95

def taxon_name_id_facet
  return nil if taxon_name_id.empty?
  table[:subject_taxon_name_id].eq_any(taxon_name_id)
    .or(
      table[:object_taxon_name_id].eq_any(taxon_name_id)
    )
end

#taxon_name_relationship_set_facetObject



85
86
87
88
# File 'lib/queries/taxon_name_relationship/filter.rb', line 85

def taxon_name_relationship_set_facet
  return nil unless taxon_name_relationship_set && taxon_name_relationship_set.any?
  table[:type].eq_any(relationship_types)
end

#taxon_name_relationship_type_facetObject



90
91
92
93
# File 'lib/queries/taxon_name_relationship/filter.rb', line 90

def taxon_name_relationship_type_facet
  return nil unless taxon_name_relationship_type && taxon_name_relationship_type.any?
  table[:type].eq_any(taxon_name_relationship_type)
end