trains 0.0.10 → 0.0.12

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: 706372a84f876f65aec9c638d6ad2c17dd6255f6dbc942bccbe1f0f81b86e069
4
- data.tar.gz: 431b9033167ed91a657dd00ca806c16c1c3df454f74ec276eb7e79baf8407adf
3
+ metadata.gz: c13daf893e01409b469a93f5f786390e671de242b5ac2f4565f3d5c4bcb23d93
4
+ data.tar.gz: e6b5cb947bcab9841e852895e3a23c4d9966c936ea48e9f5ec5a11deb8f7a7f1
5
5
  SHA512:
6
- metadata.gz: a9b1de71a3735c43afee1f793ad0af94e9d8d7d0fced82023c3927d52e2821cf730b997ae7eeab53816208c11320ed4168c381d3d5fa18d0c1c8c8cb3f008006
7
- data.tar.gz: c3eac1a413ece2f2ff937f642843aefb7188b63cd25c03d6f394a223b63a1ebb8c25c0a05e7d63c6c903d94687b2f59beb2eebed7b196c7f2e2405573a363075
6
+ metadata.gz: 05eebe5b58eff0181d4d9d1c7d6f3cd59a3a70d3c62bd4ac29ca7bdc3ca7f6ca2bdd0227140e1bb2ff1031536d2ebff87fd308d805463492ad89be72e1cc57ff
7
+ data.tar.gz: 0ddd9c546ee1364226336301a2ff7933533b0219c92da788c21889f1de64ac22472a8b93853d1ce70d9467294fb5d62db1544a3a2ddbbb4d0865668f2d816b5c
@@ -44,12 +44,10 @@ module Trains
44
44
 
45
45
  private
46
46
 
47
- # Generate models from either db/schema.rb
48
- # else stitch together migrations to create models
47
+ # Stitch together migrations to create models
49
48
  def generate_models
50
- return get_models if File.exist?(File.join(@dir, 'db', 'schema.rb'))
51
-
52
49
  migrations = get_migrations.flatten.reject(&:nil?)
50
+ migrations = [*migrations, *parse_models.flatten.reject(&:nil?)]
53
51
  Utils::MigrationTailor.stitch(migrations)
54
52
  end
55
53
 
@@ -63,7 +61,7 @@ module Trains
63
61
  .flatten
64
62
  end
65
63
 
66
- def get_models
64
+ def parse_schema
67
65
  result_hash = {}
68
66
  schema_file = [File.join(@dir, 'db', 'schema.rb')]
69
67
  models_results = parse_util(schema_file, Visitor::Schema)
@@ -77,6 +75,15 @@ module Trains
77
75
  result_hash
78
76
  end
79
77
 
78
+ def parse_models
79
+ models = Dir.glob(File.join(@dir, 'app', 'models', '**', '*.rb'))
80
+ model_results = parse_util(models, Visitor::Model)
81
+
82
+ model_results
83
+ .select { |result| result.error.nil? }
84
+ .map(&:data)
85
+ end
86
+
80
87
  def get_helpers; end
81
88
 
82
89
  def get_gemfile; end
@@ -1,3 +1,3 @@
1
1
  module Trains
2
- VERSION = '0.0.10'.freeze
2
+ VERSION = '0.0.12'.freeze
3
3
  end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Trains
4
+ module Visitor
5
+ # Visit ActiveRecord models and add migrations for reference fields
6
+ class Model < Base
7
+ POSSIBLE_ASSOCIATIONS = %i[
8
+ has_many
9
+ belongs_to
10
+ has_one
11
+ has_and_belongs_to_many
12
+ ]
13
+ attr_reader :result
14
+
15
+ # skipcq: RB-LI1087
16
+ def initialize
17
+ @result = []
18
+ end
19
+
20
+ def on_class(node)
21
+ return unless node.parent_class
22
+ return unless node.parent_class.source == 'ApplicationRecord'
23
+
24
+ @model_class = node.identifier.source
25
+ node.each_descendant(:send) { |send_node| parse_model(send_node) }
26
+ end
27
+
28
+ def parse_model(node)
29
+ return unless POSSIBLE_ASSOCIATIONS.include?(node.method_name)
30
+
31
+ @result << DTO::Migration.new(
32
+ @model_class,
33
+ :add_column,
34
+ [DTO::Field.new(node.arguments.first.value, :bigint)],
35
+ nil
36
+ )
37
+
38
+ return unless node.method_name == :has_and_belongs_to_many
39
+
40
+ @result <<
41
+ DTO::Migration.new(
42
+ node.arguments.first.value.to_s.singularize.camelize,
43
+ :add_column,
44
+ [DTO::Field.new(@model_class.tableize.to_sym, :bigint)],
45
+ nil
46
+ )
47
+ end
48
+ end
49
+ end
50
+ end
@@ -76,6 +76,11 @@ module Trains
76
76
  @table_name = table.send_node.first_argument.value
77
77
  @columns = build_columns(table)
78
78
 
79
+ @columns << DTO::Field.new(
80
+ name: :id,
81
+ type: :bigint
82
+ )
83
+
79
84
  @models << DTO::Model.new(
80
85
  name: @table_name.singularize.camelize,
81
86
  fields: @columns,
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trains
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Syed Faraaz Ahmad
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-15 00:00:00.000000000 Z
11
+ date: 2023-06-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -103,6 +103,7 @@ files:
103
103
  - lib/trains/visitor/controller.rb
104
104
  - lib/trains/visitor/helper.rb
105
105
  - lib/trains/visitor/migration.rb
106
+ - lib/trains/visitor/model.rb
106
107
  - lib/trains/visitor/route.rb
107
108
  - lib/trains/visitor/schema.rb
108
109
  - trains.gemspec