drawerd 0.1.0

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
+ SHA256:
3
+ metadata.gz: ae10c530c0301c3a0d363cb7bb877f0d0e98bbd9c4f2defee45392cce5287040
4
+ data.tar.gz: '08b07ecc0ccf861a8582a76cabd619541c3ed0a3b91f99ff6b90d3a361fc4ec7'
5
+ SHA512:
6
+ metadata.gz: 0cfde721641347a5f1a07769a3e67911ecc9ebea944298d6d969eff42a3c5e302354100ca926ef8e434d0a603603d6edc0eb97c0b8baf20427342cdcfcb82450
7
+ data.tar.gz: ee91480ba75b1cd680ae86f72c861c58f349067e35caa2604deba05ce9b322f3edd2bfd780edef957ee3591a789a471224805080e6134aa05ed7a45672e0b39f
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.5
7
+ before_install: gem install bundler -v 1.17.2
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in drawerd.gemspec
8
+ gemspec
9
+ gem 'rubocop', require: false
data/Gemfile.lock ADDED
@@ -0,0 +1,67 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ drawerd (0.1.0)
5
+ rails-erd (~> 1.6)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (6.0.2.2)
11
+ activesupport (= 6.0.2.2)
12
+ activerecord (6.0.2.2)
13
+ activemodel (= 6.0.2.2)
14
+ activesupport (= 6.0.2.2)
15
+ activesupport (6.0.2.2)
16
+ concurrent-ruby (~> 1.0, >= 1.0.2)
17
+ i18n (>= 0.7, < 2)
18
+ minitest (~> 5.1)
19
+ tzinfo (~> 1.1)
20
+ zeitwerk (~> 2.2)
21
+ ast (2.4.0)
22
+ choice (0.2.0)
23
+ concurrent-ruby (1.1.6)
24
+ i18n (1.8.2)
25
+ concurrent-ruby (~> 1.0)
26
+ jaro_winkler (1.5.4)
27
+ minitest (5.14.0)
28
+ parallel (1.19.1)
29
+ parser (2.7.1.1)
30
+ ast (~> 2.4.0)
31
+ rails-erd (1.6.0)
32
+ activerecord (>= 4.2)
33
+ activesupport (>= 4.2)
34
+ choice (~> 0.2.0)
35
+ ruby-graphviz (~> 1.2)
36
+ rainbow (3.0.0)
37
+ rake (12.3.3)
38
+ rexml (3.2.4)
39
+ rubocop (0.82.0)
40
+ jaro_winkler (~> 1.5.1)
41
+ parallel (~> 1.10)
42
+ parser (>= 2.7.0.1)
43
+ rainbow (>= 2.2.2, < 4.0)
44
+ rexml
45
+ ruby-progressbar (~> 1.7)
46
+ unicode-display_width (>= 1.4.0, < 2.0)
47
+ ruby-graphviz (1.2.5)
48
+ rexml
49
+ ruby-progressbar (1.10.1)
50
+ thread_safe (0.3.6)
51
+ tzinfo (1.2.7)
52
+ thread_safe (~> 0.1)
53
+ unicode-display_width (1.7.0)
54
+ zeitwerk (2.3.0)
55
+
56
+ PLATFORMS
57
+ ruby
58
+
59
+ DEPENDENCIES
60
+ bundler (~> 1.17)
61
+ drawerd!
62
+ minitest (~> 5.0)
63
+ rake (~> 12.3.3)
64
+ rubocop
65
+
66
+ BUNDLED WITH
67
+ 1.17.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Hooopo Wang
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,39 @@
1
+ # Drawerd
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/drawerd`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'drawerd'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install drawerd
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 test` 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/[USERNAME]/drawerd.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'drawerd'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
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
data/drawerd.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'drawerd/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'drawerd'
9
+ spec.version = Drawerd::VERSION
10
+ spec.authors = ['Hooopo Wang']
11
+ spec.email = ['hoooopo@gmail.com']
12
+
13
+ spec.summary = 'generate erd for rails, using rails-erd and drawerd.com service.'
14
+ spec.description = 'Generate erd for rails, usiing rails-erd and drawerd.com service.'
15
+ spec.homepage = 'https://help.drawerd.com'
16
+ spec.license = 'MIT'
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = 'exe'
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ['lib']
26
+ spec.add_runtime_dependency 'rails-erd', '~> 1.6'
27
+ spec.add_development_dependency 'bundler', '~> 1.17'
28
+ spec.add_development_dependency 'minitest', '~> 5.0'
29
+ spec.add_development_dependency 'rake', '~> 12.3.3'
30
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Drawerd::Railtie < Rails::Railtie
4
+ rake_tasks do
5
+ Drawerd.load_tasks
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Drawerd
4
+ VERSION = '0.1.0'
5
+ end
data/lib/drawerd.rb ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'drawerd/version'
4
+ require 'drawerd/railtie' if defined?(Rails)
5
+
6
+ module Drawerd
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+ class << self
10
+ def loaded_tasks=(val); @loaded_tasks = val; end
11
+ def loaded_tasks; return @loaded_tasks; end
12
+
13
+ def load_tasks
14
+ return if(self.loaded_tasks)
15
+ self.loaded_tasks = true
16
+
17
+ Dir[File.join(File.dirname(__FILE__), 'tasks', '**/*.rake')].each { |rake| load rake }
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,4 @@
1
+ Add a .rake file that automatically generate the graphical models when you do
2
+ a db:migrate in development mode:
3
+
4
+ rails generate drawerd:install
@@ -0,0 +1,14 @@
1
+ module Drawerd
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ desc "Copy drawerd rakefiles for automatic graphic generation"
5
+ source_root File.expand_path('../templates', __FILE__)
6
+
7
+ # copy rake tasks
8
+ def copy_tasks
9
+ template "auto_drawerd.rake", "lib/tasks/auto_drawerd.rake"
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,6 @@
1
+ # NOTE: only doing this in development as some production environments (Heroku)
2
+ # NOTE: are sensitive to local FS writes, and besides -- it's just not proper
3
+ # NOTE: to have a dev-mode tool do its thing in production.
4
+ if Rails.env.development?
5
+ Drawerd.load_tasks
6
+ end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ load 'rails_erd/tasks.rake'
4
+ require 'rails_erd/diagram'
5
+ require 'csv'
6
+
7
+ class TableSchema
8
+ def initialize(table_name)
9
+ if table_name.split('.').size > 1
10
+ @schema, @name = table_name.split('.')
11
+ else
12
+ @schema = 'public'
13
+ @name = table_name
14
+ end
15
+ end
16
+
17
+ attr_reader :name
18
+
19
+ attr_reader :schema
20
+ end
21
+
22
+ class DrawerdDiagram < RailsERD::Diagram
23
+ setup do
24
+ @table_csv = CSV.open(Rails.root.join('tables.csv'), 'wb')
25
+ @relation_csv = CSV.open(Rails.root.join('relations.csv'), 'wb')
26
+ @table_csv << %w[table_schema table_name column_name column_type ordinal_position is_nullable primary_key column_comment table_comment]
27
+ @relation_csv << %w[schema table column no relation_table_schema relation_table relation_column constraint_name relation_type]
28
+ end
29
+
30
+ each_entity do |entity|
31
+ model = entity.model
32
+ table_name = model.table_name
33
+ table_comment = ActiveRecord::Base.connection.table_comment(table_name)
34
+ table_name = TableSchema.new(table_name).name
35
+ table_schema = TableSchema.new(table_name).schema
36
+
37
+ entity.attributes.each_with_index do |attribute, ordinal_position|
38
+ column = attribute.column
39
+ column_name = column.name
40
+ column_type = column.type
41
+ is_nullable = column.null ? 't' : 'f'
42
+ primary_key = model.primary_key == column.name ? 't' : 'f'
43
+ column_comment = column.comment
44
+ @table_csv << [table_schema, table_name, column_name, column_type, ordinal_position, is_nullable, primary_key, column_comment, table_comment]
45
+ end
46
+ end
47
+ # Invoked once for each relationship
48
+ each_relationship do |relationship|
49
+ source = relationship.source
50
+ des = relationship.destination
51
+ source_model = source.model
52
+ des_model = des.model
53
+ source_table_name = source_model.table_name
54
+ des_table_name = des_model.table_name
55
+
56
+ schema = TableSchema.new(source_table_name).schema
57
+ table = TableSchema.new(source_table_name).name
58
+
59
+ relation_table_schema = TableSchema.new(des_table_name).schema
60
+ relation_table = TableSchema.new(des_table_name).name
61
+
62
+ # TODO: fix mutual association
63
+ unless relationship.indirect?
64
+ if ass = relationship.associations.find { |x| x.macro == :has_many || x.macro == :has_one }
65
+ column = ass.association_primary_key
66
+ relation_column = ass.foreign_key
67
+ end
68
+
69
+ if relationship.many_to? && !relationship.many_to_many?
70
+ if ass = relationship.associations.find { |x| x.macro == :belongs_to }
71
+ column = ass.association_primary_key
72
+ relation_column = ass.foreign_key
73
+ end
74
+ schema, table, column, relation_table_schema, relation_table, relation_colunm = relation_table_schema, relation_table, relation_colunm, schema, table, column
75
+ end
76
+ end
77
+
78
+ relation_type = if relationship.one_to_one? then 'one'
79
+ elsif relationship.indirect? && relationship.one_to_many? then 'm2m'
80
+ elsif relationship.one_to_many? then 'many'
81
+ elsif relationship.many_to_many? then 'm2m'
82
+ end
83
+
84
+ @relation_csv << [schema, table, column, 1, relation_table_schema, relation_table, relation_column, nil, relation_type]
85
+ end
86
+
87
+ # Should save or return the generated diagram
88
+ save do
89
+ @table_csv.close
90
+ @relation_csv.close
91
+ end
92
+ end
93
+
94
+ namespace :drawerd do
95
+ desc "generate CSV file for drawerd services"
96
+ task generate: ['erd:check_dependencies', 'erd:options', 'erd:load_models'] do
97
+ say "Generating CSV for #{ActiveRecord::Base.descendants.length} models..."
98
+
99
+ DrawerdDiagram.create
100
+
101
+ say "Done! Saved tables.csv to #{Rails.root.join('tables.csv')}."
102
+ say "Done! Saved relations.csv to #{Rails.root.join('relations.csv')}."
103
+ end
104
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: drawerd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hooopo Wang
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails-erd
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 12.3.3
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 12.3.3
69
+ description: Generate erd for rails, usiing rails-erd and drawerd.com service.
70
+ email:
71
+ - hoooopo@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - drawerd.gemspec
86
+ - lib/drawerd.rb
87
+ - lib/drawerd/railtie.rb
88
+ - lib/drawerd/version.rb
89
+ - lib/generators/drawerd/USAGE
90
+ - lib/generators/drawerd/install_generator.rb
91
+ - lib/generators/drawerd/templates/auto_drawerd.rake
92
+ - lib/tasks/drawerd.rake
93
+ homepage: https://help.drawerd.com
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubygems_version: 3.0.3
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: generate erd for rails, using rails-erd and drawerd.com service.
116
+ test_files: []