Module: ActiveRecordExtended::QueryMethods::Either

Defined in:
lib/active_record_extended/query_methods/either.rb

Constant Summary collapse

XOR_FIELD_SQL =
"(CASE WHEN %<t1>s.%<c1>s IS NULL THEN %<t2>s.%<c2>s ELSE %<t1>s.%<c1>s END) "
XOR_FIELD_KEYS =
[:t1, :c1, :t2, :c2].freeze

Instance Method Summary collapse

Instance Method Details

#either_join(initial_association, fallback_association) ⇒ Object Also known as: either_joins



11
12
13
14
15
16
# File 'lib/active_record_extended/query_methods/either.rb', line 11

def either_join(initial_association, fallback_association)
  associations        = [initial_association, fallback_association]
  association_options = xor_field_options_for_associations(associations)
  condition__query    = xor_field_sql(association_options) + "= #{table_name}.#{primary_key}"
  outer_joins(associations).where(Arel.sql(condition__query))
end

#either_order(direction, **associations_and_columns) ⇒ Object Also known as: either_orders



19
20
21
22
23
# File 'lib/active_record_extended/query_methods/either.rb', line 19

def either_order(direction, **associations_and_columns)
  reflected_columns = map_columns_to_tables(associations_and_columns)
  conditional_query = xor_field_sql(reflected_columns) + sort_order_sql(direction)
  outer_joins(associations_and_columns.keys).order(Arel.sql(conditional_query))
end