Class: ArFinderForm::JoinedTable

Inherits:
Table
  • Object
show all
Defined in:
lib/ar_finder_form/joined_table.rb

Constant Summary

Constants inherited from Table

Table::JOIN_TYPES

Instance Attribute Summary collapse

Attributes inherited from Table

#columns, #joined_tables, #model_class

Instance Method Summary collapse

Methods inherited from Table

#build_methods, #column, #join, #model_column_for

Constructor Details

#initialize(parent_table, join_type, reflection, *args) ⇒ JoinedTable

Returns a new instance of JoinedTable.



6
7
8
9
10
11
12
13
# File 'lib/ar_finder_form/joined_table.rb', line 6

def initialize(parent_table, join_type, reflection, *args)
  super(reflection.klass, *args)
  @parent_table = parent_table
  @join_type = join_type
  @reflection = reflection
  @table_name = reflection.klass.table_name
  @name = "cond_" << @table_name
end

Instance Attribute Details

#join_typeObject (readonly)

Returns the value of attribute join_type.



5
6
7
# File 'lib/ar_finder_form/joined_table.rb', line 5

def join_type
  @join_type
end

#parent_tableObject (readonly)

Returns the value of attribute parent_table.



5
6
7
# File 'lib/ar_finder_form/joined_table.rb', line 5

def parent_table
  @parent_table
end

#reflectionObject (readonly)

Returns the value of attribute reflection.



5
6
7
# File 'lib/ar_finder_form/joined_table.rb', line 5

def reflection
  @reflection
end

Instance Method Details

#build(context) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/ar_finder_form/joined_table.rb', line 22

def build(context)
  sub_context = context.new_sub_context(:connector => 'AND')
  super(sub_context)
  unless sub_context.empty?
    context.merge(sub_context) do
      context.joins << build_join
    end
  end
end

#build_joinObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ar_finder_form/joined_table.rb', line 32

def build_join
  join_on =
    case reflection.macro
    when :belongs_to then
      "#{name}.id = #{parent_table.name}.#{reflection.primary_key_name}"
    else
      "#{name}.#{reflection.primary_key_name} = #{parent_table.name}.id"
    end
  "%s JOIN %s %s ON %s" % [
    join_type.to_s.gsub(/\_/, ' ').upcase, reflection.klass.table_name, name, join_on
  ]
end

#nameObject



16
# File 'lib/ar_finder_form/joined_table.rb', line 16

def name; @name; end

#root_tableObject



18
19
20
# File 'lib/ar_finder_form/joined_table.rb', line 18

def root_table
  parent_table.root_table
end

#table_nameObject



15
# File 'lib/ar_finder_form/joined_table.rb', line 15

def table_name; @table_name; end