Module: Queries::Annotator

Includes:
Arel::Nodes
Defined in:
lib/queries/annotator.rb

Overview

Methods to facilitate permission and handling of polymorphic relationships

Class Method Summary collapse

Class Method Details

.annotated_class(params, klass) ⇒ Object

Returns String name of the class being Annotated.

Returns:

  • String name of the class being Annotated



47
48
49
50
51
# File 'lib/queries/annotator.rb', line 47

def self.annotated_class(params, klass)
  h = shallow_id(params, klass)
  return nil if h.size != 1
  h.keys.first.to_s.gsub(/_id$/, '').camelize
end

.annotator_params(params, klass) ⇒ Arel::Nodes::And

Returns for use in SomeAnnotator.where().

Returns:

  • (Arel::Nodes::And)

    for use in SomeAnnotator.where()



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/queries/annotator.rb', line 64

def self.annotator_params(params, klass)
  t = klass.arel_table

  c = [ polymorphic_params(params, klass) ]

  c.push t[:created_at].lteq(Date.new(*params[:created_before].split('-').map(&:to_i))) if params[:created_before]
  c.push t[:created_at].gteq(Date.new(*params[:created_after].split('-').map(&:to_i))) if params[:created_after]

  c.push t[klass.annotator_type].in(params[:on]) if params[:on]
  c.push t[:id].in([params[:id]].flatten.compact.uniq) if params[:id]
  c.push t[:created_by_id].in(params[:by]) if params[:by]

  c.compact!

  w = c.pop

  c.each do |q|
    w = w.and(q)
  end

  w
end

.polymorphic_nodes(hash, klass) ⇒ Array of Arel::Nodes::And

Returns:

  • (Array of Arel::Nodes::And)


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/queries/annotator.rb', line 11

def self.polymorphic_nodes(hash, klass)
  return nil if hash.empty?
  t = klass.arel_table
  nodes = []
  hash.keys.each do |k|
    model = k.to_s.gsub(/_id$/, '').camelize
    nodes.push(
      t[klass.annotator_id].eq(hash[k]).and(t[klass.annotator_type].eq(model))
    )
  end
  nodes
end

.polymorphic_params(params, klass) ⇒ Arel::Nodes::And

Returns translates params from requests like ‘otus/123/data_attributes` to a Arel::Nodes::And clause equvialent to `DataAttribute.where(attribute_subject_type: ’Otu’, attribute_subject_id: 123)‘.

Returns:

  • (Arel::Nodes::And)

    translates params from requests like ‘otus/123/data_attributes` to a Arel::Nodes::And clause equvialent to `DataAttribute.where(attribute_subject_type: ’Otu’, attribute_subject_id: 123)‘



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/queries/annotator.rb', line 30

def self.polymorphic_params(params, klass)
  t = klass.arel_table

  h = shallow_id(params, klass)

  return nil if h.size != 1

  a = polymorphic_nodes(h, klass)
  n = a.shift
  a.each do |b|
    n = n.and(b)
  end
  n
end

.shallow_id(params, klass) ⇒ Object

Returns Hash.

Returns:

  • Hash



54
55
56
57
# File 'lib/queries/annotator.rb', line 54

def self.shallow_id(params, klass)
  return {} unless params.class.name == 'ActionController::Parameters'
  params.permit(klass.related_foreign_keys).to_h # could be added into permit framework
end