Class: ParamsReady::Query::Join

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

Defined Under Namespace

Classes: Builder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, type, table_alias: nil, &block) ⇒ Join

Returns a new instance of Join.



34
35
36
37
38
39
# File 'lib/params_ready/query/join_clause.rb', line 34

def initialize(table, type, table_alias: nil, &block)
  @arel_table = Helpers::ArelBuilder::Table.instance(table, table_alias: table_alias)
  @type = arel_type(type)
  @statement, @only_if = Builder.new(&block).build
  freeze
end

Instance Attribute Details

#arel_table(context, parameter) ⇒ Object (readonly)

Returns the value of attribute arel_table.



32
33
34
# File 'lib/params_ready/query/join_clause.rb', line 32

def arel_table
  @arel_table
end

#statementObject (readonly)

Returns the value of attribute statement.



32
33
34
# File 'lib/params_ready/query/join_clause.rb', line 32

def statement
  @statement
end

#typeObject (readonly)

Returns the value of attribute type.



32
33
34
# File 'lib/params_ready/query/join_clause.rb', line 32

def type
  @type
end

Instance Method Details

#arel_type(type) ⇒ Object



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

def arel_type(type)
  case type
  when :inner then Arel::Nodes::InnerJoin
  when :outer then Arel::Nodes::OuterJoin
  else raise ParamsReadyError, "Unimplemented join type '#{type}'"
  end
end

#eligible?(context, parameter) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/params_ready/query/join_clause.rb', line 61

def eligible?(context, parameter)
  return true if @only_if.nil?

  @only_if.call(context, parameter)
end

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



53
54
55
56
57
58
59
# File 'lib/params_ready/query/join_clause.rb', line 53

def to_arel(joined_table, base_table, context, parameter)
  return joined_table unless eligible?(context, parameter)

  arel_table = arel_table(context, parameter)
  join_statement = @statement.to_arel(base_table, arel_table, context, parameter)
  joined_table.join(arel_table, @type).on(join_statement)
end