Module: ActiveHouse::Querying::Union

Extended by:
ActiveSupport::Concern
Included in:
ActiveHouse::QueryBuilder
Defined in:
lib/active_house/querying/union.rb

Instance Method Summary collapse

Instance Method Details

#build_union_query_partObject



10
11
12
13
14
# File 'lib/active_house/querying/union.rb', line 10

def build_union_query_part
  return if values[:union].values.empty?

  "UNION ALL\n#{values[:union].values.map(&:to_query).join("\n")}"
end

#except_union(name) ⇒ Object



48
49
50
# File 'lib/active_house/querying/union.rb', line 48

def except_union(name)
  dup.except_union!(name)
end

#except_union!(name) ⇒ Object



41
42
43
44
45
46
# File 'lib/active_house/querying/union.rb', line 41

def except_union!(name)
  new_unions = values[:union].map { |n, q| [n, q.dup] }.to_h
  new_unions.delete(name.to_sym)
  values[:union] = new_unions
  self
end

#format_unions(queries) ⇒ Object

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/active_house/querying/union.rb', line 52

def format_unions(queries)
  raise ArgumentError, 'unions must be a Hash' unless queries.is_a?(Hash)
  raise ArgumentError, 'unions hash is empty' if queries.empty?

  new_unions = values[:union].map { |n, q| [n, q.dup] }.to_h

  queries.each do |name, query|
    query = query.all if query.is_a?(ActiveHouse::Model)
    new_unions[name.to_sym] = query.dup
  end

  new_unions
end

#initial_valuesObject



16
17
18
# File 'lib/active_house/querying/union.rb', line 16

def initial_values
  super.merge union: {}
end

#union(queries) ⇒ Object

key needed for possibility to update/replace union query

Parameters:

  • queries (Hash)
    • hash where key is union name and value is a query



30
31
32
# File 'lib/active_house/querying/union.rb', line 30

def union(queries)
  dup.union!(queries)
end

#union!(queries) ⇒ Object

key needed for possibility to update/replace union query

Parameters:

  • queries (Hash)
    • hash where key is union name and value is a query



22
23
24
25
26
# File 'lib/active_house/querying/union.rb', line 22

def union!(queries)
  formatted_queries = format_unions(queries)
  values[:union] = formatted_queries
  self
end

#update_union(name) ⇒ Object

Raises:

  • (ArgumentError)


34
35
36
37
38
39
# File 'lib/active_house/querying/union.rb', line 34

def update_union(name)
  name = name.to_sym
  raise ArgumentError, "can't find union by name #{name}" unless values[:union].key?(name)
  new_union = yield values[:union][name.to_sym]
  union(name.to_sym => new_union)
end