Class: ArFinderForm::Table

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

Direct Known Subclasses

Builder, JoinedTable

Constant Summary collapse

JOIN_TYPES =
(%w(inner cross natual) +
%w(left right full).map{|t| [t, "#{t}_outer"]}.flatten).map(&:to_sym)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, *args) ⇒ Table

Returns a new instance of Table.



26
27
28
29
30
# File 'lib/ar_finder_form/table.rb', line 26

def initialize(model_class, *args)
  @model_class = model_class
  @columns = NameAccessableArray.new(:name)
  @joined_tables = []
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



25
26
27
# File 'lib/ar_finder_form/table.rb', line 25

def columns
  @columns
end

#joined_tablesObject (readonly)

Returns the value of attribute joined_tables.



25
26
27
# File 'lib/ar_finder_form/table.rb', line 25

def joined_tables
  @joined_tables
end

#model_classObject (readonly)

Returns the value of attribute model_class.



25
26
27
# File 'lib/ar_finder_form/table.rb', line 25

def model_class
  @model_class
end

Instance Method Details

#build(context) ⇒ Object



52
53
54
55
56
57
# File 'lib/ar_finder_form/table.rb', line 52

def build(context)
  columns.each{|column| column.build(context)}
  joined_tables.each do |joined_table|
    joined_table.build(context)
  end
end

#build_methodsObject



45
46
47
48
49
50
# File 'lib/ar_finder_form/table.rb', line 45

def build_methods
  columns.each{|column| column.setup}
  joined_tables.each do |joined_table|
    joined_table.build_methods
  end
end

#column(column_name, *args) ⇒ Object



35
36
37
# File 'lib/ar_finder_form/table.rb', line 35

def column(column_name, *args)
  @columns << Column.new(self, column_name, *args)
end

#join(join_type, options, &block) ⇒ Object

Raises:

  • (ArgumentError)


59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ar_finder_form/table.rb', line 59

def join(join_type, options, &block)
  join_as = options.delete(:as)
  join_on = options.delete(:on)
  ref_name = [:belongs_to, :has_one, :has_many].map{|k| options[k]}.compact.first
  raise ArgumentError, "#{join_type}_join requires :belongs_to, :has_one or :has_many" unless ref_name
  ref = @model_class.reflections[ref_name]
  raise ArgumentError, "no reflection for #{ref_name.inspect}" unless ref
  result = JoinedTable.new(self, join_type, ref)
  @joined_tables << result
  result.instance_eval(&block)
  result
end

#model_column_for(name) ⇒ Object



39
40
41
42
43
# File 'lib/ar_finder_form/table.rb', line 39

def model_column_for(name)
  name = name.to_s
  @model_columns ||= @model_class.columns
  @model_columns.detect{|col| col.name.to_s == name}
end

#nameObject



33
# File 'lib/ar_finder_form/table.rb', line 33

def name; table_name; end

#table_nameObject



32
# File 'lib/ar_finder_form/table.rb', line 32

def table_name; @model_class.table_name; end