gdonald-railsdb 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. data/LICENSE.txt +674 -0
  2. data/README.txt +40 -0
  3. data/README_RAILS.txt +204 -0
  4. data/Rakefile +31 -0
  5. data/app/controllers/application.rb +24 -0
  6. data/app/controllers/database_controller.rb +274 -0
  7. data/app/controllers/home_controller.rb +366 -0
  8. data/app/helpers/application_helper.rb +43 -0
  9. data/app/helpers/home_helper.rb +2 -0
  10. data/app/models/database.rb +113 -0
  11. data/app/models/driver.rb +8 -0
  12. data/app/models/field.rb +117 -0
  13. data/app/models/group.rb +12 -0
  14. data/app/models/group_permission.rb +6 -0
  15. data/app/models/permission.rb +9 -0
  16. data/app/models/row.rb +26 -0
  17. data/app/models/switch.rb +85 -0
  18. data/app/models/table.rb +155 -0
  19. data/app/models/user.rb +98 -0
  20. data/app/models/user_group.rb +6 -0
  21. data/app/views/database/_field.html.erb +20 -0
  22. data/app/views/database/_field_form.html.erb +28 -0
  23. data/app/views/database/_insert_form.html.erb +37 -0
  24. data/app/views/database/_new_field.html.erb +43 -0
  25. data/app/views/database/_new_field_header.html.erb +12 -0
  26. data/app/views/database/_pagination.html.erb +14 -0
  27. data/app/views/database/_row.html.erb +16 -0
  28. data/app/views/database/_table.html.erb +22 -0
  29. data/app/views/database/_table_form.html.erb +45 -0
  30. data/app/views/database/add_fields.html.erb +3 -0
  31. data/app/views/database/add_table.html.erb +3 -0
  32. data/app/views/database/blank_field.rjs +6 -0
  33. data/app/views/database/browse.html.erb +30 -0
  34. data/app/views/database/del_field.html.erb +31 -0
  35. data/app/views/database/del_table.html.erb +31 -0
  36. data/app/views/database/edit_field.html.erb +23 -0
  37. data/app/views/database/edit_row.html.erb +22 -0
  38. data/app/views/database/index.html.erb +23 -0
  39. data/app/views/database/insert.html.erb +3 -0
  40. data/app/views/database/table.html.erb +30 -0
  41. data/app/views/home/_database.html.erb +8 -0
  42. data/app/views/home/_database_form.html.erb +68 -0
  43. data/app/views/home/_driver.html.erb +5 -0
  44. data/app/views/home/_driver_form.html.erb +25 -0
  45. data/app/views/home/_group.html.erb +10 -0
  46. data/app/views/home/_group_form.html.erb +25 -0
  47. data/app/views/home/_group_permission.html.erb +4 -0
  48. data/app/views/home/_permission.html.erb +6 -0
  49. data/app/views/home/_permission_form.html.erb +32 -0
  50. data/app/views/home/_user.html.erb +10 -0
  51. data/app/views/home/_user_form.html.erb +64 -0
  52. data/app/views/home/_user_group.html.erb +4 -0
  53. data/app/views/home/add_database.html.erb +3 -0
  54. data/app/views/home/add_driver.html.erb +3 -0
  55. data/app/views/home/add_group.html.erb +4 -0
  56. data/app/views/home/add_permission.html.erb +3 -0
  57. data/app/views/home/add_user.html.erb +62 -0
  58. data/app/views/home/bottom.html.erb +17 -0
  59. data/app/views/home/databases.html.erb +22 -0
  60. data/app/views/home/del_database.html.erb +31 -0
  61. data/app/views/home/del_driver.html.erb +32 -0
  62. data/app/views/home/del_group.html.erb +32 -0
  63. data/app/views/home/del_group_permission.html.erb +32 -0
  64. data/app/views/home/del_permission.html.erb +32 -0
  65. data/app/views/home/del_user.html.erb +33 -0
  66. data/app/views/home/del_user_group.html.erb +32 -0
  67. data/app/views/home/drivers.html.erb +20 -0
  68. data/app/views/home/edit_database.html.erb +3 -0
  69. data/app/views/home/edit_driver.html.erb +3 -0
  70. data/app/views/home/edit_group.html.erb +4 -0
  71. data/app/views/home/edit_permission.html.erb +4 -0
  72. data/app/views/home/edit_user.html.erb +84 -0
  73. data/app/views/home/group_permissions.html.erb +38 -0
  74. data/app/views/home/groups.html.erb +20 -0
  75. data/app/views/home/index.html.erb +21 -0
  76. data/app/views/home/login.html.erb +42 -0
  77. data/app/views/home/menu.html.erb +33 -0
  78. data/app/views/home/permissions.html.erb +21 -0
  79. data/app/views/home/top.html.erb +22 -0
  80. data/app/views/home/user_groups.html.erb +44 -0
  81. data/app/views/home/users.html.erb +27 -0
  82. data/app/views/layouts/application.html.erb +18 -0
  83. data/config/boot.rb +109 -0
  84. data/config/database.yml +14 -0
  85. data/config/environment.rb +114 -0
  86. data/config/environments/development.rb +18 -0
  87. data/config/environments/production.rb +19 -0
  88. data/config/environments/test.rb +22 -0
  89. data/config/initializers/inflections.rb +10 -0
  90. data/config/initializers/mime_types.rb +5 -0
  91. data/config/routes.rb +135 -0
  92. data/db/migrate/001_create_sessions.rb +17 -0
  93. data/db/migrate/002_create_users.rb +26 -0
  94. data/db/migrate/003_create_groups.rb +17 -0
  95. data/db/migrate/004_create_permissions.rb +18 -0
  96. data/db/migrate/005_create_group_permissions.rb +22 -0
  97. data/db/migrate/006_create_user_groups.rb +19 -0
  98. data/db/migrate/007_create_drivers.rb +17 -0
  99. data/db/migrate/008_create_databases.rb +43 -0
  100. data/db/schema.rb +91 -0
  101. data/lib/ignore +0 -0
  102. data/log/ignore +0 -0
  103. data/public/404.html +30 -0
  104. data/public/422.html +30 -0
  105. data/public/500.html +30 -0
  106. data/public/dispatch.cgi +10 -0
  107. data/public/dispatch.fcgi +24 -0
  108. data/public/dispatch.rb +10 -0
  109. data/public/favicon.ico +0 -0
  110. data/public/images/rails.png +0 -0
  111. data/public/images/ss1.png +0 -0
  112. data/public/images/ss2.png +0 -0
  113. data/public/images/ss3.png +0 -0
  114. data/public/images/ss4.png +0 -0
  115. data/public/images/ss5.png +0 -0
  116. data/public/images/ss6.png +0 -0
  117. data/public/javascripts/application.js +25 -0
  118. data/public/javascripts/controls.js +963 -0
  119. data/public/javascripts/dragdrop.js +972 -0
  120. data/public/javascripts/effects.js +1120 -0
  121. data/public/javascripts/prototype.js +4225 -0
  122. data/public/robots.txt +4 -0
  123. data/public/stylesheets/railsdb.css +227 -0
  124. data/script/about +3 -0
  125. data/script/console +3 -0
  126. data/script/destroy +3 -0
  127. data/script/generate +3 -0
  128. data/script/performance/benchmarker +3 -0
  129. data/script/performance/profiler +3 -0
  130. data/script/performance/request +3 -0
  131. data/script/plugin +3 -0
  132. data/script/process/inspector +3 -0
  133. data/script/process/reaper +3 -0
  134. data/script/process/spawner +3 -0
  135. data/script/runner +3 -0
  136. data/script/server +3 -0
  137. data/test/fixtures/databases.yml +7 -0
  138. data/test/fixtures/drivers.yml +7 -0
  139. data/test/fixtures/group_permissions.yml +7 -0
  140. data/test/fixtures/groups.yml +7 -0
  141. data/test/fixtures/permissions.yml +7 -0
  142. data/test/fixtures/user_groups.yml +7 -0
  143. data/test/fixtures/users.yml +18 -0
  144. data/test/functional/home_controller_test.rb +8 -0
  145. data/test/test_helper.rb +38 -0
  146. data/test/unit/database_test.rb +8 -0
  147. data/test/unit/driver_test.rb +8 -0
  148. data/test/unit/group_permission_test.rb +8 -0
  149. data/test/unit/group_test.rb +8 -0
  150. data/test/unit/permission_test.rb +8 -0
  151. data/test/unit/user_group_test.rb +8 -0
  152. data/test/unit/user_test.rb +9 -0
  153. data/tmp/pids/ignore +0 -0
  154. data/tmp/sessions/ignore +0 -0
  155. data/tmp/sockets/ignore +0 -0
  156. metadata +287 -0
@@ -0,0 +1,40 @@
1
+
2
+ == RailsDB ==
3
+
4
+ RailsDB Homepage:
5
+
6
+ http://railsdb.org/
7
+
8
+ RailsDB is distributed under the GNU GPL License, version 3:
9
+
10
+ http://railsdb.org/LICENSE.txt
11
+
12
+ RailsDB Docs:
13
+
14
+ http://railsdb.org/docs/
15
+
16
+ Latest Release:
17
+
18
+ http://railsdb.org/src/
19
+
20
+ Git Access:
21
+
22
+ git clone git://github.com/gdonald/railsdb.git railsdb
23
+
24
+ Browse Git Tree:
25
+
26
+ http://github.com/gdonald/railsdb/tree/master
27
+
28
+ Report Bugs:
29
+
30
+ http://railsdb.lighthouseapp.com/projects/12472-railsdb/overview
31
+
32
+ Contact
33
+
34
+ Greg Donald
35
+ gdonald@gmail.com
36
+ http://destiney.com/
37
+
38
+ Ruby on Rails:
39
+
40
+ For more information about Ruby on Rails see the RAILS_README.txt.
@@ -0,0 +1,204 @@
1
+
2
+ == Welcome to Rails
3
+
4
+ Rails is a web-application and persistence framework that includes everything
5
+ needed to create database-backed web-applications according to the
6
+ Model-View-Control pattern of separation. This pattern splits the view (also
7
+ called the presentation) into "dumb" templates that are primarily responsible
8
+ for inserting pre-built data in between HTML tags. The model contains the
9
+ "smart" domain objects (such as Account, Product, Person, Post) that holds all
10
+ the business logic and knows how to persist themselves to a database. The
11
+ controller handles the incoming requests (such as Save New Account, Update
12
+ Product, Show Post) by manipulating the model and directing data to the view.
13
+
14
+ In Rails, the model is handled by what's called an object-relational mapping
15
+ layer entitled Active Record. This layer allows you to present the data from
16
+ database rows as objects and embellish these data objects with business logic
17
+ methods. You can read more about Active Record in
18
+ link:files/vendor/rails/activerecord/README.html.
19
+
20
+ The controller and view are handled by the Action Pack, which handles both
21
+ layers by its two parts: Action View and Action Controller. These two layers
22
+ are bundled in a single package due to their heavy interdependence. This is
23
+ unlike the relationship between the Active Record and Action Pack that is much
24
+ more separate. Each of these packages can be used independently outside of
25
+ Rails. You can read more about Action Pack in
26
+ link:files/vendor/rails/actionpack/README.html.
27
+
28
+
29
+ == Getting Started
30
+
31
+ 1. At the command prompt, start a new Rails application using the <tt>rails</tt> command
32
+ and your application name. Ex: rails myapp
33
+ (If you've downloaded Rails in a complete tgz or zip, this step is already done)
34
+ 2. Change directory into myapp and start the web server: <tt>script/server</tt> (run with --help for options)
35
+ 3. Go to http://localhost:3000/ and get "Welcome aboard: You’re riding the Rails!"
36
+ 4. Follow the guidelines to start developing your application
37
+
38
+
39
+ == Web Servers
40
+
41
+ By default, Rails will try to use Mongrel and lighttpd if they are installed, otherwise
42
+ Rails will use WEBrick, the webserver that ships with Ruby. When you run script/server,
43
+ Rails will check if Mongrel exists, then lighttpd and finally fall back to WEBrick. This ensures
44
+ that you can always get up and running quickly.
45
+
46
+ Mongrel is a Ruby-based webserver with a C component (which requires compilation) that is
47
+ suitable for development and deployment of Rails applications. If you have Ruby Gems installed,
48
+ getting up and running with mongrel is as easy as: <tt>gem install mongrel</tt>.
49
+ More info at: http://mongrel.rubyforge.org
50
+
51
+ If Mongrel is not installed, Rails will look for lighttpd. It's considerably faster than
52
+ Mongrel and WEBrick and also suited for production use, but requires additional
53
+ installation and currently only works well on OS X/Unix (Windows users are encouraged
54
+ to start with Mongrel). We recommend version 1.4.11 and higher. You can download it from
55
+ http://www.lighttpd.net.
56
+
57
+ And finally, if neither Mongrel or lighttpd are installed, Rails will use the built-in Ruby
58
+ web server, WEBrick. WEBrick is a small Ruby web server suitable for development, but not
59
+ for production.
60
+
61
+ But of course its also possible to run Rails on any platform that supports FCGI.
62
+ Apache, LiteSpeed, IIS are just a few. For more information on FCGI,
63
+ please visit: http://wiki.rubyonrails.com/rails/pages/FastCGI
64
+
65
+
66
+ == Debugging Rails
67
+
68
+ Sometimes your application goes wrong. Fortunately there are a lot of tools that
69
+ will help you debug it and get it back on the rails.
70
+
71
+ First area to check is the application log files. Have "tail -f" commands running
72
+ on the server.log and development.log. Rails will automatically display debugging
73
+ and runtime information to these files. Debugging info will also be shown in the
74
+ browser on requests from 127.0.0.1.
75
+
76
+ You can also log your own messages directly into the log file from your code using
77
+ the Ruby logger class from inside your controllers. Example:
78
+
79
+ class WeblogController < ActionController::Base
80
+ def destroy
81
+ @weblog = Weblog.find(params[:id])
82
+ @weblog.destroy
83
+ logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
84
+ end
85
+ end
86
+
87
+ The result will be a message in your log file along the lines of:
88
+
89
+ Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1
90
+
91
+ More information on how to use the logger is at http://www.ruby-doc.org/core/
92
+
93
+ Also, Ruby documentation can be found at http://www.ruby-lang.org/ including:
94
+
95
+ * The Learning Ruby (Pickaxe) Book: http://www.ruby-doc.org/docs/ProgrammingRuby/
96
+ * Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
97
+
98
+ These two online (and free) books will bring you up to speed on the Ruby language
99
+ and also on programming in general.
100
+
101
+
102
+ == Debugger
103
+
104
+ Debugger support is available through the debugger command when you start your Mongrel or
105
+ Webrick server with --debugger. This means that you can break out of execution at any point
106
+ in the code, investigate and change the model, AND then resume execution! Example:
107
+
108
+ class WeblogController < ActionController::Base
109
+ def index
110
+ @posts = Post.find(:all)
111
+ debugger
112
+ end
113
+ end
114
+
115
+ So the controller will accept the action, run the first line, then present you
116
+ with a IRB prompt in the server window. Here you can do things like:
117
+
118
+ >> @posts.inspect
119
+ => "[#<Post:0x14a6be8 @attributes={\"title\"=>nil, \"body\"=>nil, \"id\"=>\"1\"}>,
120
+ #<Post:0x14a6620 @attributes={\"title\"=>\"Rails you know!\", \"body\"=>\"Only ten..\", \"id\"=>\"2\"}>]"
121
+ >> @posts.first.title = "hello from a debugger"
122
+ => "hello from a debugger"
123
+
124
+ ...and even better is that you can examine how your runtime objects actually work:
125
+
126
+ >> f = @posts.first
127
+ => #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
128
+ >> f.
129
+ Display all 152 possibilities? (y or n)
130
+
131
+ Finally, when you're ready to resume execution, you enter "cont"
132
+
133
+
134
+ == Console
135
+
136
+ You can interact with the domain model by starting the console through <tt>script/console</tt>.
137
+ Here you'll have all parts of the application configured, just like it is when the
138
+ application is running. You can inspect domain models, change values, and save to the
139
+ database. Starting the script without arguments will launch it in the development environment.
140
+ Passing an argument will specify a different environment, like <tt>script/console production</tt>.
141
+
142
+ To reload your controllers and models after launching the console run <tt>reload!</tt>
143
+
144
+
145
+ == Description of Contents
146
+
147
+ app
148
+ Holds all the code that's specific to this particular application.
149
+
150
+ app/controllers
151
+ Holds controllers that should be named like weblogs_controller.rb for
152
+ automated URL mapping. All controllers should descend from ApplicationController
153
+ which itself descends from ActionController::Base.
154
+
155
+ app/models
156
+ Holds models that should be named like post.rb.
157
+ Most models will descend from ActiveRecord::Base.
158
+
159
+ app/views
160
+ Holds the template files for the view that should be named like
161
+ weblogs/index.erb for the WeblogsController#index action. All views use eRuby
162
+ syntax.
163
+
164
+ app/views/layouts
165
+ Holds the template files for layouts to be used with views. This models the common
166
+ header/footer method of wrapping views. In your views, define a layout using the
167
+ <tt>layout :default</tt> and create a file named default.erb. Inside default.erb,
168
+ call <% yield %> to render the view using this layout.
169
+
170
+ app/helpers
171
+ Holds view helpers that should be named like weblogs_helper.rb. These are generated
172
+ for you automatically when using script/generate for controllers. Helpers can be used to
173
+ wrap functionality for your views into methods.
174
+
175
+ config
176
+ Configuration files for the Rails environment, the routing map, the database, and other dependencies.
177
+
178
+ db
179
+ Contains the database schema in schema.rb. db/migrate contains all
180
+ the sequence of Migrations for your schema.
181
+
182
+ doc
183
+ This directory is where your application documentation will be stored when generated
184
+ using <tt>rake doc:app</tt>
185
+
186
+ lib
187
+ Application specific libraries. Basically, any kind of custom code that doesn't
188
+ belong under controllers, models, or helpers. This directory is in the load path.
189
+
190
+ public
191
+ The directory available for the web server. Contains subdirectories for images, stylesheets,
192
+ and javascripts. Also contains the dispatchers and the default HTML files. This should be
193
+ set as the DOCUMENT_ROOT of your web server.
194
+
195
+ script
196
+ Helper scripts for automation and generation.
197
+
198
+ test
199
+ Unit and functional tests along with fixtures. When using the script/generate scripts, template
200
+ test files will be generated for you and placed in this directory.
201
+
202
+ vendor
203
+ External libraries that the application depends on. Also includes the plugins subdirectory.
204
+ This directory is in the load path.
@@ -0,0 +1,31 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require( File.join( File.dirname( __FILE__ ), 'config', 'boot' ) )
5
+
6
+ require 'rake'
7
+ require 'rake/testtask'
8
+ require 'rake/rdoctask'
9
+ require 'tasks/rails'
10
+ require 'rcov/rcovtask'
11
+
12
+ namespace :test do
13
+ namespace :coverage do
14
+ desc 'Delete aggregate coverage data.'
15
+ task( :clean ){ rm_f 'coverage.data' }
16
+ end
17
+ desc 'Aggregate code coverage for unit, functional and integration tests'
18
+ task :coverage => 'test:coverage:clean'
19
+ %w[ unit functional integration ].each do |target|
20
+ namespace :coverage do
21
+ Rcov::RcovTask.new( target ) do |t|
22
+ t.libs << 'test'
23
+ t.test_files = FileList[ "test/#{ target }/*_test.rb" ]
24
+ t.output_dir = "test/coverage/#{ target }"
25
+ t.verbose = true
26
+ t.rcov_opts << '--rails --aggregate coverage.data'
27
+ end
28
+ end
29
+ task :coverage => "test:coverage:#{ target }"
30
+ end
31
+ end
@@ -0,0 +1,24 @@
1
+ class ApplicationController < ActionController::Base
2
+
3
+ helper :all
4
+
5
+ protect_from_forgery :secret => '3e1169688926f48ca8731b64a1c3c2126a53404c1527cd97b7db1e87998f4f619b48461c9536a068ac218e6bdb8c551edccbd1c46f82db0b8d7a3f8cced8549c'
6
+
7
+ before_filter :get_current_user
8
+
9
+ # find user by session id
10
+ def get_current_user
11
+ unless session[:user_id]
12
+ @current_user = nil
13
+ return
14
+ end
15
+ @current_user = User.find( :first, :conditions => [ 'id = ?', session[:user_id] ] )
16
+ end
17
+
18
+ def check_site_perm( perm )
19
+ return false if @current_user.nil?
20
+ return true if @current_user.has_perm? perm
21
+ false
22
+ end
23
+
24
+ end
@@ -0,0 +1,274 @@
1
+ class DatabaseController < ApplicationController
2
+
3
+ require 'paginator'
4
+
5
+ include Switch
6
+
7
+ #
8
+ # Modify a table row
9
+ #
10
+ def edit_row
11
+ get_database( params[:id] )
12
+ get_table( @database, params[:table] )
13
+ end
14
+
15
+ #
16
+ # Insert a row into a table
17
+ #
18
+ def insert
19
+ get_database( params[:id] )
20
+ get_table( @database, params[:table] )
21
+ @fields = @table.fields.collect{ |f| f.name }
22
+ if request.post?
23
+ 1.upto( params[params[:table].to_sym].size ) do |x|
24
+ @table.create( params[params[:table].to_sym][x.to_s] )
25
+ end
26
+ flash[:notice] = "#{ params[params[:table].to_sym].size } objects added"
27
+ redirect_to :controller => :database,
28
+ :table => @table.name,
29
+ :action => :browse,
30
+ :id => @database
31
+ end
32
+ end
33
+
34
+ #
35
+ # Get some rows from the table
36
+ #
37
+ def browse
38
+ get_database( params[:id] )
39
+ get_table( @database, params[:table] )
40
+ @fields = @table.fields.collect{ |f| f.name }
41
+ @link_span = LINK_SPAN
42
+ @current_page = params[:page] ? params[:page].to_i : 1
43
+ @active_count = @table.row_count
44
+ @total_pages = ( @active_count.to_f / PER_PAGE.to_f ).ceil
45
+ @order = @table.field_names.first
46
+ @order = 'name' if @table.field_names.include? 'name'
47
+ @order = 'id' if @table.field_names.include? 'id'
48
+ @pager = ::Paginator.new( @active_count, PER_PAGE ) do |offset, per_page|
49
+ @table.find( :all,
50
+ :limit => per_page,
51
+ :offset => offset,
52
+ :order => @order )
53
+ end
54
+ @page = @pager.page( params[:page] )
55
+ end
56
+
57
+ #
58
+ # List the tables in a database.
59
+ #
60
+ def index
61
+ get_database( params[:id] )
62
+ begin
63
+ @tables = @database.tables
64
+ rescue RuntimeError
65
+ flash[:notice] = $!.to_s
66
+ redirect_to :controller => :home, :action => :databases
67
+ end
68
+ end
69
+
70
+ #
71
+ # Increments the session count for blank fields, then it's RJS
72
+ # template loads the new table row partial into the table.
73
+ #
74
+ def blank_field
75
+ get_database( params[:id] )
76
+ session[:field_blanks] += 1
77
+ end
78
+
79
+ #
80
+ # Lists the fields in a table
81
+ #
82
+ def table
83
+ get_database( params[:id] )
84
+ get_table( @database, params[:table] )
85
+ @fields = @table.fields
86
+ end
87
+
88
+ #
89
+ # This parses through everything posted to create a table.
90
+ #
91
+ def add_table
92
+ get_database( params[:id] )
93
+ @errors = {}
94
+ session[:fields] = {}
95
+ field_errors = {}
96
+ field_types = get_field_types( @database.driver.name )
97
+ if request.post?
98
+ @errors['name'] = 'invalid table name' unless params[:name] =~ /[\.0-9a-z\-_]{1,64}/
99
+ @errors['add_id'] = 'selection required' if params[:add_id].nil?
100
+ if params[:fields]['1'][:name].empty?
101
+ field_errors['1'] = {}
102
+ field_errors['1'][:name] = 'at least one field required'
103
+ end
104
+ x = 1
105
+ while params[:fields][x.to_s]
106
+ unless params[:fields][x.to_s][:name].empty?
107
+ session[:fields][x.to_s] = {}
108
+ FIELD_ATTRIBUTES.collect{ |k| k.to_sym }.each do |k|
109
+ session[:fields][x.to_s][k] = params[:fields][x.to_s][k]
110
+ end
111
+ unless field_types.include? params[:fields][x.to_s][:type]
112
+ field_errors[x.to_s] = {}
113
+ field_errors[x.to_s][:type] = 'valid field type required'
114
+ end
115
+ end
116
+ x += 1
117
+ end
118
+ session[:field_blanks] = params[:fields].size
119
+ session[:name] = params[:name].to_s if params[:name] && params[:name] =~ /[\.0-9a-z\-_]{1,64}/
120
+ @errors[:fields] = field_errors unless field_errors.size.zero?
121
+ if @errors.empty?
122
+ begin
123
+ @database.create_tbl( params )
124
+ rescue RuntimeError
125
+ flash.now[:notice] = "An error occured:<br /><br />#{ $!.to_s }"
126
+ else
127
+ session[:field_blanks] = nil
128
+ session[:name] = nil
129
+ flash[:notice] = 'table created'
130
+ redirect_to :controller => :database, :action => :index, :database => params[:database]
131
+ end
132
+ end
133
+ end
134
+ session[:field_blanks] = ( session[:field_blanks] && session[:field_blanks] > 5 ) ? session[:field_blanks] : 5
135
+ @add_id_yes_checked = true
136
+ @add_id_no_checked = false
137
+ if params[:add_id] && params[:add_id] == '0'
138
+ @add_id_yes_checked = false
139
+ @add_id_no_checked = true
140
+ end
141
+ end
142
+
143
+ #
144
+ # This does just what you think it does, hope you had backups..
145
+ #
146
+ def del_table
147
+ get_database( params[:id] )
148
+ get_table( @database, params[:table] )
149
+ if request.post?
150
+ @database.del_table( @table.name )
151
+ flash[:notice] = 'table deleted'
152
+ redirect_to :controller => :database, :id => @database
153
+ end
154
+ end
155
+
156
+ def edit_table
157
+ get_database( params[:id] )
158
+ get_table( @database, params[:table] )
159
+ end
160
+
161
+ def add_fields
162
+ get_database( params[:id] )
163
+ get_table( @database, params[:table] )
164
+ @errors = {}
165
+ session[:fields] = {}
166
+ field_errors = {}
167
+ field_types = get_field_types( @database.driver.name )
168
+ if request.post?
169
+ if params[:fields]['1'][:name].empty?
170
+ field_errors['1'] = {}
171
+ field_errors['1'][:name] = 'at least one field required'
172
+ end
173
+ x = 1
174
+ while params[:fields][x.to_s]
175
+ unless params[:fields][x.to_s][:name].empty?
176
+ session[:fields][x.to_s] = {}
177
+ FIELD_ATTRIBUTES.collect{ |k| k.to_sym }.each do |k|
178
+ session[:fields][x.to_s][k] = params[:fields][x.to_s][k]
179
+ end
180
+ unless field_types.include? params[:fields][x.to_s][:type]
181
+ field_errors[x.to_s] = {}
182
+ field_errors[x.to_s][:type] = 'valid field type required'
183
+ end
184
+ end
185
+ x += 1
186
+ end
187
+ session[:field_blanks] = params[:fields].size
188
+ @errors[:fields] = field_errors unless field_errors.size.zero?
189
+ if @errors.empty?
190
+ begin
191
+ @table.add_fields( params )
192
+ rescue RuntimeError
193
+ flash.now[:notice] = "An error occured:<br /><br />#{ $!.to_s }"
194
+ else
195
+ session[:field_blanks] = nil
196
+ flash[:notice] = 'new fields added'
197
+ redirect_to :controller => :database, :id => @database, :action => :table
198
+ end
199
+ end
200
+ end
201
+ session[:field_blanks] = ( session[:field_blanks] && session[:field_blanks] > 1 ) ? session[:field_blanks] : 5
202
+ end
203
+
204
+ def edit_field
205
+ get_database( params[:id] )
206
+ get_table( @database, params[:table] )
207
+ get_field( @table, params[:field] )
208
+ @errors = {}
209
+ session[:fields] = { '1' => @field.attributes }
210
+ field_errors = { '1' => {} }
211
+ field_types = get_field_types( @database.driver.name )
212
+ if request.post?
213
+ field_errors['1'][:name] = 'field name required' if params[:fields]['1'][:name].empty?
214
+ field_errors['1'][:type] = 'valid field type required'unless field_types.include? params[:fields]['1'][:type]
215
+ FIELD_ATTRIBUTES.collect{ |k| k.to_sym }.each do |k|
216
+ session[:fields]['1'][k] = params[:fields]['1'][k]
217
+ end
218
+ @errors[:fields] = field_errors unless field_errors['1'].size.zero?
219
+ if @errors.empty?
220
+ begin
221
+ @field.update( params )
222
+ rescue RuntimeError
223
+ flash.now[:notice] = "An error occured:<br /><br />#{ $!.to_s }"
224
+ else
225
+ flash[:notice] = 'field updated'
226
+ redirect_to :controller => :database, :id => @database, :action => :table, :table => @table.name
227
+ end
228
+ end
229
+ end
230
+ end
231
+
232
+ def del_field
233
+ get_database( params[:id] )
234
+ get_table( @database, params[:table] )
235
+ if @table.field_count == 1
236
+ flash[:notice] = 'last field cannot be deleted, delete table instead'
237
+ redirect_to :controller => :database, :id => @database
238
+ end
239
+ get_field( @table, params[:field] )
240
+ if request.post?
241
+ @table.del_field( @field.name )
242
+ flash[:notice] = 'field deleted'
243
+ redirect_to :controller => :database, :id => @database, :action => :table, :table => @table.name
244
+ end
245
+ end
246
+
247
+ private
248
+
249
+ def get_field( table, field )
250
+ @field = table.get_field( field )
251
+ if @field.nil?
252
+ flash[:notice] = "field &quot;#{ field }&quot; not found"
253
+ redirect_to :controller => :database, :id => table.database, :table => table.name
254
+ end
255
+ end
256
+
257
+ def get_table( database, table )
258
+ # TODO make this database.get_table
259
+ @table = Table.new( database, table )
260
+ if @table.nil?
261
+ flash[:notice] = "table &quot;#{ table }&quot; not found"
262
+ redirect_to :controller => :home, :action => :databases
263
+ end
264
+ end
265
+
266
+ def get_database( id )
267
+ @database = Database.find( :first, :conditions => [ 'id = ?', id ] )
268
+ if @database.nil?
269
+ flash[:notice] = "database &quot;#{ database }&quot; not found"
270
+ redirect_to :controller => :home, :action => :databases
271
+ end
272
+ end
273
+
274
+ end