ari 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +99 -0
  3. data/Rakefile +38 -0
  4. data/lib/ari.rb +6 -0
  5. data/lib/ari/active_record.rb +33 -0
  6. data/lib/ari/inspector.rb +153 -0
  7. data/lib/ari/version.rb +3 -0
  8. data/lib/generators/USAGE +4 -0
  9. data/lib/generators/ari_generator.rb +17 -0
  10. data/lib/generators/templates/controllers/ari_controller.rb +10 -0
  11. data/lib/generators/templates/views/_paging.html.erb +9 -0
  12. data/lib/generators/templates/views/_styles.html.erb +49 -0
  13. data/lib/generators/templates/views/index.html.erb +23 -0
  14. data/lib/generators/templates/views/show.html.erb +34 -0
  15. data/lib/tasks/ari_tasks.rake +4 -0
  16. data/test/ari_test.rb +7 -0
  17. data/test/dummy/README.rdoc +261 -0
  18. data/test/dummy/Rakefile +7 -0
  19. data/test/dummy/app/assets/javascripts/application.js +15 -0
  20. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  21. data/test/dummy/app/controllers/application_controller.rb +3 -0
  22. data/test/dummy/app/controllers/ari_controller.rb +10 -0
  23. data/test/dummy/app/controllers/inspect_controller.rb +5 -0
  24. data/test/dummy/app/helpers/application_helper.rb +2 -0
  25. data/test/dummy/app/models/album.rb +3 -0
  26. data/test/dummy/app/models/package.rb +3 -0
  27. data/test/dummy/app/models/ttt/d/fff.rb +0 -0
  28. data/test/dummy/app/models/ttt/ttt.rb +2 -0
  29. data/test/dummy/app/models/user.rb +10 -0
  30. data/test/dummy/app/views/ari/_paging.html.erb +9 -0
  31. data/test/dummy/app/views/ari/_styles.html.erb +47 -0
  32. data/test/dummy/app/views/ari/index.html.erb +23 -0
  33. data/test/dummy/app/views/ari/show.html.erb +34 -0
  34. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  35. data/test/dummy/config.ru +4 -0
  36. data/test/dummy/config/application.rb +56 -0
  37. data/test/dummy/config/boot.rb +10 -0
  38. data/test/dummy/config/database.yml +25 -0
  39. data/test/dummy/config/environment.rb +5 -0
  40. data/test/dummy/config/environments/development.rb +37 -0
  41. data/test/dummy/config/environments/production.rb +67 -0
  42. data/test/dummy/config/environments/test.rb +37 -0
  43. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  44. data/test/dummy/config/initializers/inflections.rb +15 -0
  45. data/test/dummy/config/initializers/mime_types.rb +5 -0
  46. data/test/dummy/config/initializers/secret_token.rb +7 -0
  47. data/test/dummy/config/initializers/session_store.rb +8 -0
  48. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  49. data/test/dummy/config/locales/en.yml +5 -0
  50. data/test/dummy/config/routes.rb +59 -0
  51. data/test/dummy/db/development.sqlite3 +0 -0
  52. data/test/dummy/db/migrate/20120326121018_test.rb +20 -0
  53. data/test/dummy/db/production.sqlite3 +0 -0
  54. data/test/dummy/db/schema.rb +30 -0
  55. data/test/dummy/db/seeds.rb +22 -0
  56. data/test/dummy/log/development.log +10921 -0
  57. data/test/dummy/log/production.log +0 -0
  58. data/test/dummy/public/404.html +26 -0
  59. data/test/dummy/public/422.html +26 -0
  60. data/test/dummy/public/500.html +25 -0
  61. data/test/dummy/public/favicon.ico +0 -0
  62. data/test/dummy/script/rails +6 -0
  63. data/test/dummy/tmp/cache/assets/CA8/5F0/sprockets%2F73d5979d76f05673b860289b5c7f929f +0 -0
  64. data/test/dummy/tmp/cache/assets/CD8/370/sprockets%2F357970feca3ac29060c1e3861e2c0953 +0 -0
  65. data/test/dummy/tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705 +0 -0
  66. data/test/dummy/tmp/cache/assets/D3B/B90/sprockets%2F902a443f4ab24e9dc49b54c4c421f4d9 +0 -0
  67. data/test/dummy/tmp/cache/assets/D4E/1B0/sprockets%2Ff7cbd26ba1d28d48de824f0e94586655 +0 -0
  68. data/test/dummy/tmp/cache/assets/D5A/EA0/sprockets%2Fd771ace226fc8215a3572e0aa35bb0d6 +0 -0
  69. data/test/dummy/tmp/cache/assets/DCA/390/sprockets%2Fae0dd35d6f3f652aeaad15984ee1e362 +0 -0
  70. data/test/dummy/tmp/cache/assets/DDC/400/sprockets%2Fcffd775d018f68ce5dba1ee0d951a994 +0 -0
  71. data/test/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af +0 -0
  72. data/test/dummy/tmp/cache/assets/E6C/A50/sprockets%2F43aeffaab9beb5f93bf658988fdb11ec +0 -0
  73. data/test/test_helper.rb +10 -0
  74. metadata +205 -0
@@ -0,0 +1,20 @@
1
+ Copyright 2012 AtlantBH
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,99 @@
1
+ Ari
2
+ ===
3
+
4
+ Active Record Inspector - extend rails project to easily display and navigate through ActiveRecord Models. Ari will display model data including columns, relations (has and belongs) and custom functions. It's easy to customize and extend Ari for your purposes.
5
+
6
+ Features
7
+ --------
8
+
9
+ * Generators - automatically generate Ari controller and corresponding views
10
+ * Ari will display both belongs to and has relations as links for easy navigation
11
+ * Customize each model which columns you don't wanna to display
12
+ * Add functions that you like to display
13
+
14
+ Requirements
15
+ --------
16
+ * Rails 3+
17
+
18
+ Installation
19
+ --------
20
+
21
+ Install 'ari' gem:
22
+
23
+ ```
24
+ gem 'ari'
25
+ ```
26
+
27
+ or
28
+
29
+ ```
30
+ gem 'ari', :git => git://github.com/ATLANTBH/ari.git
31
+ ```
32
+
33
+ Note: Currently ari was tested only on rails 3.0+
34
+
35
+ Setup
36
+ --------
37
+
38
+ After installation run ari generator.
39
+
40
+ ```
41
+ rails generate ari
42
+ ```
43
+
44
+ Add following resource to routes.rb.
45
+
46
+ ```
47
+ resources :ari, :only => [:index, :show]
48
+ ```
49
+
50
+ Run rails and point your browser to ari route, e.g. http://localhost:3000/ari
51
+
52
+ Customization
53
+ --------
54
+
55
+ By default ari will display following information from model:
56
+
57
+ * all columns
58
+ * all has_ relations
59
+ * all belongs_ relations
60
+
61
+ You can exclude informations via ari_options
62
+
63
+ ```
64
+ class User < ActiveRecord::Base
65
+ ari_options :exclude => [:password, :money]
66
+ end
67
+ ```
68
+
69
+ This will exclude columns/relations names 'password' and 'money' from displaying.
70
+
71
+ In addition to excluding, sometimes is good idea to include results from some functions, e.g.
72
+
73
+ ```
74
+ class User < ActiveRecord::Base
75
+ ari_options :exclude => [:password, :money], :functions => ['displayable_name']
76
+
77
+ def displayable_name
78
+ return self.name || self.email # if name is nil return email as displayable_name
79
+ end
80
+ end
81
+ ```
82
+
83
+ Preview
84
+ ------
85
+
86
+ Here is preview of ari:
87
+
88
+ [Model listing](https://github.com/ATLANTBH/ari/raw/master/images/table-list.png "Model listing")
89
+ [Sample table 1](https://github.com/ATLANTBH/ari/raw/master/images/sample-1.png "Sample table 1")
90
+ [Sample table 2](https://github.com/ATLANTBH/ari/raw/master/images/sample-2.png "Sample table 2")
91
+
92
+ License
93
+ -------
94
+
95
+ Copyright 2012 AtlantBH
96
+
97
+ ari is licensed under the MIT License.
98
+
99
+ For more info see MIT_LICENSE file.
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Ari'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
28
+ require 'rake/testtask'
29
+
30
+ Rake::TestTask.new(:test) do |t|
31
+ t.libs << 'lib'
32
+ t.libs << 'test'
33
+ t.pattern = 'test/**/*_test.rb'
34
+ t.verbose = false
35
+ end
36
+
37
+
38
+ task :default => :test
@@ -0,0 +1,6 @@
1
+ module Ari
2
+ autoload :ModelHelpers, 'ari/active_record'
3
+ autoload :Inspector, 'ari/inspector'
4
+
5
+ ActiveRecord::Base.send(:include, Ari::ModelHelpers)
6
+ end
@@ -0,0 +1,33 @@
1
+ module Ari
2
+ module ModelHelpers
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+
7
+ #
8
+ # options:
9
+ # :exclude => ['column_name', 'relation_name']
10
+ # :functions => ['function']
11
+ #
12
+ def ari_options(options = {})
13
+ puts "ARI OPTIONS"
14
+ @ari_functions ||= {}
15
+ @ari_exclude ||= {}
16
+ options[:exclude].each { |x| @ari_exclude[x] = true } if options[:exclude]
17
+ options[:functions].each { |x| @ari_functions[x] = true } if options[:functions]
18
+ puts "ARI OPTIONS INSPECT : #{@ari_functions} : #{@ari_exclude}"
19
+ end
20
+
21
+ def ari_functions
22
+ puts "GET FOOS"
23
+ return @ari_functions || {}
24
+ end
25
+
26
+ def ari_exclude
27
+ puts "GET EXS"
28
+ return @ari_exclude || {}
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,153 @@
1
+ module Ari
2
+ module Inspector
3
+
4
+ # TODO - find smarter method to resolve non-conventional file/model names
5
+ def self.collect_models
6
+ models = []
7
+
8
+ # touch classes (to preload them)
9
+ Dir["app/models/**/*.rb"].each do |model_file|
10
+ print "Resolving model : #{model_file} ... "
11
+ begin
12
+ File.basename(model_file, '.rb').camelize.constantize
13
+ rescue
14
+ puts "ERROR"
15
+ # Silent night, holy night, .... - skip files
16
+ end
17
+ puts "DONE"
18
+ end
19
+
20
+ ActiveRecord::Base.descendants.each do |desc|
21
+ models << { :model => desc.name, :table => desc.table_name, :count => desc.count} if desc.table_exists?
22
+ end
23
+
24
+ return models.sort! {|a, b| a[:model] <=> b[:model]}
25
+ end
26
+
27
+ def self.inspect_data(model_name, method, where_clause, parent_model_name, parent_id, belongs, collection, offset)
28
+ offset ||= 0
29
+ collection ||= true
30
+ where_clause ||= ''
31
+
32
+
33
+ model_class = eval(model_name)
34
+ columns = model_class.columns
35
+
36
+ puts "GET GET"
37
+ ari_functions = model_class.ari_functions
38
+ ari_exclude = model_class.ari_exclude
39
+
40
+ columns = model_class.columns
41
+ rows = []
42
+ count = 0
43
+ sql = ''
44
+
45
+ if (belongs.nil?)
46
+ if (where_clause)
47
+ rows = model_class.where(where_clause).limit(10).offset(offset).order(:id)
48
+ sql = "#{model_name}.where(#{where_clause})"
49
+ count = model_class.where(where_clause).count()
50
+ else
51
+ rows = model_class.send(method).limit(10).offset(offset).order(:id)
52
+ sql = "#{model_name}.#{method}()"
53
+ count = model_class.send(method).count()
54
+ end
55
+ else
56
+ parent_model_class = eval(parent_model_name)
57
+ parent_model = parent_model_class.find(parent_id)
58
+ result = nil
59
+ if (collection == 'true')
60
+ result = parent_model.send(method).limit(10).offset(offset).order(:id)
61
+ sql = "#{parent_model_name}.find(#{parent_id}).#{method}()"
62
+ count = parent_model.send(method).count()
63
+ rows = result if result != nil
64
+ else
65
+ result = parent_model.send(method)
66
+ sql = "#{parent_model_name}.find(#{parent_id}).#{method}()"
67
+ rows = [result] if result != nil
68
+ end
69
+ end
70
+
71
+ belongs_to_relations = {}
72
+ has_relations = {}
73
+ model_class.reflect_on_all_associations.each do |rel|
74
+ begin
75
+ if (rel.belongs_to?)
76
+ belongs_to_relations[rel.association_foreign_key] = rel
77
+ else
78
+ has_relations[rel.name.to_s] = rel
79
+ end
80
+ rescue
81
+ end
82
+ end
83
+
84
+ inspected_data = {
85
+ :columns => [],
86
+ :rows => [],
87
+ :count => count,
88
+ :sql => sql
89
+ }
90
+
91
+ columns.each do |column|
92
+ rel = belongs_to_relations[column.name]
93
+
94
+ label = rel ? rel.name : column.name
95
+
96
+ if (ari_exclude[label].nil?)
97
+ if rel
98
+ inspected_data[:columns] << { :label => label, :type => :bt }
99
+ else
100
+ inspected_data[:columns] << { :label => label, :type => :simple }
101
+ end
102
+ end
103
+ end
104
+ has_relations.each do |name, rel|
105
+ if (ari_exclude[rel.name].nil?)
106
+ inspected_data[:columns] << { :label => rel.name, :type => :has }
107
+ end
108
+ end
109
+
110
+ ari_functions.each do |funct, dummy|
111
+ inspected_data[:columns] << { :label => funct, :type => :function }
112
+ end
113
+
114
+
115
+ rows.each do |row|
116
+ one_row = []
117
+
118
+ columns.each do |column|
119
+ rel = belongs_to_relations[column.name]
120
+ if rel
121
+ if row[column.name]
122
+ one_row << { :type =>:bt, :label => row[column.name], :params => {:parent_model =>model_name, :parent_id => row.id, :model => rel.class_name, :method => rel.name, :collection => rel.collection?.to_s, :belongs_to => true} }
123
+ else
124
+ one_row << { :type =>:bt, :label => '', :params => {:parent_model =>model_name, :parent_id => row.id, :model => rel.class_name, :method => rel.name, :collection => rel.collection?.to_s, :belongs_to => true} }
125
+ end
126
+ else
127
+ if column.name == 'id'
128
+ one_row << { :type => :id, :label => row[column.name], :params => {:model => model_name, :where => "id=#{row[column.name]}" } }
129
+ else
130
+ one_row << { :type => :simple, :label => row[column.name] }
131
+ end
132
+ end
133
+ end
134
+
135
+ has_relations.each do |name, rel|
136
+ one_row << {:type => :has, :label => rel.name, :params => {:parent_model => model_name, :parent_id => row.id, :model => rel.class_name, :method => rel.name, :collection => rel.collection?.to_s, :belongs_to => 'false' }}
137
+ end
138
+
139
+ ari_functions.each do |funct, dummy|
140
+ one_row << { :type => :function, :label => row.send(funct) }
141
+ end
142
+
143
+
144
+ inspected_data[:rows] << one_row
145
+ end
146
+
147
+ return inspected_data
148
+
149
+ end
150
+
151
+
152
+ end
153
+ end
@@ -0,0 +1,3 @@
1
+ module Ari
2
+ VERSION = "0.2.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ Ari
2
+ =============
3
+ # Generate MVC
4
+ rails generate ari
@@ -0,0 +1,17 @@
1
+ require 'rails/generators'
2
+
3
+ class AriGenerator < Rails::Generators::Base
4
+ include Rails::Generators::Migration
5
+
6
+ def self.source_root
7
+ @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates/'))
8
+ end
9
+
10
+ def create
11
+ template "views/index.html.erb", File.join('app/views', 'ari', "index.html.erb")
12
+ template "views/show.html.erb", File.join('app/views', 'ari', "show.html.erb")
13
+ template "views/_styles.html.erb", File.join('app/views', 'ari', "_styles.html.erb")
14
+ template "views/_paging.html.erb", File.join('app/views', 'ari', "_paging.html.erb")
15
+ template "controllers/ari_controller.rb", File.join('app/controllers', 'ari_controller.rb')
16
+ end
17
+ end
@@ -0,0 +1,10 @@
1
+ class AriController < ActionController::Base
2
+ def index
3
+ @models = Ari::Inspector.collect_models()
4
+ end
5
+
6
+ def show
7
+ @data = Ari::Inspector.inspect_data(params[:id], params[:method], params[:where], params[:parent_model], params[:parent_id], params[:belongs_to], params[:collection], params[:offset].to_i)
8
+ end
9
+
10
+ end
@@ -0,0 +1,9 @@
1
+ <div style="height:40px; margin-top:40px;">
2
+ <%% (0..((count / 10)-1)).each do |i|%>
3
+ <div style="float:left; padding:4px; line-height:13px; height:13px; background-color:<%%=offset/10 == i ? 'gray;' : 'white'%>; margin-left:3px;">
4
+ <%% offset = i * 10 %>
5
+ <%% params[:offset] = offset %>
6
+ <%%=link_to i, {:controller => "ari", :action => "show", :id => params[:model], }.merge(params) %>
7
+ </div>
8
+ <%% end %>
9
+ </div>
@@ -0,0 +1,49 @@
1
+ <style>
2
+ .ari-table {
3
+ font-family:"lucida grande",tahoma,verdana,arial,sans-serif;
4
+ font-size: 12px;
5
+ border-width: 0px;
6
+ border-spacing: 0px;
7
+ border-style: solid;
8
+ border-color: gray;
9
+ border-collapse: collapse;
10
+ }
11
+ .ari-table th {
12
+ font-size: 12px;
13
+ max-width: 300px;
14
+ word-wrap: break-word;
15
+ padding: 6px;
16
+ }
17
+ .ari-table td {
18
+ font-size: 12px;
19
+ max-width: 300px;
20
+ word-wrap: break-word;
21
+ padding: 6px;
22
+ border-width: 1px;
23
+ border-color: gray;
24
+ border-style: inset;
25
+ }
26
+ .ari-name-div {
27
+ font-size:20px;
28
+ line-height:20px;
29
+ padding:10px;
30
+ }
31
+ .ari-simple {
32
+
33
+ }
34
+ .ari-function {
35
+ background-color:#eeeeee;
36
+ }
37
+ .ari-bt {
38
+ background-color:#CDEBE8;
39
+ font-weight:bold;
40
+ }
41
+ .ari-id {
42
+ font-weight:bold;
43
+ }
44
+ .ari-has {
45
+ background-color:#BDEBBB;
46
+ font-weight:bold;
47
+ }
48
+
49
+ </style>