rails_db_admin 2.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.
Files changed (69) hide show
  1. data/GPL-3-LICENSE +674 -0
  2. data/README.md +5 -0
  3. data/Rakefile +30 -0
  4. data/app/assets/javascripts/rails_db_admin/application.js +9 -0
  5. data/app/assets/stylesheets/rails_db_admin/application.css +7 -0
  6. data/app/controllers/rails_db_admin/erp_app/desktop/base_controller.rb +182 -0
  7. data/app/controllers/rails_db_admin/erp_app/desktop/queries_controller.rb +173 -0
  8. data/app/helpers/rails_db_admin/application_helper.rb +4 -0
  9. data/app/views/layouts/application.html.erb +14 -0
  10. data/app/views/layouts/rails_db_admin/application.html.erb +14 -0
  11. data/config/initializers/rails_db_admin.rb +4 -0
  12. data/config/routes.rb +4 -0
  13. data/db/data_migrations/20110816005525_rails_db_admin_application.rb +31 -0
  14. data/lib/rails_db_admin.rb +15 -0
  15. data/lib/rails_db_admin/config.rb +27 -0
  16. data/lib/rails_db_admin/connection_handler.rb +17 -0
  17. data/lib/rails_db_admin/engine.rb +11 -0
  18. data/lib/rails_db_admin/extjs.rb +2 -0
  19. data/lib/rails_db_admin/extjs/json_column_builder.rb +139 -0
  20. data/lib/rails_db_admin/extjs/json_data_builder.rb +82 -0
  21. data/lib/rails_db_admin/query_support.rb +72 -0
  22. data/lib/rails_db_admin/table_support.rb +147 -0
  23. data/lib/rails_db_admin/version.rb +3 -0
  24. data/lib/tasks/rails_db_admin_tasks.rake +4 -0
  25. data/public/images/icons/rails_db_admin/rails_db_admin_16x16.png +0 -0
  26. data/public/images/icons/rails_db_admin/rails_db_admin_24x24.png +0 -0
  27. data/public/images/icons/rails_db_admin/rails_db_admin_32x32.png +0 -0
  28. data/public/images/icons/rails_db_admin/rails_db_admin_48x48.png +0 -0
  29. data/public/javascripts/erp_app/desktop/applications/rails_db_admin/database_combo.js +52 -0
  30. data/public/javascripts/erp_app/desktop/applications/rails_db_admin/module.js +429 -0
  31. data/public/javascripts/erp_app/desktop/applications/rails_db_admin/queries_tree_menu.js +86 -0
  32. data/public/javascripts/erp_app/desktop/applications/rails_db_admin/query_panel.js +206 -0
  33. data/public/javascripts/erp_app/desktop/applications/rails_db_admin/readonly_table_data_grid.js +27 -0
  34. data/public/javascripts/erp_app/desktop/applications/rails_db_admin/tables_tree_menu.js +87 -0
  35. data/public/stylesheets/erp_app/desktop/applications/rails_db_admin/rails_db_admin.css +10 -0
  36. data/spec/controllers/rails_db_admin/base_controller_spec.rb +481 -0
  37. data/spec/controllers/rails_db_admin/queries_controller_spec.rb +134 -0
  38. data/spec/dummy/Rakefile +7 -0
  39. data/spec/dummy/app/assets/javascripts/application.js +9 -0
  40. data/spec/dummy/app/assets/stylesheets/application.css +7 -0
  41. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  42. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  43. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  44. data/spec/dummy/config.ru +4 -0
  45. data/spec/dummy/config/application.rb +45 -0
  46. data/spec/dummy/config/boot.rb +10 -0
  47. data/spec/dummy/config/database.yml +14 -0
  48. data/spec/dummy/config/environment.rb +5 -0
  49. data/spec/dummy/config/environments/cucumber.rb +3 -0
  50. data/spec/dummy/config/environments/spec.rb +27 -0
  51. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  52. data/spec/dummy/config/initializers/inflections.rb +10 -0
  53. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  54. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  55. data/spec/dummy/config/initializers/session_store.rb +8 -0
  56. data/spec/dummy/config/initializers/wrap_parameters.rb +12 -0
  57. data/spec/dummy/config/locales/en.yml +5 -0
  58. data/spec/dummy/config/routes.rb +5 -0
  59. data/spec/dummy/public/404.html +26 -0
  60. data/spec/dummy/public/422.html +26 -0
  61. data/spec/dummy/public/500.html +26 -0
  62. data/spec/dummy/public/favicon.ico +0 -0
  63. data/spec/dummy/script/rails +6 -0
  64. data/spec/lib/rails_db_admin/extjs/json_column_builder_spec.rb +206 -0
  65. data/spec/lib/rails_db_admin/extjs/json_data_builder_spec.rb +201 -0
  66. data/spec/lib/rails_db_admin/query_support_spec.rb +40 -0
  67. data/spec/lib/rails_db_admin/table_support_spec.rb +349 -0
  68. data/spec/spec_helper.rb +60 -0
  69. metadata +183 -0
@@ -0,0 +1,86 @@
1
+ Ext.define("Compass.ErpApp.Desktop.Applications.RailsDbAdmin.QueriesMenuTreePanel",{
2
+ extend:"Ext.tree.TreePanel",
3
+ alias:'widget.railsdbadmin_queriestreemenu',
4
+
5
+ constructor : function(config) {
6
+ var self = this;
7
+
8
+ config = Ext.apply({
9
+ title:'Queries',
10
+ autoScroll:true,
11
+ store:Ext.create('Ext.data.TreeStore', {
12
+ proxy: {
13
+ type: 'ajax',
14
+ url: '/rails_db_admin/erp_app/desktop/queries/saved_queries_tree'
15
+ },
16
+ root:{
17
+ text: 'Queries',
18
+ expanded: true,
19
+ draggable:false,
20
+ iconCls:'icon-content'
21
+ }
22
+ }),
23
+ animate:false,
24
+ //TODO_EXTJS4 this is added to fix error should be removed when extjs 4 releases fix.
25
+ viewConfig:{
26
+ loadMask: false
27
+ },
28
+ listeners:{
29
+ 'itemclick':function(view, record, item, index, e){
30
+ e.stopEvent();
31
+ if(record.data.leaf){
32
+ self.initialConfig.module.displayQuery(record.data.id);
33
+ }
34
+ },
35
+ 'itemcontextmenu':function(view, record, item, index, e){
36
+ e.stopEvent();
37
+ var contextMenu = null;
38
+ if(record.data.leaf){
39
+ contextMenu = new Ext.menu.Menu({
40
+ items:[
41
+ {
42
+ text:"Execute",
43
+ iconCls:'icon-settings',
44
+ listeners:{
45
+ scope:record,
46
+ 'click':function(){
47
+ self.initialConfig.module.displayAndExecuteQuery(record.data.id);
48
+ }
49
+ }
50
+ },
51
+ {
52
+ text:"Delete",
53
+ iconCls:'icon-delete',
54
+ listeners:{
55
+ scope:record,
56
+ 'click':function(){
57
+ self.initialConfig.module.deleteQuery(record.data.id);
58
+ }
59
+ }
60
+ }
61
+ ]
62
+ });
63
+ }
64
+ else{
65
+ contextMenu = new Ext.menu.Menu({
66
+ items:[
67
+ {
68
+ text:"New Query",
69
+ iconCls:'icon-new',
70
+ listeners:{
71
+ 'click':function(){
72
+ self.initialConfig.module.addNewQueryTab();
73
+ }
74
+ }
75
+ }
76
+ ]
77
+ });
78
+ }
79
+ contextMenu.showAt(e.xy);
80
+ }
81
+ }
82
+ }, config);
83
+
84
+ this.callParent([config]);
85
+ }
86
+ });
@@ -0,0 +1,206 @@
1
+ Ext.define("Compass.ErpApp.Desktop.Applications.RailsDbAdmin.QueryPanel",{
2
+ extend:"Ext.Panel",
3
+ alias:'widget.railsdbadmin_querypanel',
4
+ gridContainer : null,
5
+ initComponent: function() {
6
+ var self = this;
7
+ var messageBox = null;
8
+
9
+ var savedQueriesJsonStore = Ext.create('Ext.data.Store', {
10
+ proxy: {
11
+ type: 'ajax',
12
+ url: '/rails_db_admin/erp_app/desktop/queries/saved_queries',
13
+ reader: {
14
+ type: 'json',
15
+ root: 'data'
16
+ }
17
+ },
18
+ fields:[
19
+ {
20
+ name:'value'
21
+ },
22
+ {
23
+ name:'display'
24
+ }
25
+ ]
26
+ });
27
+
28
+ var tableGridContainer = new Ext.Panel({
29
+ layout:'card',
30
+ region : 'center',
31
+ margins : '0 0 0 0',
32
+ autoScroll:true,
33
+ items:[]
34
+ });
35
+
36
+ this.gridContainer = tableGridContainer;
37
+
38
+ var codeMirrorPanel = new Ext.Panel({
39
+ height:250,
40
+ region:'north',
41
+ margins : '0 0 0 0',
42
+ frame:false,
43
+ border:false,
44
+ autoScroll:true,
45
+ layout:'fit',
46
+ items:[
47
+ {
48
+ xtype:'codemirror',
49
+ parser:'sql',
50
+ sourceCode:this.initialConfig['sqlQuery'],
51
+ disableSave:true
52
+ }
53
+ ]
54
+
55
+ });
56
+
57
+ this.tbar = {
58
+ items:[{
59
+ text: 'Execute',
60
+ iconCls: 'icon-settings',
61
+ handler: function(button) {
62
+ var textarea = self.query('.codemirror')[0];
63
+ var sql = textarea.getValue();
64
+ var selected_sql = textarea.getSelection();
65
+ var cursor_pos = textarea.getCursor().line;
66
+ var database = self.module.getDatabase();
67
+
68
+ messageBox = Ext.Msg.wait('Status', 'Executing..');
69
+
70
+ var conn = new Ext.data.Connection();
71
+ conn.request({
72
+ url: '/rails_db_admin/erp_app/desktop/queries/execute_query',
73
+ params:{
74
+ sql:sql,
75
+ database:database,
76
+ cursor_pos: cursor_pos,
77
+ selected_sql : selected_sql
78
+ },
79
+ method:'post',
80
+ success: function(responseObject) {
81
+
82
+ messageBox.hide();
83
+ var response = Ext.decode(responseObject.responseText);
84
+
85
+ if(response.success)
86
+ {
87
+ var columns = response.columns;
88
+ var fields = response.fields;
89
+ var data = response.data;
90
+
91
+ var grid = new Compass.ErpApp.Desktop.Applications.RailsDbAdmin.ReadOnlyTableDataGrid({
92
+ columns:columns,
93
+ fields:fields,
94
+ data:data
95
+ });
96
+
97
+ tableGridContainer.removeAll(true);
98
+ tableGridContainer.add(grid);
99
+ tableGridContainer.getLayout().setActiveItem(0);
100
+ }
101
+ else
102
+ {
103
+ Ext.Msg.alert("Error", response.exception);
104
+ }
105
+
106
+ },
107
+ failure: function() {
108
+ messageBox.hide();
109
+ Ext.Msg.alert('Status', 'Error loading grid');
110
+ }
111
+ });
112
+ }
113
+ },
114
+ {
115
+ text: 'Save',
116
+ iconCls: 'icon-save',
117
+ handler:function(){
118
+ var textarea = self.query('.codemirror')[0];
119
+ var save_window = new Ext.Window({
120
+ layout:'fit',
121
+ width:375,
122
+ title:'Save Query',
123
+ height:125,
124
+ buttonAlign:'center',
125
+ closeAction:'hide',
126
+ plain: true,
127
+ items: new Ext.FormPanel({
128
+ frame:false,
129
+ bodyStyle:'padding:5px 5px 0',
130
+ width: 500,
131
+ items: [{
132
+ xtype: 'combo',
133
+ fieldLabel: 'Query Name',
134
+ name: 'query_name',
135
+ allowBlank:false,
136
+ store:savedQueriesJsonStore,
137
+ valueField:'value',
138
+ displayField:'display',
139
+ triggerAction:'all',
140
+ forceSelection:false,
141
+ mode:'remote'
142
+ },
143
+ {
144
+ xtype: 'hidden',
145
+ value: textarea.getValue(),
146
+ name: 'query'
147
+ },
148
+ {
149
+ xtype: 'hidden',
150
+ value: self.module.getDatabase(),
151
+ name: 'database'
152
+ }]
153
+ }),
154
+ buttons: [{
155
+ text: 'Save',
156
+ handler: function(){
157
+ var fp = this.findParentByType('window').query('.form')[0];
158
+ if(fp.getForm().isValid()){
159
+ fp.getForm().submit({
160
+ url: '/rails_db_admin/erp_app/desktop/queries/save_query',
161
+ waitMsg: 'Saving Query...',
162
+ success: function(fp, o){
163
+ Ext.Msg.alert('Success', 'Saved Query');
164
+ var database = self.module.getDatabase();
165
+ self.module.queriesTreePanel().store.setProxy({
166
+ type: 'ajax',
167
+ url: '/rails_db_admin/erp_app/desktop/queries/saved_queries_tree',
168
+ extraParams:{
169
+ database:database
170
+ }
171
+ });
172
+ self.module.queriesTreePanel().store.load();
173
+ save_window.hide();
174
+ }
175
+ });
176
+ }
177
+ }
178
+ },{
179
+ text: 'Cancel',
180
+ handler: function(){
181
+ save_window.hide();
182
+ }
183
+ }]
184
+
185
+ });
186
+ save_window.show();
187
+ }
188
+ }]
189
+ };
190
+
191
+ this.items = [codeMirrorPanel, tableGridContainer];
192
+ this.callParent(arguments);
193
+ },
194
+
195
+ constructor : function(config) {
196
+ config = Ext.apply({
197
+ title:'Query',
198
+ layout:'border',
199
+ autoScroll:true,
200
+ closable: true,
201
+ border:false
202
+ }, config);
203
+ this.callParent([config]);
204
+ }
205
+
206
+ });
@@ -0,0 +1,27 @@
1
+ Ext.define("Compass.ErpApp.Desktop.Applications.RailsDbAdmin.ReadOnlyTableDataGrid",{
2
+ extend:"Ext.grid.GridPanel",
3
+ alias:'widget.railsdbadmin_readonlytabledatagrid',
4
+ constructor : function(config) {
5
+ var jsonStore = new Ext.data.JsonStore({
6
+ fields:config.fields,
7
+ data:config.data
8
+ })
9
+
10
+ config = Ext.apply({
11
+ store:jsonStore,
12
+ layout:'fit',
13
+ frame: false,
14
+ closable: true,
15
+ autoScroll:true,
16
+ region:'center',
17
+ loadMask:true
18
+ }, config);
19
+
20
+ this.callParent([config]);
21
+ }
22
+ });
23
+
24
+
25
+
26
+
27
+
@@ -0,0 +1,87 @@
1
+ Ext.define("Compass.ErpApp.Desktop.Applications.RailsDbAdmin.TablesMenuTreePanel",{
2
+ extend:"Ext.tree.TreePanel",
3
+ alias:'widget.railsdbadmin_tablestreemenu',
4
+ constructor : function(config) {
5
+ var self = this;
6
+
7
+ config = Ext.apply({
8
+ title:'Tables',
9
+ autoScroll:true,
10
+ store:Ext.create('Ext.data.TreeStore', {
11
+ proxy: {
12
+ type: 'ajax',
13
+ url: '/rails_db_admin/erp_app/desktop/base/tables'
14
+ },
15
+ root: {
16
+ text: 'Tables',
17
+ expanded:false,
18
+ draggable:false,
19
+ iconCls:'icon-content'
20
+ },
21
+ fields:[
22
+ {
23
+ name:'leaf'
24
+ },
25
+
26
+ {
27
+ name:'iconCls'
28
+ },
29
+
30
+ {
31
+ name:'text'
32
+ },
33
+
34
+ {
35
+ name:'id'
36
+ },
37
+
38
+ {
39
+ name:'isTable'
40
+ }
41
+ ]
42
+ }),
43
+ animate:false,
44
+ //TODO_EXTJS4 this is added to fix error should be removed when extjs 4 releases fix.
45
+ viewConfig:{
46
+ loadMask: false
47
+ },
48
+ listeners:{
49
+ 'itemcontextmenu':function(view, record, item, index, e){
50
+ e.stopEvent();
51
+ if(!record.data['isTable']){
52
+ return false;
53
+ }
54
+ else{
55
+ var contextMenu = new Ext.menu.Menu({
56
+ items:[
57
+ {
58
+ text:"Select Top 50",
59
+ iconCls:'icon-settings',
60
+ listeners:{
61
+ scope:record,
62
+ 'click':function(){
63
+ self.initialConfig.module.selectTopFifty(this.data.id);
64
+ }
65
+ }
66
+ },
67
+ {
68
+ text:"Edit Table Data",
69
+ iconCls:'icon-edit',
70
+ listeners:{
71
+ scope:record,
72
+ 'click':function(){
73
+ self.initialConfig.module.getTableData(this.data.id);
74
+ }
75
+ }
76
+ }
77
+ ]
78
+ });
79
+ contextMenu.showAt(e.xy);
80
+ }
81
+ }
82
+ }
83
+ }, config);
84
+
85
+ this.callParent([config]);
86
+ }
87
+ });
@@ -0,0 +1,10 @@
1
+ .icon-rails_db_admin {
2
+ background-image:url( /images/icons/rails_db_admin/rails_db_admin_16x16.png ) !important;
3
+ filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/icons/rails_db_admin/rails_db_admin_16x16.png', sizingMethod='crop');
4
+ }
5
+
6
+
7
+
8
+
9
+
10
+
@@ -0,0 +1,481 @@
1
+ require "spec_helper"
2
+
3
+ describe RailsDbAdmin::BaseController do
4
+
5
+ #handle devise user auth
6
+ before(:each) do
7
+ basic_user_auth
8
+ end
9
+
10
+ before(:all) do
11
+ Factory.create(:role_type, :internal_identifier => "execute_query_test_role")
12
+ Factory.create(:role_type, :internal_identifier => "execute_query_test_role_2")
13
+ end
14
+
15
+ describe "POST setup_table_grid" do
16
+
17
+ it "should return success:true" do
18
+
19
+ post :base, {:use_route => :rails_db_admin,
20
+ :action => "setup_table_grid",
21
+ :table => 'role_types'}
22
+
23
+ parsed_body = JSON.parse(response.body)
24
+ parsed_body["success"].should eq(true)
25
+ end
26
+
27
+ it "should return model, columns, and fields" do
28
+
29
+ post :base, {:use_route => :rails_db_admin,
30
+ :action => "setup_table_grid",
31
+ :table => 'role_types'}
32
+
33
+ parsed_body = JSON.parse(response.body)
34
+ parsed_body["model"].should eq("role_types")
35
+ parsed_body["columns"].should include(
36
+ {"header"=>"id", "type"=>"number", "dataIndex"=>"id", "width"=>150})
37
+ end
38
+
39
+ it "should return true even if there is not an 'id' column on the table in question" do
40
+
41
+ post :base, {:use_route => :rails_db_admin,
42
+ :action => "setup_table_grid",
43
+ :table => 'preference_options_preference_types'}
44
+
45
+ parsed_body = JSON.parse(response.body)
46
+ parsed_body["success"].should eq(true)
47
+ end
48
+
49
+ it "should return model, columns, and fields even if there is not an 'id' column on a table
50
+ and should have a 'fake_id' column in the fields and columns array" do
51
+
52
+ post :base, {:use_route => :rails_db_admin,
53
+ :action => "setup_table_grid",
54
+ :table => 'preference_options_preference_types'}
55
+
56
+ parsed_body = JSON.parse(response.body)
57
+
58
+ parsed_body["model"].should eq("preference_options_preference_types")
59
+ parsed_body["columns"][0].should include(
60
+ {"header"=>"preference_type_id",
61
+ "type"=>"number",
62
+ "dataIndex"=>"preference_type_id",
63
+ "width"=>150,
64
+ "editor"=>{"xtype"=>"textfield"}})
65
+ parsed_body["columns"][4].should include(
66
+ {"header"=>"fake_id", "type"=>"number", "dataIndex"=>"fake_id", "hidden"=>true}
67
+ )
68
+ parsed_body["fields"].should include(
69
+ {"name" => "preference_type_id"})
70
+ parsed_body["fields"].should include(
71
+ {"name" => "fake_id"})
72
+ end
73
+
74
+ it "should return a value called id_property that equals 'fake_id'" do
75
+
76
+ post :base, {:use_route => :rails_db_admin,
77
+ :action => "setup_table_grid",
78
+ :table => 'preference_options_preference_types'}
79
+
80
+ parsed_body = JSON.parse(response.body)
81
+ parsed_body["id_property"].should eq("fake_id")
82
+ end
83
+
84
+ it "should return a value called id_property that equals 'id'" do
85
+
86
+ post :base, {:use_route => :rails_db_admin,
87
+ :action => "setup_table_grid",
88
+ :table => 'role_types'}
89
+
90
+ parsed_body = JSON.parse(response.body)
91
+ parsed_body["id_property"].should eq("id")
92
+ end
93
+ end
94
+
95
+ describe "GET table_data" do
96
+
97
+ it "should return JSON to display in an ExtJS data grid for Rails tables with an Id column" do
98
+
99
+ get :base, {:use_route => :rails_db_admin,
100
+ :action => "table_data",
101
+ :table => "role_types"}
102
+
103
+ parsed_body = JSON.parse(response.body)
104
+ parsed_body["totalCount"].should eq(2)
105
+ end
106
+
107
+ it "should return 1 row because start is increased by 1, but totalCount should remain 2" do
108
+
109
+ get :base, {:use_route => :rails_db_admin,
110
+ :action => "table_data",
111
+ :table => "role_types",
112
+ :start => "1"}
113
+
114
+ parsed_body = JSON.parse(response.body)
115
+ parsed_body["totalCount"].should eq(2)
116
+ parsed_body["data"].length.should eq(1)
117
+ end
118
+
119
+ it "should return 1 row because limit is set to 1, but totalCount should remain 2" do
120
+
121
+ get :base, {:use_route => :rails_db_admin,
122
+ :action => "table_data",
123
+ :table => "role_types",
124
+ :limit => "1"}
125
+
126
+ parsed_body = JSON.parse(response.body)
127
+ parsed_body["totalCount"].should eq(2)
128
+ parsed_body["data"].length.should eq(1)
129
+ end
130
+
131
+ #TODO: Need to setup Factory Girl to dummy up data for this test
132
+ it "should return successfully with a fake_id column because there is no id column defined in the DB" do
133
+
134
+ get :base, {:use_route => :rails_db_admin,
135
+ :action => "table_data",
136
+ :table => 'preference_options_preference_types'}
137
+
138
+ parsed_body = JSON.parse(response.body)
139
+ parsed_body["totalCount"].should eq(0)
140
+ end
141
+ end
142
+
143
+
144
+ describe "PUT table_data" do
145
+
146
+ before(:each) do
147
+ @pref_opt_types_data = {"preference_type_id" => "1",
148
+ "preference_option_id" => "2",
149
+ "created_at" => "2011-10-11 00:54:56.137144",
150
+ "updated_at" => "2011-10-11 00:54:56.137144"}
151
+ @role_types_data = {"id" => 2,
152
+ "parent_id" => "",
153
+ "lft" => "3",
154
+ "rgt" => "4",
155
+ "description" => "Partner-Test",
156
+ "comments" => "",
157
+ "internal_identifier" => "partner",
158
+ "external_identifier" => "",
159
+ "external_id_source" => "",
160
+ "created_at" => "2011-10-11 00:54:56.137144",
161
+ "updated_at" => "2011-10-11 00:54:56.137144"}
162
+ @mod_role_types_data = {"parent_id" => "",
163
+ "lft" => "3",
164
+ "rgt" => "4",
165
+ "description" => "Partner-Test",
166
+ "comments" => "",
167
+ "internal_identifier" => "partner",
168
+ "external_identifier" => "",
169
+ "external_id_source" => "",
170
+ "created_at" => "2011-10-11 00:54:56.137144",
171
+ "updated_at" => "2011-10-11 00:54:56.137144"}
172
+ @table_support = double("RailsDbAdmin::TableSupport")
173
+ @json_data_builder = double("RailsDbAdmin::Extjs::JsonDataBuilder")
174
+
175
+ RailsDbAdmin::TableSupport.should_receive(
176
+ :new).and_return(@table_support)
177
+ RailsDbAdmin::Extjs::JsonDataBuilder.should_receive(
178
+ :new).and_return(@json_data_builder)
179
+ end
180
+
181
+ it "should return success" do
182
+
183
+ @table = "role_types"
184
+ @pkey = ['id', "2"]
185
+
186
+ @table_support.should_receive(:primary_key?).with(@table).and_return(true)
187
+ @table_support.should_receive(:primary_key).with(@table).and_return(['id', nil])
188
+
189
+ @table_support.should_receive(:update_table).with(
190
+ @table, @pkey, @mod_role_types_data)
191
+ @json_data_builder.should_receive(:get_row_data).with(
192
+ @table, @pkey).and_return(@role_types_data)
193
+
194
+ put :base, {:use_route => :rails_db_admin,
195
+ :action => "table_data",
196
+ :table => @table,
197
+ :data => [@role_types_data, @role_types_data] }
198
+
199
+ parsed_body = JSON.parse(response.body)
200
+ parsed_body["success"].should eq(true)
201
+ end
202
+
203
+ it "should update table data and return success even if there is not an 'id' column" do
204
+
205
+ @pref_opt_types_data = {"preference_type_id" => "1",
206
+ "preference_option_id" => "2",
207
+ "created_at" => "2011-10-11 00:54:56.137144",
208
+ "updated_at" => "",
209
+ "fake_id" => "162"}
210
+
211
+ @table = "preference_options_preference_types"
212
+
213
+ @table_support.should_receive(:primary_key?).with(@table).and_return(false)
214
+
215
+ @table_support.should_receive(:update_table_without_id).with(
216
+ @table, [@pref_opt_types_data, @pref_opt_types_data])
217
+
218
+ @json_data_builder.should_receive(:get_row_data_no_id).with(
219
+ @table, @pref_opt_types_data).and_return(
220
+ @pref_opt_types_data)
221
+
222
+ put :base, {:use_route => :rails_db_admin,
223
+ :action => "table_data",
224
+ :table => @table,
225
+ :data => [@pref_opt_types_data, @pref_opt_types_data]}
226
+
227
+ parsed_body = JSON.parse(response.body)
228
+ parsed_body["success"].should eq(true)
229
+ parsed_body["data"].should eq(@pref_opt_types_data)
230
+ end
231
+ end
232
+
233
+ describe "POST table_data" do
234
+
235
+ before(:each) do
236
+ @table_support = double("RailsDbAdmin::TableSupport")
237
+ @json_data_builder = double("RailsDbAdmin::Extjs::JsonDataBuilder")
238
+
239
+ RailsDbAdmin::TableSupport.should_receive(
240
+ :new).and_return(@table_support)
241
+ RailsDbAdmin::Extjs::JsonDataBuilder.should_receive(
242
+ :new).and_return(@json_data_builder)
243
+ end
244
+
245
+ it "should return success and the row that was created" do
246
+
247
+ @table = "role_types"
248
+
249
+ @role_types_data = {"id" => 3,
250
+ "parent_id" => "",
251
+ "lft" => "3",
252
+ "rgt" => "4",
253
+ "description" => "Partner-Test",
254
+ "comments" => "",
255
+ "internal_identifier" => "partner",
256
+ "external_identifier" => "",
257
+ "external_id_source" => "",
258
+ "created_at" => "2011-10-11 00:54:56.137144",
259
+ "updated_at" => "2011-10-11 00:54:56.137144"}
260
+
261
+ @mod_types_data = {"parent_id" => "",
262
+ "lft" => "3",
263
+ "rgt" => "4",
264
+ "description" => "Partner-Test",
265
+ "comments" => "",
266
+ "internal_identifier" => "partner",
267
+ "external_identifier" => "",
268
+ "external_id_source" => "",
269
+ "created_at" => "2011-10-11 00:54:56.137144",
270
+ "updated_at" => "2011-10-11 00:54:56.137144"}
271
+
272
+ @table_support.should_receive(:primary_key?).with(@table).and_return(true)
273
+ @table_support.should_receive(:primary_key).with(@table).and_return(['id', nil])
274
+ @table_support.should_receive(:insert_row).with(@table, @mod_types_data).
275
+ and_return(3)
276
+ @json_data_builder.should_receive(:get_row_data).with(@table,["id",3]).
277
+ and_return(@role_types_data)
278
+
279
+ post :base, {:use_route => :rails_db_admin,
280
+ :action => "table_data",
281
+ :table => @table,
282
+ :data => @role_types_data}
283
+
284
+ parsed_body = JSON.parse(response.body)
285
+ parsed_body["success"].should eq(true)
286
+ parsed_body[:data].should eq(@roles_types_data)
287
+ end
288
+
289
+ it "should return success even with empty row hashed passed in" do
290
+
291
+ @table = "role_types"
292
+ @role_types_data = {"id" => "",
293
+ "parent_id" => "",
294
+ "lft" => "",
295
+ "rgt" => "",
296
+ "description" => "",
297
+ "comments" => "",
298
+ "internal_identifier" => "",
299
+ "external_identifier" => "",
300
+ "external_id_source" => "",
301
+ "created_at" => "",
302
+ "updated_at" => ""}
303
+
304
+ @mod_types_data = {"parent_id" => "",
305
+ "lft" => "",
306
+ "rgt" => "",
307
+ "description" => "",
308
+ "comments" => "",
309
+ "internal_identifier" => "",
310
+ "external_identifier" => "",
311
+ "external_id_source" => "",
312
+ "created_at" => "",
313
+ "updated_at" => ""}
314
+
315
+ @table_support.should_receive(:primary_key?).with(@table).and_return(true)
316
+ @table_support.should_receive(:primary_key).with(@table).and_return(['id', nil])
317
+
318
+ @table_support.should_receive(:insert_row).with(@table, @mod_types_data).
319
+ and_return(3)
320
+ @json_data_builder.should_receive(:get_row_data).with(@table,["id",3]).
321
+ and_return(@role_types_data)
322
+
323
+ post :base, {:use_route => :rails_db_admin,
324
+ :action => "table_data",
325
+ :table => @table,
326
+ :data => @role_types_data}
327
+
328
+ parsed_body = JSON.parse(response.body)
329
+ parsed_body["success"].should eq(true)
330
+ parsed_body[:data].should eq(@roles_types_data)
331
+ end
332
+
333
+ it "should return success and the inserted data, with a row with a 'fake_id'" do
334
+
335
+ @table = "preference_options_preference_types"
336
+ @pref_opt_types_data = {"preference_type_id" => "1",
337
+ "preference_option_id" => "2",
338
+ "created_at" => "2011-10-11 00:54:56.137144",
339
+ "updated_at" => "2011-10-11 00:54:56.137144",
340
+ "fake_id" => "5"}
341
+
342
+ @mod_data = {"preference_type_id" => "1",
343
+ "preference_option_id" => "2",
344
+ "created_at" => "2011-10-11 00:54:56.137144",
345
+ "updated_at" => "2011-10-11 00:54:56.137144"}
346
+
347
+ @final_data = {"preference_type_id" => "1",
348
+ "preference_option_id" => "2",
349
+ "created_at" => "2011-10-11 00:54:56.137144",
350
+ "updated_at" => "2011-10-11 00:54:56.137144",
351
+ "fake_id" => 300}
352
+
353
+ @table_support.should_receive(:primary_key?).with(@table).and_return(false)
354
+ @table_support.should_receive(:insert_row).with(@table, @mod_data, true).and_return(300)
355
+
356
+ @json_data_builder.should_receive(:get_row_data_no_id).with(
357
+ @table, @mod_data).and_return(
358
+ @pref_opt_types_data)
359
+
360
+ post :base, {:use_route => :rails_db_admin,
361
+ :action => "table_data",
362
+ :table => @table,
363
+ :data => @pref_opt_types_data}
364
+
365
+ parsed_body = JSON.parse(response.body)
366
+ parsed_body["success"].should eq(true)
367
+ parsed_body["data"].should eq(@final_data)
368
+ end
369
+
370
+ it "should return success and the inserted data, with a row with a 'fake_id', with empty data passed in" do
371
+
372
+ @table = "preference_options_preference_types"
373
+ @pref_opt_types_data = {"preference_type_id" => "",
374
+ "preference_option_id" => "",
375
+ "created_at" => "",
376
+ "updated_at" => "",
377
+ "fake_id" => ""}
378
+
379
+ @mod_data = {"preference_type_id" => "",
380
+ "preference_option_id" => "",
381
+ "created_at" => "",
382
+ "updated_at" => ""}
383
+
384
+ @final_data = {"preference_type_id" => "",
385
+ "preference_option_id" => "",
386
+ "created_at" => "",
387
+ "updated_at" => "",
388
+ "fake_id" => 300}
389
+
390
+ @table_support.should_receive(:primary_key?).with(@table).and_return(false)
391
+ @table_support.should_receive(:insert_row).with(@table, @mod_data, true).and_return(300)
392
+
393
+ @json_data_builder.should_receive(:get_row_data_no_id).with(
394
+ @table, @mod_data).and_return(
395
+ @pref_opt_types_data)
396
+
397
+ post :base, {:use_route => :rails_db_admin,
398
+ :action => "table_data",
399
+ :table => @table,
400
+ :data => @pref_opt_types_data}
401
+
402
+ parsed_body = JSON.parse(response.body)
403
+ parsed_body["success"].should eq(true)
404
+ parsed_body["data"].should eq(@final_data)
405
+ end
406
+ end
407
+
408
+
409
+ describe "DELETE table_data" do
410
+
411
+ before(:each) do
412
+ @table_support = double("RailsDbAdmin::TableSupport")
413
+ RailsDbAdmin::TableSupport.should_receive(
414
+ :new).and_return(@table_support)
415
+ end
416
+
417
+ it "should process the request successfully" do
418
+
419
+ @table = "role_types"
420
+ @table_support.should_receive(:primary_key?).with(@table).and_return(true)
421
+ @table_support.should_receive(:primary_key).with(@table).and_return(['id', nil])
422
+ id = ["id", "2"]
423
+
424
+ @table_support.should_receive(:delete_row).with(
425
+ @table, id)
426
+
427
+ delete :base, {:use_route => :rails_db_admin,
428
+ :action => "table_data",
429
+ :table => @table,
430
+ :id => "2"}
431
+ end
432
+
433
+ it "should fail because we don't send an id parameter" do
434
+
435
+ id = "2"
436
+ @table_support.should_receive(:primary_key?).and_return(false)
437
+
438
+ delete :base, {:use_route => :rails_db_admin,
439
+ :action => "table_data",
440
+ :table => "role_types",
441
+ :id => id}
442
+
443
+ parsed_body = JSON.parse(response.body)
444
+ parsed_body["success"].should eq(false)
445
+ parsed_body["exception"].should eq("Unable to determine primary key "\
446
+ "on this table. Delete not performed")
447
+ end
448
+ end
449
+
450
+ describe "GET tables" do
451
+
452
+ it "should return data for a tree grid of tables" do
453
+
454
+ get :base, {:use_route => :rails_db_admin,
455
+ :action => "tables",
456
+ :node => "root"}
457
+
458
+ parsed_body = JSON.parse(response.body)
459
+ parsed_body[0].should eq({"isTable"=>true, "text"=>"app_containers",
460
+ "id"=>"app_containers", "iconCls"=>"icon-data",
461
+ "leaf"=>false})
462
+ end
463
+
464
+ it "should create nested tree of column names if a node id is passed in" do
465
+
466
+ get :base, {:use_route => :rails_db_admin,
467
+ :action => "tables",
468
+ :node => "app_containers"}
469
+
470
+ parsed_body = JSON.parse(response.body)
471
+ parsed_body[0].should eq({"text"=>"id : integer",
472
+ "iconCls"=>"icon-gear",
473
+ "leaf"=>true})
474
+ end
475
+ end
476
+
477
+ after(:all) do
478
+ RoleType.where(:internal_identifier => "execute_query_test_role").destroy_all
479
+ RoleType.where(:internal_identifier => "execute_query_test_role_2").destroy_all
480
+ end
481
+ end