Class: Arel::SelectManager

Inherits:
Object
  • Object
show all
Defined in:
app/models/product_type.rb

Instance Method Summary collapse

Instance Method Details

#polymorphic_join(hash = {polytable: nil, table: nil, model: nil, polymodel: nil, record_type_name: nil, record_id_name: nil, table_model: nil}) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/models/product_type.rb', line 203

def polymorphic_join(hash={polytable: nil, table: nil, model: nil, polymodel: nil, record_type_name: nil, record_id_name: nil, table_model: nil})
  # Left Outer Join with 2 possible hash argument sets:
  #   1) model (AR model), polymodel (AR model), record_type_name (symbol), record_id_name (symbol)
  #   2) polytable (arel_table), table (arel_table), record_type_name (symbol), record_id_name (symbol), table_model (string)

  if hash[:model] && hash[:polymodel]
    self.join(hash[:polymodel].arel_table, Arel::Nodes::OuterJoin).on(hash[:polymodel].arel_table[hash[:record_id_name]].eq(hash[:model].arel_table[:id]).and(hash[:polymodel].arel_table[hash[:record_type_name]].eq(hash[:model].to_s)))
  elsif hash[:polytable] && hash[:record_type_name]
    self.join(hash[:polytable], Arel::Nodes::OuterJoin).on(hash[:polytable][hash[:record_id_name]].eq(hash[:table][:id]).and(hash[:polytable][hash[:record_type_name]].eq(hash[:table_model])))
  else
    raise 'Invalid Args'
  end
end