Class: Queries::Confidence::Filter

Inherits:
Object
  • Object
show all
Includes:
Concerns::Polymorphic
Defined in:
lib/queries/confidence/filter.rb

Overview

!! does not inherit from base query

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Filter) initialize(params)

Returns a new instance of Filter



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

def initialize(params)
  @confidence_level_id = [params[:confidence_level_id]].flatten.compact
  @object_global_id = params[:object_global_id]
  @confidence_object_type = params[:confidence_object_type]
  set_polymorphic_ids(params)
end

Instance Attribute Details

- (Object) confidence_level_id

Array, Integer



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

def confidence_level_id
  @confidence_level_id
end

- (Object) confidence_object_type

Array, Integer



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

def confidence_object_type
  @confidence_object_type
end

- (Object) object_global_id

Array, Integer



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

def object_global_id
  @object_global_id
end

Instance Method Details

- (ActiveRecord::Relation) all

Returns:

  • (ActiveRecord::Relation)


73
74
75
76
77
78
79
# File 'lib/queries/confidence/filter.rb', line 73

def all
  if a = and_clauses
    ::Confidence.where(and_clauses).distinct
  else
    ::Confidence.all
  end
end

- (ActiveRecord::Relation) and_clauses

Returns:

  • (ActiveRecord::Relation)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/queries/confidence/filter.rb', line 23

def and_clauses
  clauses = [
    matching_confidence_level_id,
    matching_confidence_object_type,
    matching_object,
    matching_polymorphic_ids
  ].compact

  return nil if clauses.empty? 
  
  a = clauses.shift
  clauses.each do |b|
    a = a.and(b)
  end
  a
end

- (Arel::Node?) matching_confidence_level_id

Returns:

  • (Arel::Node, nil)


63
64
65
# File 'lib/queries/confidence/filter.rb', line 63

def matching_confidence_level_id
  !confidence_level_id.blank? ? table[:confidence_level_id].eq_any(confidence_level_id)  : nil
end

- (Arel::Node?) matching_confidence_object_type

Returns:

  • (Arel::Node, nil)


68
69
70
# File 'lib/queries/confidence/filter.rb', line 68

def matching_confidence_object_type
  !confidence_object_type.blank? ? table[:confidence_object_type].eq(confidence_object_type)  : nil
end

- (Arel::Node?) matching_object

TODO: Dry

Returns:

  • (Arel::Node, nil)


52
53
54
55
56
57
58
59
60
# File 'lib/queries/confidence/filter.rb', line 52

def matching_object
  if o = object_for
    table["confidence_object_id"].eq(o.id).and(
        table["confidence_object_type"].eq(o.metamorphosize.class.name)
    )
  else
    nil
  end
end

- (ActiveRecord object?) object_for

TODO: DRY

Returns:

  • (ActiveRecord object, nil)


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

def object_for
  if o = GlobalID::Locator.locate(object_global_id)
    o
  else
    nil
  end
end

- (Arel::Table) table

Returns:

  • (Arel::Table)


82
83
84
# File 'lib/queries/confidence/filter.rb', line 82

def table
  ::Confidence.arel_table
end