bullet_train-super_scaffolding 1.3.23 → 1.3.25

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: bbcde213b30f69abe18110f85a59c1e5b19d7f4bdfcb31cf587fbe28918c382e
4
- data.tar.gz: c64d0d9b5e650e198fc25694ad24e53d914e3e3fee3954d87bcbe3e384989def
3
+ metadata.gz: 4edc8d1768ad95b11223504d7208df2c04d5944b901686e707b55e95b63d5d70
4
+ data.tar.gz: 33895a551f0de25018b16d5eb7936abb2106e4faa8881c154e446b24eed10818
5
5
  SHA512:
6
- metadata.gz: 981b5c308a58f8ad114892237c98d9ef1c4e93e59126ddc55eba47b4cc14aaef974f41a46f3932d64736a09dd3c36d499b31160a9c5c023901a79f13e4aafe72
7
- data.tar.gz: e66e54444da188afcfbc8756d16bfb17e9f0178056cd57adf85dabeb5699b22489f9a9ef6f9a02c60086e17bcdd29889fc0d9c2f4ab91beeaac0e6d716bb5e0d
6
+ metadata.gz: faa965190ccca41fcc80460ed3b563d75957d502b9cee98cb938f0818bc3b0db3baf9fc8406b72a57393ca4663fd5d218472909767fc8cb0f5e6c98dd95c26dd
7
+ data.tar.gz: 545373c5b53ef717fae804a05c54d6a6c6947b755151234ec1325ec61d518260bc8b83c4806d30dd52ceab4dc292a4682d8b0d40e8316e8ed30e53a779efdf3a
@@ -34,6 +34,46 @@ module BulletTrain
34
34
  parents = argv[1] ? argv[1].split(",") : []
35
35
  parents = parents.map(&:classify).uniq
36
36
  parent = parents.first
37
+ child_parts = child.split("::")
38
+ parent_parts = parent.split("::")
39
+
40
+ # Pop off however many spaces match.
41
+ child_parts_dup = child_parts.dup
42
+ parent_parts_dup = parent_parts.dup
43
+ parent_without_namespace = nil
44
+ child_parts_dup.each.with_index do |child_part, idx|
45
+ if child_part == parent_parts_dup[idx]
46
+ child_parts.shift
47
+ parent_parts.shift
48
+ else
49
+ parent_without_namespace = parent_parts.join("::")
50
+ break
51
+ end
52
+ end
53
+
54
+ # `tr` here compensates for namespaced models (i.e. - `Projects::Deliverable` to `projects/deliverable`).
55
+ parent_reference = parent_without_namespace.tableize.singularize.tr("/", "_")
56
+ tableized_child = child.tableize.tr("/", "_")
57
+
58
+ # Pull the parent foreign key from the `create_table` call
59
+ # if a migration with `add_reference` hasn't been created.
60
+ migration_file_name = `grep "add_reference :#{tableized_child}, :#{parent_reference}" db/migrate/*`.split(":").shift
61
+ migration_file_name ||= `grep "create_table :#{tableized_child}" db/migrate/*`.split(":").shift
62
+ parent_t_references = "t.references :#{parent_reference}"
63
+ parent_add_reference = "add_reference :#{tableized_child}, :#{parent_reference}"
64
+ parent_foreign_key = nil
65
+ File.open(migration_file_name).readlines.each do |line|
66
+ parent_foreign_key = line.match?(/#{parent_add_reference}|#{parent_t_references}/)
67
+ break if parent_foreign_key
68
+ end
69
+
70
+ unless parent_foreign_key
71
+ puts "#{child} does not have a foreign key referencing #{parent}".red
72
+ puts ""
73
+ puts "Please re-generate your model, or execute the following to add the foreign key:"
74
+ puts "rails generate migration add_#{parent_reference}_to_#{tableized_child} #{parent_reference}:references\n"
75
+ exit 1
76
+ end
37
77
 
38
78
  unless parents.include?("Team")
39
79
  raise "Parents for #{child} should trace back to the Team model, but Team wasn't provided. Please confirm that all of the parents tracing back to the Team model are present and try again.\n" \
@@ -1,5 +1,5 @@
1
1
  module BulletTrain
2
2
  module SuperScaffolding
3
- VERSION = "1.3.23"
3
+ VERSION = "1.3.25"
4
4
  end
5
5
  end
@@ -8,6 +8,7 @@ require "bullet_train/super_scaffolding/scaffolders/join_model_scaffolder"
8
8
  require "bullet_train/super_scaffolding/scaffolders/oauth_provider_scaffolder"
9
9
 
10
10
  require "indefinite_article"
11
+ require "colorizer"
11
12
 
12
13
  module BulletTrain
13
14
  module SuperScaffolding
@@ -1,4 +1,5 @@
1
1
  require "scaffolding/block_manipulator"
2
+ require "masamune"
2
3
 
3
4
  class Scaffolding::RoutesFileManipulator
4
5
  attr_accessor :child, :parent, :lines, :transformer_options, :concerns
@@ -10,6 +11,7 @@ class Scaffolding::RoutesFileManipulator
10
11
  @filename = filename
11
12
  self.lines = File.readlines(@filename)
12
13
  self.transformer_options = transformer_options
14
+ @msmn = Masamune::AbstractSyntaxTree.new(lines.join)
13
15
  end
14
16
 
15
17
  def child_parts
@@ -225,15 +227,19 @@ class Scaffolding::RoutesFileManipulator
225
227
  # Finds namespace blocks no matter how many levels deep they are nested in resource blocks, etc.
226
228
  # However, will not find namespace blocks inside namespace blocks.
227
229
  def top_level_namespace_block_lines(within)
230
+ namespaces = @msmn.method_calls(name: "namespace")
231
+ namespace_line_numbers = namespaces.map { |namespace| namespace[:line_number] }
232
+
228
233
  local_namespace_blocks = []
229
234
  Scaffolding::FileManipulator.lines_within(lines, within).each do |line|
230
- # i.e. - Retrieve "foo" from "namespace :foo do"
231
- match_data = line.match(/(\s*namespace\s:)(.*)(\sdo$)/)
235
+ # Masamune gets the actual line number, whereas File.readlines etc. start at 0.
236
+ line_index = lines.index(line) + 1
232
237
 
233
238
  # Since we only want top-level namespace blocks, we ensure that
234
239
  # all other namespace blocks INSIDE the top-level namespace blocks are skipped
235
- if match_data.present?
236
- namespace_name = match_data[2]
240
+ if namespace_line_numbers.include?(line_index)
241
+ # Grab the first symbol token on the same line as the namespace.
242
+ namespace_name = @msmn.symbols.find { |sym| sym[:line_number] == line_index }[:token]
237
243
  local_namespace = find_namespaces([namespace_name], within)
238
244
  starting_line_number = local_namespace[namespace_name]
239
245
  local_namespace_block = ((starting_line_number + 1)..(Scaffolding::BlockManipulator.find_block_end(starting_from: starting_line_number, lines: lines) + 1))
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bullet_train-super_scaffolding
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.23
4
+ version: 1.3.25
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Culver
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-09-05 00:00:00.000000000 Z
11
+ date: 2023-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: standard