rails-erd 1.3.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7ada96cc38a16ade059a334c1b0c228f1d950799
4
- data.tar.gz: 4ce8b31b96b63dd9da6651d932787d78067fbc74
3
+ metadata.gz: 7e5205eb093cf7ca1ffd731eb201853c1b4134d3
4
+ data.tar.gz: 04deb363195d35173242a3479176013eeb1b5402
5
5
  SHA512:
6
- metadata.gz: 05bebbfc35caa2f8db94a5fd444de5342d8fa1ebae29f5d954b7858b2a61ab9dccd7c48093e19dbac693584de9e41e0c50c1030fbd4b30e59e44e58a73838a0f
7
- data.tar.gz: 8b6d762b352f2638e482deb1f8e2330af32a040a651264b70cee13d1bfbfce7dc594ede091d8a3df66954be5dc3b0c798e289c0eb64f2979a1f68b946aa6d110
6
+ metadata.gz: b9e3a914f4eefec4a97304d2a621b0dd3105741401ab1438db75292ecfc50f968aeb6e7f36a0937088f6152ed355692ee53b5585f0090d97e5b800aa06c39dce
7
+ data.tar.gz: 11b65a72efea78e56f6bf25d1363d3ef1fe94336f8556e266480216aa573d27784aae16784cef7f79d1860e34375fef6906f2a9c352ae2b4c73ec067db2dc904
data/README.md CHANGED
@@ -5,7 +5,7 @@ Rails ERD - Generate Entity-Relationship Diagrams for Rails applications
5
5
 
6
6
  The second goal of Rails ERD is to provide you with a tool to inspect your application's domain model. If you don't like the default output, it is very easy to use the API to build your own diagrams.
7
7
 
8
- Rails ERD was created specifically for Rails 3. It uses Active Record's built-in reflection capabilities to figure out how your models are associated.
8
+ Rails ERD was created specifically for Rails and works on versions 3.0-4.2. It uses Active Record's built-in reflection capabilities to figure out how your models are associated.
9
9
 
10
10
 
11
11
  Preview
@@ -53,10 +53,12 @@ markup: true
53
53
  notation: simple
54
54
  orientation: horizontal
55
55
  polymorphism: false
56
+ sort: true
56
57
  warn: true
57
58
  title: sample title
58
59
  exclude: null
59
- only:
60
+ only: null
61
+ prepend_primary: false
60
62
  ```
61
63
 
62
64
 
data/lib/rails_erd.rb CHANGED
@@ -46,10 +46,12 @@ module RailsERD
46
46
  :notation, :simple,
47
47
  :orientation, :horizontal,
48
48
  :polymorphism, false,
49
+ :sort, true,
49
50
  :warn, true,
50
51
  :title, true,
51
52
  :exclude, nil,
52
- :only, nil
53
+ :only, nil,
54
+ :prepend_primary, false
53
55
  ]
54
56
  end
55
57
  end
data/lib/rails_erd/cli.rb CHANGED
@@ -57,6 +57,18 @@ Choice.options do
57
57
  desc "Filter to exclude listed models in diagram."
58
58
  end
59
59
 
60
+ option :sort do
61
+ long "--sort=BOOLEAN"
62
+ desc "Sort attribute list alphabetically"
63
+ default "false"
64
+ end
65
+
66
+ option :prepend_primary do
67
+ long "--prepend_primary=BOOLEAN"
68
+ desc "Ensure primary key is at start of attribute list"
69
+ default "false"
70
+ end
71
+
60
72
  separator ""
61
73
  separator "Output options:"
62
74
 
@@ -118,7 +130,7 @@ module RailsERD
118
130
  if key.start_with? "no_"
119
131
  opts[key.gsub("no_", "").to_sym] = !value
120
132
  elsif value.to_s.include? ","
121
- opts[key.to_sym] = value.split(",").map(&:to_sym)
133
+ opts[key.to_sym] = value.split(",").map(&:to_s)
122
134
  else
123
135
  opts[key.to_sym] = value
124
136
  end
@@ -60,12 +60,15 @@ module RailsERD
60
60
  when :filetype, :notation, :orientation
61
61
  value.to_sym
62
62
 
63
+ # [<string>]
64
+ when :only, :exclude
65
+ Array(value).join(",").split(",").map { |v| v.strip }
63
66
  # true | false
64
67
  when :disconnected, :indirect, :inheritance, :markup, :polymorphism, :warn
65
68
  !!value
66
69
 
67
70
  # nil | <string>
68
- when :filename, :only, :exclude
71
+ when :filename
69
72
  value.nil? ? nil : value.to_s
70
73
 
71
74
  # true | false | <string>
@@ -150,8 +150,8 @@ module RailsERD
150
150
 
151
151
  def filtered_entities
152
152
  @domain.entities.reject { |entity|
153
- options.exclude && entity.model && [options.exclude].flatten.include?(entity.name.to_sym) or
154
- options.only && entity.model && ![options.only].flatten.include?(entity.name.to_sym) or
153
+ options.exclude && entity.model && [options.exclude].flatten.include?(entity.name.to_s) or
154
+ options.only && entity.model && ![options.only].flatten.include?(entity.name.to_s) or
155
155
  !options.inheritance && entity.specialized? or
156
156
  !options.polymorphism && entity.generalized? or
157
157
  !options.disconnected && entity.disconnected?
@@ -10,7 +10,20 @@ module RailsERD
10
10
 
11
11
  class << self
12
12
  def from_model(domain, model) # @private :nodoc:
13
- model.columns.collect { |column| new(domain, model, column) }.sort
13
+ attributes = model.columns.collect { |column| new(domain, model, column) }
14
+ attributes.sort! if RailsERD.options[:sort]
15
+
16
+ if RailsERD.options[:prepend_primary]
17
+ primary_key = ActiveRecord::Base.get_primary_key(model)
18
+ primary = attributes.detect{ |column| column.name == primary_key }
19
+
20
+ if primary
21
+ attributes.delete(primary)
22
+ attributes.unshift(primary)
23
+ end
24
+ end
25
+
26
+ attributes
14
27
  end
15
28
  end
16
29
 
@@ -47,6 +60,10 @@ module RailsERD
47
60
  !column.null or @model.validators_on(name).map(&:kind).include?(:presence)
48
61
  end
49
62
 
63
+ def unique?
64
+ @model.validators_on(name).map(&:kind).include?(:uniqueness)
65
+ end
66
+
50
67
  # Returns +true+ if this attribute is the primary key of the entity.
51
68
  def primary_key?
52
69
  @model.primary_key.to_s == name.to_s
@@ -93,7 +110,10 @@ module RailsERD
93
110
  def type_description
94
111
  type.to_s.tap do |desc|
95
112
  desc << " #{limit_description}" if limit_description
96
- desc << " ∗" if mandatory? # Add a hair space + low asterisk (Unicode characters).
113
+ desc << " ∗" if mandatory? && !primary_key? # Add a hair space + low asterisk (Unicode characters)
114
+ desc << " U" if unique? && !primary_key? && !foreign_key? # Add U if unique but non-key
115
+ desc << " PK" if primary_key?
116
+ desc << " FK" if foreign_key?
97
117
  end
98
118
  end
99
119
 
@@ -40,7 +40,7 @@ module RailsERD
40
40
 
41
41
  # Returns an array of attributes for this entity.
42
42
  def attributes
43
- @attributes ||= if generalized? then [] else Attribute.from_model(domain, model) end
43
+ @attributes ||= generalized? ? [] : Attribute.from_model(domain, model)
44
44
  end
45
45
 
46
46
  # Returns an array of all relationships that this entity has with other
@@ -190,7 +190,7 @@ module RailsERD
190
190
  end
191
191
 
192
192
  def foreign_key_required?(association)
193
- if association.belongs_to?
193
+ if !association.active_record.abstract_class? and association.belongs_to?
194
194
  column = association.active_record.columns_hash[association.send(Domain.foreign_key_method_name)] and !column.null
195
195
  end
196
196
  end
@@ -1,4 +1,4 @@
1
1
  module RailsERD
2
- VERSION = "1.3.0"
2
+ VERSION = "1.3.1"
3
3
  BANNER = "RailsERD #{VERSION}"
4
4
  end
@@ -13,6 +13,6 @@ orientation: horizontal
13
13
  polymorphism: false
14
14
  warn: true
15
15
  title: sample title
16
- exclude: null
16
+ exclude:
17
17
  only:
18
18
 
@@ -0,0 +1,18 @@
1
+ attributes:
2
+ - content
3
+ - foreign_key
4
+ - inheritance
5
+ disconnected: true
6
+ filename: erd
7
+ filetype: pdf
8
+ indirect: true
9
+ inheritance: false
10
+ markup: true
11
+ notation: simple
12
+ orientation: horizontal
13
+ polymorphism: false
14
+ warn: true
15
+ title: sample title
16
+ exclude: Book,Author
17
+ only:
18
+
@@ -30,6 +30,30 @@ class AttributeTest < ActiveSupport::TestCase
30
30
  Domain::Attribute.from_model(Domain.new, Foo).reject(&:primary_key?).first.column
31
31
  end
32
32
 
33
+ test "from_model should return attributes with sorted order if sort is true" do
34
+ RailsERD.options[:sort] = true
35
+ create_model "Foo"
36
+ add_column :foos, :a, :string
37
+ assert_equal %w{a id}, Domain::Attribute.from_model(Domain.new, Foo).map(&:name)
38
+ end
39
+
40
+ test "from_model should return attributes with original order if sort is false" do
41
+ RailsERD.options[:sort] = false
42
+ create_model "Foo"
43
+ add_column :foos, :a, :string
44
+ assert_equal %w{id a}, Domain::Attribute.from_model(Domain.new, Foo).map(&:name)
45
+ end
46
+
47
+ test "from_model should return attributes with PK first if sort is false and prepend_primary is true" do
48
+ RailsERD.options[:sort] = false
49
+ RailsERD.options[:prepend_primary] = true
50
+
51
+ create_model "Foo"
52
+ add_column :foos, :a, :string
53
+
54
+ assert_equal %w{id a}, Domain::Attribute.from_model(Domain.new, Foo).map(&:name)
55
+ end
56
+
33
57
  test "spaceship should sort attributes by name" do
34
58
  create_model "Foo", :a => :string, :b => :string, :c => :string
35
59
  a = create_attribute(Foo, "a")
@@ -24,8 +24,30 @@ class ConfigTest < ActiveSupport::TestCase
24
24
  polymorphism: false,
25
25
  warn: true,
26
26
  title: "sample title",
27
- exclude: nil,
28
- only: nil
27
+ exclude: [],
28
+ only: []
29
+ }
30
+ assert_equal expected_hash, RailsERD::Config.load
31
+ end
32
+
33
+ test "load_config_file should return a hash from USER_WIDE_CONFIG_FILE when only USER_WIDE_CONFIG_FILE exists." do
34
+ set_user_config_file_to("erdconfig.exclude.example")
35
+
36
+ expected_hash = {
37
+ attributes: [:content, :foreign_key, :inheritance],
38
+ disconnected: true,
39
+ filename: "erd",
40
+ filetype: :pdf,
41
+ indirect: true,
42
+ inheritance: false,
43
+ markup: true,
44
+ notation: :simple,
45
+ orientation: :horizontal,
46
+ polymorphism: false,
47
+ warn: true,
48
+ title: "sample title",
49
+ exclude: ['Book', 'Author'],
50
+ only: []
29
51
  }
30
52
  assert_equal expected_hash, RailsERD::Config.load
31
53
  end
@@ -56,8 +78,8 @@ class ConfigTest < ActiveSupport::TestCase
56
78
  polymorphism: false,
57
79
  warn: true,
58
80
  title: "sample title",
59
- exclude: nil,
60
- only: nil
81
+ exclude: [],
82
+ only: []
61
83
  }
62
84
  assert_equal expected_hash, RailsERD::Config.load
63
85
  end
@@ -126,27 +126,27 @@ class DiagramTest < ActiveSupport::TestCase
126
126
  test "generate should filter excluded entity" do
127
127
  create_model "Book"
128
128
  create_model "Author"
129
- assert_equal [Book], retrieve_entities(:exclude => :Author).map(&:model)
129
+ assert_equal [Book], retrieve_entities(:exclude => ['Author']).map(&:model)
130
130
  end
131
131
 
132
132
  test "generate should filter excluded entities" do
133
133
  create_model "Book"
134
134
  create_model "Author"
135
135
  create_model "Editor"
136
- assert_equal [Book], retrieve_entities(:exclude => [:Author, :Editor]).map(&:model)
136
+ assert_equal [Book], retrieve_entities(:exclude => ['Author', 'Editor']).map(&:model)
137
137
  end
138
138
 
139
139
  test "generate should include only specified entity" do
140
140
  create_model "Book"
141
141
  create_model "Author"
142
- assert_equal [Book], retrieve_entities(:only => [:Book]).map(&:model)
142
+ assert_equal [Book], retrieve_entities(:only => ['Book']).map(&:model)
143
143
  end
144
144
 
145
145
  test "generate should include only specified entities" do
146
146
  create_model "Book"
147
147
  create_model "Author"
148
148
  create_model "Editor"
149
- assert_equal [Author, Editor], retrieve_entities(:only => [:Author, :Editor]).map(&:model)
149
+ assert_equal [Author, Editor], retrieve_entities(:only => ['Author', 'Editor']).map(&:model)
150
150
  end
151
151
 
152
152
  test "generate should filter disconnected entities if disconnected is false" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-erd
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rolf Timmermans
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-12 00:00:00.000000000 Z
11
+ date: 2015-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -97,6 +97,7 @@ files:
97
97
  - lib/rails_erd/version.rb
98
98
  - test/support_files/erdconfig.another_example
99
99
  - test/support_files/erdconfig.example
100
+ - test/support_files/erdconfig.exclude.example
100
101
  - test/test_helper.rb
101
102
  - test/unit/attribute_test.rb
102
103
  - test/unit/cardinality_test.rb
@@ -109,7 +110,8 @@ files:
109
110
  - test/unit/relationship_test.rb
110
111
  - test/unit/specialization_test.rb
111
112
  homepage: https://github.com/voormedia/rails-erd
112
- licenses: []
113
+ licenses:
114
+ - MIT
113
115
  metadata: {}
114
116
  post_install_message:
115
117
  rdoc_options: []
@@ -134,6 +136,7 @@ summary: Entity-relationship diagram for your Rails models.
134
136
  test_files:
135
137
  - test/support_files/erdconfig.another_example
136
138
  - test/support_files/erdconfig.example
139
+ - test/support_files/erdconfig.exclude.example
137
140
  - test/test_helper.rb
138
141
  - test/unit/attribute_test.rb
139
142
  - test/unit/cardinality_test.rb