Class: Queries::OriginRelationship::Filter

Inherits:
Query
  • Object
show all
Includes:
Concerns::Polymorphic
Defined in:
lib/queries/origin_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.



14
15
16
17
18
19
# File 'lib/queries/origin_relationship/filter.rb', line 14

def initialize(params)
  @new_object_global_id = params[:new_object_global_id]
  @old_object_global_id = params[:old_object_global_id]

  set_polymorphic_ids(params)
end

Instance Attribute Details

#new_object_global_idObject

attr_accessor :object_global_id from Queries::Concerns::Polymorphic attr_accessor :polymorphic_ids from Queries::Concerns::Polymorphic



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

def new_object_global_id
  @new_object_global_id
end

#old_object_global_idObject

Returns the value of attribute old_object_global_id.



12
13
14
# File 'lib/queries/origin_relationship/filter.rb', line 12

def old_object_global_id
  @old_object_global_id
end

Instance Method Details

#allActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/queries/origin_relationship/filter.rb', line 78

def all
  a = and_clauses
  b = merge_clauses
  if a && b
    b.where(a).distinct
  elsif a
    ::OriginRelationship.where(a).distinct
  elsif b
    b.distinct
  else
    ::OriginRelationship.all
  end
end

#and_clausesActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/queries/origin_relationship/filter.rb', line 50

def and_clauses
  clauses = [
    matching_new_object_facet,
    matching_old_object_facet,
    matching_polymorphic_ids
  ].compact

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

#matching_new_object_facetObject



37
38
39
40
41
# File 'lib/queries/origin_relationship/filter.rb', line 37

def matching_new_object_facet
  return nil if new_object_global_id.nil?
  table[:new_object_type].eq(new_object.class.base_class)
    .and(table[:new_object_id].eq(new_object.id))  
end

#matching_old_object_facetObject



43
44
45
46
47
# File 'lib/queries/origin_relationship/filter.rb', line 43

def matching_old_object_facet
  return nil if old_object_global_id.nil?
  table[:old_object_type].eq(old_object.class.base_class)
    .and(table[:old_object_id].eq(old_object.id))  
end

#merge_clausesObject



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/queries/origin_relationship/filter.rb', line 64

def merge_clauses
  clauses = [
  ].compact

  return nil if clauses.empty?

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

#new_objectObject



21
22
23
24
25
26
27
# File 'lib/queries/origin_relationship/filter.rb', line 21

def new_object
  if o = GlobalID::Locator.locate(new_object_global_id)
    o
  else
    nil
  end
end

#old_objectObject



29
30
31
32
33
34
35
# File 'lib/queries/origin_relationship/filter.rb', line 29

def old_object
  if o = GlobalID::Locator.locate(old_object_global_id)
    o
  else
    nil
  end
end

#tableArel::Table

Returns:

  • (Arel::Table)


93
94
95
# File 'lib/queries/origin_relationship/filter.rb', line 93

def table
  ::OriginRelationship.arel_table
end