schema_doctor 0.0.2 → 0.0.4

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
  SHA256:
3
- metadata.gz: 74042fa873e37695d586611d9dc2b5c54c6b293f2c0bf545769ff165cf3972e3
4
- data.tar.gz: b65964fe3c1c06ed555fdbad5c5ad78a876513605c84e4776dfcaff2f060042a
3
+ metadata.gz: da7654f58e8c8dff14e477cdf437a0c3532a2d650a299158eddda22d5ca2b794
4
+ data.tar.gz: 3a336c98c4420b1aa9f8b5d55ee99389aeeff4d893366eca32ad3e91e2ca18d5
5
5
  SHA512:
6
- metadata.gz: b40f4e2c85e7530fc7ec798c203f19a2b96a5ae01439c2118fcaa290dd0ce02e13ce695f02a3fda308a376be3ee2350a269b9f77eb7fa6bbb3db997a8d8972d9
7
- data.tar.gz: 13ba870f8f7f3e675b1f209fe5f07f243ebdd377f3de72774e94d65600feb23c4e3fe196e3bd047d3e753cdfc81a6da442b76b7fbbf37e1d3bdc37c839ff5713
6
+ metadata.gz: c7420bc7730dc74855950fefe32ca6352691ae2ee52f320556286f5ed8f5a6073fdcd75dec5d188d35a7f99759a493970a88d07fea01e2009552f5b067e6e084
7
+ data.tar.gz: 3d3fbe268ffc5cd71faca72598e70c3f35a1418cecdc835f5273c27c556448e1004ad555a1d8094eba0dedbdf9e0563c4de186a873d82e0d03088a60b084eccf
@@ -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: Utils.sefety_dump_hash(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: Utils.sefety_dump_hash(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}"
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SchemaDoctor
4
+ class Utils
5
+ class << self
6
+ def sefety_dump_hash(obj)
7
+ case obj
8
+ when Hash
9
+ obj.transform_values { |v| sefety_dump_hash(v) }
10
+ when Array
11
+ obj.map { |v| sefety_dump_hash(v) }
12
+ when TrueClass, FalseClass, NilClass, Integer, Float, String
13
+ obj
14
+ else
15
+ obj.to_s
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SchemaDoctor
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.4"
5
5
  end
data/lib/schema_doctor.rb CHANGED
@@ -4,6 +4,7 @@ require_relative "schema_doctor/version"
4
4
  require_relative "schema_doctor/schema_file"
5
5
  require_relative "schema_doctor/analyzer"
6
6
  require_relative "schema_doctor/exporter"
7
+ require_relative "schema_doctor/utils"
7
8
  require_relative "schema_doctor/railtie"
8
9
 
9
10
  module SchemaDoctor
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.2
4
+ version: 0.0.4
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-07-18 00:00:00.000000000 Z
11
+ date: 2024-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -71,6 +71,7 @@ files:
71
71
  - lib/schema_doctor/railtie.rb
72
72
  - lib/schema_doctor/schema_file.rb
73
73
  - lib/schema_doctor/tasks/schemadoc.rake
74
+ - lib/schema_doctor/utils.rb
74
75
  - lib/schema_doctor/version.rb
75
76
  - sig/schema_doctor.rbs
76
77
  homepage: https://github.com/lnit/schema_doctor