Class: Queries::Citation::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/queries/citation/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



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

def initialize(params)
  @citation_object_type = params[:citation_object_type]
  @citation_object_id = params[:citation_object_id]
  @source_id = params[:source_id]
  set_polymorphic_ids(params)
end

Instance Attribute Details

- (Object) citation_object_id

Array, Integer



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

def citation_object_id
  @citation_object_id
end

- (Object) citation_object_type

Array, Integer



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

def citation_object_type
  @citation_object_type
end

- (Object) polymorphic_ids

Array, Integer



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

def polymorphic_ids
  @polymorphic_ids
end

- (Object) source_id

Array, Integer



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

def source_id
  @source_id
end

Instance Method Details

- (ActiveRecord::Relation) all

Returns:

  • (ActiveRecord::Relation)


79
80
81
82
83
84
85
# File 'lib/queries/citation/filter.rb', line 79

def all
  if a = and_clauses
    ::Citation.where(and_clauses)
  else
    ::Citation.none
  end
end

- (ActiveRecord::Relation?) and_clauses

Returns:

  • (ActiveRecord::Relation, nil)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/queries/citation/filter.rb', line 34

def and_clauses
  clauses = [
    matching_citation_object_type,
    matching_citation_object_id,
    matching_source_id,
    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_citation_object_id

Returns:

  • (Arel::Node, nil)


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

def matching_citation_object_id
  citation_object_id.blank? ? nil : table[:citation_object_id].eq(citation_object_id)  
end

- (Arel::Node?) matching_citation_object_type

Returns:

  • (Arel::Node, nil)


64
65
66
# File 'lib/queries/citation/filter.rb', line 64

def matching_citation_object_type
  citation_object_type.blank? ? nil : table[:citation_object_type].eq(citation_object_type) 
end

- (Arel::Node?) matching_polymorphic_ids

TODO: Dry with polymorphic_params

Returns:

  • (Arel::Node, nil)


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

def matching_polymorphic_ids
  nodes = Queries::Annotator.polymorphic_nodes(polymorphic_ids, ::Citation)
  return nil if nodes.nil?
  a = nodes.shift
  nodes.each do |b|
    a = a.and(b)
  end
  a
end

- (Arel::Node?) matching_source_id

Returns:

  • (Arel::Node, nil)


74
75
76
# File 'lib/queries/citation/filter.rb', line 74

def matching_source_id
  source_id.blank? ? nil : table[:source_id].eq(source_id)  
end

- (Object) set_polymorphic_ids(hash)



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

def set_polymorphic_ids(hash)
  @polymorphic_ids = hash.select{|k,v| ::Citation.related_foreign_keys.include?(k.to_s)} 
  @polymorphic_ids ||= {}
end

- (Arel::Table) table

Returns:

  • (Arel::Table)


88
89
90
# File 'lib/queries/citation/filter.rb', line 88

def table
  ::Citation.arel_table
end