Class: RecordFilter::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/record_filter/table.rb

Direct Known Subclasses

PivotTable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, table_alias = nil) ⇒ Table

Returns a new instance of Table.



5
6
7
8
9
10
11
12
13
# File 'lib/record_filter/table.rb', line 5

def initialize(model_class, table_alias = nil)
  @model_class = model_class
  @aliased = !table_alias.nil?
  @table_alias = table_alias || model_class.quoted_table_name
  @joins_cache = {}
  @joins = []
  @orders = []
  @group_bys = []
end

Instance Attribute Details

#group_bysObject (readonly)

Returns the value of attribute group_bys.



3
4
5
# File 'lib/record_filter/table.rb', line 3

def group_bys
  @group_bys
end

#model_classObject (readonly)

Returns the value of attribute model_class.



3
4
5
# File 'lib/record_filter/table.rb', line 3

def model_class
  @model_class
end

#ordersObject (readonly)

Returns the value of attribute orders.



3
4
5
# File 'lib/record_filter/table.rb', line 3

def orders
  @orders
end

#table_aliasObject (readonly)

Returns the value of attribute table_alias.



3
4
5
# File 'lib/record_filter/table.rb', line 3

def table_alias
  @table_alias
end

Instance Method Details

#all_joinsObject



51
52
53
54
55
56
# File 'lib/record_filter/table.rb', line 51

def all_joins
  @joins + @joins.inject([]) do |child_joins, join|
    child_joins.concat(join.right_table.all_joins)
    child_joins
  end
end

#group_by_column(column) ⇒ Object



62
63
64
# File 'lib/record_filter/table.rb', line 62

def group_by_column(column)
  @group_bys << GroupBy.new(column, self)
end

#has_column(column_name) ⇒ Object



66
67
68
# File 'lib/record_filter/table.rb', line 66

def has_column(column_name)
  @model_class.column_names.include?(column_name.to_s)
end

#join_association(association_name, join_type = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/record_filter/table.rb', line 19

def join_association(association_name, join_type=nil)
  @joins_cache[association_name] ||=
    begin
      association = @model_class.reflect_on_association(association_name)
      if association.nil?
        raise AssociationNotFoundException.new("The association #{association_name} was not found on #{@model_class.name}.")
      end

      if (association.options[:through])
        through_association = @model_class.reflect_on_association(association.options[:through])
        through_join = join_association(association.options[:through], join_type)
        through_join.right_table.join_association(association_name, join_type)
      else
        case association.macro
        when :belongs_to, :has_many, :has_one
          simple_join(association, join_type)
        when :has_and_belongs_to_many
          compound_join(association, join_type)
        end
      end
    end
end

#join_class(clazz, join_type, table_alias, conditions) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/record_filter/table.rb', line 42

def join_class(clazz, join_type, table_alias, conditions)
  @joins_cache[clazz] ||= 
    begin
      join_table = Table.new(clazz, table_alias || alias_for_class(clazz))
      @joins << (join = Join.new(self, join_table, conditions, join_type))
      join
    end
end

#order_column(column, direction) ⇒ Object



58
59
60
# File 'lib/record_filter/table.rb', line 58

def order_column(column, direction)
  @orders << Order.new(column, direction, self)
end

#table_nameObject



15
16
17
# File 'lib/record_filter/table.rb', line 15

def table_name
  @model_class.quoted_table_name
end