rails-annotate-solargraph 0.2.3 → 0.3.0

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
2
  SHA256:
3
- metadata.gz: 0f1d0f284eac106e51e1abc19eec0d7b8790aab9ee188e1cabea5defb04417da
4
- data.tar.gz: 392519ce6da6313e8a3308dee6f54b66629ee108120b8abcb94e1d838dc86470
3
+ metadata.gz: 4ca6f0073923760a583abb52af713540b2f53cf6e8dd896584e3a76b97444c8b
4
+ data.tar.gz: a07141ca737ee0c60153cabd5e28540d3507ba09401b4cc31d30ea491bc32eb2
5
5
  SHA512:
6
- metadata.gz: 23e25ae122d969be4f5d4081638592e3db7d6ab46adf60cdaf82057953441576d842bc2db79af664371637e332b25c5fee091fe930e53ffd88f97d78a04533c9
7
- data.tar.gz: 43d0752100b231426877a84665ba8030d5121b8f0d7b07549f2312c7e30cb7f1c0f9c9807b99bc085394fd369cb81a95ec63c15366e92a9eb96116fdb91b8fee
6
+ metadata.gz: f3a40df235de87414059053670f344e0e6606b9538ecba40b7fbb2c849c07f8009e5cec055e485033bae5529bce9146d2369d76b8a6ab4288664703bef29da2f
7
+ data.tar.gz: d920e8b59c64f57d22fba128b4db1acffd88b718799e24e2f361d43e99ae6505258faa2665f57fafdee9d9cd6d4dc47231c9e5e7bb99a7e687707c2642e211e5
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.0] - 2022-04-17
4
+
5
+ - `has_many :through` and `has_one :through` relations get documented
6
+
3
7
  ## [0.2.3] - 2022-04-16
4
8
 
5
9
  - A nicer fix for the previous problem
@@ -15,6 +19,7 @@
15
19
  ## [0.2.0] - 2022-04-16
16
20
 
17
21
  - Associations get fully documented
22
+ - `has_many`, `has_one` and `belongs_to` relations get documented
18
23
 
19
24
  ## [0.1.1] - 2022-04-15
20
25
 
@@ -24,4 +29,5 @@
24
29
 
25
30
  - Initial release
26
31
  - Automatic generation of annotations after migrations
32
+ - Database fields get documented
27
33
  - Manual rake tasks `annotate:solargraph:generate`, `annotate:solargraph:remove`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails-annotate-solargraph (0.2.3)
4
+ rails-annotate-solargraph (0.3.0)
5
5
  rails (>= 5.0, < 8.0)
6
6
 
7
7
  GEM
@@ -142,37 +142,57 @@ module Rails
142
142
  @klass.table_name[..-2]
143
143
  end
144
144
 
145
+ # @param reflection [ActiveRecord::Reflection::AbstractReflection]
146
+ # @return [Class]
147
+ def reflection_class(reflection)
148
+ reflection.klass
149
+ rescue ::NameError
150
+ Object
151
+ end
152
+
153
+ # @param reflection [ActiveRecord::Reflection::AbstractReflection]
154
+ # @return [String]
155
+ def reflection_foreign_key(reflection)
156
+ reflection.try(:foreign_key) || '<unknown>'
157
+ end
158
+
159
+ # @param klass [Class]
160
+ # @return [String]
161
+ def class_table_name(klass)
162
+ klass.try(:table_name) || '<unknown>'
163
+ end
164
+
145
165
  # @param doc_string [String]
146
166
  # @param attr_name [String]
147
167
  # @param reflection [ActiveRecord::Reflection::AbstractReflection]
148
168
  # @return [void]
149
169
  def document_relation(doc_string, attr_name, reflection)
150
- reflection_class = begin
151
- reflection.klass
152
- rescue ::NameError
153
- Object
154
- end
155
-
156
- associated_table_name = reflection_class.try(:table_name) || '<unknown>'
157
- foreign_key = reflection.try(:foreign_key) || '<unknown>'
158
- db_description = \
170
+ reflection_klass = reflection_class(reflection)
171
+ type_docstring, db_description = \
159
172
  case reflection
160
173
  when ::ActiveRecord::Reflection::BelongsToReflection
161
- type_docstring = reflection_class
162
- "`belongs_to` relation with `#{reflection_class}`. Database column `#{@klass.table_name}.#{foreign_key}`."
174
+ belongs_to_description(reflection_klass,
175
+ class_table_name(@klass),
176
+ reflection_foreign_key(reflection))
163
177
  when ::ActiveRecord::Reflection::HasOneReflection
164
- type_docstring = reflection_class
165
- "`has_one` relation with `#{reflection_class}`. Database column `#{associated_table_name}.#{foreign_key}`."
178
+ has_one_description(reflection_klass,
179
+ class_table_name(reflection_klass),
180
+ reflection_foreign_key(reflection))
166
181
  when ::ActiveRecord::Reflection::HasManyReflection
167
- type_docstring = "Array<#{reflection_class}>"
168
- "`has_many` relation with `#{reflection_class}`. Database column `#{associated_table_name}.#{foreign_key}`."
182
+ has_many_description(reflection_klass,
183
+ class_table_name(reflection_klass),
184
+ reflection_foreign_key(reflection))
185
+ when ::ActiveRecord::Reflection::ThroughReflection
186
+ through_description(reflection)
187
+ else
188
+ [::Object.to_s, '']
169
189
  end
170
190
 
171
191
  doc_string << <<~DOC
172
- # # #{db_description}
192
+ # ##{db_description}
173
193
  # # @param val [#{type_docstring}, nil]
174
194
  # def #{attr_name}=(val); end
175
- # # #{db_description}
195
+ # ##{db_description}
176
196
  # # @return [#{type_docstring}, nil]
177
197
  # def #{attr_name}; end
178
198
  DOC
@@ -187,7 +207,7 @@ module Rails
187
207
  model_class.reflections[klass_relation_name]&.options&.[](:as)&.to_sym == attr_name.to_sym
188
208
  end
189
209
 
190
- classes_string = classes.join(', ')
210
+ classes_string = classes.empty? ? ::Object.to_s : classes.join(', ')
191
211
  doc_string << <<~DOC
192
212
  # # Polymorphic relation. Database columns `#{@klass.table_name}.#{attr_name}_id` and `#{@klass.table_name}.#{attr_name}_type`.
193
213
  # # @param val [#{classes_string}, nil]
@@ -198,6 +218,91 @@ module Rails
198
218
  DOC
199
219
  end
200
220
 
221
+ # @param reflection [ActiveRecord::Reflection::AbstractReflection]
222
+ # @return [Array<String>]
223
+ def through_description(reflection)
224
+ through_klass = reflection_class(reflection.through_reflection)
225
+
226
+ case (reflection.__send__(:delegate_reflection) rescue nil)
227
+ when ::ActiveRecord::Reflection::HasOneReflection
228
+ has_one_description(reflection_class(reflection.source_reflection),
229
+ class_table_name(through_klass),
230
+ reflection_foreign_key(reflection.source_reflection),
231
+ through: through_klass)
232
+ when ::ActiveRecord::Reflection::HasManyReflection
233
+ has_many_description(reflection_class(reflection.source_reflection),
234
+ class_table_name(through_klass),
235
+ reflection_foreign_key(reflection.source_reflection),
236
+ through: through_klass)
237
+ else
238
+ [::Object.to_s, '']
239
+ end
240
+ end
241
+
242
+ # @param through [Class, nil]
243
+ # @return [String]
244
+ def through_sentence(through = nil)
245
+ return '' unless through
246
+
247
+ " through `#{through}`"
248
+ end
249
+
250
+ # @param table_name [String]
251
+ # @param foreign_key [String]
252
+ # @param through [Class, nil]
253
+ # @return [String]
254
+ def column_description(table_name, foreign_key, through = nil)
255
+ return '' if through
256
+
257
+ " Database column `#{table_name}.#{foreign_key}`."
258
+ end
259
+
260
+ # @param relation [Symbol, String]
261
+ # @param klass [Class]
262
+ # @param table_name [String]
263
+ # @param foreign_key [String]
264
+ # @param through [Class, nil]
265
+ # @return [String]
266
+ def relation_description(relation, klass, table_name, foreign_key, through = nil)
267
+ " `#{relation}` relation with `#{klass}`#{through_sentence(through)}.#{column_description(table_name, foreign_key, through)}"
268
+ end
269
+
270
+
271
+ # @param klass [Class]
272
+ # @param table_name [String]
273
+ # @param foreign_key [String]
274
+ # @param :through [Class, nil]
275
+ # @return [Array<String>] Type docstring followed by the description of the method.
276
+ def has_many_description(klass, table_name, foreign_key, through: nil)
277
+ type_docstring = "Array<#{klass}>"
278
+ desc = relation_description(:has_many, klass, table_name, foreign_key, through)
279
+
280
+ [type_docstring, desc]
281
+ end
282
+
283
+ # @param klass [Class]
284
+ # @param table_name [String]
285
+ # @param foreign_key [String]
286
+ # @param :through [Class, nil]
287
+ # @return [Array<String>] Type docstring followed by the description of the method.
288
+ def has_one_description(klass, table_name, foreign_key, through: nil)
289
+ type_docstring = klass
290
+ desc = relation_description(:has_one, klass, table_name, foreign_key, through)
291
+
292
+ [type_docstring, desc]
293
+ end
294
+
295
+ # @param klass [Class]
296
+ # @param table_name [String]
297
+ # @param foreign_key [String]
298
+ # @return [Array<String>] Type docstring followed by the description of the method.
299
+ def belongs_to_description(klass, table_name, foreign_key)
300
+ type_docstring = klass
301
+ desc = relation_description(:belongs_to, klass, table_name, foreign_key)
302
+
303
+ [type_docstring, desc]
304
+ end
305
+
201
306
  # @param attr_type [ActiveModel::Type::Value]
202
307
  # @return [String]
203
308
  def yard_type(attr_type)
@@ -3,7 +3,7 @@
3
3
  module Rails
4
4
  module Annotate
5
5
  module Solargraph
6
- VERSION = '0.2.3'
6
+ VERSION = '0.3.0'
7
7
  end
8
8
  end
9
9
  end
@@ -16,7 +16,7 @@ Gem::Specification.new do |spec|
16
16
 
17
17
  spec.metadata["homepage_uri"] = spec.homepage
18
18
  spec.metadata["source_code_uri"] = "https://gitlab.com/mateuszdrewniak/rails-annotate-solargraph"
19
- # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
19
+ spec.metadata["changelog_uri"] = "https://gitlab.com/mateuszdrewniak/rails-annotate-solargraph/-/raw/main/CHANGELOG.md"
20
20
 
21
21
  # Specify which files should be added to the gem when it is released.
22
22
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-annotate-solargraph
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mateusz Drewniak
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-16 00:00:00.000000000 Z
11
+ date: 2022-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -67,6 +67,7 @@ licenses:
67
67
  metadata:
68
68
  homepage_uri: https://gitlab.com/mateuszdrewniak/rails-annotate-solargraph
69
69
  source_code_uri: https://gitlab.com/mateuszdrewniak/rails-annotate-solargraph
70
+ changelog_uri: https://gitlab.com/mateuszdrewniak/rails-annotate-solargraph/-/raw/main/CHANGELOG.md
70
71
  post_install_message:
71
72
  rdoc_options: []
72
73
  require_paths: