caravaggio 0.0.12

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 (71) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +37 -0
  4. data/app/assets/images/david_and_goliath.jpg +0 -0
  5. data/app/assets/javascripts/caravaggio/application.js +14 -0
  6. data/app/assets/javascripts/caravaggio/caravaggio.js +265 -0
  7. data/app/assets/stylesheets/caravaggio/application.css +15 -0
  8. data/app/assets/stylesheets/caravaggio/caravaggio.css +90 -0
  9. data/app/controllers/caravaggio/application_controller.rb +5 -0
  10. data/app/controllers/caravaggio/c.txt +2 -0
  11. data/app/controllers/caravaggio/canvas_controller.rb +14 -0
  12. data/app/helpers/caravaggio/application_helper.rb +4 -0
  13. data/app/views/caravaggio/canvas/_association.html.erb +13 -0
  14. data/app/views/caravaggio/canvas/_figure.html.erb +24 -0
  15. data/app/views/caravaggio/canvas/index.html.erb +54 -0
  16. data/app/views/layouts/caravaggio/application.html.erb +14 -0
  17. data/app/views/untitled.txt +0 -0
  18. data/config/initializers/assets.rb +4 -0
  19. data/config/routes.rb +3 -0
  20. data/lib/caravaggio/canvas.rb +35 -0
  21. data/lib/caravaggio/engine.rb +5 -0
  22. data/lib/caravaggio/figure.rb +104 -0
  23. data/lib/caravaggio/version.rb +3 -0
  24. data/lib/caravaggio.rb +38 -0
  25. data/lib/tasks/caravaggio_tasks.rake +4 -0
  26. data/test/caravaggio_test.rb +7 -0
  27. data/test/dummy/README.rdoc +28 -0
  28. data/test/dummy/Rakefile +6 -0
  29. data/test/dummy/app/assets/javascripts/application.js +13 -0
  30. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  31. data/test/dummy/app/controllers/application_controller.rb +5 -0
  32. data/test/dummy/app/helpers/application_helper.rb +2 -0
  33. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  34. data/test/dummy/bin/bundle +3 -0
  35. data/test/dummy/bin/rails +4 -0
  36. data/test/dummy/bin/rake +4 -0
  37. data/test/dummy/bin/setup +29 -0
  38. data/test/dummy/config/application.rb +26 -0
  39. data/test/dummy/config/boot.rb +5 -0
  40. data/test/dummy/config/database.yml +25 -0
  41. data/test/dummy/config/environment.rb +5 -0
  42. data/test/dummy/config/environments/development.rb +41 -0
  43. data/test/dummy/config/environments/production.rb +79 -0
  44. data/test/dummy/config/environments/test.rb +42 -0
  45. data/test/dummy/config/initializers/assets.rb +11 -0
  46. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  47. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  48. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  49. data/test/dummy/config/initializers/inflections.rb +16 -0
  50. data/test/dummy/config/initializers/mime_types.rb +4 -0
  51. data/test/dummy/config/initializers/session_store.rb +3 -0
  52. data/test/dummy/config/initializers/to_time_preserves_timezone.rb +10 -0
  53. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  54. data/test/dummy/config/locales/en.yml +23 -0
  55. data/test/dummy/config/routes.rb +4 -0
  56. data/test/dummy/config/secrets.yml +22 -0
  57. data/test/dummy/config.ru +4 -0
  58. data/test/dummy/public/404.html +67 -0
  59. data/test/dummy/public/422.html +67 -0
  60. data/test/dummy/public/500.html +66 -0
  61. data/test/dummy/public/favicon.ico +0 -0
  62. data/test/integration/navigation_test.rb +8 -0
  63. data/test/test_helper.rb +21 -0
  64. data/vendor/assets/javascripts/caravaggio_d3/API.md +1434 -0
  65. data/vendor/assets/javascripts/caravaggio_d3/CHANGES.md +1643 -0
  66. data/vendor/assets/javascripts/caravaggio_d3/LICENSE +27 -0
  67. data/vendor/assets/javascripts/caravaggio_d3/README.caravaggio.txt +1 -0
  68. data/vendor/assets/javascripts/caravaggio_d3/README.md +57 -0
  69. data/vendor/assets/javascripts/caravaggio_d3/caravaggio_d3.js +19651 -0
  70. data/vendor/assets/javascripts/caravaggio_d3/caravaggio_d3.min.js +2 -0
  71. metadata +151 -0
@@ -0,0 +1,5 @@
1
+ module Caravaggio
2
+ class ApplicationController < ActionController::Base
3
+ protect_from_forgery with: :exception
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ class CanvasController < Caravaggio::ApplicationController
2
+ end
@@ -0,0 +1,14 @@
1
+ require 'caravaggio/canvas'
2
+
3
+ module Caravaggio
4
+ class CanvasController < Caravaggio::ApplicationController
5
+ protect_from_forgery with: :exception
6
+
7
+ def index
8
+ @canvas = Canvas.new
9
+ @models = @canvas.models
10
+ @figures = @canvas.figures
11
+ @associations = @canvas.associations
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ module Caravaggio
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,13 @@
1
+ <div class="association hidden detailed-content" id="association-<%= association[:index] %>" >
2
+ <h2><%= association[:source] %>#<%= association[:name] %></h2>
3
+
4
+ <ul>
5
+ <li><%= association[:name] %></li>
6
+ <li><%= association[:association_type] %></li>
7
+ <li>From: <%= association[:source] %></li>
8
+ <li>To: <%= association[:target] %></li>
9
+ <% association[:options].each do |option, value| %>
10
+ <li><%= option %> : <%= value %></li>
11
+ <% end %>
12
+ </ul>
13
+ </div>
@@ -0,0 +1,24 @@
1
+ <div class="figure hidden detailed-content" id="figure-<%= figure.friendly_name %>" >
2
+ <h2><%= figure.name %></h2>
3
+ <h3>Columns</h3>
4
+ <ul>
5
+ <% figure.columns.each do |column| %>
6
+ <li><%= column.name %> : <%= column.sql_type %></li>
7
+ <% end %>
8
+ </ul>
9
+ <h3>Associations</h3>
10
+ <ul>
11
+ <% figure.associations.each do |association| %>
12
+ <li>
13
+ <%= association[:association_type] %> <%= association[:name] %>
14
+ <% if association[:options].present? %>
15
+ <ul>
16
+ <% association[:options].each do |option, value| %>
17
+ <li><%= option %> : <%= value %></li>
18
+ <% end %>
19
+ </ul>
20
+ <% end %>
21
+ </li>
22
+ <% end %>
23
+ </ul>
24
+ </div>
@@ -0,0 +1,54 @@
1
+ <script>
2
+ var models = <%= @models.to_json.html_safe %>;
3
+ var associations = <%= @associations.to_json.html_safe %>;
4
+ </script>
5
+
6
+ <div id="header">
7
+ <h1>Welcome to Caravaggio!</h1>
8
+ </div>
9
+
10
+ <div id="container">
11
+ <div class="sidebar left-sidebar">
12
+ <div class="color-key">
13
+ <h3>Key</h3>
14
+ <ul>
15
+ <li><span class="belongs_to">Belongs To</span></li>
16
+ <li><span class="has_many">Has Many</span></li>
17
+ <li><span class="has_one">Has One</span></li>
18
+ <li><span class="has_and_belongs_to_many">HABTM</span></li>
19
+ <li><input type="checkbox" name="links_from" id="links_from" onclick="displayModels()" checked/>Links from</li>
20
+ <li><input type="checkbox" name="links_to" id="links_to" onclick="displayModels()" />Links to</li>
21
+ <li><input type="checkbox" name="only_checked" id="only_checked" onclick="displayModels()" />Checked only</li>
22
+ </ul>
23
+ </div>
24
+ <div>
25
+ <h3>Classes</h3>
26
+ <ul>
27
+ <li><%= check_box_tag "ALL", "ALL", false, class: "all-model-checkbox", onclick: "setAllModels(this)" %>ALL</li>
28
+ <% @models.each do |model| %>
29
+ <li><%= check_box_tag model[:friendly_name], model[:friendly_name], false, data: {class_name: model[:id]}, class: "model-checkbox", onclick: "setModel(this)" %> <%= model[:short_name] %></li>
30
+ <% end %>
31
+ </ul>
32
+ </div>
33
+ </div>
34
+ <div id="canvas">
35
+ <svg id="model-diagram" width="1000" height="1000" />
36
+ </div>
37
+ <div id="exhibit" class="sidebar right-sidebar">
38
+ <div class="detailed-content">
39
+ <h2>Instructions</h2>
40
+ <p>
41
+ Select a class from the left sidebar.
42
+ </p>
43
+ <p>
44
+ Click on a link or a dot to get more detail.
45
+ </p>
46
+ </div>
47
+ <% @figures.each do |figure| %>
48
+ <%= render "figure", figure: figure %>
49
+ <% end %>
50
+ <% @associations.each do |association| %>
51
+ <%= render "association", association: association %>
52
+ <% end %>
53
+ </div>
54
+ </div>
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Caravaggio</title>
5
+ <%= stylesheet_link_tag "caravaggio/application", media: "all" %>
6
+ <%= javascript_include_tag "caravaggio/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body onload="initializeCaravaggio()">
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
File without changes
@@ -0,0 +1,4 @@
1
+ Rails.application.config.assets.precompile += %w[
2
+ caravaggio/application.css
3
+ caravaggio/application.js
4
+ ]
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Caravaggio::Engine.routes.draw do
2
+ root to: "canvas#index"
3
+ end
@@ -0,0 +1,35 @@
1
+ require "caravaggio/figure"
2
+
3
+ # A Canvas is an entire picture of a system. It contains many figures. (Get it?)
4
+ module Caravaggio
5
+ class Canvas
6
+ attr_reader :figures
7
+
8
+ def initialize
9
+ Rails.application.eager_load!
10
+ @figures = []
11
+ ActiveRecord::Base.descendants.reject{|c| c.abstract_class}.sort{|s1, s2| s1.name <=> s2.name}.each do |model|
12
+ begin
13
+ figure = Figure.new(model)
14
+ @figures << figure
15
+ rescue => e
16
+ Rails.logger.error "Failed to load #{model.name} with error #{e}"
17
+ end
18
+ end
19
+ end
20
+
21
+ def models
22
+ @figures.map{|figure| figure.model}
23
+ end
24
+
25
+ def associations
26
+ if @associations.nil?
27
+ @associations = @figures.map{|figure| figure.associations}.flatten
28
+ @associations.each_with_index do |association, index|
29
+ association[:index] = index
30
+ end
31
+ end
32
+ @associations
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ module Caravaggio
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Caravaggio
4
+ end
5
+ end
@@ -0,0 +1,104 @@
1
+ # a Figure is a representation of a model. (Get it?)
2
+ module Caravaggio
3
+ class Figure
4
+ def initialize(source_model)
5
+ @source_model = source_model
6
+
7
+ # make sure the model works - it won/t for some weird situations involving not loading all gems as expected.
8
+ model
9
+ end
10
+
11
+ def name
12
+ @name ||= clean_name(@source_model.name)
13
+ end
14
+
15
+ def columns
16
+ @columns ||= @source_model.columns.sort{|c1, c2| c1.name <=> c2.name}
17
+ end
18
+
19
+ def friendly_name
20
+ @friendly_name ||= name.gsub(/::/, '_')
21
+ end
22
+
23
+ def short_name
24
+ @short_name ||= (name.length > 20 ? "..." : "") + name.chars.last(20).join
25
+ end
26
+
27
+ def to_s
28
+ "Figure for #{@source_model.name}"
29
+ end
30
+
31
+ def to_json
32
+ @to_json ||= to_h.to_json
33
+ end
34
+
35
+ def model
36
+ @source_model_hash ||= {
37
+ id: name,
38
+ friendly_name: friendly_name,
39
+ short_name: short_name,
40
+ columns: @source_model.column_names.sort,
41
+ to_associated_classes: associations.map{|association| association[:target]}
42
+ }
43
+ end
44
+
45
+ def associations(association_type = nil)
46
+ if association_type
47
+ associations.select{ |association| association[:association_type] == association_type }
48
+ else
49
+ @associations ||= (associations_to_h(:belongs_to, @source_model.reflect_on_all_associations(:belongs_to)) +
50
+ associations_to_h(:has_one, @source_model.reflect_on_all_associations(:has_one)) +
51
+ associations_to_h(:has_many, @source_model.reflect_on_all_associations(:has_many)) +
52
+ associations_to_h(:has_and_belongs_to_many, @source_model.reflect_on_all_associations(:has_and_belongs_to_many))).flatten
53
+ end
54
+ end
55
+
56
+ def associations_to_h(association_type, associations)
57
+ associations.map{|association| association_to_h(association_type, association)}
58
+ end
59
+
60
+ def to_models
61
+ @to_models ||= associations.map{|association| association[:to]}.sort.uniq
62
+ end
63
+
64
+ def association_to_h(association_type, association)
65
+ options = {}
66
+ association.options.each do |k, v|
67
+ options[k.to_s] = v.to_s
68
+ end
69
+
70
+ {
71
+ source: name,
72
+ target: to_class_name(association),
73
+ name: association.macro == :belongs_to || association.macro == :has_one ? association.name : association.plural_name,
74
+ options: options,
75
+ association_type: association_type
76
+ }
77
+ end
78
+
79
+ def clean_name(_name)
80
+ _name.gsub(/^::/, "")
81
+ end
82
+
83
+ def self.add_backlinks(figures)
84
+ figures.each do |from_figure|
85
+ from_figures[:from] = []
86
+ figures.each do |to_figure|
87
+ if to_figure.name != from_figure.name && to_figure.to_models.include?(from_figure.name)
88
+ from_figures[:from] << to_figure.name
89
+ end
90
+ from_figures[:from].sort!
91
+ end
92
+ end
93
+ end
94
+
95
+ def to_class_name(association)
96
+ class_name = association.options[:class_name] || association.name.to_s.singularize.camelize
97
+ begin
98
+ class_name = association.klass.name
99
+ rescue => e
100
+ end
101
+ clean_name(class_name)
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,3 @@
1
+ module Caravaggio
2
+ VERSION = "0.0.12"
3
+ end
data/lib/caravaggio.rb ADDED
@@ -0,0 +1,38 @@
1
+ require "caravaggio/engine"
2
+
3
+ module Caravaggio
4
+ def self.gem_path
5
+ Pathname(File.realpath(__FILE__)).join('../..')
6
+ end
7
+
8
+ def self.load_paths
9
+ gem_path.join('app/assets').each_child.to_a
10
+ end
11
+
12
+ def self.dependencies
13
+ [
14
+
15
+ ]
16
+ end
17
+
18
+ if defined?(Rails)
19
+ class Engine < ::Rails::Engine
20
+ # Rails -> use app/assets directory.
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ class RailsAssets
27
+ @components ||= []
28
+
29
+ class << self
30
+ attr_accessor :components
31
+
32
+ def load_paths
33
+ components.flat_map(&:load_paths)
34
+ end
35
+ end
36
+ end
37
+
38
+ RailsAssets.components << Caravaggio
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :caravaggio do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class CaravaggioTest < ActiveSupport::TestCase
4
+ test "truth" do
5
+ assert_kind_of Module, Caravaggio
6
+ end
7
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end