annotate_model 0.1.1 → 0.2.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 +4 -4
- data/README.md +26 -3
- data/lib/annotate_model/annotation_decider.rb +8 -0
- data/lib/annotate_model/annotator.rb +8 -15
- data/lib/annotate_model/model_file.rb +18 -0
- data/lib/annotate_model/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 702f03dcd1fc38f6e6d05d4f9c49b0c13f8491629f0c6999c1176ee2e51042a9
|
4
|
+
data.tar.gz: 6b12bfc2604b2fe1947f44bf4f489605c12ab54afd9f6bf3321b69c588ce2a08
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 74b2b1467ac782f82d9c94ad0f8c0a3d82f713161990b38c9f1eec7a3fa23e34f97883c2659822dca343c693ae466a4db0a591ec08cf61f7bd940e4fbe494dc7
|
7
|
+
data.tar.gz: 14542f117a30d48faa47e79df28c971b01be998fffc0db4bd7d40d6f9b2d5be9c5d1d360fa6710382af8baa53e49ae17a5869454b52695b999cdca18060942bf
|
data/README.md
CHANGED
@@ -1,7 +1,10 @@
|
|
1
|
-
# AnnotateModel [![Gem Version]
|
1
|
+
# AnnotateModel [![Gem Version][]][gem-version]
|
2
2
|
|
3
3
|
AnnotateModel is a lightweight gem to annotate Rails models with database schema information.
|
4
4
|
|
5
|
+
[Gem Version]: https://badge.fury.io/rb/annotate_model.svg?v=1
|
6
|
+
[gem-version]: https://rubygems.org/gems/annotate_model
|
7
|
+
|
5
8
|
## Example
|
6
9
|
|
7
10
|
Here's an example of the schema information in your model file after annotation:
|
@@ -16,6 +19,18 @@ class Product < ApplicationRecord
|
|
16
19
|
end
|
17
20
|
```
|
18
21
|
|
22
|
+
```ruby
|
23
|
+
# == Schema Information
|
24
|
+
#
|
25
|
+
# id :integer not null
|
26
|
+
# name :string
|
27
|
+
# email :string
|
28
|
+
# created_at :datetime not null
|
29
|
+
# updated_at :datetime not null
|
30
|
+
class Admin::User < ApplicationRecord
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
19
34
|
## Installation
|
20
35
|
|
21
36
|
Add this line to the application's Gemfile:
|
@@ -36,11 +51,19 @@ gem install annotate_model
|
|
36
51
|
|
37
52
|
To annotate all models, run:
|
38
53
|
|
39
|
-
`bundle exec annotate_model
|
54
|
+
`bundle exec annotate_model`
|
40
55
|
|
41
56
|
To annotate specific models, run:
|
42
57
|
|
43
|
-
`bundle exec annotate_model
|
58
|
+
`bundle exec annotate_model Product Admin::User`
|
59
|
+
|
60
|
+
## Skipping Annotation
|
61
|
+
|
62
|
+
If you want to skip annotation for a specific model, add the following skip flag to the model file:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
# == annotate_model:skip
|
66
|
+
```
|
44
67
|
|
45
68
|
## Development
|
46
69
|
|
@@ -2,11 +2,15 @@
|
|
2
2
|
|
3
3
|
module AnnotateModel
|
4
4
|
class AnnotationDecider
|
5
|
+
SKIP_FLAG = "# == annotate_model:skip"
|
6
|
+
|
5
7
|
def initialize(file)
|
6
8
|
@file = file
|
7
9
|
end
|
8
10
|
|
9
11
|
def annotate?
|
12
|
+
return false if skip_flag_present?
|
13
|
+
|
10
14
|
begin
|
11
15
|
klass = @file.model_name.constantize
|
12
16
|
rescue NameError
|
@@ -18,6 +22,10 @@ module AnnotateModel
|
|
18
22
|
|
19
23
|
private
|
20
24
|
|
25
|
+
def skip_flag_present?
|
26
|
+
File.foreach(@file.to_s).any? { |line| line.include?(SKIP_FLAG) }
|
27
|
+
end
|
28
|
+
|
21
29
|
def annotate_conditions(klass)
|
22
30
|
[
|
23
31
|
klass.is_a?(Class),
|
@@ -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,18 +19,16 @@ 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
33
|
content = File.read(file.to_s)
|
37
34
|
return :skipped if content.include?("# == Schema Information")
|
@@ -50,12 +47,10 @@ module AnnotateModel
|
|
50
47
|
end
|
51
48
|
|
52
49
|
def self.fetch_schema_info(file)
|
53
|
-
table_name = file.model_name.tableize
|
54
|
-
|
55
50
|
begin
|
56
|
-
columns = ActiveRecord::Base.connection.columns(table_name)
|
51
|
+
columns = ActiveRecord::Base.connection.columns(file.table_name)
|
57
52
|
rescue ActiveRecord::StatementInvalid
|
58
|
-
warn "Could not find table '#{table_name}'"
|
53
|
+
warn "Could not find table '#{file.table_name}'"
|
59
54
|
return
|
60
55
|
end
|
61
56
|
|
@@ -64,15 +59,13 @@ module AnnotateModel
|
|
64
59
|
end.join("\n")
|
65
60
|
end
|
66
61
|
|
67
|
-
def self.log_results(run_files,
|
68
|
-
puts "Annotated files
|
62
|
+
def self.log_results(run_files, failed_files)
|
63
|
+
puts "Annotated files:"
|
69
64
|
run_files.each { |file| puts " - #{file.relative_path}" }
|
70
65
|
|
71
|
-
puts "
|
72
|
-
skipped_files.each { |file| puts " - #{file.relative_path}" }
|
73
|
-
|
74
|
-
puts "Failed files (#{failed_files.size}):"
|
66
|
+
puts "Failed files:"
|
75
67
|
failed_files.each { |file| puts " - #{file.relative_path}" }
|
68
|
+
puts "#{run_files.size} runs, #{failed_files.size} failures"
|
76
69
|
end
|
77
70
|
end
|
78
71
|
end
|
@@ -11,10 +11,28 @@ module AnnotateModel
|
|
11
11
|
@file_path.relative_path_from(Rails.root).to_s
|
12
12
|
end
|
13
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".
|
14
21
|
def model_name
|
15
22
|
@file_path.relative_path_from(Rails.root.join('app', 'models')).to_s.sub('.rb', '').camelize
|
16
23
|
end
|
17
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
|
+
|
18
36
|
def to_s
|
19
37
|
@file_path.to_s
|
20
38
|
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.
|
4
|
+
version: 0.2.0
|
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:
|