mongo3 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/HISTORY +7 -1
  2. data/data/populate +20 -0
  3. data/lib/controllers/collections.rb +54 -13
  4. data/lib/controllers/databases.rb +2 -2
  5. data/lib/controllers/explore.rb +8 -13
  6. data/lib/helpers/flash_helper.rb +9 -0
  7. data/lib/helpers/main_helper.rb +33 -21
  8. data/lib/mongo3/connection.rb +40 -4
  9. data/lib/mongo3.rb +1 -1
  10. data/lib/public/images/button_act.png +0 -0
  11. data/lib/public/images/button_act.psd +0 -0
  12. data/lib/public/images/clear.png +0 -0
  13. data/lib/public/images/clear.psd +0 -0
  14. data/lib/public/images/delete_big.png +0 -0
  15. data/lib/public/images/delete_big.psd +0 -0
  16. data/lib/public/images/loading.gif +0 -0
  17. data/lib/public/javascripts/jquery.confirm.js +132 -0
  18. data/lib/public/javascripts/jquery.example.js +160 -0
  19. data/lib/public/stylesheets/mongo3.css +37 -1
  20. data/lib/views/{_fields_form.erb → collections/_fields_form.erb} +7 -6
  21. data/lib/views/collections/_results.erb +12 -0
  22. data/lib/views/collections/_rows.erb +53 -0
  23. data/lib/views/collections/_search_form.erb +27 -0
  24. data/lib/views/collections/list.erb +52 -0
  25. data/lib/views/collections/results.js.erb +2 -0
  26. data/lib/views/collections/update.js.erb +1 -0
  27. data/lib/views/{db_list.erb → databases/list.erb} +3 -3
  28. data/lib/views/explore/center_js.erb +3 -0
  29. data/lib/views/{explore.erb → explore/explore.erb} +1 -4
  30. data/lib/views/explore/more_data_js.erb +3 -0
  31. data/lib/views/explore/update_crumb_js.erb +1 -0
  32. data/lib/views/layout.erb +2 -0
  33. data/lib/views/shared/_flash.erb +3 -0
  34. data/lib/views/shared/flash.js.erb +8 -0
  35. metadata +49 -44
  36. data/lib/views/_cltn.erb +0 -25
  37. data/lib/views/_cltn_info.erb +0 -95
  38. data/lib/views/_collection.erb +0 -5
  39. data/lib/views/_search_form.erb +0 -30
  40. data/lib/views/center_js.erb +0 -3
  41. data/lib/views/cltn_list.erb +0 -16
  42. data/lib/views/cltn_show.erb +0 -1
  43. data/lib/views/cltn_update_js.erb +0 -1
  44. data/lib/views/collection.erb +0 -44
  45. data/lib/views/database.erb +0 -37
  46. data/lib/views/db_show.erb +0 -1
  47. data/lib/views/more_data_js.erb +0 -3
  48. data/lib/views/show_cltn.erb +0 -1
  49. data/lib/views/update_crumb_js.erb +0 -1
  50. /data/lib/views/{_crumbs.erb → explore/_crumbs.erb} +0 -0
  51. /data/lib/views/{_dump_array.erb → explore/_dump_array.erb} +0 -0
  52. /data/lib/views/{_dump_hash.erb → explore/_dump_hash.erb} +0 -0
  53. /data/lib/views/{_info.erb → explore/_info.erb} +0 -0
data/HISTORY CHANGED
@@ -1,2 +1,8 @@
1
1
  0.0.1 - Initial Drop
2
- not much there yet...
2
+ not much there yet...
3
+ 0.0.2 -
4
+ Added mongo auth
5
+ Added collection/db views
6
+ 0.0.3 -
7
+ Added collection crud
8
+ Refactored view code
data/data/populate ADDED
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'mongo'
4
+
5
+ connection = Mongo::Connection.new( 'localhost', 27018 )
6
+ db = connection.db( 'admin' )
7
+ db.authenticate( 'admin', 'admin')
8
+
9
+ db = connection.db( 'fernand' )
10
+ cltn = db.create_collection( "blee" )
11
+ cltn.remove
12
+
13
+ 10.times do |i|
14
+ row = { :name => "fred_#{i}", :last_name => "Blee_#{i}" }
15
+ puts "Inserting row #{row.inspect}"
16
+ cltn.insert( row )
17
+ end
18
+
19
+ cltn = db["fred"]
20
+ puts "Inserted `#{cltn.count} records"
@@ -4,55 +4,96 @@ module Collections
4
4
 
5
5
  # ---------------------------------------------------------------------------
6
6
  # Paginate on a collection
7
- get "/cltn/:page" do
7
+ get "/collections/:page" do
8
8
  @back_url = "/explore/back"
9
9
  @page = params[:page].to_i || 1
10
10
  @title = title_for( session[:path_names] )
11
11
 
12
12
  load_cltn( params[:page].to_i )
13
- erb :cltn_list
13
+ erb :'collections/list'
14
14
  end
15
15
 
16
16
  # ---------------------------------------------------------------------------
17
- post "/cltn_refresh/:page" do
17
+ post "/collections/refresh/:page/" do
18
18
  selected_cols = params[:cols].keys.sort
19
19
  session[:selected_cols] = selected_cols
20
20
 
21
21
  load_cltn( params[:page].to_i )
22
22
 
23
- erb :cltn_update_js, :layout => false
23
+ erb :'collections/update.js', :layout => false
24
24
  end
25
25
 
26
26
  # ---------------------------------------------------------------------------
27
27
  # BOZO !! Validation....
28
- post "/cltn_search" do
29
- json = params[:search].gsub( /'/, "\"" )
28
+ post "/collections/search/" do
29
+ json = params[:search].gsub( /'/, "\"" )
30
30
  if json.empty?
31
31
  @query = {}
32
32
  @sort = {}
33
33
  else
34
- tokens = json.split( "|" )
35
- @query = JSON.parse( tokens.shift )
36
- @sort = tokens.empty? ? [] : JSON.parse( tokens.first )
34
+ tokens = json.split( "|" )
35
+ begin
36
+ @query = JSON.parse( tokens.shift )
37
+ @sort = tokens.empty? ? [] : JSON.parse( tokens.first )
38
+ rescue => boom
39
+ flash_it!( :error, boom )
40
+ return erb(:'shared/flash.js', :layout => false)
41
+ end
37
42
  end
38
43
  session[:query_params] = [@query, @sort]
39
44
 
40
45
  load_cltn
41
- erb :cltn_update_js, :layout => false
46
+ erb :'collections/update.js', :layout => false
42
47
  end
43
48
 
49
+ post '/collections/delete/' do
50
+ path_names = session[:path_names]
51
+ options.connection.delete_row( path_names, params[:id] )
52
+ load_cltn
53
+ erb :'collections/update.js', :layout => false
54
+ end
55
+
56
+ get '/collections/clear/' do
57
+ path_names = session[:path_names]
58
+ options.connection.clear_cltn( path_names )
59
+ load_cltn
60
+
61
+ flash_it!( :info, "Collection #{path_names.split('|').last} was cleared successfully!" )
62
+ erb :'collections/results.js', :layout => false
63
+ end
64
+
65
+ get '/collections/drop/' do
66
+ path_names = session[:path_names]
67
+ options.connection.drop_cltn( path_names )
68
+ load_cltn
69
+
70
+ flash_it!( :info, "Collection #{path_names.split('|').last} was dropped successfully!" )
71
+ erb :'collections/results.js', :layout => false
72
+ end
73
+
44
74
  # ===========================================================================
45
75
  helpers do
46
76
  def load_cltn( page=1 )
47
77
  query_params = session[:query_params] || [{},[]]
48
- @query = [query_params.first.to_json, query_params.last.to_json].join(",")
78
+
79
+ if query_params.first.empty? and query_params.last.empty?
80
+ @query = nil
81
+ else
82
+ @query = [query_params.first.to_json, query_params.last.to_json].join( " | " )
83
+ @query.gsub!( /\"/, "'" )
84
+ end
85
+ puts "QUERY #{@query.inspect}"
49
86
  @page = page
50
87
  path_names = session[:path_names]
51
88
  path_ids = session[:path_ids]
52
89
 
53
90
  @cltn = options.connection.paginate_cltn( path_names, query_params, @page, 15 )
54
- @cols = @cltn.first.keys.sort
55
- @selected_cols = session[:selected_cols] || @cols[0...5]
91
+ @cols = []
92
+ @selected_cols = []
93
+ unless @cltn.empty?
94
+ @cols = @cltn.first.keys.sort
95
+ @selected_cols = session[:selected_cols] || @cols[0...5]
96
+ end
56
97
  end
57
98
  end
58
99
  end
@@ -1,7 +1,7 @@
1
1
  module Databases
2
2
 
3
3
  # ---------------------------------------------------------------------------
4
- get "/db/:page" do
4
+ get "/databases/:page" do
5
5
  page = params[:page].to_i || 1
6
6
  path_ids = session[:path_ids]
7
7
  path_names = session[:path_names]
@@ -10,7 +10,7 @@ module Databases
10
10
  @title = title_for( path_names )
11
11
  @back_url = "/explore/back"
12
12
 
13
- erb :db_list
13
+ erb :'databases/list'
14
14
  end
15
15
 
16
16
  end
@@ -4,8 +4,7 @@ module Explore
4
4
  get '/explore' do
5
5
  reset_crumbs!
6
6
  @root = options.connection.build_tree
7
- Mongo3::Node.dump( @root )
8
- erb :explore
7
+ erb :'explore/explore'
9
8
  end
10
9
 
11
10
  # -----------------------------------------------------------------------------
@@ -21,12 +20,11 @@ Mongo3::Node.dump( @root )
21
20
  crumbs_from_path( path_ids, path_names )
22
21
 
23
22
  @root = options.connection.build_partial_tree( path_ids, path_names )
24
- # Mongo3::Node.dump( @root )
25
23
 
26
24
  # need to adjust crumbs in case something got blown...
27
25
  @center = path_ids.split( "|" ).last
28
26
 
29
- erb :explore
27
+ erb :'explore/explore'
30
28
  end
31
29
 
32
30
  # -----------------------------------------------------------------------------
@@ -39,7 +37,7 @@ Mongo3::Node.dump( @root )
39
37
  session[:path_ids] = path_ids
40
38
  session[:path_names] = path_names
41
39
 
42
- partial :info
40
+ partial :'explore/info'
43
41
  end
44
42
 
45
43
  # -----------------------------------------------------------------------------
@@ -53,25 +51,22 @@ Mongo3::Node.dump( @root )
53
51
  crumbs_from_path( path_ids, path_names )
54
52
 
55
53
  @sub_tree = options.connection.build_sub_tree( path_ids, path_names )
56
- # Mongo3::Node.dump_adj( @sub_tree )
57
54
  @node_id = @sub_tree.first[:id]
58
55
 
59
- erb :more_data_js, :layout => false
56
+ erb :'explore/more_data_js', :layout => false
60
57
  end
61
58
 
62
59
  # -----------------------------------------------------------------------------
63
60
  get '/explore/update_crumb/:path/:crumbs' do
64
61
  crumbs_from_path( params[:path], params[:crumbs] )
65
- erb :update_crumb_js, :layout => false
62
+ erb :'explore/update_crumb_js', :layout => false
66
63
  end
67
64
 
68
65
  # -----------------------------------------------------------------------------
69
66
  get '/explore/center/:node_id' do
70
- @node_id = params[:node_id]
71
-
72
- pop_crumb!( @node_id )
73
-
74
- erb :center_js, :layout => false
67
+ @node_id = params[:node_id]
68
+ pop_crumb!( @node_id )
69
+ erb :'explore/center_js', :layout => false
75
70
  end
76
71
 
77
72
  end
@@ -0,0 +1,9 @@
1
+ module FlashHelper
2
+ helpers do
3
+
4
+ def flash_it!( type, msg )
5
+ @flash = session[:flash] || OrderedHash.new
6
+ @flash[type] = msg
7
+ end
8
+ end
9
+ end
@@ -1,18 +1,17 @@
1
- module MainHelper
2
-
3
- JS_ESCAPE_MAP =
4
- {
5
- '\\' => '\\\\',
6
- '</' => '<\/',
7
- "\r\n" => '\n',
8
- "\n" => '\n',
9
- "\r" => '\n',
10
- '"' => '\\"',
11
- "'" => "\\'"
12
- }
13
-
1
+ module MainHelper
14
2
  helpers do
15
3
 
4
+ def align_for( value )
5
+ return "right" if value.is_a?(Fixnum)
6
+ "left"
7
+ end
8
+
9
+ # Add thousand markers
10
+ def format_number( value )
11
+ return value.to_s.gsub(/(\d)(?=\d{3}+(\.\d*)?$)/, '\1,') if value.instance_of?(Fixnum)
12
+ value
13
+ end
14
+
16
15
  def back_paths!
17
16
  path_ids = session[:path_ids]
18
17
  path_names = session[:path_names]
@@ -40,27 +39,40 @@ module MainHelper
40
39
  return info if info.is_a?( String )
41
40
  if info.is_a?( Hash )
42
41
  @info = info
43
- partial :dump_hash
42
+ partial :'explore/dump_hash'
44
43
  elsif info.is_a?( Array )
45
44
  @info = info
46
- partial :dump_array
45
+ partial :'explore/dump_array'
47
46
  else
48
- info
47
+ format_number( info )
49
48
  end
50
49
  end
51
50
 
52
51
  def partial( page, options={} )
53
- erb "_#{page}".to_sym, options.merge!( :layout => false )
52
+ if page.to_s.index( /\// )
53
+ page = page.to_s.gsub( /\//, '/_' )
54
+ else
55
+ page = "_" + page.to_s
56
+ end
57
+ erb page.to_sym, options.merge!( :layout => false )
54
58
  end
55
59
 
60
+ JS_ESCAPE_MAP =
61
+ {
62
+ '\\' => '\\\\',
63
+ '</' => '<\/',
64
+ "\r\n" => '\n',
65
+ "\n" => '\n',
66
+ "\r" => '\n',
67
+ '"' => '\\"',
68
+ "'" => "\\'"
69
+ }
56
70
  def escape_javascript(javascript)
57
71
  if javascript
58
72
  javascript.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { JS_ESCAPE_MAP[$1] }
59
73
  else
60
74
  ''
61
75
  end
62
- end
63
-
64
- end
65
-
76
+ end
77
+ end
66
78
  end
@@ -4,7 +4,43 @@ module Mongo3
4
4
  def initialize( config_file )
5
5
  @config_file = config_file
6
6
  end
7
-
7
+
8
+ def drop_cltn( path_names )
9
+ path_name_tokens = path_names.split( "|" )
10
+ env = path_name_tokens[1]
11
+ connect_for( env ) do |con|
12
+ cltn_name = path_name_tokens.pop
13
+ db_name = path_name_tokens.pop
14
+ db = con.db( db_name )
15
+ cltn = db[cltn_name]
16
+ cltn.drop
17
+ end
18
+ end
19
+
20
+ def clear_cltn( path_names )
21
+ path_name_tokens = path_names.split( "|" )
22
+ env = path_name_tokens[1]
23
+ connect_for( env ) do |con|
24
+ cltn_name = path_name_tokens.pop
25
+ db_name = path_name_tokens.pop
26
+ db = con.db( db_name )
27
+ cltn = db[cltn_name]
28
+ cltn.remove
29
+ end
30
+ end
31
+
32
+ def delete_row( path_names, id )
33
+ path_name_tokens = path_names.split( "|" )
34
+ env = path_name_tokens[1]
35
+ connect_for( env ) do |con|
36
+ cltn_name = path_name_tokens.pop
37
+ db_name = path_name_tokens.pop
38
+ db = con.db( db_name )
39
+ cltn = db[cltn_name]
40
+ cltn.remove( {:_id => Mongo::ObjectID.from_string(id) } )
41
+ end
42
+ end
43
+
8
44
  def show( path_names )
9
45
  path_name_tokens = path_names.split( "|" )
10
46
  info = OrderedHash.new
@@ -24,7 +60,7 @@ module Mongo3
24
60
  db_name = path_name_tokens.pop
25
61
  connect_for( env ) do |con|
26
62
  db = con.db( db_name )
27
- info[:edit] = "/db/1"
63
+ info[:edit] = "/databases/1"
28
64
  info[:size] = to_mb( con.database_info[db_name] )
29
65
  info[:node] = db.nodes
30
66
  info[:collections] = db.collection_names.size
@@ -39,7 +75,7 @@ module Mongo3
39
75
  cltn = db[cltn_name]
40
76
  indexes = db.index_information( cltn_name )
41
77
 
42
- info[:edit] = "/cltn/1"
78
+ info[:edit] = "/collections/1"
43
79
  info[:size] = cltn.count
44
80
  info[:indexes] = format_indexes( indexes ) if indexes and !indexes.empty?
45
81
  end
@@ -219,7 +255,7 @@ module Mongo3
219
255
 
220
256
  # =========================================================================
221
257
  private
222
-
258
+
223
259
  # Connects to mongo given an environment
224
260
  # BOZO !! Auth...
225
261
  def connect_for( env, &block )
data/lib/mongo3.rb CHANGED
@@ -2,7 +2,7 @@
2
2
  module Mongo3
3
3
 
4
4
  # :stopdoc:
5
- VERSION = '0.0.2'
5
+ VERSION = '0.0.3'
6
6
  LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
7
7
  PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
8
8
  # :startdoc:
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,132 @@
1
+ /**
2
+ * Confirm plugin 1.2
3
+ *
4
+ * Copyright (c) 2007 Nadia Alramli (http://nadiana.com/)
5
+ * Dual licensed under the MIT (MIT-LICENSE.txt)
6
+ * and GPL (GPL-LICENSE.txt) licenses.
7
+ */
8
+
9
+ /**
10
+ * For more docs and examples visit:
11
+ * http://nadiana.com/jquery-confirm-plugin
12
+ * For comments, suggestions or bug reporting,
13
+ * email me at: http://nadiana.com/contact/
14
+ */
15
+
16
+ jQuery.fn.confirm = function(options) {
17
+ options = jQuery.extend({
18
+ msg: 'Are you sure?',
19
+ stopAfter: 'never',
20
+ wrapper: '<span></span>',
21
+ eventType: 'click',
22
+ dialogShow: 'show',
23
+ dialogSpeed: '',
24
+ timeout: 0
25
+ }, options);
26
+ options.stopAfter = options.stopAfter.toLowerCase();
27
+ if (!options.stopAfter in ['never', 'once', 'ok', 'cancel']) {
28
+ options.stopAfter = 'never';
29
+ }
30
+ options.buttons = jQuery.extend({
31
+ ok: 'Yes',
32
+ cancel: 'No',
33
+ wrapper:'<a href="#"></a>',
34
+ separator: '/'
35
+ }, options.buttons);
36
+
37
+ // Shortcut to eventType.
38
+ var type = options.eventType;
39
+
40
+ return this.each(function() {
41
+ var target = this;
42
+ var $target = jQuery(target);
43
+ var timer;
44
+ var saveHandlers = function() {
45
+ var events = jQuery.data(target, 'events');
46
+ if (!events) {
47
+ // There are no handlers to save.
48
+ return;
49
+ }
50
+ target._handlers = new Array();
51
+ for (var i in events[type]) {
52
+ target._handlers.push(events[type][i]);
53
+ }
54
+ }
55
+
56
+ // Create ok button, and bind in to a click handler.
57
+ var $ok = jQuery(options.buttons.wrapper)
58
+ .append(options.buttons.ok)
59
+ .click(function() {
60
+ // Check if timeout is set.
61
+ if (options.timeout != 0) {
62
+ clearTimeout(timer);
63
+ }
64
+ $target.unbind(type, handler);
65
+ $target.show();
66
+ $dialog.hide();
67
+ // Rebind the saved handlers.
68
+ if (target._handlers != undefined) {
69
+ jQuery.each(target._handlers, function() {
70
+ $target.click(this);
71
+ });
72
+ }
73
+ // Trigger click event.
74
+ $target.click();
75
+ if (options.stopAfter != 'ok' && options.stopAfter != 'once') {
76
+ $target.unbind(type);
77
+ // Rebind the confirmation handler.
78
+ $target.one(type, handler);
79
+ }
80
+ return false;
81
+ })
82
+
83
+ var $cancel = jQuery(options.buttons.wrapper).append(options.buttons.cancel).click(function() {
84
+ // Check if timeout is set.
85
+ if (options.timeout != 0) {
86
+ clearTimeout(timer);
87
+ }
88
+ if (options.stopAfter != 'cancel' && options.stopAfter != 'once') {
89
+ $target.one(type, handler);
90
+ }
91
+ $target.show();
92
+ $dialog.hide();
93
+ return false;
94
+ });
95
+
96
+ if (options.buttons.cls) {
97
+ $ok.addClass(options.buttons.cls);
98
+ $cancel.addClass(options.buttons.cls);
99
+ }
100
+
101
+ var $dialog = jQuery(options.wrapper)
102
+ .append(options.msg)
103
+ .append($ok)
104
+ .append(options.buttons.separator)
105
+ .append($cancel);
106
+
107
+ var handler = function() {
108
+ jQuery(this).hide();
109
+
110
+ // Do this check because of a jQuery bug
111
+ if (options.dialogShow != 'show') {
112
+ $dialog.hide();
113
+ }
114
+
115
+ $dialog.insertBefore(this);
116
+ // Display the dialog.
117
+ $dialog[options.dialogShow](options.dialogSpeed);
118
+ if (options.timeout != 0) {
119
+ // Set timeout
120
+ clearTimeout(timer);
121
+ timer = setTimeout(function() {$cancel.click(); $target.one(type, handler);}, options.timeout);
122
+ }
123
+ return false;
124
+ };
125
+
126
+ saveHandlers();
127
+ $target.unbind(type);
128
+ target._confirm = handler
129
+ target._confirmEvent = type;
130
+ $target.one(type, handler);
131
+ });
132
+ }