Class: ArFinderForm::Table
- Inherits:
-
Object
- Object
- ArFinderForm::Table
- Defined in:
- lib/ar_finder_form/table.rb
Direct Known Subclasses
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
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#joined_tables ⇒ Object
readonly
Returns the value of attribute joined_tables.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
Instance Method Summary collapse
- #build(context) ⇒ Object
- #build_methods ⇒ Object
- #column(column_name, *args) ⇒ Object
-
#initialize(model_class, *args) ⇒ Table
constructor
A new instance of Table.
- #join(join_type, options, &block) ⇒ Object
- #model_column_for(name) ⇒ Object
- #name ⇒ Object
- #table_name ⇒ Object
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
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
25 26 27 |
# File 'lib/ar_finder_form/table.rb', line 25 def columns @columns end |
#joined_tables ⇒ Object (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_class ⇒ Object (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_methods ⇒ Object
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
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, , &block) join_as = .delete(:as) join_on = .delete(:on) ref_name = [:belongs_to, :has_one, :has_many].map{|k| [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 |
#name ⇒ Object
33 |
# File 'lib/ar_finder_form/table.rb', line 33 def name; table_name; end |
#table_name ⇒ Object
32 |
# File 'lib/ar_finder_form/table.rb', line 32 def table_name; @model_class.table_name; end |