rails-annotate-solargraph 0.2.1 → 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: 7a66619cade62b6636a6110b2b6f61fce7c721ed467f1aece9e04276e8a02f3b
4
- data.tar.gz: 0ee08a9c5c439826ce36b7f502a75789e57894329ea4d8f77d1bfcade1972101
3
+ metadata.gz: 4ca6f0073923760a583abb52af713540b2f53cf6e8dd896584e3a76b97444c8b
4
+ data.tar.gz: a07141ca737ee0c60153cabd5e28540d3507ba09401b4cc31d30ea491bc32eb2
5
5
  SHA512:
6
- metadata.gz: 65f3e9b439dacbe375872efe1b1d792c97227818f4673ae45de7638e6ec015f96f69f89f8e8062700700d113441db05adc65f614d9716d325236fcb8a79855b6
7
- data.tar.gz: 8edbd94f4559ed11b5566aa07e5c16a4e9641df3ae40bbec7a6bbeeb5351cdccd31570f448376b103be6b57b8682704f77f0e055bd67ab2ce588998a6037f5a3
6
+ metadata.gz: f3a40df235de87414059053670f344e0e6606b9538ecba40b7fbb2c849c07f8009e5cec055e485033bae5529bce9146d2369d76b8a6ab4288664703bef29da2f
7
+ data.tar.gz: d920e8b59c64f57d22fba128b4db1acffd88b718799e24e2f361d43e99ae6505258faa2665f57fafdee9d9cd6d4dc47231c9e5e7bb99a7e687707c2642e211e5
data/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
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
+
7
+ ## [0.2.3] - 2022-04-16
8
+
9
+ - A nicer fix for the previous problem
10
+
11
+ ## [0.2.2] - 2022-04-16
12
+
13
+ - Inexistent class loading error has been resolved
14
+
3
15
  ## [0.2.1] - 2022-04-16
4
16
 
5
17
  - Rakefile template refactoring and bug fix
@@ -7,6 +19,7 @@
7
19
  ## [0.2.0] - 2022-04-16
8
20
 
9
21
  - Associations get fully documented
22
+ - `has_many`, `has_one` and `belongs_to` relations get documented
10
23
 
11
24
  ## [0.1.1] - 2022-04-15
12
25
 
@@ -16,4 +29,5 @@
16
29
 
17
30
  - Initial release
18
31
  - Automatic generation of annotations after migrations
32
+ - Database fields get documented
19
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.1)
4
+ rails-annotate-solargraph (0.3.0)
5
5
  rails (>= 5.0, < 8.0)
6
6
 
7
7
  GEM
@@ -142,29 +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
- db_description = \
170
+ reflection_klass = reflection_class(reflection)
171
+ type_docstring, db_description = \
151
172
  case reflection
152
173
  when ::ActiveRecord::Reflection::BelongsToReflection
153
- type_docstring = reflection.klass
154
- "`belongs_to` relation with `#{reflection.klass}`. Database column `#{@klass.table_name}.#{reflection.foreign_key}`."
174
+ belongs_to_description(reflection_klass,
175
+ class_table_name(@klass),
176
+ reflection_foreign_key(reflection))
155
177
  when ::ActiveRecord::Reflection::HasOneReflection
156
- type_docstring = reflection.klass
157
- "`has_one` relation with `#{reflection.klass}`. Database column `#{reflection.klass.table_name}.#{reflection.foreign_key}`."
178
+ has_one_description(reflection_klass,
179
+ class_table_name(reflection_klass),
180
+ reflection_foreign_key(reflection))
158
181
  when ::ActiveRecord::Reflection::HasManyReflection
159
- type_docstring = "Array<#{reflection.klass}>"
160
- "`has_many` relation with `#{reflection.klass}`. Database column `#{reflection.klass.table_name}.#{reflection.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, '']
161
189
  end
162
190
 
163
191
  doc_string << <<~DOC
164
- # # #{db_description}
192
+ # ##{db_description}
165
193
  # # @param val [#{type_docstring}, nil]
166
194
  # def #{attr_name}=(val); end
167
- # # #{db_description}
195
+ # ##{db_description}
168
196
  # # @return [#{type_docstring}, nil]
169
197
  # def #{attr_name}; end
170
198
  DOC
@@ -179,7 +207,7 @@ module Rails
179
207
  model_class.reflections[klass_relation_name]&.options&.[](:as)&.to_sym == attr_name.to_sym
180
208
  end
181
209
 
182
- classes_string = classes.join(', ')
210
+ classes_string = classes.empty? ? ::Object.to_s : classes.join(', ')
183
211
  doc_string << <<~DOC
184
212
  # # Polymorphic relation. Database columns `#{@klass.table_name}.#{attr_name}_id` and `#{@klass.table_name}.#{attr_name}_type`.
185
213
  # # @param val [#{classes_string}, nil]
@@ -190,6 +218,91 @@ module Rails
190
218
  DOC
191
219
  end
192
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
+
193
306
  # @param attr_type [ActiveModel::Type::Value]
194
307
  # @return [String]
195
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.1'
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.1
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: