annotate_model 0.1.2 → 1.0.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 +30 -3
- data/lib/annotate_model/annotation_decider.rb +8 -0
- data/lib/annotate_model/annotator.rb +7 -3
- 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: acaa898d77121490f69c9253e67f0da9788299581ca7baca9a91a02e7dde69d8
|
4
|
+
data.tar.gz: d600bb6a190d68a3aa29b6e3159956b3da07d80bb521ba9feaf65b7e70fb875b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8c7757e023d793050a2e4b0f99b0fbca36e3a8f157213c85a1d3b10d81fe751a8876751a7b851af0347bc007b10909301a1b8d3de7927ebab159fad3842fb236
|
7
|
+
data.tar.gz: 7d38492f6fcdfc8ce390c595eb2c6caf398c501d249eb873df835a57342e5fa0fd462a6b4c6087d2f1544b2139c4bf8c7cceee0e413eb792a925233af57ae478
|
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=2
|
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:
|
@@ -12,10 +15,26 @@ Here's an example of the schema information in your model file after annotation:
|
|
12
15
|
# name :string
|
13
16
|
# created_at :datetime not null
|
14
17
|
# updated_at :datetime not null
|
18
|
+
# ==
|
15
19
|
class Product < ApplicationRecord
|
16
20
|
end
|
17
21
|
```
|
18
22
|
|
23
|
+
```ruby
|
24
|
+
# == Schema Information
|
25
|
+
#
|
26
|
+
# id :integer not null
|
27
|
+
# name :string
|
28
|
+
# email :string
|
29
|
+
# created_at :datetime not null
|
30
|
+
# updated_at :datetime not null
|
31
|
+
# ==
|
32
|
+
class Admin::User < ApplicationRecord
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
The annotation block is re-written each time the command runs. The starting line `# == Schema Information` and the ending line `# ==` should never be changed as they are important for the regex match used to remove existing annotations.
|
37
|
+
|
19
38
|
## Installation
|
20
39
|
|
21
40
|
Add this line to the application's Gemfile:
|
@@ -36,11 +55,19 @@ gem install annotate_model
|
|
36
55
|
|
37
56
|
To annotate all models, run:
|
38
57
|
|
39
|
-
`bundle exec annotate_model
|
58
|
+
`bundle exec annotate_model`
|
40
59
|
|
41
60
|
To annotate specific models, run:
|
42
61
|
|
43
|
-
`bundle exec annotate_model
|
62
|
+
`bundle exec annotate_model Product Admin::User`
|
63
|
+
|
64
|
+
## Skipping Annotation
|
65
|
+
|
66
|
+
If you want to skip annotation for a specific model, add the following skip flag to the model file:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
# == annotate_model:skip
|
70
|
+
```
|
44
71
|
|
45
72
|
## Development
|
46
73
|
|
@@ -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),
|
@@ -30,22 +30,26 @@ module AnnotateModel
|
|
30
30
|
def self.annotate_file(file)
|
31
31
|
return :skipped unless AnnotationDecider.new(file).annotate?
|
32
32
|
|
33
|
-
content = File.read(file.to_s)
|
34
|
-
return :skipped if content.include?("# == Schema Information")
|
35
|
-
|
36
33
|
schema_info = fetch_schema_info(file)
|
37
34
|
return :failed unless schema_info
|
38
35
|
|
36
|
+
content = File.read(file.to_s)
|
37
|
+
content = remove_existing_annotation(content)
|
39
38
|
annotated_content = <<~ANNOTATION + content
|
40
39
|
# == Schema Information
|
41
40
|
#
|
42
41
|
#{schema_info}
|
42
|
+
# ==
|
43
43
|
ANNOTATION
|
44
44
|
|
45
45
|
File.write(file.to_s, annotated_content)
|
46
46
|
:run
|
47
47
|
end
|
48
48
|
|
49
|
+
def self.remove_existing_annotation(content)
|
50
|
+
content.sub(/^# == Schema Information\n(#.*\n)*# ==\n/m, '')
|
51
|
+
end
|
52
|
+
|
49
53
|
def self.fetch_schema_info(file)
|
50
54
|
begin
|
51
55
|
columns = ActiveRecord::Base.connection.columns(file.table_name)
|
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: 1.0.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-22 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:
|