databrowser 0.8 → 0.9

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.
@@ -4,6 +4,7 @@ require 'digest/sha1'
4
4
  module DataBrowser
5
5
  @@user_digest = nil
6
6
  @@models = []
7
+ @@tables = []
7
8
 
8
9
  class << self
9
10
  def should_auth
@@ -36,5 +37,13 @@ module DataBrowser
36
37
  def models=(models)
37
38
  @@models = models if models.is_a?(Array)
38
39
  end
40
+
41
+ def tables
42
+ @@tables
43
+ end
44
+
45
+ def tables=(tables)
46
+ @@tables = tables if tables.is_a?(Array)
47
+ end
39
48
  end
40
49
  end
@@ -3,16 +3,15 @@ module DataBrowser
3
3
  layout "data_browser"
4
4
  protect_from_forgery :secret => Time.now.to_i.to_s
5
5
  before_filter :load_models, :authenticate
6
- before_filter :load_current_model, :except => [:index]
7
6
 
8
- @@models = nil
9
-
7
+ helper_method :current_model, :current_model_id
8
+
10
9
  # all the work here is being done by :load_models
11
10
  def index; end
12
11
 
13
12
  def browse
14
- params[:select] ||= @model.column_names
15
- @objects = @model.find(:all,
13
+ params[:select] ||= current_model.column_names
14
+ @objects = current_model.find(:all,
16
15
  :conditions => params[:conditions],
17
16
  :select => params[:select] ? params[:select].join(", ") : nil
18
17
  )
@@ -24,64 +23,55 @@ module DataBrowser
24
23
  end
25
24
 
26
25
  def empty
27
- @model.delete_all
28
- flash[:notice] = "#{@model.to_s} model was emptied"
26
+ current_model.delete_all
27
+ flash[:notice] = "#{current_model.table_name} model was emptied"
29
28
  redirect_to :action => "index"
30
29
  end
31
30
 
32
31
  def new
33
- @obj = @model.new
32
+ @obj = current_model.new
34
33
  end
35
34
 
36
35
  def edit
37
- @obj = @model.find(params[:id])
36
+ @obj = current_model.find(params[:id])
38
37
  end
39
38
 
40
39
  def destroy
41
- @obj = @model.find(params[:id])
40
+ @obj = current_model.find(params[:id])
42
41
  @obj.destroy()
43
- flash[:notice] = "#{@model.to_s} #{@obj.to_param} successfuly deleted!"
44
- redirect_to :action => "browse", :model => @model.to_s
42
+ flash[:notice] = "#{current_model.table_name} #{@obj.to_param} successfuly deleted!"
43
+ redirect_to :action => "browse", :model => current_model_id
45
44
  end
46
45
 
47
46
  def update
48
- @obj = @model.find(params[:id])
49
- @obj.attributes = params[@model.to_s.underscore]
47
+ @obj = current_model.find(params[:id])
48
+ @obj.update_attributes(params[current_model.to_s.underscore])
50
49
 
51
- @obj.save(false)
52
- flash[:notice] = "#{@model.to_s} #{@obj.to_param} successfuly saved!"
53
- redirect_to :action => "browse", :model => @model.to_s
50
+ flash[:notice] = "#{current_model.table_name} #{@obj.to_param} successfuly saved!"
51
+ redirect_to :action => "browse", :model => current_model_id
54
52
  end
55
53
 
56
54
  def create
57
- @obj = @model.new(params[@model.to_s.underscore])
55
+ @obj = current_model.new(params[current_model.to_s.underscore])
58
56
 
59
57
  @obj.save(false)
60
- flash[:notice] = "#{@model.to_s} #{@obj.to_param} successfuly saved!"
61
- redirect_to :action => "browse", :model => @model.to_s
58
+ flash[:notice] = "#{current_model.table_name} #{@obj.to_param} successfuly saved!"
59
+ redirect_to :action => "browse", :model => current_model_id
62
60
  end
63
61
 
64
62
  protected
65
63
 
66
64
  def load_models
67
- # this will make that the models won't be fetched in every request
68
65
  if DataBrowser.models.size == 0
69
- models_root = File.join(RAILS_ROOT, "app", "models", "*.rb")
70
- models = Dir.glob(models_root).collect do |c|
71
- c = File.basename(c).gsub(/\.rb/, "").classify
72
- c = Kernel.const_get(c)
73
- c if c < ActiveRecord::Base
66
+ (DataBrowser.tables.empty?? ActiveRecord::Base.connection.tables : DataBrowser.tables).each do |table|
67
+ model_name = "DataBrowser::#{table.classify}"
68
+ eval("#{model_name} = Class.new(ActiveRecord::Base); DataBrowser.models.push(#{model_name})")
74
69
  end
75
- DataBrowser.models = models.compact.sort { |a, b| a.to_s <=> b.to_s }
76
70
  end
77
71
 
78
72
  @models = DataBrowser.models
79
73
  end
80
74
 
81
- def load_current_model
82
- @model = Kernel.const_get(params[:model]) if params[:model]
83
- end
84
-
85
75
  def authenticate
86
76
  if DataBrowser::should_auth && !DataBrowser::check_digest(session[:databrowser])
87
77
  authenticate_or_request_with_http_basic("DataBrowser") do |user, pass|
@@ -89,5 +79,13 @@ module DataBrowser
89
79
  end
90
80
  end
91
81
  end
82
+
83
+ def current_model
84
+ @model ||= DataBrowser.models[current_model_id] if current_model_id
85
+ end
86
+
87
+ def current_model_id
88
+ params[:model].to_i if params[:model]
89
+ end
92
90
  end
93
91
  end
@@ -3,11 +3,21 @@ module DataBrowser
3
3
  def databrowser(opts={})
4
4
  root = opts[:root] || "databrowser"
5
5
  with_options :controller => "data_browser/data_browser" do |db|
6
- db.connect "/#{root}"
7
- db.connect "/#{root}/about", :action => "about"
8
- db.connect "/#{root}/:model", :action => "browse"
9
- db.connect "/#{root}/:model/:action"
10
- db.connect "/#{root}/:model/:id/:action"
6
+ # Databrowser pages
7
+ db.data_browser_home "/#{root}", :action => "index"
8
+ db.data_browser_about "/#{root}/about", :action => "about"
9
+ # Model manipulation
10
+ db.with_options :model => /\d+/ do |model|
11
+ model.data_browser_model "/#{root}/:model", :action => "browse", :conditions => {:method => :get}
12
+ model.data_browser_new "/#{root}/:model/new", :action => "new", :conditions => {:method => :get}
13
+ model.data_browser_object "/#{root}/:model/:id", :action => "show", :conditions => {:method => :get}
14
+ model.data_browser_edit "/#{root}/:model/:id/edit", :action => "edit", :conditions => {:method => :get}
15
+
16
+ model.connect "/#{root}/:model", :action => "empty", :conditions => {:method => :delete}
17
+ model.connect "/#{root}/:model/:id", :action => "update", :conditions => {:method => :put}
18
+ model.connect "/#{root}/:model/:id", :action => "destroy", :conditions => {:method => :delete}
19
+ model.connect "/#{root}/:model", :action => "create", :conditions => {:method => :post}
20
+ end
11
21
  end
12
22
  end
13
23
  end
@@ -1,6 +1,6 @@
1
1
  <ul>
2
2
  <%
3
- @model.columns_hash.each do |name, column|
3
+ current_model.columns_hash.each do |name, column|
4
4
  next if name.eql?("id")
5
5
  %>
6
6
  <li>
@@ -1,6 +1,6 @@
1
1
  <tr class="<%= cycle("odd", "") %>">
2
- <td><%= link_to("Edit", :action => "edit", :id => obj.to_param, :model => @model.to_s) %></td>
3
- <td><%= link_to("Delete", :action => "destroy", :id => obj.to_param, :model => @model.to_s) %></td>
2
+ <td><%= link_to("Edit", data_browser_edit_path(:id => obj.to_param, :model => current_model_id)) unless obj.to_param.nil? %></td>
3
+ <td><%= link_to("Delete", data_browser_object_path(:id => obj.to_param, :model => current_model_id), :method => :delete, :confirm => "Are you sure?") unless obj.to_param.nil? %></td>
4
4
  <% for column in params[:select] %>
5
5
  <td><%= obj.attributes[column] %></td>
6
6
  <% end %>
@@ -2,6 +2,6 @@
2
2
 
3
3
  <p>Rails DataBrowser is a data browser based on the idea of <a href="http://www.djangoproject.com/documentation/databrowse/">Django's Databrowser</a>.</p>
4
4
  <p>Using Rails DataBrowser you can browse, view, edit, crate and delete data through your model classes. It's not a replacement to your DBMS or to your CMS, it's just a helper for developers and WebMasters to manage data without.</p>
5
- <p>If you find a bug, please report it <a href="http://trac.milk-it.net/open/newticket">here</a></p>
5
+ <p>If you find a bug, please report it <a href="http://redmine.milk-it.net/projects/databrowser/issues/new">here</a></p>
6
6
  <p />
7
- <p>Rails DataBrowser plugin is sponsored by <a href="http://milk-it.net">Milk-it Brasil Software House</a> and maintained by <%= mail_to "carlos@milk-it.net", "Carlos Júnior", :encode => "hex", :replace_at => " at ", :replace_dot => "dot" %>.</p>
7
+ <p>Rails DataBrowser plugin is sponsored by <a href="http://milk-it.net">Milk-it Software House</a> and maintained by <%= mail_to "carlos@milk-it.net", "Carlos Júnior", :encode => "hex", :replace_at => " at ", :replace_dot => "dot" %>.</p>
@@ -1,8 +1,8 @@
1
- <% form_tag({:model => @model.to_s}, {:id => "search_form"}) do %>
1
+ <% form_tag(data_browser_model_path(:model => current_model_id), {:id => "search_form"}) do %>
2
2
  <div id="search-box" style="display:none">
3
3
  <p>
4
4
  <label>Select</label><br />
5
- <% for column in @model.column_names %>
5
+ <% for column in current_model.column_names %>
6
6
  <label><%= check_box_tag("select[]", column, params[:select].include?(column)) %><%= column %></label>
7
7
  <% end %>
8
8
  </p>
@@ -1,8 +1,8 @@
1
1
  <ul>
2
- <% @models.each do |model| %>
2
+ <% @models.each_with_index do |model, i| %>
3
3
  <li>
4
- <%= link_to(model.to_s, :action => "browse", :model => model.to_s) %> (<%=model.count%> records)
5
- <%= link_to("empty", {:action => "empty", :model => model.to_s}, :confirm => "Are you sure?") %>
4
+ <%= link_to(model.table_name, data_browser_model_path(:model => i)) %> (<%= model.count %> records)
5
+ <%= link_to("empty", data_browser_model_path(:model => i), :confirm => "Are you sure?", :method => :delete) %>
6
6
  </li>
7
7
  <% end %>
8
8
  </ul>
@@ -1,4 +1,4 @@
1
- <% form_for(@obj, :url => {:action => "create", :model => @model.to_s}) do |f| %>
1
+ <% form_for(@obj, :url => data_browser_model_path(:model => current_model_id), :method => :post) do |f| %>
2
2
  <%= render :partial => "form", :locals => {:form => f} %>
3
3
  <div class="button">
4
4
  <%= submit_tag("Create") %> or <%= link_to("cancel", :action => "browse", :model => @model.to_s) %>
@@ -209,15 +209,15 @@ document.getElementById("show-search").style.display = "none";
209
209
  </script>
210
210
  </head>
211
211
  <body id="home">
212
- <h1><%= link_to("<b>Rails</b>DataBrowser", :action => "index") %></h1>
212
+ <h1><%= link_to("<b>Rails</b>DataBrowser", data_browser_home_path()) %></h1>
213
213
  <ul id="models">
214
- <% for model in @models %>
215
- <li><%= link_to(model.to_s, :action => "browse", :model => model.to_s) %></li>
214
+ <% @models.each_with_index do |model, i| %>
215
+ <li><%= link_to(model.table_name, data_browser_model_path(:model => i)) %></li>
216
216
  <% end %>
217
217
  </ul>
218
218
  <div id="content">
219
- <% if @model %>
220
- <h2><%= @model.to_s %> (<%=@model.table_name%> table) <%= link_to("new", :action => "new", :model => @model.to_s) %></h2>
219
+ <% if current_model %>
220
+ <h2><%= current_model.table_name %> <%= link_to("new", data_browser_new_path(:model => current_model_id)) %></h2>
221
221
  <% end %>
222
222
  <%= content_tag(:h3, flash[:notice], :class => "notice") if flash[:notice] %>
223
223
  <%= yield %>
@@ -225,12 +225,12 @@ document.getElementById("show-search").style.display = "none";
225
225
  <div style="clear:both">&nbsp;</div>
226
226
  <div id="footer">
227
227
  <p class="left">
228
- Powered by <%= link_to "DataBrowser", :action => "about" %><br />
228
+ Powered by <%= link_to("DataBrowser", data_browser_about_path()) %><br />
229
229
  by <a href="http://milk-it.net">Milk-it</a>
230
230
  </p>
231
231
  <p class="right">
232
232
  Visit Rails DataBrowser Official WebSite.<br />
233
- <a href="http://trac.milk-it.net/open/wiki/DataBrowser">trac.milk-it.net/open</a>
233
+ <a href="http://redmine.milk-it.net/projects/show/databrowser">redmine.milk-it.net/projects</a>
234
234
  </p>
235
235
  </div>
236
236
  </body>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: databrowser
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.8"
4
+ version: "0.9"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carlos Junior
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-06-07 00:00:00 -03:00
12
+ date: 2008-08-05 00:00:00 -03:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency