Class DatabaseController
In: app/controllers/database_controller.rb
Parent: ApplicationController

Methods

Included Modules

Switch

Public Instance methods

This method provides a way to add any number of new fields to a database

[Source]

     # File app/controllers/database_controller.rb, line 231
231:   def add_fields
232:     get_database( params[:id] )
233:     get_table( @database, params[:table] )
234:     @errors           = {}
235:     session[:fields]  = {}
236:     field_errors      = {}
237:     field_types       = get_field_types( @database.driver.name )
238:     if request.post?
239:       if params[:fields]['1'][:name].empty?
240:         field_errors['1'] = {}
241:         field_errors['1'][:name] = 'at least one field required'
242:       end
243:       x = 1
244:       while params[:fields][x.to_s]
245:         unless params[:fields][x.to_s][:name].empty?
246:           session[:fields][x.to_s] = {}
247:           FIELD_ATTRIBUTES.collect{ |k| k.to_sym }.each do |k|
248:             session[:fields][x.to_s][k] = params[:fields][x.to_s][k]
249:           end
250:           unless field_types.include? params[:fields][x.to_s][:type]
251:             field_errors[x.to_s] = {}
252:             field_errors[x.to_s][:type] = 'valid field type required'
253:           end
254:         end
255:         x += 1
256:       end
257:       session[:field_blanks] = params[:fields].size
258:       @errors[:fields] = field_errors unless field_errors.size.zero?
259:       if @errors.empty?
260:         begin
261:           @table.add_fields( params )
262:         rescue RuntimeError
263:           flash.now[:notice] = "An error occured:<br /><br />#{ $!.to_s }"
264:         else
265:           session[:field_blanks] = nil
266:           flash[:notice] = 'new fields added'
267:           redirect_to :controller => :database,
268:                       :action     => :table,
269:                       :id         => @database
270:         end
271:       end
272:     end
273:     session[:field_blanks] = ( session[:field_blanks] && session[:field_blanks] > 1 ) ? session[:field_blanks] : 5
274:   end

This method is used to create new tables

[Source]

     # File app/controllers/database_controller.rb, line 157
157:   def add_table
158:     get_database( params[:id] )
159:     @errors           = {}
160:     session[:fields]  = {}
161:     field_errors      = {}
162:     field_types       = get_field_types( @database.driver.name )
163:     if request.post?
164:       @errors['name']   = 'invalid table name' unless params[:name] =~ /[\.0-9a-z\-_]{1,64}/
165:       @errors['add_id'] = 'selection required' if params[:add_id].nil?
166:       if params[:fields]['1'][:name].empty?
167:         field_errors['1'] = {}
168:         field_errors['1'][:name] = 'at least one field required'
169:       end
170:       x = 1
171:       while params[:fields][x.to_s]
172:         unless params[:fields][x.to_s][:name].empty?
173:           session[:fields][x.to_s] = {}
174:           FIELD_ATTRIBUTES.collect{ |k| k.to_sym }.each do |k|
175:             session[:fields][x.to_s][k] = params[:fields][x.to_s][k]
176:           end
177:           unless field_types.include? params[:fields][x.to_s][:type]
178:             field_errors[x.to_s] = {}
179:             field_errors[x.to_s][:type] = 'valid field type required'
180:           end
181:         end
182:         x += 1
183:       end
184:       session[:field_blanks] = params[:fields].size
185:       session[:name] = params[:name].to_s if params[:name] && params[:name] =~ /[\.0-9a-z\-_]{1,64}/
186:       @errors[:fields] = field_errors unless field_errors.size.zero?
187:       if @errors.empty?
188:         begin
189:           @database.create_tbl( params )
190:         rescue RuntimeError
191:           flash.now[:notice] = "An error occured:<br /><br />#{ $!.to_s }"
192:         else
193:           session[:field_blanks] = nil
194:           session[:name] = nil
195:           flash[:notice] = 'table created'
196:           redirect_to :controller => :database, :action => :index, :database => params[:database]
197:         end
198:       end
199:     end
200:     session[:field_blanks] = ( session[:field_blanks] && session[:field_blanks] > 5 ) ? session[:field_blanks] : 5
201:     @add_id_yes_checked = true
202:     @add_id_no_checked  = false
203:     if params[:add_id] && params[:add_id] == '0'
204:       @add_id_yes_checked = false
205:       @add_id_no_checked  = true
206:     end
207:   end

Increments the session count for blank fields, then it‘s RJS template loads the new table row partial into the table.

[Source]

     # File app/controllers/database_controller.rb, line 140
140:   def blank_field
141:     get_database( params[:id] )
142:     session[:field_blanks] += 1
143:   end

Increments the session count for blank insert fields, then it‘s RJS template loads the new insert partial into the table.

[Source]

    # File app/controllers/database_controller.rb, line 74
74:   def blank_insert
75:     if request.xhr?
76:       get_database( params[:id] )
77:       get_table( @database, params[:table] )
78:       get_fields( @table )
79:       session[:num_rows] += 1
80:     end
81:   end

Decrements the session count for blank insert fields, then it‘s RJS template removes the last insert row.

[Source]

    # File app/controllers/database_controller.rb, line 87
87:   def blank_remove
88:     if request.xhr?
89:       if session[:num_rows] > 1
90:         get_database( params[:id] )
91:         get_table( @database, params[:table] )
92:         get_fields( @table )
93:         @row_id = session[:num_rows]
94:         session[:num_rows] -= 1
95:       end
96:     end
97:   end

Get some rows from the table

[Source]

     # File app/controllers/database_controller.rb, line 102
102:   def browse
103:     get_database( params[:id] )
104:     get_table( @database, params[:table] )
105:     get_fields( @table )
106:     @link_span = LINK_SPAN
107:     @current_page = params[:page] ? params[:page].to_i : 1
108:     @active_count = @table.row_count
109:     @total_pages = ( @active_count.to_f / PER_PAGE.to_f ).ceil
110:     @order = @table.field_names.first
111:     @order = 'name' if @table.field_names.include? 'name'
112:     @order = 'id' if @table.field_names.include? 'id'
113:     @pager = ::Paginator.new( @active_count, PER_PAGE ) do |offset, per_page|
114:       @table.find(  :all,
115:                     :limit  => per_page,
116:                     :offset => offset,
117:                     :order  => @order )
118:     end
119:     @page = @pager.page( params[:page] )
120:   end

This method deletes a field from a table. It prevents the last field from being deleted.

[Source]

     # File app/controllers/database_controller.rb, line 314
314:   def del_field
315:     get_database( params[:id] )
316:     get_table( @database, params[:table] )
317:     if @table.field_count == 1
318:       flash[:notice] = 'last field cannot be deleted, delete table instead'
319:       redirect_to :controller => :database,
320:                   :id         => @database
321:     end
322:     get_field( @table, params[:field] )
323:     if request.post?
324:       @table.del_field( @field.name )
325:       flash[:notice] = 'field deleted'
326:       redirect_to :controller => :database,
327:                   :action     => :table,
328:                   :id         => @database,
329:                   :table      => @table.name
330:     end
331:   end

Deletes a table row

[Source]

    # File app/controllers/database_controller.rb, line 13
13:   def del_row
14:     if request.post?
15:       get_database( params[:id] )
16:       get_table( @database, params[:table] )
17:       get_fields( @table )
18:       get_row( @table, params[:pk] )
19:       if @row
20:         @table.del_row( @row[ 'id' ] )
21:         flash[:notice] = "row #{ @row[ 'id' ] } deleted"
22:       else
23:         flash[:notice] = "row #{ params[:pk] } not found"
24:       end
25:       redirect_to :controller => :database,
26:                   :table      => @table.name,
27:                   :action     => :browse,
28:                   :id         => @database
29:     end
30:   end

This method provides a way to delete an entire table

[Source]

     # File app/controllers/database_controller.rb, line 212
212:   def del_table
213:     get_database( params[:id] )
214:     get_table( @database, params[:table] )
215:     if request.post?
216:       @database.del_table( @table.name )
217:       flash[:notice] = 'table deleted'
218:       redirect_to :controller => :database,
219:                   :id         => @database
220:     end
221:   end

This method provides a way to edit a single table field

[Source]

     # File app/controllers/database_controller.rb, line 279
279:   def edit_field
280:     get_database( params[:id] )
281:     get_table( @database, params[:table] )
282:     get_field( @table, params[:field] )
283:     @errors          = {}
284:     session[:fields] = { '1' => @field.attributes }
285:     field_errors     = { '1' => {} }
286:     field_types      = get_field_types( @database.driver.name )
287:     if request.post?
288:       field_errors['1'][:name] = 'field name required' if params[:fields]['1'][:name].empty?
289:       field_errors['1'][:type] = 'valid field type required'unless field_types.include? params[:fields]['1'][:type]
290:       FIELD_ATTRIBUTES.collect{ |k| k.to_sym }.each do |k|
291:         session[:fields]['1'][k] = params[:fields]['1'][k]
292:       end
293:       @errors[:fields] = field_errors unless field_errors['1'].size.zero?
294:       if @errors.empty?
295:         begin
296:           @field.update( params )
297:         rescue RuntimeError
298:           flash.now[:notice] = "An error occured:<br /><br />#{ $!.to_s }"
299:         else
300:           flash[:notice] = 'field updated'
301:           redirect_to :controller => :database,
302:                       :action     => :table,
303:                       :id         => @database,
304:                       :table      => @table.name
305:         end
306:       end
307:     end
308:   end

Used to modify a table row

[Source]

    # File app/controllers/database_controller.rb, line 35
35:   def edit_row
36:     get_database( params[:id] )
37:     get_table( @database, params[:table] )
38:     get_fields( @table )
39:     get_row( @table, params[:pk] )
40:     if request.post? && @row
41:       @table.update_row( @row[ 'id' ], params[:row] )
42:       flash[:notice] = "row #{ @row[ 'id' ] } updated"
43:       redirect_to :controller => :database,
44:                   :action     => :browse,
45:                   :table      => @table.name,
46:                   :id         => @database
47:     end
48:   end

[Source]

     # File app/controllers/database_controller.rb, line 223
223:   def edit_table
224:     get_database( params[:id] )
225:     get_table( @database, params[:table] )
226:   end

[Source]

     # File app/controllers/database_controller.rb, line 333
333:   def export_table
334:     get_database( params[:id] )
335:     get_table( @database, params[:table] )
336:     checked_field_names = (params[:field].delete_if { |k,v| v == '0' }).keys
337:     rows = @table.find(:all, :select => "#{checked_field_names.join(',')}")
338:     export_data = []
339:     rows.each { |r| export_data << r.values  }
340:     format = params[:format]
341:     delimiter = ','
342:     type = 'text/csv'
343:     case format
344:       when 'CVS'
345:         delimiter = ','
346:         type = 'text/csv'
347:       when 'TSV'
348:         delimiter = "\t"
349:         type = 'text/tab-separated-values'
350:     end
351:     exporter = DsvExporter.new(delimiter)
352:     exporter.header = params[:field].keys
353:     send_data exporter.export_as_text(export_data), :filename => "#{@database.name}$#{params[:table]}.csv", :type => type
354:   end

List the tables in a database.

[Source]

     # File app/controllers/database_controller.rb, line 125
125:   def index
126:     get_database( params[:id] )
127:     begin
128:       @tables = @database.tables
129:     rescue RuntimeError
130:       flash[:notice] = $!.to_s
131:       redirect_to :controller => :home,
132:                   :action     => :databases
133:     end
134:   end

Inserts rows of data into a table

[Source]

    # File app/controllers/database_controller.rb, line 53
53:   def insert
54:     session[:num_rows] = 1
55:     get_database( params[:id] )
56:     get_table( @database, params[:table] )
57:     get_fields( @table )
58:     if request.post?
59:       1.upto( params[params[:table].to_sym].size ) do |x|
60:         @table.create( params[params[:table].to_sym][x.to_s] )
61:       end
62:       flash[:notice] = "#{ params[params[:table].to_sym].size } objects added"
63:       redirect_to :controller => :database,
64:                   :table      => @table.name,
65:                   :action     => :browse,
66:                   :id         => @database
67:     end
68:   end

Lists the fields in a table

[Source]

     # File app/controllers/database_controller.rb, line 148
148:   def table
149:     get_database( params[:id] )
150:     get_table( @database, params[:table] )
151:     @fields = @table.fields
152:   end

[Validate]