annotate_model 0.1.0 → 0.1.2
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 +4 -4
- data/README.md +27 -7
- data/lib/annotate_model/annotation_decider.rb +5 -2
- data/lib/annotate_model/annotator.rb +12 -20
- data/lib/annotate_model/finder.rb +5 -2
- data/lib/annotate_model/model_file.rb +40 -0
- data/lib/annotate_model/version.rb +1 -1
- metadata +3 -3
- data/lib/annotate_model/model_class_getter.rb +0 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f530a132985bc423c3f6a32005be46df58acddc550bdd68dce0658010407d4e7
|
4
|
+
data.tar.gz: d57060bbee598d5914359717d716a80ff3b60463993e62fb15ee66d3843cd723
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a0f926c606fbfdf0346a9e7e5f2161cbc1315cddd6c03ba3a2b2b6d2e5962564bd9a7cffb905b9e3a25c56a65bdc35dd45e0442687a481f3e950d861792fd1d
|
7
|
+
data.tar.gz: 44a4d4409f9605ce19972c9a111cded649ccdc7c0c6b0c23c8faf66a7e22d9ad79e6e1fcb1cfcacdd7934d22075046d32aee194746ffaeb18ce5c1790eca951b
|
data/README.md
CHANGED
@@ -1,26 +1,46 @@
|
|
1
|
-
# AnnotateModel
|
1
|
+
# AnnotateModel [](https://badge.fury.io/rb/annotate_model)
|
2
2
|
|
3
3
|
AnnotateModel is a lightweight gem to annotate Rails models with database schema information.
|
4
4
|
|
5
|
+
## Example
|
6
|
+
|
7
|
+
Here's an example of the schema information in your model file after annotation:
|
8
|
+
```ruby
|
9
|
+
# == Schema Information
|
10
|
+
#
|
11
|
+
# id :integer not null
|
12
|
+
# name :string
|
13
|
+
# created_at :datetime not null
|
14
|
+
# updated_at :datetime not null
|
15
|
+
class Product < ApplicationRecord
|
16
|
+
end
|
17
|
+
```
|
18
|
+
|
5
19
|
## Installation
|
6
20
|
|
7
|
-
|
21
|
+
Add this line to the application's Gemfile:
|
8
22
|
|
9
|
-
|
23
|
+
```
|
24
|
+
group :development do
|
25
|
+
...
|
26
|
+
gem "annotate_model"
|
27
|
+
```
|
10
28
|
|
11
|
-
|
29
|
+
Or install it globally:
|
12
30
|
|
13
|
-
|
31
|
+
```sh
|
32
|
+
gem install annotate_model
|
33
|
+
```
|
14
34
|
|
15
35
|
## Usage
|
16
36
|
|
17
37
|
To annotate all models, run:
|
18
38
|
|
19
|
-
`annotate_model --all`
|
39
|
+
`bundle exec annotate_model --all`
|
20
40
|
|
21
41
|
To annotate specific models, run:
|
22
42
|
|
23
|
-
`annotate_model [model_name1] [model_name2] ...`
|
43
|
+
`bundle exec annotate_model [model_name1] [model_name2] ...`
|
24
44
|
|
25
45
|
## Development
|
26
46
|
|
@@ -12,7 +12,6 @@ module AnnotateModel
|
|
12
12
|
|
13
13
|
def self.process_files(files)
|
14
14
|
run_files = []
|
15
|
-
skipped_files = []
|
16
15
|
failed_files = []
|
17
16
|
|
18
17
|
files.each do |file|
|
@@ -20,20 +19,18 @@ module AnnotateModel
|
|
20
19
|
case result
|
21
20
|
when :run
|
22
21
|
run_files << file
|
23
|
-
when :skipped
|
24
|
-
skipped_files << file
|
25
22
|
when :failed
|
26
23
|
failed_files << file
|
27
24
|
end
|
28
25
|
end
|
29
26
|
|
30
|
-
log_results(run_files,
|
27
|
+
log_results(run_files, failed_files)
|
31
28
|
end
|
32
29
|
|
33
30
|
def self.annotate_file(file)
|
34
|
-
return :
|
31
|
+
return :skipped unless AnnotationDecider.new(file).annotate?
|
35
32
|
|
36
|
-
content = File.read(file)
|
33
|
+
content = File.read(file.to_s)
|
37
34
|
return :skipped if content.include?("# == Schema Information")
|
38
35
|
|
39
36
|
schema_info = fetch_schema_info(file)
|
@@ -45,18 +42,15 @@ module AnnotateModel
|
|
45
42
|
#{schema_info}
|
46
43
|
ANNOTATION
|
47
44
|
|
48
|
-
File.write(file, annotated_content)
|
45
|
+
File.write(file.to_s, annotated_content)
|
49
46
|
:run
|
50
47
|
end
|
51
48
|
|
52
49
|
def self.fetch_schema_info(file)
|
53
|
-
model_name = File.basename(file, ".rb").camelize
|
54
|
-
table_name = model_name.tableize
|
55
|
-
|
56
50
|
begin
|
57
|
-
columns = ActiveRecord::Base.connection.columns(table_name)
|
51
|
+
columns = ActiveRecord::Base.connection.columns(file.table_name)
|
58
52
|
rescue ActiveRecord::StatementInvalid
|
59
|
-
warn "Could not find table '#{table_name}'"
|
53
|
+
warn "Could not find table '#{file.table_name}'"
|
60
54
|
return
|
61
55
|
end
|
62
56
|
|
@@ -65,15 +59,13 @@ module AnnotateModel
|
|
65
59
|
end.join("\n")
|
66
60
|
end
|
67
61
|
|
68
|
-
def self.log_results(run_files,
|
69
|
-
puts "Annotated files
|
70
|
-
run_files.each { |file| puts " - #{file}" }
|
71
|
-
|
72
|
-
puts "Skipped files (#{skipped_files.size}):"
|
73
|
-
skipped_files.each { |file| puts " - #{file}" }
|
62
|
+
def self.log_results(run_files, failed_files)
|
63
|
+
puts "Annotated files:"
|
64
|
+
run_files.each { |file| puts " - #{file.relative_path}" }
|
74
65
|
|
75
|
-
puts "Failed files
|
76
|
-
failed_files.each { |file| puts " - #{file}" }
|
66
|
+
puts "Failed files:"
|
67
|
+
failed_files.each { |file| puts " - #{file.relative_path}" }
|
68
|
+
puts "#{run_files.size} runs, #{failed_files.size} failures"
|
77
69
|
end
|
78
70
|
end
|
79
71
|
end
|
@@ -7,12 +7,15 @@ module AnnotateModel
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def self.all_model_files
|
10
|
-
Dir.glob(Rails.root.join("app", "models", "**", "*.rb"))
|
10
|
+
files = Dir.glob(Rails.root.join("app", "models", "**", "*.rb")).reject { |file_path| file_path.include?('application_record.rb') }
|
11
|
+
files.map do |file|
|
12
|
+
ModelFile.new(Pathname.new(file))
|
13
|
+
end
|
11
14
|
end
|
12
15
|
|
13
16
|
def self.find_model_file(model_name)
|
14
17
|
model_path = Rails.root.join("app", "models", "#{model_name.underscore}.rb")
|
15
|
-
model_path if File.exist?(model_path)
|
18
|
+
ModelFile.new(model_path) if File.exist?(model_path)
|
16
19
|
end
|
17
20
|
|
18
21
|
def self.find_model_file!(model_name)
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module AnnotateModel
|
2
|
+
class ModelFile
|
3
|
+
# Initializes a new ModelFile instance.
|
4
|
+
#
|
5
|
+
# @param file_path [Pathname] the file path of the model file
|
6
|
+
def initialize(file_path)
|
7
|
+
@file_path = file_path
|
8
|
+
end
|
9
|
+
|
10
|
+
def relative_path
|
11
|
+
@file_path.relative_path_from(Rails.root).to_s
|
12
|
+
end
|
13
|
+
|
14
|
+
# Returns the model name corresponding to the model file path.
|
15
|
+
#
|
16
|
+
# @return [String] the model name corresponding to the model file path.
|
17
|
+
#
|
18
|
+
# @example
|
19
|
+
# For a file path "app/models/admin/user.rb", it returns "Admin::User".
|
20
|
+
# For a file path "app/models/product.rb", it returns "Product".
|
21
|
+
def model_name
|
22
|
+
@file_path.relative_path_from(Rails.root.join('app', 'models')).to_s.sub('.rb', '').camelize
|
23
|
+
end
|
24
|
+
|
25
|
+
# Returns the table name corresponding to the model file path.
|
26
|
+
#
|
27
|
+
# @return [String] the table name corresponding to the model file path.
|
28
|
+
#
|
29
|
+
# @example
|
30
|
+
# For a file path "app/models/admin/user.rb", it returns "admin_users".
|
31
|
+
# For a file path "app/models/product.rb", it returns "products".
|
32
|
+
def table_name
|
33
|
+
@file_path.relative_path_from(Rails.root.join('app', 'models')).to_s.sub('.rb', '').classify.tableize.sub('/', '_')
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_s
|
37
|
+
@file_path.to_s
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: annotate_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Taeyang Lee
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-01-
|
11
|
+
date: 2025-01-21 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A lightweight gem to annotate Rails models based on the database schema.
|
14
14
|
email:
|
@@ -26,7 +26,7 @@ files:
|
|
26
26
|
- lib/annotate_model/annotator.rb
|
27
27
|
- lib/annotate_model/cli.rb
|
28
28
|
- lib/annotate_model/finder.rb
|
29
|
-
- lib/annotate_model/
|
29
|
+
- lib/annotate_model/model_file.rb
|
30
30
|
- lib/annotate_model/model_file_not_found_error.rb
|
31
31
|
- lib/annotate_model/version.rb
|
32
32
|
homepage: https://github.com/taeyang91/annotate_model
|
@@ -1,23 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module AnnotateModel
|
4
|
-
class ModelClassGetter
|
5
|
-
class << self
|
6
|
-
def call(file)
|
7
|
-
model_name = file_to_model_name(file)
|
8
|
-
begin
|
9
|
-
model_name.constantize
|
10
|
-
rescue NameError
|
11
|
-
nil
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
private
|
16
|
-
|
17
|
-
def file_to_model_name(file)
|
18
|
-
relative_path = file.sub(Rails.root.join('app', 'models').to_s + '/', '')
|
19
|
-
relative_path.to_s.sub('.rb', '').camelize
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|