annotate_model 0.1.0 → 0.1.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: 7cbc5209bfbb4f497c00889bc95d34144d8e60e628f2495ffa04e36b14cebccc
4
- data.tar.gz: bd791e489c057c52be7057bee4b4e0d8fdb7cd28fac10de187f980daf125a7f7
3
+ metadata.gz: 9bfe01af59acdb784fe61f48a4bb623e63a27aca48a843b8a66db1bb2329a2af
4
+ data.tar.gz: 9abd685d77a61a462f6ada1af4822ea1e9d77ef656061d6f2eca39bdab355784
5
5
  SHA512:
6
- metadata.gz: 15bd5a3e57f5c48671fad01351acf4b70a20f03ebe4d2d6df70c38b97709f41b11a5060bce9bd9a11daf3f6544429fbda8e9da376de3508c6f1abae9a2d03b05
7
- data.tar.gz: 31c6a6bfdad46c5614f0793afd4e1a02e52b913f9a253ccd6b32081e55476cb58997bd39ac63b586529fa850ce1294e7eb357f158a541b27a65abe91d51f85da
6
+ metadata.gz: dedfd1261bd55bc14d447019478223c699ee7b5922254848772a3a8243e0b0874ba32d065ed02105f5f59e3a4ca7763cf2bc24cdab50b13fc0fe55bbf4dce218
7
+ data.tar.gz: 8a98c76426119678016dda6d058b8026d108cca62013da16fd7a2b7e134ebd1b8d4a6df3910075315c63ed888379c227b23a139d8919f19a0f5be473cfc3d3cd
data/README.md CHANGED
@@ -1,26 +1,46 @@
1
- # AnnotateModel
1
+ # AnnotateModel [![Gem Version](https://badge.fury.io/rb/annotate_model.svg)](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
- Install the gem and add to the application's Gemfile by executing:
21
+ Add this line to the application's Gemfile:
8
22
 
9
- `bundle add annotate_model`
23
+ ```
24
+ group :development do
25
+ ...
26
+ gem "annotate_model"
27
+ ```
10
28
 
11
- If bundler is not being used to manage dependencies, install the gem by executing:
29
+ Or install it globally:
12
30
 
13
- `gem install annotate_model`
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
 
@@ -7,8 +7,11 @@ module AnnotateModel
7
7
  end
8
8
 
9
9
  def annotate?
10
- klass = ModelClassGetter.call(@file)
11
- return false unless klass
10
+ begin
11
+ klass = @file.model_name.constantize
12
+ rescue NameError
13
+ return false
14
+ end
12
15
 
13
16
  annotate_conditions(klass).all?
14
17
  end
@@ -33,7 +33,7 @@ module AnnotateModel
33
33
  def self.annotate_file(file)
34
34
  return :failed unless AnnotationDecider.new(file).annotate?
35
35
 
36
- content = File.read(file)
36
+ content = File.read(file.to_s)
37
37
  return :skipped if content.include?("# == Schema Information")
38
38
 
39
39
  schema_info = fetch_schema_info(file)
@@ -45,13 +45,12 @@ module AnnotateModel
45
45
  #{schema_info}
46
46
  ANNOTATION
47
47
 
48
- File.write(file, annotated_content)
48
+ File.write(file.to_s, annotated_content)
49
49
  :run
50
50
  end
51
51
 
52
52
  def self.fetch_schema_info(file)
53
- model_name = File.basename(file, ".rb").camelize
54
- table_name = model_name.tableize
53
+ table_name = file.model_name.tableize
55
54
 
56
55
  begin
57
56
  columns = ActiveRecord::Base.connection.columns(table_name)
@@ -67,13 +66,13 @@ module AnnotateModel
67
66
 
68
67
  def self.log_results(run_files, skipped_files, failed_files)
69
68
  puts "Annotated files (#{run_files.size}):"
70
- run_files.each { |file| puts " - #{file}" }
69
+ run_files.each { |file| puts " - #{file.relative_path}" }
71
70
 
72
71
  puts "Skipped files (#{skipped_files.size}):"
73
- skipped_files.each { |file| puts " - #{file}" }
72
+ skipped_files.each { |file| puts " - #{file.relative_path}" }
74
73
 
75
74
  puts "Failed files (#{failed_files.size}):"
76
- failed_files.each { |file| puts " - #{file}" }
75
+ failed_files.each { |file| puts " - #{file.relative_path}" }
77
76
  end
78
77
  end
79
78
  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,22 @@
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
+ def model_name
15
+ @file_path.relative_path_from(Rails.root.join('app', 'models')).to_s.sub('.rb', '').camelize
16
+ end
17
+
18
+ def to_s
19
+ @file_path.to_s
20
+ end
21
+ end
22
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AnnotateModel
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: annotate_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taeyang Lee
@@ -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/model_class_getter.rb
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