schema_doctor 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/schema_doctor/analyzer.rb +36 -1
- data/lib/schema_doctor/exporter.rb +67 -0
- data/lib/schema_doctor/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 36282d15058adc20e4b9c2013bb9ddaa1d95f1e9457a10278be99a7b40c900e4
|
4
|
+
data.tar.gz: baf1eae580dedd1d8cbe4c5ca5bfef36e08849589aec89856731a9680cfc268a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 917c1f0b805be6028f319f00698fdc153aedc38ffbf3b7ff6b780fc1d9eb4fd3fb7c0a106d4185cd32c54547279b509c869645a24ac3ca8849d2d5af6e4061a8
|
7
|
+
data.tar.gz: b309be6513ad10e6bf0563c9da4c87d1c9b17507f87d1a670527cc6c73f0f1b41ce0dab22a5b5b395fa3a196df991f96415078c33aaad3677e1163c87a6bb5ec
|
@@ -32,11 +32,13 @@ module SchemaDoctor
|
|
32
32
|
table_comment: connection.table_comment(model.table_name),
|
33
33
|
extra_comment: schema.dig(model.name, :extra_comment),
|
34
34
|
columns: columns(model, schema.dig(model.name, :columns) || {}),
|
35
|
-
indexes: indexes(model)
|
35
|
+
indexes: indexes(model),
|
36
|
+
associations: associations(model)
|
36
37
|
}
|
37
38
|
rescue ActiveRecord::TableNotSpecified
|
38
39
|
nil
|
39
40
|
rescue => e
|
41
|
+
# Skip analyzing if an error occurs
|
40
42
|
puts "Failed to process #{model.name}: #{e.inspect}"
|
41
43
|
puts "\e[31mWe're sorry, Failed to process \e[33m#{model.name}\e[31m:\n #{e.inspect}\e[0m"
|
42
44
|
end
|
@@ -86,5 +88,38 @@ module SchemaDoctor
|
|
86
88
|
}
|
87
89
|
end
|
88
90
|
end
|
91
|
+
|
92
|
+
def associations(model)
|
93
|
+
result = {
|
94
|
+
belongs: {},
|
95
|
+
has: {}
|
96
|
+
}
|
97
|
+
|
98
|
+
model.reflect_on_all_associations.each do |association|
|
99
|
+
case association.macro.to_s
|
100
|
+
when /\Abelongs_to/
|
101
|
+
result[:belongs][association.name] = {
|
102
|
+
macro: association.macro.to_s,
|
103
|
+
name: association.name,
|
104
|
+
class_name: association.class_name,
|
105
|
+
foreign_key: association.foreign_key,
|
106
|
+
options: association.options,
|
107
|
+
polymorphic: association.polymorphic?
|
108
|
+
}
|
109
|
+
when /\Ahas/
|
110
|
+
result[:has][association.name] = {
|
111
|
+
macro: association.macro.to_s,
|
112
|
+
name: association.name,
|
113
|
+
class_name: association.class_name,
|
114
|
+
options: association.options
|
115
|
+
}
|
116
|
+
else
|
117
|
+
puts "Unknown association type: #{association.macro}: :#{association.name}"
|
118
|
+
next
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
result
|
123
|
+
end
|
89
124
|
end
|
90
125
|
end
|
@@ -23,6 +23,8 @@ module SchemaDoctor
|
|
23
23
|
:toclevels: 1
|
24
24
|
:toc-title: Table of Contents
|
25
25
|
:linkattrs:
|
26
|
+
:sectlinks:
|
27
|
+
:sectanchors:
|
26
28
|
|
27
29
|
TEXT
|
28
30
|
|
@@ -85,6 +87,71 @@ module SchemaDoctor
|
|
85
87
|
|
86
88
|
TEXT
|
87
89
|
end
|
90
|
+
|
91
|
+
# Output associations schema
|
92
|
+
f.puts <<~TEXT
|
93
|
+
|
94
|
+
=== Associations
|
95
|
+
TEXT
|
96
|
+
belongs = model[:associations][:belongs]
|
97
|
+
if belongs.present?
|
98
|
+
f.puts <<~TEXT
|
99
|
+
[cols="1,1,1,1"]
|
100
|
+
|===
|
101
|
+
|macro|name|foreign_key|options
|
102
|
+
|
103
|
+
TEXT
|
104
|
+
|
105
|
+
belongs.each_value do |association|
|
106
|
+
class_name = association.delete(:class_name).to_s
|
107
|
+
polymorphic = association.delete(:polymorphic)
|
108
|
+
# TODO: polymorphicのリンク先を特定できるようにする。今のところリンクを生成しないように。
|
109
|
+
association[:name] = "link:#_#{class_name.downcase.gsub("::", "")}[#{association[:name]}]" unless polymorphic
|
110
|
+
|
111
|
+
str = association.values.map do |v|
|
112
|
+
escaped = v.to_s.gsub("|", "{vbar}")
|
113
|
+
"|#{escaped}\n"
|
114
|
+
end.join
|
115
|
+
|
116
|
+
f.puts <<~TEXT
|
117
|
+
#{str}
|
118
|
+
TEXT
|
119
|
+
end
|
120
|
+
|
121
|
+
f.puts "|==="
|
122
|
+
end
|
123
|
+
|
124
|
+
has = model[:associations][:has]
|
125
|
+
if has.present?
|
126
|
+
f.puts <<~TEXT
|
127
|
+
[cols="1,1,1"]
|
128
|
+
|===
|
129
|
+
|macro|name|options
|
130
|
+
|
131
|
+
TEXT
|
132
|
+
|
133
|
+
has.each_value do |association|
|
134
|
+
class_name = association.delete(:class_name).to_s
|
135
|
+
association[:name] = "link:#_#{class_name.gsub("::", "").downcase}[#{association[:name]}]"
|
136
|
+
str = association.values.map do |v|
|
137
|
+
escaped = v.to_s.gsub("|", "{vbar}")
|
138
|
+
"|#{escaped}\n"
|
139
|
+
end.join
|
140
|
+
|
141
|
+
f.puts <<~TEXT
|
142
|
+
#{str}
|
143
|
+
TEXT
|
144
|
+
end
|
145
|
+
|
146
|
+
f.puts "|==="
|
147
|
+
end
|
148
|
+
|
149
|
+
if belongs.empty? & has.empty?
|
150
|
+
f.puts <<~TEXT
|
151
|
+
None
|
152
|
+
|
153
|
+
TEXT
|
154
|
+
end
|
88
155
|
end
|
89
156
|
end
|
90
157
|
puts "Done! => #{adoc_path}"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: schema_doctor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- lni_T
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-08-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|