rails-annotate-solargraph 0.5.0 → 0.5.1

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: a3f9d275d9de0b0a37b8fae4f7f65ec2121ca25990ccfcc75e246e4c28711531
4
+ data.tar.gz: 1467357d585931d57d440d964a54ead827a964112757ef53a3253c86ac5d3790
5
5
  SHA512:
6
- metadata.gz: 8a2c58e2d0dbb0dd0df66d1f2f04242095081c4159059c8830021cc16f9ece5222dc9fee73392757b88cf1798f5e53256a09fc2291585b8474c36db8777d64f2
7
- data.tar.gz: 6335dab053b82ff232fdcc69c3453eea68ae44432bd6f4032f29ab812072e9053795955556f14578479bd47ebd37a3bfc6ac592254de63801d38369834dcc358
6
+ metadata.gz: d93389956d525b72fff9a08ab11bf48b65733fe3eed85c114fee771be0733b27205521d49512c8d3403a5ab1e6dbae699e40d434a10e642ec6eadc4426b42908
7
+ data.tar.gz: 225b3e7c0937c65940a7020162a0d2c2d91c1e83420a300d4d0740b187e16ca7119f554e61f73d20ce79fcb5e26a36b99051d8d0cfd3a17afd7c3e366e5cb977
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.5.1] - 2022-04-20
4
+
5
+ - `ActiveRecord` scopes are now documented
6
+
3
7
  ## [0.5.0] - 2022-04-19
4
8
 
5
9
  - 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.1)
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,30 @@
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
+ end
29
+ end
30
+ 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,21 @@ module Rails
32
34
  TYPE_MAP.freeze
33
35
 
34
36
  class << self
37
+ # @return [Hash{Class => Array<Rails::Annotate::Solargraph::Model::Scope>}]
38
+ attr_reader :scopes
39
+
40
+ # @param name [Symbol]
41
+ # @param model_class [Class]
42
+ # @param proc_parameters [Array<Symbol>]
43
+ # @param definition [String]
44
+ def add_scope(name, model_class, proc_parameters, definition)
45
+ scope = Scope.new(name: name, model_class: model_class, proc_parameters: proc_parameters, definition: definition)
46
+ @scopes ||= {}
47
+ @scopes[model_class] ||= []
48
+ @scopes[model_class] << scope
49
+ @scopes[model_class].sort_by! { |scope| scope.name }
50
+ end
51
+
35
52
  # @param klass [Class]
36
53
  # @return [String]
37
54
  def annotation_start(klass = nil)
@@ -95,7 +112,6 @@ module Rails
95
112
  end
96
113
 
97
114
  return new_file_content unless write
98
- # debugger
99
115
  return new_file_content if old_content == new_file_content
100
116
 
101
117
  write_file @file_name, new_file_content
@@ -126,12 +142,51 @@ module Rails
126
142
  # class #{@klass} < #{@klass.superclass}
127
143
  DOC
128
144
 
145
+ document_scopes(doc_string)
146
+ document_relations(doc_string)
147
+ document_fields(doc_string)
148
+
149
+ doc_string << <<~DOC.chomp
150
+ # end
151
+ # #{annotation_end}
152
+ DOC
153
+
154
+ # uncomment the generated annotations if they're saved in the schema file
155
+ return doc_string.gsub(/^#\ {3}/, '').gsub(/^#\n/, "\n") if CONFIG.schema_file?
156
+
157
+ doc_string
158
+ end
159
+
160
+ private
161
+
162
+ # @param doc_string [String]
163
+ # @return [void]
164
+ def document_scopes(doc_string)
165
+ self.class.scopes[@klass]&.each do |scope|
166
+ doc_string << <<~DOC
167
+ # # Scope `#{scope.name.inspect}`.
168
+ # #
169
+ # # #{scope.definition}
170
+ # #
171
+ # # @return [Array<#{@klass}>, nil]
172
+ # def self.#{scope.name}(#{scope.proc_parameters.join(', ')}); end
173
+ DOC
174
+ end
175
+ end
176
+
177
+ # @param doc_string [String]
178
+ # @return [void]
179
+ def document_relations(doc_string)
129
180
  @klass.reflections.sort.each do |attr_name, reflection|
130
181
  next document_polymorphic_relation(doc_string, attr_name, reflection) if reflection.polymorphic?
131
182
 
132
183
  document_relation(doc_string, attr_name, reflection)
133
184
  end
185
+ end
134
186
 
187
+ # @param doc_string [String]
188
+ # @return [void]
189
+ def document_fields(doc_string)
135
190
  @klass.attribute_types.each do |name, attr_type|
136
191
  doc_string << <<~DOC
137
192
  # # Database column `#{@klass.table_name}.#{name}`, type: `#{attr_type.type}`.
@@ -142,20 +197,9 @@ module Rails
142
197
  # def #{name}; end
143
198
  DOC
144
199
  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
200
  end
156
201
 
157
- private
158
-
202
+ # @return [String, nil]
159
203
  def parse_clause
160
204
  return if CONFIG.schema_file?
161
205
 
@@ -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.1'
7
7
  end
8
8
  end
9
9
  end
@@ -8,6 +8,7 @@ require_relative "solargraph/configuration"
8
8
  require_relative "solargraph/terminal_colors"
9
9
  require_relative "solargraph/model"
10
10
 
11
+
11
12
  module Rails
12
13
  module Annotate
13
14
  module Solargraph
@@ -75,6 +76,7 @@ module Rails
75
76
  changed_files = []
76
77
  model_files = ::Dir[::File.join(::Rails.root, MODEL_DIR, '**/*.rb')].map { |file| file.sub("#{::Rails.root}/", '') }.to_set
77
78
 
79
+ require_relative "overrides"
78
80
  ::Rails.application.eager_load!
79
81
  model_classes.each do |subclass|
80
82
  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.1
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