Class: Queries::Role::Filter

Inherits:
Query
  • Object
show all
Includes:
Concerns::Polymorphic, Concerns::Users
Defined in:
lib/queries/role/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, #alphanumeric_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, #string_fragments, #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.



16
17
18
19
20
21
22
# File 'lib/queries/role/filter.rb', line 16

def initialize(params)
  @role_type = params[:role_type]
  @object_global_id = params[:object_global_id]

  set_user_dates(params)
  set_polymorphic_ids(params)
end

Instance Attribute Details

#role_typeArray

Parameters:

  • role (Array, String)

    A valid role name like 'Author' or array like ['TaxonDeterminer', 'SourceEditor']. See Role descendants.

Returns:

  • (Array)


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

def role_type
  @role_type
end

Instance Method Details

#allActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/queries/role/filter.rb', line 87

def all
  a = and_clauses
  b = merge_clauses

  if a && b
    b.where(a).distinct
  elsif a
    ::Role.where(a).distinct
  elsif b
    b.distinct
  else
    ::Role.all
  end
end

#and_clausesActiveRecord::Relation?

Returns:

  • (ActiveRecord::Relation, nil)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/queries/role/filter.rb', line 56

def and_clauses
  clauses = [
    role_type_facet,
    object_global_id_facet
  ].compact

  return nil if clauses.empty?

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

#base_queryObject



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

def base_query
  ::Role.select('roles.*')
end

#merge_clausesObject



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

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

  return nil if clauses.empty?

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

#object_global_id_facetObject

TODO: DRY. See also Notes filter



47
48
49
50
51
52
53
# File 'lib/queries/role/filter.rb', line 47

def object_global_id_facet
  return nil if object_global_id.nil?
  o = GlobalID::Locator.locate(object_global_id)
  k = o.class.base_class.name
  id = o.id
  table[:role_object_id].eq(o.id).and(table[:role_object_type].eq(k))
end

#role_type_facetObject



41
42
43
44
# File 'lib/queries/role/filter.rb', line 41

def role_type_facet
  return nil if role_type.empty?
  table[:type].eq_any(role_type)
end

#tableArel::Table

Returns:

  • (Arel::Table)


33
34
35
# File 'lib/queries/role/filter.rb', line 33

def table
  ::Role.arel_table
end