Class: ParamsReady::Query::JoinStatement

Inherits:
Object
  • Object
show all
Defined in:
lib/params_ready/query/join_clause.rb

Instance Method Summary collapse

Constructor Details

#initialize(on: nil, eq: nil, &block) ⇒ JoinStatement

Returns a new instance of JoinStatement.

Raises:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/params_ready/query/join_clause.rb', line 29

def initialize(on: nil, eq: nil, &block)
  @conditions = []
  if on
    condition = on(on)
    if eq
      condition.eq(eq)
    end
  else
    raise ParamsReadyError('Parameter :eq unexpected') unless eq.nil?
  end

  instance_eval(&block) unless block.nil?
  raise ParamsReadyError, "Join clause is empty" if @conditions.empty?
end

Instance Method Details

#on(expression, arel_table: nil) ⇒ Object



44
45
46
47
48
# File 'lib/params_ready/query/join_clause.rb', line 44

def on(expression, arel_table: nil)
  condition = JoinCondition.new(expression, arel_table: arel_table)
  @conditions << condition
  condition
end

#to_arel(base_table, join_table, context, parameter) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/params_ready/query/join_clause.rb', line 50

def to_arel(base_table, join_table, context, parameter)
  @conditions.reduce(nil) do |result, condition|
    arel = condition.to_arel(base_table, join_table, context, parameter)
    next arel if result.nil?

    result.and(arel)
  end
end