schemerd 0.1.3 → 0.1.5
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 +4 -4
- data/lib/schemerd/generator.rb +19 -4
- data/lib/schemerd/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ca6f1b673c235b1bdefd3373a34bd56e3d573c2bc9d9e152692937d47335ae58
|
|
4
|
+
data.tar.gz: bb3b90351c3cd8b28679fe9abf9c253405c420dbbc80a7ab589357bffd523195
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4fbb96858dc176e329b092c7ef6badba3064e75f8256252b83e856b8409192066e78c12ac628539ff6ecf44ac454e050694547cdddc7d2ac24d59ce9c1a6588f
|
|
7
|
+
data.tar.gz: 74737e1813ab6e52296db20f5bfc5ab4cab970f73008ba10115b4ba4c44756b6fc3bc3d0489e0e13112948e844001acb590d7fa60c299e2663137033ed28ec13
|
data/lib/schemerd/generator.rb
CHANGED
|
@@ -27,17 +27,28 @@ module Schemerd
|
|
|
27
27
|
Rails.application.eager_load!
|
|
28
28
|
|
|
29
29
|
base = @config.base_class.constantize
|
|
30
|
-
base.descendants
|
|
30
|
+
all_models = base.descendants
|
|
31
31
|
.reject(&:abstract_class?)
|
|
32
32
|
.reject { |m| excluded?(m.name) }
|
|
33
33
|
.select { |m| m.table_exists? rescue false }
|
|
34
|
-
|
|
34
|
+
|
|
35
|
+
@sti_children = Hash.new { |h, k| h[k] = [] }
|
|
36
|
+
sti, non_sti = all_models.partition { |m| sti_child?(m) }
|
|
37
|
+
sti.each { |m| @sti_children[m.superclass.name] << m.name }
|
|
38
|
+
|
|
39
|
+
non_sti.sort_by(&:name)
|
|
35
40
|
end
|
|
36
41
|
|
|
37
42
|
def excluded?(model_name)
|
|
38
43
|
@config.excluded_prefixes.any? { |prefix| model_name.start_with?(prefix) }
|
|
39
44
|
end
|
|
40
45
|
|
|
46
|
+
def sti_child?(model)
|
|
47
|
+
model.superclass != @config.base_class.constantize &&
|
|
48
|
+
!model.superclass.abstract_class? &&
|
|
49
|
+
model.table_name == model.superclass.table_name
|
|
50
|
+
end
|
|
51
|
+
|
|
41
52
|
def build_diagram(models)
|
|
42
53
|
lines = []
|
|
43
54
|
|
|
@@ -60,7 +71,7 @@ module Schemerd
|
|
|
60
71
|
model_names = models.map(&:name).to_set
|
|
61
72
|
|
|
62
73
|
models.each do |model|
|
|
63
|
-
model.reflect_on_all_associations.each do |assoc|
|
|
74
|
+
model.reflect_on_all_associations.reject { |a| a.options[:through] }.each do |assoc|
|
|
64
75
|
target_name = assoc.klass.name rescue next
|
|
65
76
|
next unless model_names.include?(target_name)
|
|
66
77
|
|
|
@@ -81,10 +92,12 @@ module Schemerd
|
|
|
81
92
|
|
|
82
93
|
models.each do |model|
|
|
83
94
|
lines << " #{model.name} {"
|
|
95
|
+
subtypes = @sti_children.fetch(model.name, [])
|
|
84
96
|
sort_columns(model.columns).each do |col|
|
|
85
97
|
pk = col.name == model.primary_key ? "PK" : ""
|
|
98
|
+
comment = col.name == "type" && subtypes.any? ? "\"#{subtypes.sort.join(', ')}\"" : ""
|
|
86
99
|
type = col.type || "string"
|
|
87
|
-
lines << " #{type} #{col.name} #{pk}".rstrip
|
|
100
|
+
lines << " #{type} #{col.name} #{pk} #{comment}".rstrip
|
|
88
101
|
end
|
|
89
102
|
lines << " }"
|
|
90
103
|
lines << ""
|
|
@@ -121,6 +134,8 @@ module Schemerd
|
|
|
121
134
|
"#{source} ||--o{ #{target} : \"#{assoc.name}\""
|
|
122
135
|
when :has_one
|
|
123
136
|
"#{source} ||--o| #{target} : \"#{assoc.name}\""
|
|
137
|
+
when :has_and_belongs_to_many
|
|
138
|
+
"#{source} }o--o{ #{target} : \"#{assoc.name}\""
|
|
124
139
|
end
|
|
125
140
|
end
|
|
126
141
|
end
|
data/lib/schemerd/version.rb
CHANGED