rails-annotate-solargraph 0.5.0 → 0.5.3

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: 641aad34ff5a1a7f98a1eca52baf74c311308b2e9e062cf8c5ac3c0186d1f1ec
4
- data.tar.gz: 70657a49775862f8b50dc2e04f7b489b04f124cbfb4893f777f5a11f69b0ba51
3
+ metadata.gz: f47a9a65cb37174ca12847169325afbecef1182e7d34bf555accf5551fbb7f1c
4
+ data.tar.gz: 97d1aae264b49adc45222847f6d2f25278abacec90134226cc7c023e205df6d5
5
5
  SHA512:
6
- metadata.gz: 8a2c58e2d0dbb0dd0df66d1f2f04242095081c4159059c8830021cc16f9ece5222dc9fee73392757b88cf1798f5e53256a09fc2291585b8474c36db8777d64f2
7
- data.tar.gz: 6335dab053b82ff232fdcc69c3453eea68ae44432bd6f4032f29ab812072e9053795955556f14578479bd47ebd37a3bfc6ac592254de63801d38369834dcc358
6
+ metadata.gz: fa14e6c6068da07e1a4df3c2e41c021882ee65178fe124f2a729001964624900aa0578173870fac7bfac26bb474e530261a561a9082e267dfc8d4a48c3a3adc8
7
+ data.tar.gz: 84a6120026334a68f175c8fba73ddee4720d9d6913b52ef36cfc797409543a444419000d3a687843ee57cd1ad264523f761b2988984a74137079b67f413bb396
data/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.5.3] - 2022-04-20
4
+
5
+ - fix a minor bug which in some rails apps prevented scopes from being documented
6
+
7
+ ## [0.5.2] - 2022-04-20
8
+
9
+ - proxy method overrides get loaded during app initialization
10
+
11
+ ## [0.5.1] - 2022-04-20
12
+
13
+ - `ActiveRecord` scopes are now documented
14
+
3
15
  ## [0.5.0] - 2022-04-19
4
16
 
5
17
  - Add some static comments to the schema file to improve general Rails intellisense
data/Gemfile CHANGED
@@ -5,6 +5,7 @@ source "https://rubygems.org"
5
5
  # Specify your gem's dependencies in rails-annotate-solargraph.gemspec
6
6
  gemspec
7
7
 
8
+ gem 'byebug'
8
9
  gem 'debug'
9
10
  gem 'git'
10
11
  gem "minitest", "~> 5.0"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails-annotate-solargraph (0.5.0)
4
+ rails-annotate-solargraph (0.5.3)
5
5
  rails (>= 5.0, < 8.0)
6
6
  solargraph
7
7
  yard
@@ -78,6 +78,7 @@ GEM
78
78
  backport (1.2.0)
79
79
  benchmark (0.2.0)
80
80
  builder (3.2.4)
81
+ byebug (11.1.3)
81
82
  concurrent-ruby (1.1.10)
82
83
  crass (1.0.6)
83
84
  debug (1.5.0)
@@ -221,6 +222,7 @@ PLATFORMS
221
222
  ruby
222
223
 
223
224
  DEPENDENCIES
225
+ byebug
224
226
  debug
225
227
  git
226
228
  minitest (~> 5.0)
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ActiveRecord::Base
4
+ class << self
5
+ alias orig_scope scope
6
+
7
+ def scope(*args, **kwargs, &block)
8
+ file_path, scope_line_number = caller.first.split(':')
9
+ scope_line_number = scope_line_number.to_i
10
+ scope_name = args.first
11
+ scope_proc = args[1]
12
+ proc_parameters = scope_proc.respond_to?(:parameters) ? scope_proc.parameters.map(&:last) : []
13
+ scope_line = nil
14
+ scope_model_class = self
15
+
16
+ ::File.open(file_path) do |file|
17
+ file.each_line.with_index(1) do |line, current_line_number|
18
+ next unless current_line_number == scope_line_number
19
+
20
+ scope_line = line.strip
21
+ break
22
+ end
23
+ end
24
+
25
+ ::Rails::Annotate::Solargraph::Model.add_scope(scope_name.to_sym, scope_model_class, proc_parameters, scope_line)
26
+
27
+ orig_scope(*args, **kwargs, &block)
28
+ rescue
29
+ orig_scope(*args, **kwargs, &block)
30
+ end
31
+ end
32
+ end
@@ -8,6 +8,8 @@ module Rails
8
8
  class Model
9
9
  using TerminalColors::Refinement
10
10
 
11
+ Scope = ::Struct.new(:name, :model_class, :proc_parameters, :definition, keyword_init: true)
12
+
11
13
  # @return [Regexp]
12
14
  MAGIC_COMMENT_REGEXP = /(^#\s*encoding:.*(?:\n|r\n))|(^# coding:.*(?:\n|\r\n))|(^# -\*- coding:.*(?:\n|\r\n))|(^# -\*- encoding\s?:.*(?:\n|\r\n))|(^#\s*frozen_string_literal:.+(?:\n|\r\n))|(^# -\*- frozen_string_literal\s*:.+-\*-(?:\n|\r\n))/.freeze
13
15
 
@@ -32,6 +34,24 @@ module Rails
32
34
  TYPE_MAP.freeze
33
35
 
34
36
  class << self
37
+ # @return [Hash{Class => Array<Rails::Annotate::Solargraph::Model::Scope>}]
38
+ def scopes
39
+ @scopes ||= {}
40
+ @scopes
41
+ end
42
+
43
+ # @param name [Symbol]
44
+ # @param model_class [Class]
45
+ # @param proc_parameters [Array<Symbol>]
46
+ # @param definition [String]
47
+ def add_scope(name, model_class, proc_parameters, definition)
48
+ scope = Scope.new(name: name, model_class: model_class, proc_parameters: proc_parameters, definition: definition)
49
+ @scopes ||= {}
50
+ @scopes[model_class] ||= []
51
+ @scopes[model_class] << scope
52
+ @scopes[model_class].sort_by! { |scope| scope.name }
53
+ end
54
+
35
55
  # @param klass [Class]
36
56
  # @return [String]
37
57
  def annotation_start(klass = nil)
@@ -95,7 +115,6 @@ module Rails
95
115
  end
96
116
 
97
117
  return new_file_content unless write
98
- # debugger
99
118
  return new_file_content if old_content == new_file_content
100
119
 
101
120
  write_file @file_name, new_file_content
@@ -126,12 +145,51 @@ module Rails
126
145
  # class #{@klass} < #{@klass.superclass}
127
146
  DOC
128
147
 
148
+ document_scopes(doc_string)
149
+ document_relations(doc_string)
150
+ document_fields(doc_string)
151
+
152
+ doc_string << <<~DOC.chomp
153
+ # end
154
+ # #{annotation_end}
155
+ DOC
156
+
157
+ # uncomment the generated annotations if they're saved in the schema file
158
+ return doc_string.gsub(/^#\ {3}/, '').gsub(/^#\n/, "\n") if CONFIG.schema_file?
159
+
160
+ doc_string
161
+ end
162
+
163
+ private
164
+
165
+ # @param doc_string [String]
166
+ # @return [void]
167
+ def document_scopes(doc_string)
168
+ self.class.scopes[@klass]&.each do |scope|
169
+ doc_string << <<~DOC
170
+ # # Scope `#{scope.name.inspect}`.
171
+ # #
172
+ # # #{scope.definition}
173
+ # #
174
+ # # @return [Array<#{@klass}>, nil]
175
+ # def self.#{scope.name}(#{scope.proc_parameters.join(', ')}); end
176
+ DOC
177
+ end
178
+ end
179
+
180
+ # @param doc_string [String]
181
+ # @return [void]
182
+ def document_relations(doc_string)
129
183
  @klass.reflections.sort.each do |attr_name, reflection|
130
184
  next document_polymorphic_relation(doc_string, attr_name, reflection) if reflection.polymorphic?
131
185
 
132
186
  document_relation(doc_string, attr_name, reflection)
133
187
  end
188
+ end
134
189
 
190
+ # @param doc_string [String]
191
+ # @return [void]
192
+ def document_fields(doc_string)
135
193
  @klass.attribute_types.each do |name, attr_type|
136
194
  doc_string << <<~DOC
137
195
  # # Database column `#{@klass.table_name}.#{name}`, type: `#{attr_type.type}`.
@@ -142,20 +200,9 @@ module Rails
142
200
  # def #{name}; end
143
201
  DOC
144
202
  end
145
-
146
- doc_string << <<~DOC.chomp
147
- # end
148
- # #{annotation_end}
149
- DOC
150
-
151
- # uncomment the generated annotations if they're saved in the schema file
152
- return doc_string.gsub(/^#\ {3}/, '').gsub(/^#\n/, "\n") if CONFIG.schema_file?
153
-
154
- doc_string
155
203
  end
156
204
 
157
- private
158
-
205
+ # @return [String, nil]
159
206
  def parse_clause
160
207
  return if CONFIG.schema_file?
161
208
 
@@ -3,7 +3,7 @@
3
3
  module Rails
4
4
  module Annotate
5
5
  module Solargraph
6
- VERSION = '0.5.0'
6
+ VERSION = '0.5.3'
7
7
  end
8
8
  end
9
9
  end
@@ -8,6 +8,12 @@ require_relative "solargraph/configuration"
8
8
  require_relative "solargraph/terminal_colors"
9
9
  require_relative "solargraph/model"
10
10
 
11
+ begin
12
+ require_relative "overrides"
13
+ rescue
14
+ nil
15
+ end
16
+
11
17
  module Rails
12
18
  module Annotate
13
19
  module Solargraph
@@ -75,6 +81,7 @@ module Rails
75
81
  changed_files = []
76
82
  model_files = ::Dir[::File.join(::Rails.root, MODEL_DIR, '**/*.rb')].map { |file| file.sub("#{::Rails.root}/", '') }.to_set
77
83
 
84
+ require_relative "overrides"
78
85
  ::Rails.application.eager_load!
79
86
  model_classes.each do |subclass|
80
87
  subclass_file = ::File.join MODEL_DIR, "#{subclass.to_s.underscore}.rb"
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.5.0
4
+ version: 0.5.3
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-19 00:00:00.000000000 Z
11
+ date: 2022-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -83,6 +83,7 @@ files:
83
83
  - lib/generators/annotate/solargraph/install_generator.rb
84
84
  - lib/generators/annotate/solargraph/templates/.annotate_solargraph_schema
85
85
  - lib/generators/annotate/solargraph/templates/rails_annotate_solargraph.rake
86
+ - lib/rails/annotate/overrides.rb
86
87
  - lib/rails/annotate/solargraph.rb
87
88
  - lib/rails/annotate/solargraph/configuration.rb
88
89
  - lib/rails/annotate/solargraph/model.rb