rails-annotate-solargraph 0.3.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.vscode/settings.json +5 -0
- data/CHANGELOG.md +16 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +5 -1
- data/README.md +22 -2
- data/lib/generators/annotate/solargraph/install_generator.rb +16 -0
- data/lib/generators/annotate/solargraph/templates/.annotate_solargraph_schema +69 -0
- data/lib/generators/annotate/solargraph/templates/rails_annotate_solargraph.rake +1 -0
- data/lib/rails/annotate/overrides.rb +30 -0
- data/lib/rails/annotate/solargraph/configuration.rb +6 -2
- data/lib/rails/annotate/solargraph/model.rb +124 -40
- data/lib/rails/annotate/solargraph/version.rb +1 -1
- data/lib/rails/annotate/solargraph.rb +19 -0
- data/rails-annotate-solargraph.gemspec +2 -0
- metadata +32 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a3f9d275d9de0b0a37b8fae4f7f65ec2121ca25990ccfcc75e246e4c28711531
|
4
|
+
data.tar.gz: 1467357d585931d57d440d964a54ead827a964112757ef53a3253c86ac5d3790
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d93389956d525b72fff9a08ab11bf48b65733fe3eed85c114fee771be0733b27205521d49512c8d3403a5ab1e6dbae699e40d434a10e642ec6eadc4426b42908
|
7
|
+
data.tar.gz: 225b3e7c0937c65940a7020162a0d2c2d91c1e83420a300d4d0740b187e16ca7119f554e61f73d20ce79fcb5e26a36b99051d8d0cfd3a17afd7c3e366e5cb977
|
data/.vscode/settings.json
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,21 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.5.1] - 2022-04-20
|
4
|
+
|
5
|
+
- `ActiveRecord` scopes are now documented
|
6
|
+
|
7
|
+
## [0.5.0] - 2022-04-19
|
8
|
+
|
9
|
+
- Add some static comments to the schema file to improve general Rails intellisense
|
10
|
+
- Rename schema file from `app/models/annotate_solargraph_schema.rb` to `.annotate-solargraph-schema`
|
11
|
+
- Generate schema file as a regular ruby file
|
12
|
+
- Add `yard` and `solargraph` as dependencies
|
13
|
+
- Add `.solargraph.yml` to the installation generator
|
14
|
+
|
15
|
+
## [0.4.0] - 2022-04-17
|
16
|
+
|
17
|
+
- Annotations get saved to a schema file by default `app/models/annotate_solargraph_schema.rb`
|
18
|
+
|
3
19
|
## [0.3.0] - 2022-04-17
|
4
20
|
|
5
21
|
- `has_many :through` and `has_one :through` relations get documented
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
rails-annotate-solargraph (0.
|
4
|
+
rails-annotate-solargraph (0.5.1)
|
5
5
|
rails (>= 5.0, < 8.0)
|
6
|
+
solargraph
|
7
|
+
yard
|
6
8
|
|
7
9
|
GEM
|
8
10
|
remote: https://rubygems.org/
|
@@ -76,6 +78,7 @@ GEM
|
|
76
78
|
backport (1.2.0)
|
77
79
|
benchmark (0.2.0)
|
78
80
|
builder (3.2.4)
|
81
|
+
byebug (11.1.3)
|
79
82
|
concurrent-ruby (1.1.10)
|
80
83
|
crass (1.0.6)
|
81
84
|
debug (1.5.0)
|
@@ -219,6 +222,7 @@ PLATFORMS
|
|
219
222
|
ruby
|
220
223
|
|
221
224
|
DEPENDENCIES
|
225
|
+
byebug
|
222
226
|
debug
|
223
227
|
git
|
224
228
|
minitest (~> 5.0)
|
data/README.md
CHANGED
@@ -63,11 +63,31 @@ You can change the gem's default configuration like so:
|
|
63
63
|
```ruby
|
64
64
|
# config/initializers/rails_annotate_solargraph.rb
|
65
65
|
|
66
|
-
Rails
|
67
|
-
|
66
|
+
if ::Rails.env.development?
|
67
|
+
::Rails::Annotate::Solargraph.configure do |conf|
|
68
|
+
conf.annotation_position = :top # `:schema_file` by default
|
69
|
+
end
|
68
70
|
end
|
69
71
|
```
|
70
72
|
|
73
|
+
#### annotation_position
|
74
|
+
|
75
|
+
There are a few values for this option:
|
76
|
+
|
77
|
+
- `:schema_file` -- default value, annotations get saved to a special file `app/models/annotate_solargraph_schema.rb`
|
78
|
+
- `:bottom` -- annotations are appended to the model files
|
79
|
+
- `:top` -- annotations are prepended to the model files
|
80
|
+
|
81
|
+
### Update
|
82
|
+
|
83
|
+
To update this gem you should generate the rakefiles once again. Overwrite them.
|
84
|
+
|
85
|
+
```sh
|
86
|
+
$ rails g annotate:solargraph:install
|
87
|
+
$ rake annotate:solargraph:remove
|
88
|
+
$ rake annotate:solargraph:generate
|
89
|
+
```
|
90
|
+
|
71
91
|
## Development
|
72
92
|
|
73
93
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -1,3 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'yaml'
|
4
|
+
require 'set'
|
5
|
+
|
1
6
|
module Annotate
|
2
7
|
module Solargraph
|
3
8
|
module Generators
|
@@ -8,6 +13,17 @@ module Annotate
|
|
8
13
|
# copy rake tasks
|
9
14
|
def copy_tasks
|
10
15
|
template ::Rails::Annotate::Solargraph::RAKEFILE_NAME, ::File.join('lib', 'tasks', ::Rails::Annotate::Solargraph::RAKEFILE_NAME)
|
16
|
+
template ::Rails::Annotate::Solargraph::SCHEMA_FILE_NAME, ::Rails::Annotate::Solargraph::SCHEMA_RAILS_PATH
|
17
|
+
|
18
|
+
solargraph_config_file = ::File.join(::Rails.root, ::Rails::Annotate::Solargraph::SOLARGRAPH_FILE_NAME)
|
19
|
+
system 'solargraph config' unless ::File.exist? solargraph_config_file
|
20
|
+
solargraph_config = ::YAML.load_file solargraph_config_file
|
21
|
+
solargraph_config['include'] = solargraph_config['include'] || []
|
22
|
+
solargraph_config['include'].unshift ::Rails::Annotate::Solargraph::SCHEMA_RAILS_PATH
|
23
|
+
# make sure there are no duplicated entries
|
24
|
+
solargraph_config['include'] = solargraph_config['include'].to_set.to_a
|
25
|
+
|
26
|
+
::File.write(solargraph_config_file, solargraph_config.to_yaml)
|
11
27
|
end
|
12
28
|
end
|
13
29
|
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# This is a dummy file generated by `rails-annotate-solargraph`
|
2
|
+
# to extend solargraph's understanding of your Rails app.
|
3
|
+
# You should probably add it to `.gitignore`
|
4
|
+
|
5
|
+
# Some static comments to fill a few gaps
|
6
|
+
# in Rails comprehension.
|
7
|
+
|
8
|
+
class ActionController::Base
|
9
|
+
include ActionController::MimeResponds
|
10
|
+
include ActionController::Redirecting
|
11
|
+
include ActionController::Cookies
|
12
|
+
include AbstractController::Rendering
|
13
|
+
extend ActiveSupport::Callbacks::ClassMethods
|
14
|
+
extend ActiveSupport::Rescuable::ClassMethods
|
15
|
+
extend AbstractController::Callbacks::ClassMethods
|
16
|
+
extend ActionController::RequestForgeryProtection::ClassMethods
|
17
|
+
end
|
18
|
+
class ActiveRecord::Base
|
19
|
+
extend ActiveRecord::Reflection::ClassMethods
|
20
|
+
extend ActiveModel::SecurePassword::ClassMethods
|
21
|
+
extend ActiveModel::Attributes::ClassMethods
|
22
|
+
include ActiveModel::Attributes
|
23
|
+
include ActiveModel::Dirty
|
24
|
+
extend ActiveRecord::Validations::ClassMethods
|
25
|
+
include ActiveRecord::Validations
|
26
|
+
extend ActiveModel::Validations::ClassMethods
|
27
|
+
include ActiveModel::Validations
|
28
|
+
extend ActiveRecord::Calculations
|
29
|
+
extend ActiveRecord::Batches
|
30
|
+
extend ActiveRecord::QueryMethods
|
31
|
+
extend ActiveRecord::FinderMethods
|
32
|
+
extend ActiveRecord::Associations::ClassMethods
|
33
|
+
extend ActiveRecord::Inheritance::ClassMethods
|
34
|
+
extend ActiveRecord::ModelSchema::ClassMethods
|
35
|
+
extend ActiveRecord::Transactions::ClassMethods
|
36
|
+
extend ActiveRecord::Scoping::Named::ClassMethods
|
37
|
+
include ActiveRecord::Persistence
|
38
|
+
|
39
|
+
<% (ActiveRecord::Callbacks::CALLBACKS rescue []).each do |callback| -%>
|
40
|
+
# Registers a callback to be called <%= callback.to_s.gsub('_', ' ') %>.
|
41
|
+
# See `ActiveRecord::Callbacks` for more information.
|
42
|
+
# @return [void]
|
43
|
+
def self.<%= callback %>(*args, &block); end
|
44
|
+
<% end -%>
|
45
|
+
|
46
|
+
end
|
47
|
+
class Rails
|
48
|
+
# @return [Rails::Application]
|
49
|
+
def self.application; end
|
50
|
+
end
|
51
|
+
class Rails::Application
|
52
|
+
# @return [ActionDispatch::Routing::RouteSet]
|
53
|
+
def routes; end
|
54
|
+
end
|
55
|
+
class ActionDispatch::Routing::Mapper
|
56
|
+
include ActionDispatch::Routing::Mapper::Base
|
57
|
+
include ActionDispatch::Routing::Mapper::HttpHelpers
|
58
|
+
include ActionDispatch::Routing::Mapper::Redirection
|
59
|
+
include ActionDispatch::Routing::Mapper::Scoping
|
60
|
+
include ActionDispatch::Routing::Mapper::Concerns
|
61
|
+
include ActionDispatch::Routing::Mapper::Resources
|
62
|
+
include ActionDispatch::Routing::Mapper::CustomUrls
|
63
|
+
end
|
64
|
+
class ActionDispatch::Routing::RouteSet
|
65
|
+
# @yieldself [ActionDispatch::Routing::Mapper]
|
66
|
+
def draw; end
|
67
|
+
end
|
68
|
+
|
69
|
+
# Dynamically generated documentation
|
@@ -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
|
@@ -9,10 +9,10 @@ module Rails
|
|
9
9
|
# @return [Symbol]
|
10
10
|
attr_reader :annotation_position
|
11
11
|
|
12
|
-
ANNOTATION_POSITIONS = ::Set[:bottom, :top].freeze
|
12
|
+
ANNOTATION_POSITIONS = ::Set[:bottom, :top, :schema_file].freeze
|
13
13
|
|
14
14
|
def initialize
|
15
|
-
@annotation_position = :
|
15
|
+
@annotation_position = :schema_file
|
16
16
|
end
|
17
17
|
|
18
18
|
# @param val [Symbol]
|
@@ -22,6 +22,10 @@ module Rails
|
|
22
22
|
|
23
23
|
@annotation_position = val
|
24
24
|
end
|
25
|
+
|
26
|
+
def schema_file?
|
27
|
+
@annotation_position == :schema_file
|
28
|
+
end
|
25
29
|
end
|
26
30
|
end
|
27
31
|
end
|
@@ -1,44 +1,72 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'fileutils'
|
4
|
+
|
3
5
|
module Rails
|
4
6
|
module Annotate
|
5
7
|
module Solargraph
|
6
8
|
class Model
|
7
9
|
using TerminalColors::Refinement
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
# @return [String]
|
12
|
-
ANNOTATION_END = "%%<RailsAnnotateSolargraph:End>%%\n\n"
|
13
|
-
# @return [Regexp]
|
14
|
-
ANNOTATION_REGEXP = /#{ANNOTATION_START}.*#{ANNOTATION_END}/m.freeze
|
11
|
+
Scope = ::Struct.new(:name, :model_class, :proc_parameters, :definition, keyword_init: true)
|
12
|
+
|
15
13
|
# @return [Regexp]
|
16
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
|
17
15
|
|
16
|
+
# @return [Hash{Symbol => String}]
|
17
|
+
TYPE_MAP = {
|
18
|
+
float: 'BigDecimal',
|
19
|
+
decimal: 'BigDecimal',
|
20
|
+
integer: 'Integer',
|
21
|
+
datetime: 'ActiveSupport::TimeWithZone',
|
22
|
+
date: 'Date',
|
23
|
+
string: 'String',
|
24
|
+
boolean: 'Boolean',
|
25
|
+
text: 'String',
|
26
|
+
jsonb: 'Hash',
|
27
|
+
citext: 'String',
|
28
|
+
json: 'Hash',
|
29
|
+
bigint: 'Integer',
|
30
|
+
uuid: 'String',
|
31
|
+
inet: 'IPAddr'
|
32
|
+
}
|
33
|
+
TYPE_MAP.default = 'Object'
|
34
|
+
TYPE_MAP.freeze
|
35
|
+
|
18
36
|
class << self
|
19
|
-
# @
|
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
|
+
|
52
|
+
# @param klass [Class]
|
20
53
|
# @return [String]
|
21
|
-
def
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
when :boolean
|
38
|
-
'Boolean'
|
39
|
-
else
|
40
|
-
::Object.to_s
|
41
|
-
end
|
54
|
+
def annotation_start(klass = nil)
|
55
|
+
table_name = klass && CONFIG.schema_file? ? ":#{klass.table_name}" : ''
|
56
|
+
"\n# %%<RailsAnnotateSolargraph:Start#{table_name}>%%"
|
57
|
+
end
|
58
|
+
|
59
|
+
# @param klass [Class]
|
60
|
+
# @return [String]
|
61
|
+
def annotation_end(klass = nil)
|
62
|
+
table_name = klass && CONFIG.schema_file? ? ":#{klass.table_name}" : ''
|
63
|
+
"%%<RailsAnnotateSolargraph:End#{table_name}>%%\n\n"
|
64
|
+
end
|
65
|
+
|
66
|
+
# @param klass [Class]
|
67
|
+
# @return [Regexp]
|
68
|
+
def annotation_regexp(klass = nil)
|
69
|
+
/#{annotation_start(klass)}.*#{annotation_end(klass)}/m
|
42
70
|
end
|
43
71
|
end
|
44
72
|
|
@@ -51,7 +79,22 @@ module Rails
|
|
51
79
|
# @param klass [Class]
|
52
80
|
def initialize(klass)
|
53
81
|
@klass = klass
|
54
|
-
@file_name = ::File.join(::Rails.root, MODEL_DIR, "#{klass.to_s.underscore}.rb")
|
82
|
+
@file_name = CONFIG.schema_file? ? SCHEMA_RAILS_PATH : ::File.join(::Rails.root, MODEL_DIR, "#{klass.to_s.underscore}.rb")
|
83
|
+
end
|
84
|
+
|
85
|
+
# @return [String]
|
86
|
+
def annotation_start
|
87
|
+
self.class.annotation_start(@klass)
|
88
|
+
end
|
89
|
+
|
90
|
+
# @return [String]
|
91
|
+
def annotation_end
|
92
|
+
self.class.annotation_end(@klass)
|
93
|
+
end
|
94
|
+
|
95
|
+
# @return [Regexp]
|
96
|
+
def annotation_regexp
|
97
|
+
self.class.annotation_regexp(@klass)
|
55
98
|
end
|
56
99
|
|
57
100
|
# @param :write [Boolean]
|
@@ -69,7 +112,6 @@ module Rails
|
|
69
112
|
end
|
70
113
|
|
71
114
|
return new_file_content unless write
|
72
|
-
# debugger
|
73
115
|
return new_file_content if old_content == new_file_content
|
74
116
|
|
75
117
|
write_file @file_name, new_file_content
|
@@ -79,8 +121,10 @@ module Rails
|
|
79
121
|
# @param :write [Boolean]
|
80
122
|
# @return [Array<String>] Old file content followed by new content.
|
81
123
|
def remove_annotation(write: true)
|
124
|
+
return ['', ''] unless ::File.exist?(@file_name)
|
125
|
+
|
82
126
|
file_content = ::File.read(@file_name)
|
83
|
-
new_file_content = file_content.sub(
|
127
|
+
new_file_content = file_content.sub(annotation_regexp, '')
|
84
128
|
result = [file_content, new_file_content]
|
85
129
|
return result unless write
|
86
130
|
return result if file_content == new_file_content
|
@@ -93,17 +137,56 @@ module Rails
|
|
93
137
|
def annotation
|
94
138
|
doc_string = ::String.new
|
95
139
|
doc_string << <<~DOC
|
96
|
-
#{
|
97
|
-
|
140
|
+
#{annotation_start}
|
141
|
+
##{parse_clause}
|
98
142
|
# class #{@klass} < #{@klass.superclass}
|
99
143
|
DOC
|
100
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)
|
101
180
|
@klass.reflections.sort.each do |attr_name, reflection|
|
102
181
|
next document_polymorphic_relation(doc_string, attr_name, reflection) if reflection.polymorphic?
|
103
182
|
|
104
183
|
document_relation(doc_string, attr_name, reflection)
|
105
184
|
end
|
185
|
+
end
|
106
186
|
|
187
|
+
# @param doc_string [String]
|
188
|
+
# @return [void]
|
189
|
+
def document_fields(doc_string)
|
107
190
|
@klass.attribute_types.each do |name, attr_type|
|
108
191
|
doc_string << <<~DOC
|
109
192
|
# # Database column `#{@klass.table_name}.#{name}`, type: `#{attr_type.type}`.
|
@@ -114,14 +197,14 @@ module Rails
|
|
114
197
|
# def #{name}; end
|
115
198
|
DOC
|
116
199
|
end
|
117
|
-
|
118
|
-
doc_string << <<~DOC.chomp
|
119
|
-
# end
|
120
|
-
# #{ANNOTATION_END}
|
121
|
-
DOC
|
122
200
|
end
|
123
201
|
|
124
|
-
|
202
|
+
# @return [String, nil]
|
203
|
+
def parse_clause
|
204
|
+
return if CONFIG.schema_file?
|
205
|
+
|
206
|
+
" @!parse\n#"
|
207
|
+
end
|
125
208
|
|
126
209
|
# @param file_name [String]
|
127
210
|
# @return [String]
|
@@ -133,6 +216,7 @@ module Rails
|
|
133
216
|
# @param content [String]
|
134
217
|
# @return [void]
|
135
218
|
def write_file(file_name, content)
|
219
|
+
::FileUtils.touch(file_name) unless ::File.exists?(file_name)
|
136
220
|
::File.write(file_name, content)
|
137
221
|
puts "modify".rjust(12).with_styles(:bold, :green) + " #{relative_file_name(file_name)}"
|
138
222
|
end
|
@@ -309,7 +393,7 @@ module Rails
|
|
309
393
|
return attr_type.coder.object_class.to_s if attr_type.respond_to?(:coder) && attr_type.coder.respond_to?(:object_class)
|
310
394
|
return 'Object' if attr_type.respond_to?(:coder) && attr_type.coder.is_a?(::ActiveRecord::Coders::JSON)
|
311
395
|
|
312
|
-
|
396
|
+
TYPE_MAP[attr_type.type]
|
313
397
|
end
|
314
398
|
end
|
315
399
|
end
|
@@ -1,12 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'set'
|
4
|
+
require 'fileutils'
|
4
5
|
|
5
6
|
require_relative "solargraph/version"
|
6
7
|
require_relative "solargraph/configuration"
|
7
8
|
require_relative "solargraph/terminal_colors"
|
8
9
|
require_relative "solargraph/model"
|
9
10
|
|
11
|
+
|
10
12
|
module Rails
|
11
13
|
module Annotate
|
12
14
|
module Solargraph
|
@@ -19,11 +21,20 @@ module Rails
|
|
19
21
|
CONFIG = Configuration.new
|
20
22
|
# @return [Set<Symbol>]
|
21
23
|
VALID_MODIFICATION_METHODS = ::Set[:annotate, :remove_annotation].freeze
|
24
|
+
# @return [String]
|
25
|
+
SCHEMA_CLASS_NAME = 'AnnotateSolargraphSchema'
|
26
|
+
# @return [String]
|
27
|
+
SOLARGRAPH_FILE_NAME = '.solargraph.yml'
|
28
|
+
# @return [String]
|
29
|
+
SCHEMA_FILE_NAME = '.annotate_solargraph_schema'
|
30
|
+
# @return [String]
|
31
|
+
SCHEMA_RAILS_PATH = SCHEMA_FILE_NAME
|
22
32
|
|
23
33
|
class << self
|
24
34
|
# @return [Array<String>] Array of changed files.
|
25
35
|
def generate
|
26
36
|
title 'Generating model schema annotations'
|
37
|
+
create_schema_file
|
27
38
|
modify_models :annotate
|
28
39
|
end
|
29
40
|
|
@@ -49,6 +60,13 @@ module Rails
|
|
49
60
|
|
50
61
|
include TerminalColors
|
51
62
|
|
63
|
+
def create_schema_file
|
64
|
+
schema_file = ::File.join ::Rails.root, SCHEMA_RAILS_PATH
|
65
|
+
return if ::File.exist?(schema_file)
|
66
|
+
|
67
|
+
system 'rails g annotate:solargraph:install'
|
68
|
+
end
|
69
|
+
|
52
70
|
# @param method [Symbol] Name of the method that will be called on every loaded Model
|
53
71
|
# @return [Array<String>] Array of changed files.
|
54
72
|
def modify_models(method)
|
@@ -58,6 +76,7 @@ module Rails
|
|
58
76
|
changed_files = []
|
59
77
|
model_files = ::Dir[::File.join(::Rails.root, MODEL_DIR, '**/*.rb')].map { |file| file.sub("#{::Rails.root}/", '') }.to_set
|
60
78
|
|
79
|
+
require_relative "overrides"
|
61
80
|
::Rails.application.eager_load!
|
62
81
|
model_classes.each do |subclass|
|
63
82
|
subclass_file = ::File.join MODEL_DIR, "#{subclass.to_s.underscore}.rb"
|
@@ -31,6 +31,8 @@ Gem::Specification.new do |spec|
|
|
31
31
|
|
32
32
|
# Uncomment to register a new dependency of your gem
|
33
33
|
spec.add_dependency 'rails', ">= 5.0", '< 8.0'
|
34
|
+
spec.add_dependency 'solargraph'
|
35
|
+
spec.add_dependency 'yard'
|
34
36
|
|
35
37
|
# For more information and examples about making a new gem, check out our
|
36
38
|
# guide at: https://bundler.io/guides/creating_gem.html
|
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.
|
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-
|
11
|
+
date: 2022-04-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -30,6 +30,34 @@ dependencies:
|
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '8.0'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: solargraph
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: yard
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
33
61
|
description: Annotate ActiveRecord models with schema comments formatted in YARD that
|
34
62
|
are compatible with Solargraph.
|
35
63
|
email:
|
@@ -53,7 +81,9 @@ files:
|
|
53
81
|
- bin/console
|
54
82
|
- bin/setup
|
55
83
|
- lib/generators/annotate/solargraph/install_generator.rb
|
84
|
+
- lib/generators/annotate/solargraph/templates/.annotate_solargraph_schema
|
56
85
|
- lib/generators/annotate/solargraph/templates/rails_annotate_solargraph.rake
|
86
|
+
- lib/rails/annotate/overrides.rb
|
57
87
|
- lib/rails/annotate/solargraph.rb
|
58
88
|
- lib/rails/annotate/solargraph/configuration.rb
|
59
89
|
- lib/rails/annotate/solargraph/model.rb
|