Class: QueryBase

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/service/install/templates/services/query_base.rb

Constant Summary collapse

RelationRequired =
Class.new(StandardError)

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ QueryBase

Returns a new instance of QueryBase.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/generators/service/install/templates/services/query_base.rb', line 6

def initialize(*args)
  @options = args.extract_options!
  @relation = args.first || base_relation

  if relation.nil?
    raise(
      RelationRequired,
      "Queries require a base relation defined. Use .queries method to define relation."
    )
  elsif !relation.is_a?(ActiveRecord::Relation)
    raise(
      RelationRequired,
      "Queries accept only ActiveRecord::Relation as input"
    )
  end
end

Class Attribute Details

.base_relationObject

Returns the value of attribute base_relation.



44
45
46
# File 'lib/generators/service/install/templates/services/query_base.rb', line 44

def base_relation
  @base_relation
end

Class Method Details

.call(*args) ⇒ Object



23
24
25
# File 'lib/generators/service/install/templates/services/query_base.rb', line 23

def self.call(*args)
  new(*args).query
end

.queries(subject) ⇒ Object



27
28
29
# File 'lib/generators/service/install/templates/services/query_base.rb', line 27

def self.queries(subject)
  self.base_relation = subject
end

Instance Method Details

#base_relationObject



31
32
33
34
35
36
37
38
39
# File 'lib/generators/service/install/templates/services/query_base.rb', line 31

def base_relation
  return nil if self.class.base_relation.nil?

  if self.class.base_relation.is_a?(ActiveRecord::Relation)
    self.class.base_relation
  elsif self.class.base_relation < ActiveRecord::Base
    self.class.base_relation.all
  end
end