rails_graph 0.2.0 → 0.3.0

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: 9f0e310d51090d130b13e29719da0379cbc968b6a6438f3d412059286d70c1b7
4
- data.tar.gz: a8e0b5adfb146a711cd8377581a653a747d6c78084b92242f99f2962e0c02471
3
+ metadata.gz: 2aa9e5821416a34e7eca9ba18199bea6007190821c50cabb84147db6232b06e2
4
+ data.tar.gz: f54cb43940dba30d7ddc5915cb8669601d0e05b9b5eb4582c18a504859101ed0
5
5
  SHA512:
6
- metadata.gz: c964bc4dfd48309e1602fad6acdd68cccf6cd7276424b863e56b2058c4a10b4ce2a280017f316c160402507bb86bdc3d9665ae0de9c2876b039e2ab858e49c29
7
- data.tar.gz: 04e5f5a29a22c4fda077844de345e8feb6ca7d04b502b1f62209f3c3f310f7eace577906e7e56dd5aa330fd3d4679fcf7d97da08b5597c0a471b26117032c15c
6
+ metadata.gz: 714e4e3d097f8fda7958665d9f4f8ee0ca78978ecab77be2a59ad84fa1921c3204ae93d111072882068ce33ee3966786a3d7473d787e275077d61785f105815a
7
+ data.tar.gz: f3889f7437868743a38971f1808e2b3e9d6ac8445a8567f3e96ebb7722d35a24795830f220bdf7eeb1940bfefe472c1cc33f2539a4bd380358af006ad4c62035
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_graph (0.2.0)
4
+ rails_graph (0.3.0)
5
5
  activerecord (>= 6.0, < 7.1)
6
6
  neo4j-ruby-driver (>= 4.4.0.alpha.8)
7
7
  railties (>= 6.0, < 7.1)
@@ -9,25 +9,25 @@ PATH
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- actionpack (7.0.4.3)
13
- actionview (= 7.0.4.3)
14
- activesupport (= 7.0.4.3)
15
- rack (~> 2.0, >= 2.2.0)
12
+ actionpack (7.0.5)
13
+ actionview (= 7.0.5)
14
+ activesupport (= 7.0.5)
15
+ rack (~> 2.0, >= 2.2.4)
16
16
  rack-test (>= 0.6.3)
17
17
  rails-dom-testing (~> 2.0)
18
18
  rails-html-sanitizer (~> 1.0, >= 1.2.0)
19
- actionview (7.0.4.3)
20
- activesupport (= 7.0.4.3)
19
+ actionview (7.0.5)
20
+ activesupport (= 7.0.5)
21
21
  builder (~> 3.1)
22
22
  erubi (~> 1.4)
23
23
  rails-dom-testing (~> 2.0)
24
24
  rails-html-sanitizer (~> 1.1, >= 1.2.0)
25
- activemodel (7.0.4.3)
26
- activesupport (= 7.0.4.3)
27
- activerecord (7.0.4.3)
28
- activemodel (= 7.0.4.3)
29
- activesupport (= 7.0.4.3)
30
- activesupport (7.0.4.3)
25
+ activemodel (7.0.5)
26
+ activesupport (= 7.0.5)
27
+ activerecord (7.0.5)
28
+ activemodel (= 7.0.5)
29
+ activesupport (= 7.0.5)
30
+ activesupport (7.0.5)
31
31
  concurrent-ruby (~> 1.0, >= 1.0.2)
32
32
  i18n (>= 1.6, < 2)
33
33
  minitest (>= 5.1)
@@ -63,12 +63,12 @@ GEM
63
63
  async-io
64
64
  connection_pool
65
65
  zeitwerk (>= 2.1.10)
66
- nokogiri (1.15.1)
66
+ nokogiri (1.15.2)
67
67
  mini_portile2 (~> 2.8.2)
68
68
  racc (~> 1.4)
69
- nokogiri (1.15.1-arm64-darwin)
69
+ nokogiri (1.15.2-arm64-darwin)
70
70
  racc (~> 1.4)
71
- nokogiri (1.15.1-x86_64-linux)
71
+ nokogiri (1.15.2-x86_64-linux)
72
72
  racc (~> 1.4)
73
73
  parallel (1.22.1)
74
74
  parser (3.1.2.1)
@@ -80,11 +80,12 @@ GEM
80
80
  rails-dom-testing (2.0.3)
81
81
  activesupport (>= 4.2.0)
82
82
  nokogiri (>= 1.6)
83
- rails-html-sanitizer (1.5.0)
84
- loofah (~> 2.19, >= 2.19.1)
85
- railties (7.0.4.3)
86
- actionpack (= 7.0.4.3)
87
- activesupport (= 7.0.4.3)
83
+ rails-html-sanitizer (1.6.0)
84
+ loofah (~> 2.21)
85
+ nokogiri (~> 1.14)
86
+ railties (7.0.5)
87
+ actionpack (= 7.0.5)
88
+ activesupport (= 7.0.5)
88
89
  method_source
89
90
  rake (>= 12.2)
90
91
  thor (~> 1.0)
@@ -21,6 +21,7 @@ require_relative "../graph/relationships/pack_model"
21
21
  require_relative "../helpers/associations"
22
22
  require_relative "../helpers/models"
23
23
 
24
+ require_relative "./builders/associations"
24
25
  require_relative "./builders/models"
25
26
  require_relative "./builders/packs"
26
27
  require_relative "./builders/databases"
@@ -36,7 +37,8 @@ module RailsGraph
36
37
  setup_generic_nodes
37
38
 
38
39
  RailsGraph::Commands::Builders::Databases.enrich(graph: graph) if configuration.databases?
39
- RailsGraph::Commands::Builders::Models.enrich(graph: graph, classes: classes, configuration: configuration)
40
+ RailsGraph::Commands::Builders::Models.enrich(inspector: inspector, graph: graph, classes: classes,
41
+ configuration: configuration)
40
42
  RailsGraph::Commands::Builders::Packs.enrich(graph: graph) if configuration.include_packwerk?
41
43
 
42
44
  graph
@@ -44,12 +46,13 @@ module RailsGraph
44
46
 
45
47
  private
46
48
 
47
- attr_reader :configuration, :classes, :graph
49
+ attr_reader :configuration, :classes, :graph, :inspector
48
50
 
49
51
  def initialize(configuration:)
50
52
  @configuration = configuration
51
53
  @classes = ActiveRecord::Base.descendants + configuration.include_classes
52
54
  @graph = RailsGraph::Graph::Graph.new
55
+ @inspector = RailsGraph::Inspector.new
53
56
  end
54
57
 
55
58
  def setup_generic_nodes
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsGraph
4
+ module Commands
5
+ module Builders
6
+ class Associations
7
+ attr_reader :graph, :inspector
8
+
9
+ def self.enrich(...)
10
+ new(...).enrich
11
+ end
12
+
13
+ def enrich
14
+ build_associations_relationships
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :classes
20
+
21
+ def initialize(inspector:, graph:, classes:)
22
+ @inspector = inspector
23
+ @graph = graph
24
+ @classes = classes
25
+ end
26
+
27
+ def build_associations_relationships
28
+ classes.each do |model|
29
+ model.reflect_on_all_associations.each do |association|
30
+ association.check_validity!
31
+
32
+ source_node, target_node = fetch_association_nodes(graph, association)
33
+ next report_invalid_class(model) if source_node.nil? || target_node.nil?
34
+
35
+ add_relationship(association, source_node, target_node)
36
+ rescue ActiveRecord::ActiveRecordError => _e
37
+ report_invalid_association(association)
38
+ end
39
+ end
40
+ end
41
+
42
+ def fetch_association_nodes(graph, association)
43
+ source_node = RailsGraph::Helpers::Associations.source_node(graph, association)
44
+ target_node = RailsGraph::Helpers::Associations.target_node(graph, association)
45
+ [source_node, target_node]
46
+ end
47
+
48
+ def add_relationship(association, source_node, target_node)
49
+ relationship = RailsGraph::Graph::Relationships::Association.new(association, source_node, target_node)
50
+ graph.add_relationship(relationship)
51
+ end
52
+
53
+ def report_invalid_association(association)
54
+ inspector.add_association(association)
55
+ end
56
+
57
+ def report_invalid_class(klass)
58
+ inspector.add_class(klass)
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -4,25 +4,29 @@ module RailsGraph
4
4
  module Commands
5
5
  module Builders
6
6
  class Models
7
- def self.enrich(graph:, classes:, configuration:)
8
- new(graph: graph, classes: classes, configuration: configuration).enrich
7
+ attr_reader :graph, :inspector
9
8
 
10
- graph
9
+ def self.enrich(...)
10
+ new(...).enrich
11
11
  end
12
12
 
13
13
  def enrich
14
14
  build_model_nodes
15
- build_associations_relationships
15
+ build_associations
16
16
  build_column_nodes if configuration.columns?
17
17
  build_model_table_relationships if configuration.databases?
18
18
  build_inheritance_relationships if configuration.inheritance?
19
+
20
+ inspector.log
21
+ graph
19
22
  end
20
23
 
21
24
  private
22
25
 
23
- attr_reader :graph, :classes, :configuration
26
+ attr_reader :classes, :configuration
24
27
 
25
- def initialize(graph:, classes:, configuration:)
28
+ def initialize(inspector:, graph:, classes:, configuration:)
29
+ @inspector = inspector
26
30
  @graph = graph
27
31
  @configuration = configuration
28
32
  @classes = classes
@@ -41,16 +45,8 @@ module RailsGraph
41
45
  end
42
46
  end
43
47
 
44
- def build_associations_relationships
45
- classes.each do |model|
46
- model.reflect_on_all_associations.each do |association|
47
- source_node = RailsGraph::Helpers::Associations.source_node(graph, association)
48
- target_node = RailsGraph::Helpers::Associations.target_node(graph, association)
49
-
50
- relationship = RailsGraph::Graph::Relationships::Association.new(association, source_node, target_node)
51
- graph.add_relationship(relationship)
52
- end
53
- end
48
+ def build_associations
49
+ Builders::Associations.enrich(inspector: inspector, graph: graph, classes: classes)
54
50
  end
55
51
 
56
52
  def build_column_nodes
@@ -91,6 +87,7 @@ module RailsGraph
91
87
 
92
88
  identifier = RailsGraph::Helpers::Models.identifier(model)
93
89
  node = graph.node(identifier)
90
+
94
91
  relationship = build_represents_table_relationship(node, table_node)
95
92
  graph.add_relationship(relationship)
96
93
  end
@@ -6,6 +6,8 @@ module RailsGraph
6
6
  module_function
7
7
 
8
8
  def identifier(model)
9
+ return unless model
10
+
9
11
  model.to_s.delete_prefix("::")
10
12
  end
11
13
  end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+
5
+ module RailsGraph
6
+ class Inspector
7
+ def initialize
8
+ @classes = Set.new
9
+ @associations = Set.new
10
+ end
11
+
12
+ def add_class(klass)
13
+ @classes.add(klass)
14
+ end
15
+
16
+ def add_association(association)
17
+ @associations.add(association)
18
+ end
19
+
20
+ def log
21
+ @classes.each do |klass|
22
+ puts "[WARN][RailsGraph]: Invalid class configuration: #{klass}"
23
+ end
24
+
25
+ @associations.each do |association|
26
+ puts "[WARN][RailsGraph]: #{association.name} association defined " \
27
+ "under #{association.active_record.name} class has invalid configurations!"
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsGraph
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/rails_graph.rb CHANGED
@@ -7,6 +7,7 @@ require "rails_graph/railtie"
7
7
 
8
8
  require "rails_graph/commands/build_graph"
9
9
  require "rails_graph/commands/export_graph"
10
+ require "rails_graph/inspector"
10
11
 
11
12
  module RailsGraph
12
13
  module_function
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_graph
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ahmad Elassuty
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-22 00:00:00.000000000 Z
11
+ date: 2023-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -85,6 +85,7 @@ files:
85
85
  - graph.svg
86
86
  - lib/rails_graph.rb
87
87
  - lib/rails_graph/commands/build_graph.rb
88
+ - lib/rails_graph/commands/builders/associations.rb
88
89
  - lib/rails_graph/commands/builders/databases.rb
89
90
  - lib/rails_graph/commands/builders/models.rb
90
91
  - lib/rails_graph/commands/builders/packs.rb
@@ -114,6 +115,7 @@ files:
114
115
  - lib/rails_graph/helpers/associations.rb
115
116
  - lib/rails_graph/helpers/models.rb
116
117
  - lib/rails_graph/helpers/options_parser.rb
118
+ - lib/rails_graph/inspector.rb
117
119
  - lib/rails_graph/railtie.rb
118
120
  - lib/rails_graph/tasks/rails_graph.rake
119
121
  - lib/rails_graph/version.rb