trains 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 227d3fc953f2317d57846387bf0c358b0a1bfd6f70f89ddb760b70410616270e
4
- data.tar.gz: edb13b82f62463858971853cd62bb21a7f6223df35625c2a88e651c9da279bbe
3
+ metadata.gz: c13daf893e01409b469a93f5f786390e671de242b5ac2f4565f3d5c4bcb23d93
4
+ data.tar.gz: e6b5cb947bcab9841e852895e3a23c4d9966c936ea48e9f5ec5a11deb8f7a7f1
5
5
  SHA512:
6
- metadata.gz: 9550034596435821878df66664809d21e01a0eced82a0f7579bf1f7d84d501d9043b93248527ab52d5c6b77d13d274449132f7f377ac285294361b4472662470
7
- data.tar.gz: ddb3f8f34f5e7940cd98a8a05fae0ecdd600c18bea6a3206d9ef2c4910365f3b673946d8085340cddf4c96f782abe061f8cbfd967a3a4dbb7386a3737a0107ad
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.11'.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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trains
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Syed Faraaz Ahmad
@@ -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