rails-erd 1.6.1 → 1.7.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 16bfef1b79087e3017df7723bef8cf286df5a812
4
- data.tar.gz: 7162f90b6dce224a0ec89693e7d307fbc15a9b45
2
+ SHA256:
3
+ metadata.gz: 8b58372ac1652af1b21bd00ea88b434bb2d303247eca241f57efd84210bc9367
4
+ data.tar.gz: aecdcd4cfdb25ed3ef1713da019b456512d2b7bc965ebe0d7acea86e56b4f586
5
5
  SHA512:
6
- metadata.gz: 34e2582512bfe6313f626d4de969eebc328415c97456b1cd151df3e19d75e55cc0b65a9ded7ba0c363d3708072d001266d96657e2a8669ee6a7c4ca7b0cc3ee0
7
- data.tar.gz: 16ddf4ba4c7d640a4c139905fe6f238f76c70cf20712c467ebb9bf9075f0ee6603c0c532114b86bd0e66f7d275f5ae0200802e91c8978a469c6e39208dd3c709
6
+ metadata.gz: 1d8c7c596bf41d7753c0c34a468c142021ed66e7b7168dab70f68e57cf587640b5d5c35693b14c4a4c7778cf55b30f5740393454773901720f29c71f16e91c73
7
+ data.tar.gz: 733fe6e3c637a1f31f750621af09b9803102663b8d5cb611f978c600bc0940856e491a853325f57b4c126cf6e1ec50d55a4d0425ed573f1b722f144333ff4cf9
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  Rails ERD - Generate Entity-Relationship Diagrams for Rails applications
2
2
  ========================================================================
3
- [![Build Status](https://travis-ci.org/voormedia/rails-erd.svg?branch=master)](https://travis-ci.org/voormedia/rails-erd) [![Code Climate](https://codeclimate.com/github/voormedia/rails-erd/badges/gpa.svg)](https://codeclimate.com/github/voormedia/rails-erd)
3
+ [![Tests](https://github.com/voormedia/rails-erd/actions/workflows/test.yml/badge.svg)](https://github.com/voormedia/rails-erd/actions/workflows/test.yml) [![Code Climate](https://codeclimate.com/github/voormedia/rails-erd/badges/gpa.svg)](https://codeclimate.com/github/voormedia/rails-erd)
4
4
 
5
5
  [Rails ERD](https://voormedia.github.io/rails-erd/) is a gem that allows you to easily generate a diagram based on your application's Active Record models. The diagram gives an overview of how your models are related. Having a diagram that describes your models is perfect documentation for your application.
6
6
 
@@ -32,6 +32,8 @@ See the [installation instructions](https://voormedia.github.io/rails-erd/instal
32
32
 
33
33
  * Install Graphviz 2.22+ ([how?](https://voormedia.github.io/rails-erd/install.html)). On macOS with Homebrew run `brew install graphviz`.
34
34
 
35
+ * on linux - `sudo apt-get install graphviz`
36
+
35
37
  * Add <tt>gem 'rails-erd', group: :development</tt> to your application's Gemfile
36
38
 
37
39
  * Run <tt>bundle exec erd</tt>
@@ -44,7 +46,7 @@ Rails ERD has the ability to be configured via the command line or through the u
44
46
  ```yaml
45
47
  attributes:
46
48
  - content
47
- - foreign_key
49
+ - foreign_keys
48
50
  - inheritance
49
51
  disconnected: true
50
52
  filename: erd
@@ -64,6 +66,10 @@ only_recursion_depth: null
64
66
  prepend_primary: false
65
67
  cluster: false
66
68
  splines: spline
69
+ fonts:
70
+ normal: "Arial"
71
+ bold: "Arial Bold"
72
+ italic: "Arial Italic"
67
73
  ```
68
74
 
69
75
  Auto generation
data/lib/rails_erd/cli.rb CHANGED
@@ -174,17 +174,25 @@ module RailsERD
174
174
 
175
175
  def load_application
176
176
  $stderr.puts "Loading application in '#{File.basename(path)}'..."
177
- begin
178
- environment_path = "#{path}/config/environment.rb"
179
- require environment_path
180
- rescue ::LoadError
181
- puts "Please create a file in '#{environment_path}' that loads your application environment."
182
- raise
183
- end
177
+ environment_path = "#{path}/config/environment.rb"
178
+ require environment_path
179
+
184
180
  if defined? Rails
185
181
  Rails.application.eager_load!
186
182
  Rails.application.config.eager_load_namespaces.each(&:eager_load!) if Rails.application.config.respond_to?(:eager_load_namespaces)
187
183
  end
184
+ rescue ::LoadError
185
+ error_message = <<~EOS
186
+ Tried to load your application environment from '#{environment_path}' but the file was not present.
187
+ This means that your models might not get loaded fully when the diagram gets built. This can
188
+ make your entity diagram incomplete.
189
+
190
+ However, if you are using ActiveRecord without Rails just make sure your models get
191
+ loaded eagerly before we generate the ERD (for example, explicitly require your application
192
+ bootstrap file before calling rails-erd from your Rakefile). We will continue without loading the environment file,
193
+ and recommend you check your diagram for missing models after it gets generated.
194
+ EOS
195
+ puts error_message
188
196
  rescue TypeError
189
197
  end
190
198
 
@@ -81,6 +81,12 @@ module RailsERD
81
81
  when :title
82
82
  value.is_a?(String) ? value : !!value
83
83
 
84
+ # nil | <Hash>
85
+ when :fonts
86
+ if value
87
+ Hash(value).transform_keys(&:to_sym)
88
+ end
89
+
84
90
  else
85
91
  value
86
92
  end
@@ -50,7 +50,7 @@ module RailsERD
50
50
 
51
51
  NODE_WIDTH = 130 # @private :nodoc:
52
52
 
53
- FONTS = Config.font_names_based_on_os
53
+ FONTS = Config.font_names_based_on_os.merge(RailsERD.options[:fonts])
54
54
 
55
55
  # Default graph attributes.
56
56
  GRAPH_ATTRIBUTES = {
@@ -78,8 +78,8 @@ module RailsERD
78
78
  # relationship.
79
79
  def foreign_key?
80
80
  @domain.relationships_by_entity_name(@model.name).map(&:associations).flatten.map { |associaton|
81
- associaton.send(Domain.foreign_key_method_name)
82
- }.include?(name)
81
+ associaton.send(Domain.foreign_key_method_name).to_sym
82
+ }.include?(name.to_sym)
83
83
  end
84
84
 
85
85
  # Returns +true+ if this attribute is used for single table inheritance.
@@ -119,7 +119,7 @@ module RailsERD
119
119
  # <tt>:decimal, :precision => 5, :scale => 2/tt>:: decimal (5,2)
120
120
  # <tt>:boolean, :null => false</tt>:: boolean *
121
121
  def type_description
122
- type.to_s.tap do |desc|
122
+ type.to_s.dup.tap do |desc|
123
123
  desc << " #{limit_description}" if limit_description
124
124
  desc << " ∗" if mandatory? && !primary_key? # Add a hair space + low asterisk (Unicode characters)
125
125
  desc << " U" if unique? && !primary_key? && !foreign_key? # Add U if unique but non-key
@@ -30,7 +30,14 @@ module RailsERD
30
30
  end
31
31
 
32
32
  def abstract_from_models(domain, models)
33
- models.select(&:abstract_class?).collect(&:direct_descendants).flatten.collect { |model|
33
+ abstract_classes = models.select(&:abstract_class?)
34
+ direct_descendants = if ActiveRecord.version >= Gem::Version.new("7.0.0")
35
+ abstract_classes.collect(&:subclasses)
36
+ else
37
+ abstract_classes.collect(&:direct_descendants)
38
+ end
39
+
40
+ direct_descendants.flatten.collect { |model|
34
41
  new(domain, domain.entity_by_name(model.superclass.name), domain.entity_by_name(model.name))
35
42
  }
36
43
  end
@@ -120,7 +120,32 @@ module RailsERD
120
120
  end
121
121
 
122
122
  def models
123
- @models ||= @source_models.select { |model| check_model_validity(model) }.reject { |model| check_habtm_model(model) }
123
+ @models ||= @source_models
124
+ .reject { |model| tableless_rails_models.include?(model) }
125
+ .select { |model| check_model_validity(model) }
126
+ .reject { |model| check_habtm_model(model) }
127
+ end
128
+
129
+ # Returns Rails model classes defined in the app
130
+ def rails_models
131
+ %w(
132
+ ActionMailbox::InboundEmail
133
+ ActiveStorage::Attachment
134
+ ActiveStorage::Blob
135
+ ActiveStorage::VariantRecord
136
+ ActionText::RichText
137
+ ActionText::EncryptedRichText
138
+ ).map{ |model| Object.const_get(model) rescue nil }.compact
139
+ end
140
+
141
+ def tableless_rails_models
142
+ @tableless_rails_models ||= begin
143
+ if defined? Rails
144
+ rails_models.reject{ |model| model.table_exists? }
145
+ else
146
+ []
147
+ end
148
+ end
124
149
  end
125
150
 
126
151
  def associations
@@ -1,7 +1,9 @@
1
1
  require 'graphviz/utils'
2
2
 
3
- def say(message)
4
- puts message unless Rake.application.options.silent
3
+ module ErdRakeHelper
4
+ def say(message)
5
+ puts message unless Rake.application.options.silent
6
+ end
5
7
  end
6
8
 
7
9
  namespace :erd do
@@ -31,6 +33,8 @@ namespace :erd do
31
33
  end
32
34
 
33
35
  task :load_models do
36
+ include ErdRakeHelper
37
+
34
38
  say "Loading application environment..."
35
39
  Rake::Task[:environment].invoke
36
40
 
@@ -55,12 +59,14 @@ namespace :erd do
55
59
  end
56
60
 
57
61
  task :generate => [:check_dependencies, :options, :load_models] do
62
+ include ErdRakeHelper
63
+
58
64
  say "Generating Entity-Relationship Diagram for #{ActiveRecord::Base.descendants.length} models..."
59
65
 
60
66
  require "rails_erd/diagram/graphviz"
61
67
  file = RailsERD::Diagram::Graphviz.create
62
68
 
63
- say "Done! Saved diagram to #{file}."
69
+ say "Done! Saved diagram to ./#{file}"
64
70
  end
65
71
  end
66
72
 
@@ -1,4 +1,4 @@
1
1
  module RailsERD
2
- VERSION = "1.6.1"
2
+ VERSION = "1.7.2"
3
3
  BANNER = "RailsERD #{VERSION}"
4
4
  end
data/lib/rails_erd.rb CHANGED
@@ -40,6 +40,7 @@ module RailsERD
40
40
  :disconnected, true,
41
41
  :filename, "erd",
42
42
  :filetype, :pdf,
43
+ :fonts, {},
43
44
  :indirect, true,
44
45
  :inheritance, false,
45
46
  :markup, true,
data/test/test_helper.rb CHANGED
@@ -34,9 +34,11 @@ class ActiveSupport::TestCase
34
34
  opts = if pk then { :primary_key => pk } else { :id => false } end
35
35
  ActiveRecord::Schema.instance_eval do
36
36
  suppress_messages do
37
- create_table table, opts do |t|
38
- columns.each do |column, type|
39
- t.send type, column
37
+ unless ActiveRecord::Base.connection.table_exists?(table)
38
+ create_table table, **opts do |t|
39
+ columns.each do |column, type|
40
+ t.send type, column
41
+ end
40
42
  end
41
43
  end
42
44
  end
@@ -47,7 +49,8 @@ class ActiveSupport::TestCase
47
49
  def add_column(*args)
48
50
  ActiveRecord::Schema.instance_eval do
49
51
  suppress_messages do
50
- add_column(*args)
52
+ opts = args.slice!(3) || {}
53
+ add_column(*args, **opts)
51
54
  end
52
55
  end
53
56
  ActiveRecord::Base.clear_cache!
@@ -82,7 +85,7 @@ class ActiveSupport::TestCase
82
85
  klass = Object.const_set name.to_sym, Class.new(superklass)
83
86
 
84
87
  if superklass == ActiveRecord::Base || superklass.abstract_class?
85
- create_table Object.const_get(name.to_sym).table_name, columns, Object.const_get(name.to_sym).primary_key rescue nil
88
+ create_table Object.const_get(name.to_sym).table_name, columns, Object.const_get(name.to_sym).primary_key
86
89
  end
87
90
  klass.class_eval(&block) if block_given?
88
91
  Object.const_get(name.to_sym)
@@ -212,15 +215,18 @@ class ActiveSupport::TestCase
212
215
  ActiveRecord::Base.connection.drop_table table
213
216
  end
214
217
 
215
- if ActiveRecord.version >= Gem::Version.new("6.0.0.rc1")
218
+ if ActiveRecord.version >= Gem::Version.new("7.0.0")
219
+ ActiveSupport::DescendantsTracker.clear(ActiveRecord::Base.subclasses)
220
+ elsif ActiveRecord.version >= Gem::Version.new("6.0.0.rc1")
216
221
  cv = ActiveSupport::DescendantsTracker.class_variable_get(:@@direct_descendants)
217
222
  cv.delete(ActiveRecord::Base)
218
223
  ActiveSupport::DescendantsTracker.class_variable_set(:@@direct_descendants, cv)
224
+ ActiveSupport::Dependencies::Reference.clear!
219
225
  else
220
226
  ActiveRecord::Base.direct_descendants.clear
227
+ ActiveSupport::Dependencies::Reference.clear!
221
228
  end
222
-
223
- ActiveSupport::Dependencies::Reference.clear!
229
+
224
230
  ActiveRecord::Base.clear_cache!
225
231
  end
226
232
  end
@@ -115,6 +115,12 @@ class ConfigTest < ActiveSupport::TestCase
115
115
  assert_equal [:content, :primary_keys], normalize_value(:attributes, ["content", "primary_keys"])
116
116
  end
117
117
 
118
+ test "normalize_value should return hash with symbol keys when key is :fonts and value is a hash." do
119
+ fonts_value = { "normal" => "Arial", "bold" => "Arial Bold", "italic" => "Arial Italic" }
120
+ expected = {:normal => "Arial", :bold => "Arial Bold", :italic => "Arial Italic"}
121
+ assert_equal expected, normalize_value(:fonts, fonts_value)
122
+ end
123
+
118
124
  def normalize_value(key, value)
119
125
  RailsERD::Config.new.send(:normalize_value, key, value)
120
126
  end
@@ -283,4 +283,14 @@ class DomainTest < ActiveSupport::TestCase
283
283
  end
284
284
  assert_match(/Ignoring invalid model Foo \(table foos does not exist\)/, output)
285
285
  end
286
+
287
+ test "entities should not output a warning when a Rails model table does not exist" do
288
+ module ActionMailbox; end
289
+
290
+ Object.const_set :InboundEmail, ActionMailbox
291
+ output = collect_stdout do
292
+ Domain.generate.entities
293
+ end
294
+ assert_equal "", output
295
+ end
286
296
  end
@@ -186,6 +186,16 @@ class GraphvizTest < ActiveSupport::TestCase
186
186
  assert_equal '"Domain model\n\n"', diagram.graph.graph[:label].to_s
187
187
  end
188
188
 
189
+ test "generate should use default value for fontname attribute" do
190
+ create_simple_domain
191
+ assert_equal "\"#{RailsERD::Config.font_names_based_on_os[:bold]}\"", diagram.graph.graph[:fontname].to_s
192
+ end
193
+
194
+ test "generate should add set value for fontname attribute" do
195
+ create_simple_domain
196
+ assert_equal '"Arial Bold"', diagram(fonts: {bold: "Arial Bold"}).graph.graph[:fontname].to_s
197
+ end
198
+
189
199
  test "generate should add default value for splines attribute" do
190
200
  create_simple_domain
191
201
  assert_equal '"spline"', diagram.graph.graph[:splines].to_s
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-erd
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.1
4
+ version: 1.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rolf Timmermans
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-02-16 00:00:00.000000000 Z
12
+ date: 2022-08-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -162,8 +162,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
162
162
  - !ruby/object:Gem::Version
163
163
  version: '0'
164
164
  requirements: []
165
- rubyforge_project:
166
- rubygems_version: 2.6.14
165
+ rubygems_version: 3.3.15
167
166
  signing_key:
168
167
  specification_version: 4
169
168
  summary: Entity-relationship diagram for your Rails models.