rails-erd-d3 0.0.2

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 18d87dbe5a94992c7bdbc1aaff152a22c733cc65
4
+ data.tar.gz: bd184dadedc6008f1172f03cb81500c18cb7fcfa
5
+ SHA512:
6
+ metadata.gz: 1198f5452ab064ae1ff160d48c3832b4d4d6b76e8abb4eda3cc495b8f44229187e5ca9b9fb544927adf559f88b2748353f34dce8252d331e2fbfaafeba1b522f
7
+ data.tar.gz: fa727ad09550b0ee7775e5761b4503f7f57259e74b6b64f0588e4aadd9af52a609e7cd596f5a737f153d3b682c57c4888413a3abb02a69838047f6bb37422e3b
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails-erd-d3.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Roman Krasavtsev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Rails-ERD-D3
2
+
3
+ Gem is under active development.
4
+
5
+ Create entity–relationship diagram with D3.js for your Rails application.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rails-erd-d3'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rails-erd-d3
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/RomanKrasavtsev/rails-erd-d3.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rails/erd/d3"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,233 @@
1
+ require 'json'
2
+
3
+ class RailsErdD3
4
+ def self.get_rails_version
5
+ Rails::VERSION::MAJOR
6
+ end
7
+
8
+ def self.get_all_models
9
+ version = get_rails_version
10
+
11
+ case get_rails_version
12
+ when 4
13
+ klass = ActiveRecord::Base
14
+ when 5
15
+ klass = ApplicationRecord
16
+ end
17
+
18
+ Rails.application.eager_load!
19
+ @@models = ObjectSpace.each_object(Class).select { |o| o.superclass == klass } || []
20
+ end
21
+
22
+ def self.get_data
23
+ get_all_models
24
+
25
+ nodes = []
26
+ links = []
27
+ models_list = {}
28
+
29
+ @@models.each_with_index do |model, index|
30
+ models_list[model.name] = index
31
+ end
32
+
33
+ @@models.each_with_index do |model, index|
34
+ name = model.name.capitalize
35
+
36
+ nodes << { label: name, r: 30 }
37
+ model.reflections.keys.each do |key|
38
+ links << { source: models_list[name], target: models_list[key.capitalize] }
39
+ end
40
+ end
41
+
42
+ data = { nodes: nodes, links: links }
43
+ data.to_json
44
+ end
45
+
46
+ def self.create_erd
47
+ file = File.new("erd.html", "w")
48
+ file.puts("
49
+ <!DOCTYPE HTML>
50
+ <html>
51
+ #{get_head}
52
+ <body>
53
+ #{get_nav}
54
+ <div id='erd'>
55
+ </div>
56
+ #{get_d3}
57
+ #{get_modals}
58
+ </body>
59
+ </html>
60
+ ")
61
+ file.close
62
+ end
63
+
64
+ private
65
+
66
+ def self.get_head
67
+ "<head>
68
+ <title>ERD</title>
69
+ <meta charset='utf-8'>
70
+ <script src='https://code.jquery.com/jquery-3.1.1.min.js' integrity='sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=' crossorigin='anonymous'></script>
71
+ <script src='https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js'></script>
72
+ <script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js' integrity='sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa' crossorigin='anonymous'></script>
73
+ <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' integrity='sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u' crossorigin='anonymous'>
74
+ </head>"
75
+ end
76
+
77
+ def self.get_nav
78
+ "<nav class='navbar navbar-default'>
79
+ <div class='container-fluid'>
80
+ <div class='navbar-header'>
81
+ <div class='navbar-brand'>
82
+ <a href='https://github.com/RomanKrasavtsev/rails-erd-d3'>
83
+ Rails-ERD-D3
84
+ </a>
85
+ </div>
86
+ </div>
87
+ </div>
88
+ </nav>"
89
+ end
90
+
91
+ def self.get_d3
92
+ "<script>
93
+ var data = #{get_data};
94
+
95
+ var width = window.innerWidth
96
+ || document.documentElement.clientWidth
97
+ || document.body.clientWidth;
98
+ var height = window.innerHeight
99
+ || document.documentElement.clientHeight
100
+ || document.body.clientHeight;
101
+
102
+ var colorScale = d3.scaleOrdinal(d3.schemeCategory20);
103
+
104
+ var svg = d3.select('#erd').append('svg')
105
+ .attr('height', height)
106
+ .attr('width', width)
107
+ .style('background', 'white');
108
+
109
+ var simulation = d3.forceSimulation()
110
+ .force('link', d3.forceLink().id(function(d) { return d.index }))
111
+ .force('collide',d3.forceCollide( function(d){return d.r + 20 }).iterations(30) )
112
+ .force('charge', d3.forceManyBody())
113
+ .force('center', d3.forceCenter(width / 2, height / 2))
114
+ .force('y', d3.forceY(0))
115
+ .force('x', d3.forceX(0));
116
+
117
+ var link = svg.append('g')
118
+ .classed('links', true)
119
+ .selectAll('line')
120
+ .data(data.links)
121
+ .enter()
122
+ .append('line')
123
+ .attr('stroke', 'black');
124
+
125
+ var node = svg.selectAll('.node')
126
+ .data(data.nodes)
127
+ .enter()
128
+ .append('g')
129
+ .classed('node', true)
130
+ .style('cursor', 'pointer')
131
+ .attr('data-toggle', 'modal')
132
+ .attr('data-target', function(d, i){ return '#' + data.nodes[i].label })
133
+ .call(d3.drag()
134
+ .on('start', dragstarted)
135
+ .on('drag', dragged)
136
+ .on('end', dragended));
137
+
138
+ node.append('circle')
139
+ .classed('circle', true)
140
+ .attr('r', function(d){ return d.r })
141
+ .attr('fill', function(d, i){ return colorScale(i) });
142
+
143
+ node.append('text')
144
+ .classed('text', true)
145
+ .text(function(d) {
146
+ return d.label;
147
+ });
148
+
149
+ var ticked = function() {
150
+ link
151
+ .attr('x1', function(d) { return d.source.x; })
152
+ .attr('y1', function(d) { return d.source.y; })
153
+ .attr('x2', function(d) { return d.target.x; })
154
+ .attr('y2', function(d) { return d.target.y; });
155
+
156
+ node
157
+ .attr('transform', function(d) {
158
+ return 'translate(' + [d.x, d.y] + ')';
159
+ });
160
+ }
161
+
162
+ simulation
163
+ .nodes(data.nodes)
164
+ .on('tick', ticked);
165
+
166
+ simulation.force('link')
167
+ .links(data.links);
168
+
169
+ function dragstarted(d) {
170
+ if (!d3.event.active) simulation.alphaTarget(0.3).restart();
171
+ d.fx = d.x;
172
+ d.fy = d.y;
173
+ }
174
+
175
+ function dragged(d) {
176
+ d.fx = d3.event.x;
177
+ d.fy = d3.event.y;
178
+ }
179
+
180
+ function dragended(d) {
181
+ if (!d3.event.active) simulation.alphaTarget(0);
182
+ d.fx = null;
183
+ d.fy = null;
184
+ }
185
+ </script>"
186
+ end
187
+
188
+ def self.get_modals
189
+ modals = ""
190
+ @@models.each do |model|
191
+ modals += "<div class='modal fade' id='#{model.name.capitalize}' tabindex='-1' role='dialog'>
192
+ <div class='modal-dialog' role='document'>
193
+ <div class='modal-content'>
194
+ <div class='modal-header'>
195
+ <button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>&times;</span></button>
196
+ <h4 class='modal-title'>#{model.name.capitalize}</h4>
197
+ </div>
198
+ <div class='modal-body'>
199
+ <table class='table table-hover'>
200
+ <thead>
201
+ <tr>
202
+ <th>#</th>
203
+ <th>name</th>
204
+ <th>macro</th>
205
+ <th>foreign_key</th>
206
+ </tr>
207
+ </thead>
208
+ <tbody>"
209
+
210
+ model.reflections.each_with_index do |r, index|
211
+ name = r[0]
212
+ modals += "<tr>"\
213
+ "<th>#{index + 1}</th>"\
214
+ "<td>#{name.capitalize}</td>"\
215
+ "<td>#{model.reflections[name].macro}</td>"\
216
+ "<td>#{model.reflections[name].foreign_key}</td>"\
217
+ "</tr>"
218
+ end
219
+
220
+ modals += "</tbody>
221
+ </table>
222
+ </div>
223
+ <div class='modal-footer'>
224
+ <button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
225
+ </div>
226
+ </div>
227
+ </div>
228
+ </div>"
229
+ end
230
+
231
+ modals
232
+ end
233
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rails-erd-d3"
7
+ spec.authors = ["Roman Krasavtsev"]
8
+ spec.email = ["mr.krasavtsev@gmail.com"]
9
+ spec.version = "0.0.2"
10
+ spec.summary = "Entity–relationship diagram with D3.js for Rails application"
11
+ spec.description = "This gem creates entity–relationship diagram with D3.js for your Rails application"
12
+ spec.homepage = "https://github.com/RomanKrasavtsev/rails-erd-d3"
13
+ spec.license = "MIT"
14
+
15
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+ # if spec.respond_to?(:metadata)
18
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
19
+ # else
20
+ # raise "RubyGems 2.0 or newer is required to protect against " \
21
+ # "public gem pushes."
22
+ # end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
25
+ f.match(%r{^(test|spec|features)/})
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.13"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-erd-d3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Roman Krasavtsev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: This gem creates entity–relationship diagram with D3.js for your Rails
56
+ application
57
+ email:
58
+ - mr.krasavtsev@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/rails_erd_d3.rb
73
+ - rails-erd-d3.gemspec
74
+ homepage: https://github.com/RomanKrasavtsev/rails-erd-d3
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.6.6
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Entity–relationship diagram with D3.js for Rails application
98
+ test_files: []