authorails 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/AUTHORAILS_README +20 -0
- data/CHANGELOG +1366 -0
- data/MIT-LICENSE +20 -0
- data/README +182 -0
- data/Rakefile +334 -0
- data/bin/about +3 -0
- data/bin/authorails +19 -0
- data/bin/breakpointer +3 -0
- data/bin/console +3 -0
- data/bin/destroy +3 -0
- data/bin/generate +3 -0
- data/bin/performance/benchmarker +3 -0
- data/bin/performance/profiler +3 -0
- data/bin/plugin +3 -0
- data/bin/process/inspector +3 -0
- data/bin/process/reaper +3 -0
- data/bin/process/spawner +3 -0
- data/bin/runner +3 -0
- data/bin/server +3 -0
- data/builtin/rails_info/rails/info.rb +123 -0
- data/builtin/rails_info/rails/info_controller.rb +9 -0
- data/builtin/rails_info/rails/info_helper.rb +2 -0
- data/builtin/rails_info/rails_info_controller.rb +2 -0
- data/configs/apache.conf +40 -0
- data/configs/databases/frontbase.yml +28 -0
- data/configs/databases/mysql.yml +48 -0
- data/configs/databases/oracle.yml +39 -0
- data/configs/databases/postgresql.yml +44 -0
- data/configs/databases/sqlite2.yml +16 -0
- data/configs/databases/sqlite3.yml +19 -0
- data/configs/empty.log +0 -0
- data/configs/lighttpd.conf +54 -0
- data/configs/routes.rb +23 -0
- data/controllers/accounts_controller.rb +11 -0
- data/controllers/admin_controller.rb +76 -0
- data/controllers/begin_controller.rb +5 -0
- data/controllers/login_controller.rb +101 -0
- data/controllers/permission_controller.rb +91 -0
- data/controllers/roles_controller.rb +58 -0
- data/controllers/tables_controller.rb +714 -0
- data/dispatches/dispatch.fcgi +24 -0
- data/dispatches/dispatch.rb +10 -0
- data/dispatches/gateway.cgi +97 -0
- data/doc/README_FOR_APP +2 -0
- data/environments/boot.rb +45 -0
- data/environments/development.rb +21 -0
- data/environments/environment.rb +63 -0
- data/environments/production.rb +18 -0
- data/environments/test.rb +19 -0
- data/fresh_rakefile +10 -0
- data/helpers/application.rb +607 -0
- data/helpers/application_helper.rb +315 -0
- data/helpers/permission_helper.rb +27 -0
- data/helpers/test_helper.rb +28 -0
- data/html/404.html +30 -0
- data/html/500.html +30 -0
- data/html/favicon.ico +0 -0
- data/html/images/ArrowDn.png +0 -0
- data/html/images/ArrowUp.png +0 -0
- data/html/images/arrow.gif +0 -0
- data/html/images/authorails_logo.gif +0 -0
- data/html/images/delete.png +0 -0
- data/html/images/edit.png +0 -0
- data/html/images/minus.gif +0 -0
- data/html/images/plus.gif +0 -0
- data/html/images/rails.png +0 -0
- data/html/images/show.png +0 -0
- data/html/images/spinner.gif +0 -0
- data/html/index.html +273 -0
- data/html/javascripts/application.js +2 -0
- data/html/javascripts/controls.js +833 -0
- data/html/javascripts/dragdrop.js +942 -0
- data/html/javascripts/effects.js +1088 -0
- data/html/javascripts/live_validation.js +4 -0
- data/html/javascripts/prototype.js +2515 -0
- data/html/robots.txt +1 -0
- data/lib/authorails/version.rb +9 -0
- data/lib/binding_of_caller.rb +85 -0
- data/lib/breakpoint.rb +553 -0
- data/lib/breakpoint_client.rb +196 -0
- data/lib/code_statistics.rb +107 -0
- data/lib/commands.rb +17 -0
- data/lib/commands/about.rb +2 -0
- data/lib/commands/breakpointer.rb +1 -0
- data/lib/commands/console.rb +25 -0
- data/lib/commands/destroy.rb +6 -0
- data/lib/commands/generate.rb +6 -0
- data/lib/commands/ncgi/listener +86 -0
- data/lib/commands/ncgi/tracker +69 -0
- data/lib/commands/performance/benchmarker.rb +24 -0
- data/lib/commands/performance/profiler.rb +50 -0
- data/lib/commands/plugin.rb +918 -0
- data/lib/commands/process/inspector.rb +68 -0
- data/lib/commands/process/reaper.rb +149 -0
- data/lib/commands/process/spawner.rb +209 -0
- data/lib/commands/process/spinner.rb +57 -0
- data/lib/commands/runner.rb +48 -0
- data/lib/commands/server.rb +39 -0
- data/lib/commands/servers/base.rb +19 -0
- data/lib/commands/servers/lighttpd.rb +94 -0
- data/lib/commands/servers/mongrel.rb +65 -0
- data/lib/commands/servers/webrick.rb +59 -0
- data/lib/commands/update.rb +4 -0
- data/lib/console_app.rb +27 -0
- data/lib/console_sandbox.rb +6 -0
- data/lib/console_with_helpers.rb +23 -0
- data/lib/dispatcher.rb +173 -0
- data/lib/fcgi_handler.rb +225 -0
- data/lib/initializer.rb +702 -0
- data/lib/rails_generator.rb +43 -0
- data/lib/rails_generator/base.rb +261 -0
- data/lib/rails_generator/commands.rb +581 -0
- data/lib/rails_generator/generated_attribute.rb +42 -0
- data/lib/rails_generator/generators/applications/app/USAGE +16 -0
- data/lib/rails_generator/generators/applications/app/app_generator.rb +341 -0
- data/lib/rails_generator/generators/components/controller/USAGE +30 -0
- data/lib/rails_generator/generators/components/controller/controller_generator.rb +37 -0
- data/lib/rails_generator/generators/components/controller/templates/controller.rb +10 -0
- data/lib/rails_generator/generators/components/controller/templates/functional_test.rb +18 -0
- data/lib/rails_generator/generators/components/controller/templates/helper.rb +2 -0
- data/lib/rails_generator/generators/components/controller/templates/view.rhtml +2 -0
- data/lib/rails_generator/generators/components/integration_test/USAGE +14 -0
- data/lib/rails_generator/generators/components/integration_test/integration_test_generator.rb +16 -0
- data/lib/rails_generator/generators/components/integration_test/templates/integration_test.rb +10 -0
- data/lib/rails_generator/generators/components/mailer/USAGE +18 -0
- data/lib/rails_generator/generators/components/mailer/mailer_generator.rb +34 -0
- data/lib/rails_generator/generators/components/mailer/templates/fixture.rhtml +3 -0
- data/lib/rails_generator/generators/components/mailer/templates/mailer.rb +13 -0
- data/lib/rails_generator/generators/components/mailer/templates/unit_test.rb +37 -0
- data/lib/rails_generator/generators/components/mailer/templates/view.rhtml +3 -0
- data/lib/rails_generator/generators/components/migration/USAGE +14 -0
- data/lib/rails_generator/generators/components/migration/migration_generator.rb +7 -0
- data/lib/rails_generator/generators/components/migration/templates/migration.rb +7 -0
- data/lib/rails_generator/generators/components/model/USAGE +26 -0
- data/lib/rails_generator/generators/components/model/model_generator.rb +38 -0
- data/lib/rails_generator/generators/components/model/templates/fixtures.yml +11 -0
- data/lib/rails_generator/generators/components/model/templates/migration.rb +13 -0
- data/lib/rails_generator/generators/components/model/templates/model.rb +2 -0
- data/lib/rails_generator/generators/components/model/templates/unit_test.rb +10 -0
- data/lib/rails_generator/generators/components/observer/USAGE +15 -0
- data/lib/rails_generator/generators/components/observer/observer_generator.rb +16 -0
- data/lib/rails_generator/generators/components/observer/templates/observer.rb +2 -0
- data/lib/rails_generator/generators/components/observer/templates/unit_test.rb +10 -0
- data/lib/rails_generator/generators/components/plugin/USAGE +35 -0
- data/lib/rails_generator/generators/components/plugin/plugin_generator.rb +38 -0
- data/lib/rails_generator/generators/components/plugin/templates/README +4 -0
- data/lib/rails_generator/generators/components/plugin/templates/Rakefile +22 -0
- data/lib/rails_generator/generators/components/plugin/templates/USAGE +8 -0
- data/lib/rails_generator/generators/components/plugin/templates/generator.rb +8 -0
- data/lib/rails_generator/generators/components/plugin/templates/init.rb +1 -0
- data/lib/rails_generator/generators/components/plugin/templates/install.rb +1 -0
- data/lib/rails_generator/generators/components/plugin/templates/plugin.rb +1 -0
- data/lib/rails_generator/generators/components/plugin/templates/tasks.rake +4 -0
- data/lib/rails_generator/generators/components/plugin/templates/uninstall.rb +1 -0
- data/lib/rails_generator/generators/components/plugin/templates/unit_test.rb +8 -0
- data/lib/rails_generator/generators/components/resource/resource_generator.rb +76 -0
- data/lib/rails_generator/generators/components/resource/templates/USAGE +18 -0
- data/lib/rails_generator/generators/components/resource/templates/controller.rb +2 -0
- data/lib/rails_generator/generators/components/resource/templates/fixtures.yml +11 -0
- data/lib/rails_generator/generators/components/resource/templates/functional_test.rb +20 -0
- data/lib/rails_generator/generators/components/resource/templates/helper.rb +2 -0
- data/lib/rails_generator/generators/components/resource/templates/migration.rb +13 -0
- data/lib/rails_generator/generators/components/resource/templates/model.rb +2 -0
- data/lib/rails_generator/generators/components/resource/templates/unit_test.rb +10 -0
- data/lib/rails_generator/generators/components/scaffold/USAGE +32 -0
- data/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb +208 -0
- data/lib/rails_generator/generators/components/scaffold/templates/controller.rb +134 -0
- data/lib/rails_generator/generators/components/scaffold/templates/form.rhtml +3 -0
- data/lib/rails_generator/generators/components/scaffold/templates/form_scaffolding.rhtml +35 -0
- data/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb +102 -0
- data/lib/rails_generator/generators/components/scaffold/templates/helper.rb +2 -0
- data/lib/rails_generator/generators/components/scaffold/templates/layout.rhtml +17 -0
- data/lib/rails_generator/generators/components/scaffold/templates/partial_list_elements.rhtml +127 -0
- data/lib/rails_generator/generators/components/scaffold/templates/style.css +74 -0
- data/lib/rails_generator/generators/components/scaffold/templates/view_edit.rhtml +75 -0
- data/lib/rails_generator/generators/components/scaffold/templates/view_list.rhtml +30 -0
- data/lib/rails_generator/generators/components/scaffold/templates/view_new.rhtml +8 -0
- data/lib/rails_generator/generators/components/scaffold/templates/view_show.rhtml +37 -0
- data/lib/rails_generator/generators/components/scaffold_resource/USAGE +29 -0
- data/lib/rails_generator/generators/components/scaffold_resource/scaffold_resource_generator.rb +92 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/controller.rb +79 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/fixtures.yml +11 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/functional_test.rb +57 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/helper.rb +2 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/layout.rhtml +17 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/migration.rb +13 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/model.rb +2 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/style.css +74 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/unit_test.rb +10 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/view_edit.rhtml +19 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/view_index.rhtml +24 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/view_new.rhtml +18 -0
- data/lib/rails_generator/generators/components/scaffold_resource/templates/view_show.rhtml +10 -0
- data/lib/rails_generator/generators/components/session_migration/USAGE +15 -0
- data/lib/rails_generator/generators/components/session_migration/session_migration_generator.rb +18 -0
- data/lib/rails_generator/generators/components/session_migration/templates/migration.rb +16 -0
- data/lib/rails_generator/generators/components/web_service/USAGE +28 -0
- data/lib/rails_generator/generators/components/web_service/templates/api_definition.rb +5 -0
- data/lib/rails_generator/generators/components/web_service/templates/controller.rb +8 -0
- data/lib/rails_generator/generators/components/web_service/templates/functional_test.rb +19 -0
- data/lib/rails_generator/generators/components/web_service/web_service_generator.rb +29 -0
- data/lib/rails_generator/lookup.rb +209 -0
- data/lib/rails_generator/manifest.rb +53 -0
- data/lib/rails_generator/options.rb +143 -0
- data/lib/rails_generator/scripts.rb +83 -0
- data/lib/rails_generator/scripts/destroy.rb +7 -0
- data/lib/rails_generator/scripts/generate.rb +7 -0
- data/lib/rails_generator/scripts/update.rb +12 -0
- data/lib/rails_generator/simple_logger.rb +46 -0
- data/lib/rails_generator/spec.rb +44 -0
- data/lib/railties_path.rb +1 -0
- data/lib/ruby_version_check.rb +17 -0
- data/lib/rubyprof_ext.rb +35 -0
- data/lib/tasks/databases.rake +187 -0
- data/lib/tasks/documentation.rake +82 -0
- data/lib/tasks/framework.rake +112 -0
- data/lib/tasks/log.rake +9 -0
- data/lib/tasks/misc.rake +4 -0
- data/lib/tasks/pre_namespace_aliases.rake +53 -0
- data/lib/tasks/rails.rb +8 -0
- data/lib/tasks/statistics.rake +18 -0
- data/lib/tasks/testing.rake +120 -0
- data/lib/tasks/tmp.rake +37 -0
- data/lib/test_help.rb +19 -0
- data/lib/webrick_server.rb +166 -0
- data/libs/migration_helper.rb +11 -0
- data/migrations/001_create_logins.rb +22 -0
- data/migrations/002_create_admins.rb +16 -0
- data/migrations/003_create_roles.rb +13 -0
- data/migrations/004_create_tables.rb +11 -0
- data/migrations/005_create_attr_types.rb +13 -0
- data/migrations/006_create_table_fields.rb +22 -0
- data/migrations/007_create_tab_operations.rb +13 -0
- data/migrations/008_create_attr_operations.rb +13 -0
- data/migrations/009_create_tab_permissions.rb +21 -0
- data/migrations/010_create_attr_permissions.rb +21 -0
- data/migrations/011_create_relations.rb +23 -0
- data/migrations/012_create_scaffolds.rb +11 -0
- data/migrations/013_create_relation_permissions.rb +20 -0
- data/models/admin.rb +38 -0
- data/models/attr_operation.rb +3 -0
- data/models/attr_permission.rb +5 -0
- data/models/attr_type.rb +3 -0
- data/models/login.rb +51 -0
- data/models/relation.rb +5 -0
- data/models/relation_permission.rb +4 -0
- data/models/role.rb +13 -0
- data/models/scaffold.rb +2 -0
- data/models/tab_operation.rb +3 -0
- data/models/tab_permission.rb +5 -0
- data/models/table.rb +18 -0
- data/models/table_field.rb +6 -0
- data/plugins/redhillonrails_core/CHANGELOG +186 -0
- data/plugins/redhillonrails_core/MIT-LICENSE +20 -0
- data/plugins/redhillonrails_core/README +152 -0
- data/plugins/redhillonrails_core/init.rb +1 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/base.rb +54 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/abstract_adapter.rb +46 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/column.rb +21 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/foreign_key_definition.rb +26 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/index_definition.rb +11 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/mysql_adapter.rb +94 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/mysql_column.rb +8 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/postgresql_adapter.rb +131 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/schema_statements.rb +23 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/sqlite3_adapter.rb +9 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/connection_adapters/table_definition.rb +27 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/schema.rb +25 -0
- data/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/schema_dumper.rb +58 -0
- data/plugins/redhillonrails_core/lib/redhillonrails_core.rb +19 -0
- data/plugins/redhillonrails_core/tasks/db/comments.rake +9 -0
- data/plugins/schema_validations/CHANGELOG +149 -0
- data/plugins/schema_validations/MIT-LICENSE +20 -0
- data/plugins/schema_validations/README +54 -0
- data/plugins/schema_validations/about.yml +5 -0
- data/plugins/schema_validations/init.rb +1 -0
- data/plugins/schema_validations/install.rb +1 -0
- data/plugins/schema_validations/lib/red_hill_consulting/schema_validations/active_record/base.rb +108 -0
- data/plugins/schema_validations/lib/schema_validations.rb +1 -0
- data/plugins/svn/README +69 -0
- data/plugins/svn/Rakefile +22 -0
- data/plugins/svn/assets/javascripts/live_validation.js +4 -0
- data/plugins/svn/assets/stylesheets/live_validation.css +28 -0
- data/plugins/svn/init.rb +2 -0
- data/plugins/svn/install.rb +1 -0
- data/plugins/svn/lib/form_helpers.rb +49 -0
- data/plugins/svn/lib/live_validations.rb +73 -0
- data/plugins/svn/tasks/live_validation_tasks.rake +17 -0
- data/plugins/svn/test/form_helpers_test.rb +214 -0
- data/plugins/svn/test/live_validations_test.rb +124 -0
- data/plugins/svn/test/resource.rb +17 -0
- data/plugins/svn/uninstall.rb +1 -0
- data/stylesheets/live_validation.css +28 -0
- data/stylesheets/print.css +72 -0
- data/stylesheets/scaffold.css +175 -0
- data/views/accounts/index.rhtml +12 -0
- data/views/accounts/show.rhtml +5 -0
- data/views/admin/_form.rhtml +15 -0
- data/views/admin/add_user.rhtml +21 -0
- data/views/admin/change_pwd.rhtml +23 -0
- data/views/admin/edit_user.rhtml +12 -0
- data/views/admin/index.rhtml +19 -0
- data/views/begin/index.rhtml +0 -0
- data/views/layouts/general.rhtml +59 -0
- data/views/layouts/login.rhtml +18 -0
- data/views/login/change_pwd.rhtml +21 -0
- data/views/login/expired_pwd.rhtml +19 -0
- data/views/login/index.rhtml +15 -0
- data/views/permission/index.rhtml +17 -0
- data/views/permission/relations.rhtml +28 -0
- data/views/permission/roles.rhtml +45 -0
- data/views/permission/tables.rhtml +53 -0
- data/views/roles/_form.rhtml +8 -0
- data/views/roles/edit.rhtml +11 -0
- data/views/roles/list.rhtml +28 -0
- data/views/roles/new.rhtml +8 -0
- data/views/roles/show.rhtml +14 -0
- data/views/shared_views/_advanced_search.rhtml +35 -0
- data/views/shared_views/_child_elements.rhtml +30 -0
- data/views/shared_views/_childs.rhtml +20 -0
- data/views/shared_views/_father_elements.rhtml +27 -0
- data/views/shared_views/_fathers.rhtml +21 -0
- data/views/shared_views/_fathers_header.rhtml +58 -0
- data/views/shared_views/_many_to_many_elements.rhtml +50 -0
- data/views/shared_views/_many_to_manys.rhtml +18 -0
- data/views/shared_views/_one_to_one_elements.rhtml +24 -0
- data/views/shared_views/_one_to_one_father_elements.rhtml +26 -0
- data/views/shared_views/_one_to_ones.rhtml +42 -0
- data/views/shared_views/_one_to_ones_header.rhtml +117 -0
- data/views/shared_views/edit_child.rhtml +89 -0
- data/views/shared_views/edit_many.rhtml +75 -0
- data/views/tables/_cascade.rhtml +10 -0
- data/views/tables/_field.rhtml +10 -0
- data/views/tables/_fk.rhtml +9 -0
- data/views/tables/_form.rhtml +9 -0
- data/views/tables/_join_table_name.rhtml +16 -0
- data/views/tables/_relation_types.rhtml +5 -0
- data/views/tables/_tables.rhtml +15 -0
- data/views/tables/attributes.rhtml +15 -0
- data/views/tables/edit.rhtml +11 -0
- data/views/tables/edit_field.rhtml +18 -0
- data/views/tables/list.rhtml +39 -0
- data/views/tables/new.rhtml +8 -0
- data/views/tables/new_field.rhtml +25 -0
- data/views/tables/relations.rhtml +24 -0
- data/views/tables/show.rhtml +24 -0
- metadata +518 -0
|
@@ -0,0 +1,714 @@
|
|
|
1
|
+
class TablesController < ApplicationController
|
|
2
|
+
|
|
3
|
+
def index
|
|
4
|
+
list
|
|
5
|
+
render :action => 'list'
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
# GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
|
|
9
|
+
verify :method => :post, :only => [ :destroy, :create, :update, :create_field, :update_field, :destroy_field ],
|
|
10
|
+
:redirect_to => { :action => :list }
|
|
11
|
+
|
|
12
|
+
verify :session => :superuser, :redirect_to => {:controller => :admin}, :add_flash => {:notice => "Please login first!"}
|
|
13
|
+
|
|
14
|
+
skip_before_filter :authorize
|
|
15
|
+
|
|
16
|
+
def list
|
|
17
|
+
@table_pages, @tables = paginate :tables, :per_page => 10
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def show
|
|
21
|
+
@table = Table.find(params[:id],:include => :table_fields)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def new
|
|
25
|
+
@table = Table.new
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def create
|
|
29
|
+
if params[:table][:name]==""
|
|
30
|
+
flash[:notice] = "Table name can't be blank!"
|
|
31
|
+
redirect_to :action => 'new'
|
|
32
|
+
else
|
|
33
|
+
@table = Table.new(:name =>prepare_for_table(params[:table][:name].pluralize))
|
|
34
|
+
if @table.save
|
|
35
|
+
flash[:notice] = 'Define the attributes to complete the table\'s creation.'
|
|
36
|
+
redirect_to :action => 'attributes', :id => @table
|
|
37
|
+
else
|
|
38
|
+
@table.name=params[:table][:name]
|
|
39
|
+
render :action => 'new'
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def edit
|
|
45
|
+
@table = Table.find(params[:id])
|
|
46
|
+
@table[:name]=@table[:name].singularize
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def update
|
|
50
|
+
if params[:table][:name]==""
|
|
51
|
+
flash[:notice] = "Table name can't be blank!"
|
|
52
|
+
redirect_to :action => 'edit', :id => params[:id]
|
|
53
|
+
else
|
|
54
|
+
if params[:table][:name]==params[:old_name]
|
|
55
|
+
flash[:notice] = 'Table named unchanged.'
|
|
56
|
+
redirect_to :action => 'list'
|
|
57
|
+
else
|
|
58
|
+
@table = Table.find(params[:id])
|
|
59
|
+
params[:table][:name]=prepare_for_table(params[:table][:name].pluralize)
|
|
60
|
+
#we need to update the foreign key names
|
|
61
|
+
related=find_related("#{params[:old_name].pluralize}")
|
|
62
|
+
related.each do |relation|
|
|
63
|
+
if (relation.relation_type=="one-to-many" or relation.relation_type=="one-to-one") and relation.referenced == params[:old_name].pluralize #if the old table was referenced,we have to rename the OLDTABLENAME_id in NEWTABLENAME_id
|
|
64
|
+
table_field=TableField.find(relation.table_field_id)
|
|
65
|
+
table_field.name = "#{params[:table][:name].singularize}_id"
|
|
66
|
+
table_field.save
|
|
67
|
+
ActiveRecord::Migration.rename_column relation.referencing,"#{params[:old_name]}_id","#{params[:table][:name].singularize}_id"
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
if @table.update_attributes(params[:table])
|
|
71
|
+
|
|
72
|
+
related=find_related(@table.name)
|
|
73
|
+
related.each do |relation|
|
|
74
|
+
if relation.relation_type=="many-to-many"
|
|
75
|
+
if relation.referencing == @table.name
|
|
76
|
+
if @table.name < relation.referenced
|
|
77
|
+
join_table_name="#{@table.name}_#{relation.referenced}"
|
|
78
|
+
else
|
|
79
|
+
join_table_name="#{relation.referenced}_#{@table.name}"
|
|
80
|
+
end
|
|
81
|
+
else
|
|
82
|
+
if relation.referencing < @table.name
|
|
83
|
+
join_table_name="#{relation.referencing}_#{@table.name}"
|
|
84
|
+
else
|
|
85
|
+
join_table_name="#{@table.name}_#{relation.referencing}"
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
ActiveRecord::Migration.rename_table relation.join_table, join_table_name #renaming the join table of database
|
|
90
|
+
join=Table.find_by_name(relation.join_table) #renaming the join table from the list of the tables in Tables
|
|
91
|
+
join.name=join_table_name
|
|
92
|
+
join.save
|
|
93
|
+
r=Relation.find(relation.id_r) #renaming the join table name from the relation row
|
|
94
|
+
r.join_table=join_table_name
|
|
95
|
+
r.save
|
|
96
|
+
|
|
97
|
+
#finding old join table name for update the join table name in the 1-n relations for join table
|
|
98
|
+
r=Relation.find_by_sql("SELECT t2.name as referencing,t1.name as referenced,join_table,r.id as id_r
|
|
99
|
+
FROM relations r,table_fields tf,tables t1,tables t2
|
|
100
|
+
WHERE r.table_field_id=tf.id and r.table_id=t1.id and tf.table_id=t2.id and for_join_table=\"yes\" and t2.name=\"#{join_table_name}\"")
|
|
101
|
+
r.each {|row|
|
|
102
|
+
rel=Relation.find(row.id_r)
|
|
103
|
+
rel.join_table=join_table_name
|
|
104
|
+
rel.save
|
|
105
|
+
}
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
ActiveRecord::Migration.rename_table params[:old_name].pluralize, @table[:name]
|
|
110
|
+
|
|
111
|
+
if join=Relation.find_all_by_join_table(params[:old_name].pluralize) #if the renamed table is a join table
|
|
112
|
+
join.each {|r|
|
|
113
|
+
r.join_table=@table.name
|
|
114
|
+
r.save
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
flash[:notice] = 'Table was successfully updated.'
|
|
120
|
+
redirect_to :action => 'show', :id => @table
|
|
121
|
+
else
|
|
122
|
+
@table.name=Table.find(params[:id]).name.singularize
|
|
123
|
+
render :action => 'edit'
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def destroy
|
|
130
|
+
table=Table.find(params[:id])
|
|
131
|
+
|
|
132
|
+
#finding if the table to destroy contains relations
|
|
133
|
+
related=find_related(table.name)
|
|
134
|
+
to_erase=[]
|
|
135
|
+
#deleting relation from model files
|
|
136
|
+
related.each do |row|
|
|
137
|
+
if row.relation_type=="one-to-one" || row.relation_type=="one-to-many"
|
|
138
|
+
TableField.destroy(row.table_field_id) #removing column REFERENCEDTABLE_id from list of table fields
|
|
139
|
+
begin
|
|
140
|
+
ActiveRecord::Migration.remove_column row.referencing,"#{row.referenced.singularize}_id" #removing column REFERENCEDTABLE_id from the referencing table
|
|
141
|
+
rescue
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
if row.relation_type=="many-to-many" #if the table has a many-to-many relation,we have to destroy the join table
|
|
145
|
+
to_erase << row.join_table #but first we have to remove all column of 1-1 and 1-n relations, else an exception will be thrown
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
to_erase.each do |t|
|
|
149
|
+
begin
|
|
150
|
+
ActiveRecord::Migration.drop_table t #destroying join tables
|
|
151
|
+
Table.find_by_name(t).destroy
|
|
152
|
+
|
|
153
|
+
rescue
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
begin
|
|
157
|
+
ActiveRecord::Migration.drop_table table.name #drop the table destroyed
|
|
158
|
+
rescue
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
#deleting relations if table deleted is a join table
|
|
162
|
+
Relation.find_all_by_join_table(table.name).each {|r| r.destroy}
|
|
163
|
+
|
|
164
|
+
table.destroy
|
|
165
|
+
|
|
166
|
+
flash[:notice] = 'Table was succesfully deleted.'
|
|
167
|
+
redirect_to :action => 'list'
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def attributes
|
|
171
|
+
@table = Table.find(params[:id])
|
|
172
|
+
@attr_types = AttrType.find(:all)
|
|
173
|
+
session[:rows] = 1
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def add_field
|
|
177
|
+
@attr_types = AttrType.find(:all)
|
|
178
|
+
session[:rows]+=1
|
|
179
|
+
render :partial => "field"
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def generate_table
|
|
183
|
+
rows=session[:rows]
|
|
184
|
+
table_id=params[:table_id]
|
|
185
|
+
table_name=params[:table_name]
|
|
186
|
+
if rows > 0
|
|
187
|
+
#we insert the primary key into the table containing the table fields
|
|
188
|
+
id_integer=AttrType.find_by_name("integer").id
|
|
189
|
+
TableField.create(:name => "id", :attr_type_id => id_integer, :table_id => table_id)
|
|
190
|
+
|
|
191
|
+
i=1
|
|
192
|
+
while i <= rows
|
|
193
|
+
unless params["table_field#{i}"]["attr_name"]==""
|
|
194
|
+
TableField.create(:name => prepare_for_table(params["table_field#{i}"]["attr_name"]), :attr_type_id => params["table_field#{i}"]["attr_type"], :is_unique => params["table_field#{i}"]["unique"],:is_not_null => params["table_field#{i}"]["not_null"], :table_id => table_id)
|
|
195
|
+
end
|
|
196
|
+
i+=1
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
ActiveRecord::Migration.create_table table_name, :force => true do |t|
|
|
200
|
+
i=1
|
|
201
|
+
@unique_fields=[] #will contain all fields that must be unique
|
|
202
|
+
while i <= rows
|
|
203
|
+
unless params["table_field#{i}"]["attr_name"]==""
|
|
204
|
+
if params["table_field#{i}"]["not_null"]=="1" #if the not-null checkbox has been checked
|
|
205
|
+
null = false
|
|
206
|
+
else
|
|
207
|
+
null = true
|
|
208
|
+
end
|
|
209
|
+
if params["table_field#{i}"]["unique"]=="1" #if the unique checkbox has been checked
|
|
210
|
+
@unique_fields << params["table_field#{i}"]["attr_name"].gsub(/ +/,"_").downcase
|
|
211
|
+
end
|
|
212
|
+
t.column params["table_field#{i}"]["attr_name"].gsub(/ +/,"_").downcase, AttrType.find(params["table_field#{i}"]["attr_type"]).name, :null => null
|
|
213
|
+
end
|
|
214
|
+
i+=1
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
@unique_fields.each {|f|
|
|
218
|
+
ActiveRecord::Migration.add_index table_name, f, :unique => true
|
|
219
|
+
}
|
|
220
|
+
flash[:notice] = "Table's attributes were succesfully created."
|
|
221
|
+
redirect_to :action => 'list', :controller => 'tables'
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
def relations
|
|
226
|
+
@relations=Relation.find(:all, :conditions => "for_join_table <> \"yes\"")
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
def add_relation
|
|
230
|
+
|
|
231
|
+
@all_tables=Table.find_all.map {|u| [u.name,u.id] }
|
|
232
|
+
@tables="<option></option>"
|
|
233
|
+
@all_tables.each {|t| string="<option value='#{t[1]}'>#{t[0]}</option>"
|
|
234
|
+
@tables += string}
|
|
235
|
+
render :partial => "tables"
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
def check_tables
|
|
239
|
+
@first_table=params[:first_table]
|
|
240
|
+
@second_table=params[:second_table]
|
|
241
|
+
@all_tables=Table.find_all.map {|u| [u.name,u.id] }
|
|
242
|
+
@tables="<option></option>"
|
|
243
|
+
@all_tables.each {|t| string="<option value='#{t[1]}'>#{t[0]}</option>"
|
|
244
|
+
@tables += string}
|
|
245
|
+
if @first_table.empty? or @second_table.empty?
|
|
246
|
+
@error="Please select the tables!"
|
|
247
|
+
render :partial => "tables"
|
|
248
|
+
else
|
|
249
|
+
if @first_table==@second_table
|
|
250
|
+
@error="Please select different tables!"
|
|
251
|
+
render :partial => "tables"
|
|
252
|
+
else
|
|
253
|
+
@first=Table.find(@first_table)
|
|
254
|
+
@second=Table.find(@second_table)
|
|
255
|
+
render :partial => "relation_types"
|
|
256
|
+
end
|
|
257
|
+
end
|
|
258
|
+
else
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
def foreign_key
|
|
262
|
+
@field=TableField.find(params[:id])
|
|
263
|
+
@tables=Table.find_all
|
|
264
|
+
@type=params[:type].to_s
|
|
265
|
+
render :partial => "fk"
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
def join_table_name
|
|
269
|
+
@first=Table.find(params[:first])
|
|
270
|
+
@second=Table.find(params[:second])
|
|
271
|
+
session[:rows] = 0
|
|
272
|
+
render :partial => "join_table_name"
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
def cascade
|
|
276
|
+
@relation=params[:relation_type]
|
|
277
|
+
@referencing=Table.find(params[:second])
|
|
278
|
+
@referenced=Table.find(params[:first])
|
|
279
|
+
render :partial => "cascade"
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
def generate_fk
|
|
283
|
+
relation=params[:relation_type]
|
|
284
|
+
dependent=params[:cascade]
|
|
285
|
+
|
|
286
|
+
#for one-to-one and one-to-many relations, we need to add a column "REFERENTED_TABLE_id" into the referenting table
|
|
287
|
+
if relation=="one-to-one" or relation=="one-to-many"
|
|
288
|
+
referenting_table=Table.find(params[:second])
|
|
289
|
+
referented_table=Table.find(params[:first])
|
|
290
|
+
create_column(referenting_table,referented_table)
|
|
291
|
+
create_relation(referenting_table,referented_table,relation,dependent,"no","")
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
#name of the tables with many-to-many relation
|
|
295
|
+
first_table=Table.find(params[:first])
|
|
296
|
+
second_table=Table.find(params[:second])
|
|
297
|
+
|
|
298
|
+
if relation=="many-to-many"
|
|
299
|
+
#the name of the join table
|
|
300
|
+
if params[:table_name] != "" #a name for the join table has been selected
|
|
301
|
+
table_name=prepare_for_table(params[:table_name].pluralize)
|
|
302
|
+
else #we create a default name composed of the two table names in alphabetical order
|
|
303
|
+
if first_table.name < second_table.name
|
|
304
|
+
table_name="#{first_table.name}_#{second_table.name}"
|
|
305
|
+
else
|
|
306
|
+
table_name="#{second_table.name}_#{first_table.name}"
|
|
307
|
+
end
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
#the join table must be found or added in Tables table
|
|
311
|
+
unless join_table=Table.find_by_name(table_name)
|
|
312
|
+
join_table=Table.create(:name => table_name)
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
#the number of rows of the join table
|
|
316
|
+
rows=session[:rows]
|
|
317
|
+
if rows > 0 #if columns are added
|
|
318
|
+
i=1
|
|
319
|
+
while i <= rows
|
|
320
|
+
unless params["table_field#{i}"]["attr_name"]==""
|
|
321
|
+
TableField.create(:name => prepare_for_table(params["table_field#{i}"]["attr_name"]), :attr_type_id => params["table_field#{i}"]["attr_type"], :is_unique => params["table_field#{i}"]["unique"], :is_not_null => params["table_field#{i}"]["not_null"], :table_id => join_table.id)
|
|
322
|
+
end
|
|
323
|
+
i+=1
|
|
324
|
+
end
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
#we need to create a join table that will contains the relations between tables
|
|
328
|
+
|
|
329
|
+
ActiveRecord::Migration.create_table table_name, :force => true do |t|
|
|
330
|
+
i=1
|
|
331
|
+
while i <= rows
|
|
332
|
+
@unique_fields=[] #will contain all fields that must be unique
|
|
333
|
+
unless params["table_field#{i}"]["attr_name"]==""
|
|
334
|
+
if params["table_field#{i}"]["not_null"]=="1" #if the not-null checkbox has been checked
|
|
335
|
+
null = false
|
|
336
|
+
else
|
|
337
|
+
null = true
|
|
338
|
+
end
|
|
339
|
+
if params["table_field#{i}"]["unique"]=="1" #if the unique checkbox has been checked
|
|
340
|
+
@unique_fields << params["table_field#{i}"]["attr_name"].gsub(/ +/,"_").downcase
|
|
341
|
+
end
|
|
342
|
+
t.column prepare_for_table(params["table_field#{i}"]["attr_name"]), AttrType.find(params["table_field#{i}"]["attr_type"]).name, :null => null
|
|
343
|
+
end
|
|
344
|
+
i+=1
|
|
345
|
+
end
|
|
346
|
+
end
|
|
347
|
+
@unique_fields.each {|f|
|
|
348
|
+
ActiveRecord::Migration.add_index table_name, f, :unique => true
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
create_column(join_table,first_table)
|
|
354
|
+
create_column(join_table,second_table)
|
|
355
|
+
create_relation(join_table,first_table,"one-to-many",dependent,"yes",join_table.name)
|
|
356
|
+
create_relation(join_table,second_table,"one-to-many",dependent,"yes",join_table.name)
|
|
357
|
+
create_relation(first_table,second_table,"many-to-many",dependent,"no",join_table.name)
|
|
358
|
+
end
|
|
359
|
+
|
|
360
|
+
flash[:notice] = "Relation succesfully created."
|
|
361
|
+
redirect_to :action => "relations"
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
def destroy_relation
|
|
365
|
+
relation=find_related_table(params[:id])
|
|
366
|
+
|
|
367
|
+
if relation.relation_type=="many-to-many"
|
|
368
|
+
table=Table.find_by_name(relation.join_table).destroy
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
if relation.relation_type=="one-to-one" || relation.relation_type=="one-to-many"
|
|
372
|
+
begin
|
|
373
|
+
ActiveRecord::Migration.remove_column relation.referencing,"#{relation.referenced.singularize}_id"
|
|
374
|
+
rescue
|
|
375
|
+
end
|
|
376
|
+
end
|
|
377
|
+
Relation.find(params[:id]).destroy
|
|
378
|
+
flash[:notice]="Relation was succesfully destroy."
|
|
379
|
+
redirect_to :action => "relations"
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
def scaffold
|
|
384
|
+
scaffolds = Scaffold.find_all #list of all scaffolds created
|
|
385
|
+
scaffolds.each do |l| #deleting all scaffolds
|
|
386
|
+
system("ruby script/destroy scaffold #{l.name}")
|
|
387
|
+
FileUtils.remove_dir("app/views/#{l.name.pluralize}" , :force => true)
|
|
388
|
+
l.destroy
|
|
389
|
+
end
|
|
390
|
+
tables = Table.find_by_sql("SELECT name FROM tables") #selecting all tables
|
|
391
|
+
tables.each do |t|
|
|
392
|
+
system("ruby script/generate scaffold #{t.name.singularize} -f") #creating scaffolds for the tables
|
|
393
|
+
Scaffold.create(:name => t.name.singularize)
|
|
394
|
+
end
|
|
395
|
+
relations = find_all_relations
|
|
396
|
+
relations.each do |r|
|
|
397
|
+
if r.relation_type=="one-to-many" and r.for_join_table=="no"
|
|
398
|
+
write_into_model_file(r.referencing.singularize,"belongs_to",r.referenced.singularize,"")
|
|
399
|
+
write_into_model_file(r.referenced.singularize,"has_many",r.referencing,",:dependent => :#{r.dependent}")
|
|
400
|
+
end
|
|
401
|
+
|
|
402
|
+
if r.relation_type=="one-to-one"
|
|
403
|
+
write_into_model_file(r.referencing.singularize,"belongs_to",r.referenced.singularize,"")
|
|
404
|
+
write_into_model_file(r.referenced.singularize,"has_one",r.referencing.singularize,",:dependent => :#{r.dependent}")
|
|
405
|
+
end
|
|
406
|
+
|
|
407
|
+
if r.relation_type=="many-to-many"
|
|
408
|
+
#relations between the join table and the first table
|
|
409
|
+
write_into_model_file(r.join_table.singularize,"belongs_to",r.referencing.singularize,"")
|
|
410
|
+
write_into_model_file(r.referencing.singularize,"has_many",r.join_table,",:dependent => :#{r.dependent}")
|
|
411
|
+
#relations between the join table and the second table
|
|
412
|
+
write_into_model_file(r.join_table.singularize,"belongs_to",r.referenced.singularize,"")
|
|
413
|
+
write_into_model_file(r.referenced.singularize,"has_many",r.join_table,",:dependent => :#{r.dependent}")
|
|
414
|
+
write_validation(r.join_table.singularize,r.join_table.singularize.camelize,r.referencing.singularize,r.referenced.singularize)
|
|
415
|
+
end
|
|
416
|
+
|
|
417
|
+
end
|
|
418
|
+
flash[:notice]="Scaffolds succesfully created."
|
|
419
|
+
redirect_to :controller => "begin"
|
|
420
|
+
end
|
|
421
|
+
|
|
422
|
+
def new_field
|
|
423
|
+
@table = Table.find(params[:id])
|
|
424
|
+
@attr_types = AttrType.find(:all)
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
def create_field
|
|
428
|
+
@table=Table.find(params[:id])
|
|
429
|
+
if params[:table_field]=="" #field name can't be blank
|
|
430
|
+
flash[:notice]="Field name can't be blank!"
|
|
431
|
+
redirect_to :action => "new_field", :id => @table
|
|
432
|
+
else
|
|
433
|
+
field_name=prepare_for_table(params[:table_field])
|
|
434
|
+
#field name can't be the same of another field of the same table
|
|
435
|
+
TableField.find_all_by_table_id(params[:id]).each {|f|
|
|
436
|
+
if f.name==field_name and f.id.to_i!=params[:id].to_i
|
|
437
|
+
flash[:notice]="Field name already exists!"
|
|
438
|
+
redirect_to :action => "new_field", :id => @table and return
|
|
439
|
+
end
|
|
440
|
+
}
|
|
441
|
+
TableField.transaction do
|
|
442
|
+
if params["not_null"]=="1" #if the not-null checkbox has been checked
|
|
443
|
+
null = false
|
|
444
|
+
else
|
|
445
|
+
null = true
|
|
446
|
+
end
|
|
447
|
+
if params["unique"]=="1" #if the not-null checkbox has been checked
|
|
448
|
+
unique = true
|
|
449
|
+
else
|
|
450
|
+
unique = false
|
|
451
|
+
end
|
|
452
|
+
TableField.create(:name => field_name, :attr_type_id => params[:type], :is_not_null => !null, :is_unique => unique, :table_id => @table.id)
|
|
453
|
+
begin
|
|
454
|
+
ActiveRecord::Migration.add_column(@table.name,field_name,AttrType.find(params[:type]).name.to_sym, :null => null)
|
|
455
|
+
rescue
|
|
456
|
+
ActiveRecord::Migration.create_table @table.name do |f| end
|
|
457
|
+
id_integer=AttrType.find_by_name("integer").id
|
|
458
|
+
TableField.create(:name => "id", :attr_type_id => id_integer, :table_id => @table.id)
|
|
459
|
+
ActiveRecord::Migration.add_column(@table.name,field_name,AttrType.find(params[:type]).name.to_sym, :null => null)
|
|
460
|
+
end
|
|
461
|
+
if params["unique"]=="1" #if the unique checkbox has been checked
|
|
462
|
+
ActiveRecord::Migration.add_index(@table.name,field_name,:unique => true)
|
|
463
|
+
end
|
|
464
|
+
end
|
|
465
|
+
flash[:notice]="Field succesfully created."
|
|
466
|
+
redirect_to :action => "show", :id => @table
|
|
467
|
+
end
|
|
468
|
+
end
|
|
469
|
+
|
|
470
|
+
def edit_field
|
|
471
|
+
@field=TableField.find(params[:id],:include => :table)
|
|
472
|
+
@attr_types = AttrType.find(:all)
|
|
473
|
+
end
|
|
474
|
+
|
|
475
|
+
def update_field
|
|
476
|
+
@field=TableField.find(params[:id],:include => :table)
|
|
477
|
+
if params[:table_field]=="" #field name can't be blank
|
|
478
|
+
flash[:notice]="Field name can't be blank!"
|
|
479
|
+
redirect_to :action => "edit_field", :id => @field
|
|
480
|
+
else
|
|
481
|
+
field_name=prepare_for_table(params[:table_field])
|
|
482
|
+
#field name can't be the same of another field of the same table
|
|
483
|
+
TableField.find_all_by_table_id(@field.table.id).each {|f|
|
|
484
|
+
if f.name==field_name and f.id.to_i!=params[:id].to_i
|
|
485
|
+
flash[:notice]="Field name already exists!"
|
|
486
|
+
redirect_to :action => "edit_field", :id => @field and return
|
|
487
|
+
end
|
|
488
|
+
}
|
|
489
|
+
if params[:is_not_null].nil?
|
|
490
|
+
is_not_null=0
|
|
491
|
+
null=true
|
|
492
|
+
else
|
|
493
|
+
is_not_null=1
|
|
494
|
+
null=false
|
|
495
|
+
end
|
|
496
|
+
if params[:is_unique].nil?
|
|
497
|
+
is_unique=0
|
|
498
|
+
#removing the index if the column was unique
|
|
499
|
+
ActiveRecord::Migration.remove_index(@field.table.name,field_name) if @field.is_unique
|
|
500
|
+
else
|
|
501
|
+
is_unique=1
|
|
502
|
+
#adding the index if the column wasn't unique
|
|
503
|
+
ActiveRecord::Migration.add_index(@field.table.name,field_name,:unique => true) unless @field.is_unique
|
|
504
|
+
end
|
|
505
|
+
TableField.find(@field).update_attributes(:name => field_name, :attr_type_id => params[:type], :is_not_null => is_not_null, :is_unique => is_unique)
|
|
506
|
+
ActiveRecord::Migration.rename_column(@field.table.name,params[:old_field_name],field_name)
|
|
507
|
+
ActiveRecord::Migration.change_column(@field.table.name,field_name,AttrType.find(params[:type]).name.to_sym, :null => null)
|
|
508
|
+
|
|
509
|
+
flash[:notice]="Field name succesfully updated."
|
|
510
|
+
redirect_to :action => "show", :id => @field.table
|
|
511
|
+
end
|
|
512
|
+
end
|
|
513
|
+
|
|
514
|
+
def destroy_field
|
|
515
|
+
field=TableField.find(params[:id],:include => :table)
|
|
516
|
+
TableField.transaction do
|
|
517
|
+
ActiveRecord::Migration.remove_column(field.table.name,field.name)
|
|
518
|
+
field.destroy
|
|
519
|
+
end
|
|
520
|
+
flash[:notice]="Field was succesfully destroy."
|
|
521
|
+
redirect_to :action => "show", :id => field.table
|
|
522
|
+
end
|
|
523
|
+
|
|
524
|
+
private
|
|
525
|
+
|
|
526
|
+
def prepare_for_table(string)
|
|
527
|
+
string.gsub(/ +/,"_").downcase
|
|
528
|
+
end
|
|
529
|
+
|
|
530
|
+
def create_column(referenting_table,referented_table)
|
|
531
|
+
begin
|
|
532
|
+
#normally,the column REFERENTED_TABLE_id doesn't exists and should be created. But if exists,an exception will be thrown. So,we need to manage this kind of exception
|
|
533
|
+
ActiveRecord::Migration.add_column referenting_table.name, "#{referented_table.name.singularize}_id", :integer
|
|
534
|
+
id_integer=AttrType.find_by_name("integer").id
|
|
535
|
+
TableField.create(:name => "#{referented_table.name.singularize}_id", :attr_type_id => id_integer, :table_id => referenting_table.id)
|
|
536
|
+
rescue
|
|
537
|
+
end
|
|
538
|
+
end
|
|
539
|
+
|
|
540
|
+
def create_relation(referenting_table,referented_table,relation_type,cascade,for_join,join_table)
|
|
541
|
+
if relation_type=="one-to-one" or relation_type=="one-to-many"
|
|
542
|
+
field=TableField.find_by_name_and_table_id("#{referented_table.name.singularize}_id",referenting_table.id)
|
|
543
|
+
end
|
|
544
|
+
if relation_type=="many-to-many"
|
|
545
|
+
field=TableField.find_by_name_and_table_id("id",referenting_table.id)
|
|
546
|
+
end
|
|
547
|
+
if existing=Relation.find_by_table_field_id_and_table_id(field.id,referented_table.id)
|
|
548
|
+
existing.destroy
|
|
549
|
+
end
|
|
550
|
+
Relation.create(:table_field_id => field.id, :table_id => referented_table.id, :relation_type => relation_type, :dependent => cascade, :for_join_table => for_join, :join_table => join_table)
|
|
551
|
+
end
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
def write_into_model_file(model_file,relation,related_table,option)
|
|
555
|
+
find_num_lines(model_file)
|
|
556
|
+
write_relation(model_file,relation,related_table,option)
|
|
557
|
+
end
|
|
558
|
+
|
|
559
|
+
#calculating model file's number of rows
|
|
560
|
+
def find_num_lines(file)
|
|
561
|
+
File.open "app/models/#{file}.rb", "r" do |file|
|
|
562
|
+
@num_lines=0
|
|
563
|
+
file.each {|l| @num_lines+=1}
|
|
564
|
+
end
|
|
565
|
+
end
|
|
566
|
+
|
|
567
|
+
#inserting the relation in the referented model
|
|
568
|
+
def write_relation(file_to_open,relation,related_table,dependency)
|
|
569
|
+
File.open "app/models/#{file_to_open}.rb", "r+" do |file|
|
|
570
|
+
file.each {|l|
|
|
571
|
+
if file.lineno==@num_lines-1
|
|
572
|
+
file << "\n#{relation} :#{related_table}#{dependency}\nend"
|
|
573
|
+
end}
|
|
574
|
+
end
|
|
575
|
+
end
|
|
576
|
+
|
|
577
|
+
#inserting the validation into join table model
|
|
578
|
+
def write_validation(file_to_open,join_table_class,table1,table2)
|
|
579
|
+
find_num_lines(file_to_open)
|
|
580
|
+
File.open "app/models/#{file_to_open}.rb", "r+" do |file|
|
|
581
|
+
file.each {|l|
|
|
582
|
+
if file.lineno==@num_lines-1
|
|
583
|
+
file << "\ndef validate\nif !#{table1}_id.nil? and !#{table2}_id.nil? and #{join_table_class}.find_by_#{table1}_id_and_#{table2}_id(#{table1}_id,#{table2}_id)\nobject_id=#{join_table_class}.find_by_#{table1}_id_and_#{table2}_id(#{table1}_id,#{table2}_id).id\n if object_id!=id\n errors.add_to_base(\"(#{table1},#{table2}) already exists\")\nend\nend\nend\nend"
|
|
584
|
+
end}
|
|
585
|
+
end
|
|
586
|
+
end
|
|
587
|
+
|
|
588
|
+
def destroy_many_to_many_relation(table_name)
|
|
589
|
+
begin #if the table is a join table,we need to destroy the relation between the related tables
|
|
590
|
+
if relation=Relation.find_by_join_table(table_name)
|
|
591
|
+
related=find_related_table(relation.id)
|
|
592
|
+
delete_relation_from_file(related)
|
|
593
|
+
relation.destroy
|
|
594
|
+
end
|
|
595
|
+
rescue
|
|
596
|
+
end
|
|
597
|
+
end
|
|
598
|
+
|
|
599
|
+
def find_related_table(id_r)
|
|
600
|
+
return Relation.find_by_sql("SELECT t2.name as referencing,t1.name as referenced,relation_type,join_table,for_join_table
|
|
601
|
+
FROM relations r,table_fields tf,tables t1,tables t2
|
|
602
|
+
WHERE r.table_field_id=tf.id and r.table_id=t1.id and tf.table_id=t2.id and r.id=#{id_r}").first
|
|
603
|
+
|
|
604
|
+
end
|
|
605
|
+
|
|
606
|
+
def find_related(table_name)
|
|
607
|
+
return Relation.find_by_sql("SELECT t2.name as referencing,t1.name as referenced,relation_type,join_table,for_join_table,table_field_id,r.id as id_r
|
|
608
|
+
FROM relations r,table_fields tf,tables t1,tables t2
|
|
609
|
+
WHERE r.table_field_id=tf.id and r.table_id=t1.id and tf.table_id=t2.id and (t1.name=\"#{table_name}\" or t2.name=\"#{table_name}\")")
|
|
610
|
+
end
|
|
611
|
+
|
|
612
|
+
def find_all_relations()
|
|
613
|
+
return Relation.find_by_sql("SELECT t2.name as referencing,t1.name as referenced,relation_type,dependent,join_table,for_join_table,table_field_id,r.id as id_r
|
|
614
|
+
FROM relations r,table_fields tf,tables t1,tables t2
|
|
615
|
+
WHERE r.table_field_id=tf.id and r.table_id=t1.id and tf.table_id=t2.id")
|
|
616
|
+
|
|
617
|
+
end
|
|
618
|
+
|
|
619
|
+
def delete_relation_from_file(object)
|
|
620
|
+
if object.relation_type == "one-to-one"
|
|
621
|
+
#reading the file
|
|
622
|
+
read_file(object.referencing)
|
|
623
|
+
|
|
624
|
+
@old_file.each {|row|
|
|
625
|
+
#in the new file the row to erase will not be present
|
|
626
|
+
if row =~ /:#{object.referenced.singularize}/
|
|
627
|
+
else
|
|
628
|
+
@new_file << row
|
|
629
|
+
end
|
|
630
|
+
}
|
|
631
|
+
write_file(object.referencing)
|
|
632
|
+
|
|
633
|
+
read_file(object.referenced)
|
|
634
|
+
@old_file.each {|row|
|
|
635
|
+
#in the new file the row to erase will not be present
|
|
636
|
+
if row =~ /:#{object.referencing.singularize}/
|
|
637
|
+
else
|
|
638
|
+
@new_file << row
|
|
639
|
+
end
|
|
640
|
+
}
|
|
641
|
+
write_file(object.referenced)
|
|
642
|
+
|
|
643
|
+
end
|
|
644
|
+
|
|
645
|
+
if object.relation_type == "one-to-many"
|
|
646
|
+
#reading the file
|
|
647
|
+
read_file(object.referencing)
|
|
648
|
+
|
|
649
|
+
@old_file.each {|row|
|
|
650
|
+
#in the new file the row to erase will not be present
|
|
651
|
+
if row =~ /belongs_to :#{object.referenced.singularize}/
|
|
652
|
+
else
|
|
653
|
+
@new_file << row
|
|
654
|
+
end
|
|
655
|
+
}
|
|
656
|
+
write_file(object.referencing)
|
|
657
|
+
|
|
658
|
+
read_file(object.referenced)
|
|
659
|
+
@old_file.each {|row|
|
|
660
|
+
#in the new file the row to erase will not be present
|
|
661
|
+
if row =~ /has_many :#{object.referencing}/
|
|
662
|
+
else
|
|
663
|
+
@new_file << row
|
|
664
|
+
end
|
|
665
|
+
}
|
|
666
|
+
write_file(object.referenced)
|
|
667
|
+
|
|
668
|
+
end
|
|
669
|
+
|
|
670
|
+
if object.relation_type == "many-to-many"
|
|
671
|
+
|
|
672
|
+
read_file(object.referencing)
|
|
673
|
+
@old_file.each {|row|
|
|
674
|
+
#in the new file the row to erase will not be present
|
|
675
|
+
if row =~ /has_many :(#{object.join_table}|#{object.referenced})/
|
|
676
|
+
else
|
|
677
|
+
@new_file << row
|
|
678
|
+
end
|
|
679
|
+
}
|
|
680
|
+
write_file(object.referencing)
|
|
681
|
+
|
|
682
|
+
read_file(object.referenced)
|
|
683
|
+
@old_file.each {|row|
|
|
684
|
+
#in the new file the row to erase will not be present
|
|
685
|
+
if row =~ /has_many :(#{object.join_table}|#{object.referencing})/
|
|
686
|
+
else
|
|
687
|
+
@new_file << row
|
|
688
|
+
end
|
|
689
|
+
}
|
|
690
|
+
write_file(object.referenced)
|
|
691
|
+
|
|
692
|
+
FileUtils.remove_dir("app/views/#{object.join_table}" , :force => true)
|
|
693
|
+
|
|
694
|
+
end
|
|
695
|
+
|
|
696
|
+
end
|
|
697
|
+
|
|
698
|
+
def read_file(object)
|
|
699
|
+
@old_file=[]
|
|
700
|
+
File.open "app/models/#{object.singularize}.rb", "r+" do |file|
|
|
701
|
+
file.each {|l| @old_file << l}
|
|
702
|
+
end
|
|
703
|
+
@new_file=[]
|
|
704
|
+
end
|
|
705
|
+
|
|
706
|
+
def write_file(object)
|
|
707
|
+
File.open "app/models/#{object.singularize}.rb", "w" do |file|
|
|
708
|
+
@new_file.each do |row|
|
|
709
|
+
file << row
|
|
710
|
+
end
|
|
711
|
+
end
|
|
712
|
+
end
|
|
713
|
+
|
|
714
|
+
end
|