Class: Horza::Adapters::ArelJoin

Inherits:
Options
  • Object
show all
Defined in:
lib/horza/adapters/active_record/arel_join.rb

Constant Summary

Constants inherited from Options

Options::META_METHODS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Options

#eager_args, #eager_load?, #method_missing, #order_direction, #order_field

Constructor Details

#initialize(context, options) ⇒ ArelJoin

Returns a new instance of ArelJoin.



11
12
13
14
15
16
17
18
19
# File 'lib/horza/adapters/active_record/arel_join.rb', line 11

def initialize(context, options)
  @options = options

  @base_table_key = context.table_name.to_sym
  @join_table_key = options[:with]

  @base_table = Arel::Table.new(@base_table_key)
  @join_table = Arel::Table.new(@join_table_key)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Horza::Adapters::Options

Class Method Details

.sql(context, options) ⇒ Object



6
7
8
# File 'lib/horza/adapters/active_record/arel_join.rb', line 6

def sql(context, options)
  new(context, options).query.to_sql
end

Instance Method Details

#queryObject



21
22
23
24
25
26
27
28
# File 'lib/horza/adapters/active_record/arel_join.rb', line 21

def query
  join = @base_table.project(fields).join(@join_table).on(predicates)
  join = join.where(where_clause(@base_table, @base_table_key)) if conditions_for_table?(@base_table_key)
  join = join.where(where_clause(@join_table, @join_table_key)) if conditions_for_table?(@join_table_key)
  join = join.take(@options[:limit]) if @options[:limit]
  join = join.skip(@options[:offset]) if @options[:offset]
  join
end