Class Table
In: app/models/table.rb
Parent: Object

Methods

Included Modules

Switch

Attributes

database  [RW] 
driver  [RW] 
name  [RW] 

Public Class methods

Assign the database this table belongs to and it‘s name

[Source]

    # File app/models/table.rb, line 12
12:   def initialize( database, name )
13:     self.database = database
14:     self.name     = name
15:     self.driver   = database.driver
16:   end

Public Instance methods

This method switches ActiveRecord‘s connection to the actual database this database model represents, adds the new field, then switches back to the RailsDB database.

[Source]

    # File app/models/table.rb, line 48
48:   def add_field( name, type, options )
49:     switch( self.database ) do
50:       ActiveRecord::Base.connection.add_column( self.name.to_sym, name.to_sym, type.to_sym, options )
51:     end
52:   end

This method adds fields from the params from a form.

[Source]

    # File app/models/table.rb, line 57
57:   def add_fields( params )
58:     1.upto( params[:fields].size ) do |x|
59:       if params[:fields][x.to_s] && !params[:fields][x.to_s][:name].empty?
60:         unless self.has_field?( params[:fields][x.to_s][:name] )
61:           self.add_field( params[:fields][x.to_s][:name],
62:                           params[:fields][x.to_s][:type],
63:                           mangle_column_options( params, x.to_s ) )
64:         end
65:       end
66:     end
67:   end

This calls ActiveRecord‘s create method on the given model class

[Source]

    # File app/models/table.rb, line 72
72:   def create( args )
73:     switch_ar( self.database, self.name ) do |c|
74:       params = {}
75:       args.collect { |a| { a[0].to_sym => args[ a[0] ] } }.each do |b|
76:         params[ b.keys.first.to_sym ] = b[ b.keys.first.to_sym ]
77:       end
78:       o = c.create( params )
79:     end
80:   end

This method switches ActiveRecord‘s connection to the actual database this database model represents, deletes the field, then switches back to the RailsDB database.

[Source]

    # File app/models/table.rb, line 37
37:   def del_field( name )
38:     switch( self.database ) do
39:       ActiveRecord::Base.connection.remove_column( self.name.to_sym, name.to_sym )
40:     end
41:   end

This method deletes a table row

[Source]

    # File app/models/table.rb, line 21
21:   def del_row( id )
22:     switch_ar( self.database, self.name ) { |c| o = c.find( id ).destroy }
23:   end

How many fields in this table

[Source]

     # File app/models/table.rb, line 114
114:   def field_count
115:     self.fields.size
116:   end

Just the field names, sorted

[Source]

     # File app/models/table.rb, line 99
 99:   def field_names
100:     self.fields.collect { |f| f.name }.sort
101:   end

This method maps the field attributes for a table into known named keys, required mostly because sqlite doesn‘t match the others

[Source]

     # File app/models/table.rb, line 130
130:   def fields
131:     fields = []
132:     switch( self.database ) do
133:       cid = 0
134:       case self.driver.name
135:       when 'sqlite3'
136:         # map sqlite fields to match more common names
137:         ActiveRecord::Base.connection.table_structure( self.name.to_sym ).each do |c|
138:           attributes = {}
139:           attributes[:cid]     = c['cid']
140:           attributes[:name]    = c['name']
141:           attributes[:type]    = c['type']
142:           attributes[:primary] = c['pk']
143:           attributes[:null]    = c['notnull']
144:           attributes[:default] = c['dflt_value']
145:           fields << Field.new( self, attributes )
146:         end
147:       else
148:         # this same mapping currently works for both mysql and postgresql
149:         ActiveRecord::Base.connection.columns( self.name.to_sym ).each do |c|
150:           attributes = {}
151:           attributes[:cid]       = cid
152:           attributes[:name]      = c.name
153:           attributes[:type]      = c.type
154:           attributes[:sql_type]  = c.sql_type
155:           attributes[:primary]   = c.primary
156:           attributes[:null]      = c.null
157:           attributes[:default]   = c.default
158:           attributes[:scale]     = c.scale
159:           attributes[:precision] = c.precision
160:           attributes[:limit]     = c.limit
161:           fields << Field.new( self, attributes )
162:           cid += 1
163:         end
164:       end
165:     end
166:     fields.sort
167:   end

This calls ActiveRecord‘s find method on the given model class

[Source]

    # File app/models/table.rb, line 85
85:   def find( *args )
86:     switch_ar( self.database, self.name ) { |c| o = c.find( *args ).collect{ |r| r.attributes } }
87:   end

Returns a single field by name

[Source]

     # File app/models/table.rb, line 121
121:   def get_field( name )
122:     self.fields.each { |f| return f if f.name == name }
123:     nil
124:   end

Does this table contain a filed with passed name?

[Source]

     # File app/models/table.rb, line 106
106:   def has_field?( name )
107:     self.field_names.each { |f| return true if f == name }
108:     false
109:   end

This method gets the row count for this table.

[Source]

    # File app/models/table.rb, line 92
92:   def row_count
93:     switch_ar( self.database, self.name ) { |c| o = c.count }
94:   end

This method updates a table row

[Source]

    # File app/models/table.rb, line 28
28:   def update_row( id, attributes )
29:     switch_ar( self.database, self.name ) { |c| o = c.find( id ).update_attributes( attributes ) }
30:   end

[Validate]