bullet_train-super_scaffolding 1.7.9 → 1.7.11

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: c00c62e56f391f71ca5c5810b241411d314aae77a1197556214d57ee3ba67b72
4
- data.tar.gz: 72b87b899231851824b77399fb2e3ac90be33592914f0e5510045ae340379489
3
+ metadata.gz: 16465881966c5cc7bfb8056943d036dea6ee5e2284a4000a2f6182c41c1eb22d
4
+ data.tar.gz: aa40db2f84412ec08860dd6387f096c5e53622526ab17700bb66be85e8f0dd6b
5
5
  SHA512:
6
- metadata.gz: 28838a058935c9e621ed012b1ed60bdeb1aeb52b352c27ce7701e641ca50422a0aaf6578d352253a1db24d89a51d596c8c7c6902efda2ae87e7abb7ff3285303
7
- data.tar.gz: 4888c911c70833c745a1f659471537cca5c9ec18605dd23d0818e2b7172da5b89f1aeb084ea73b31aa5b7b84f8c045ab49303da0d6423de1c21f261bb7c8c0ae
6
+ metadata.gz: 4c78f397e90c8c4175ab3b5c02fbd841ab8a6f1f6c6f2929bdf4d850f88822ac6ce50f0359b51a88ee2ffde1f2a445b3084d1d23433122fd454e65e361795e43
7
+ data.tar.gz: 033dd98dd593f934b77f39214f35876da85ae9a9716c7e11d4b7ca93e2ba169d5e217e63326551f941a77a69277cf7a8598d140babd2c60d001186791a48f2e6
@@ -28,6 +28,8 @@ module BulletTrain
28
28
  end
29
29
 
30
30
  child = argv[0]
31
+ check_class_name_for_namespace_conflict(child)
32
+
31
33
  parents = argv[1] ? argv[1].split(",") : []
32
34
 
33
35
  # Raise an error if the developer skipped adding the parent and went straight to the attributes.
@@ -30,6 +30,8 @@ module BulletTrain
30
30
  end
31
31
 
32
32
  child = argv[0]
33
+ check_class_name_for_namespace_conflict(child)
34
+
33
35
  primary_parent = argv[1].split("class_name=").last.split(",").first.split("}").first
34
36
  secondary_parent = argv[2].split("class_name=").last.split(",").first.split("}").first
35
37
 
@@ -1,5 +1,5 @@
1
1
  module BulletTrain
2
2
  module SuperScaffolding
3
- VERSION = "1.7.9"
3
+ VERSION = "1.7.11"
4
4
  end
5
5
  end
@@ -51,6 +51,13 @@ class Scaffolding::Attribute
51
51
  options[:required] || is_first_attribute?
52
52
  end
53
53
 
54
+ def association_class_name
55
+ if options[:class_name].present?
56
+ return options[:class_name].underscore.split("/").last
57
+ end
58
+ name.split("_id").first
59
+ end
60
+
54
61
  def is_association?
55
62
  is_belongs_to? || is_has_many?
56
63
  end
@@ -49,6 +49,39 @@ def get_untracked_files
49
49
  `git ls-files --other --exclude-standard`.split("\n")
50
50
  end
51
51
 
52
+ # class_name is a potentially namespaced class like
53
+ # "Tasks::Widget" or "Task::Widget". Here we ensure that
54
+ # the namespace doesn't clobber an existing model. If it does
55
+ # we suggest that the namespace could be pluralized.
56
+ def check_class_name_for_namespace_conflict(class_name)
57
+ if class_name.include?("::")
58
+ parts = class_name.split("::") # ["Task", "Widget"]
59
+ # We drop the last segment because that's tne new model we're trying to create
60
+ parts.pop # ["Task"]
61
+ possible_conflicted_class_name = ""
62
+ parts.each do |part|
63
+ possible_conflicted_class_name += "::#{part}"
64
+ begin
65
+ klass = possible_conflicted_class_name.constantize
66
+ is_active_record_class = klass&.ancestors&.include?(ActiveRecord::Base)
67
+ is_aactive_hash_class = klass&.ancestors&.include?(ActiveHash::Base)
68
+ if klass && (is_active_record_class || is_aactive_hash_class)
69
+ problematic_namespace = possible_conflicted_class_name[2..]
70
+ puts "It looks like the namespace you gave for this model conflicts with an existing class: #{klass.name}".red
71
+ puts "You should use a namespace that doesn't clobber an existing class.".red
72
+ puts ""
73
+ puts "We reccomend using the pluralized version of the existing class.".red
74
+ puts ""
75
+ puts "For instance instead of #{problematic_namespace} use #{problematic_namespace.pluralize}".red
76
+ exit
77
+ end
78
+ rescue NameError
79
+ # this is good actually, it means we don't already have a class that will be clobbered
80
+ end
81
+ end
82
+ end
83
+ end
84
+
52
85
  def check_required_options_for_attributes(scaffolding_type, attributes, child, parent = nil)
53
86
  tableized_parent = nil
54
87
 
@@ -139,14 +172,37 @@ def check_required_options_for_attributes(scaffolding_type, attributes, child, p
139
172
 
140
173
  file_name = Dir.glob("app/models/**/*.rb").find { |model| model.match?(/#{attribute_options[:class_name].underscore}\.rb/) } || ""
141
174
 
175
+ begin
176
+ class_name_constant = attribute_options[:class_name].constantize
177
+ rescue NameError
178
+ if attribute_options[:class_name] == child
179
+ puts ""
180
+ puts "You appear to be tryingo scaffold a model that references itself. Unfotunately this needs to be a two-step process.".red
181
+ puts "First you should generate the model without the reference, and then add the reference as a new field. For instance:".red
182
+ puts ""
183
+ puts " rails generate super_scaffold #{child}#{" " + parent if parent.present?}".red
184
+ puts " rails generate super_scaffold:field #{child} #{name}:#{type}".red
185
+ puts ""
186
+ puts "If `#{name}` is just a regular field and isn't backed by an ActiveRecord association, you can skip all this with the `{vanilla}` option, e.g.:".red
187
+ puts ""
188
+ puts " rails generate super_scaffold #{child}#{" " + parent if parent.present?} #{name}:#{type}{vanilla}".red
189
+ puts ""
190
+ exit
191
+ else
192
+ # We don't do anything special here because we'll end up triggering the error message below. A self-referential model
193
+ # is kind of a special case that's worth calling out specifically. If we just can't find the model the messaging below
194
+ # should be sufficient to get folks on the right track.
195
+ end
196
+ end
197
+
142
198
  # If a model is namespaced, the parent's model file might exist under
143
199
  # `app/models/`, but sometimes these files are modules that resolve
144
200
  # table names by providing a prefix as opposed to an actual ApplicationRecord.
145
201
  # This check ensures that the _id attribute really is a model.
146
- is_active_record_class = attribute_options[:class_name].constantize.ancestors.include?(ActiveRecord::Base)
202
+ is_active_record_class = class_name_constant&.ancestors&.include?(ActiveRecord::Base)
147
203
  unless File.exist?(file_name) && is_active_record_class
148
204
  puts ""
149
- puts "Attributes that end with `_id` or `_ids` trigger awesome, powerful magic in Super Scaffolding. However, because no `#{attribute_options[:class_name]}` class was found defined in `#{file_name}`, you'll need to specify a `class_name` that exists to let us know what model class is on the other side of the association, like so:".red
205
+ puts "Attributes that end with `_id` or `_ids` trigger awesome, powerful magic in Super Scaffolding. However, because no `#{attribute_options[:class_name]}` class was found defined in your app, you'll need to specify a `class_name` that exists to let us know what model class is on the other side of the association, like so:".red
150
206
  puts ""
151
207
  puts " bin/super-scaffold #{scaffolding_type} #{child}#{" " + parent if parent.present?} #{name}:#{type}{class_name=#{name.gsub(/_ids?$/, "").classify}}".red
152
208
  puts ""
@@ -758,7 +758,7 @@ class Scaffolding::Transformer
758
758
  end
759
759
 
760
760
  if attribute.is_association?
761
- short = attribute.options[:class_name].underscore.split("/").last
761
+ short = attribute.association_class_name
762
762
  case attribute.type
763
763
  when "buttons", "options"
764
764
  field_attributes["\n options"] = "@tangible_thing.#{valid_values}.map { |#{short}| [#{short}.id, #{short}.#{attribute.options[:label]}] }"
@@ -1362,12 +1362,12 @@ class Scaffolding::Transformer
1362
1362
 
1363
1363
  # update the factory generated by `rails g`.
1364
1364
  content = if transform_string(":absolutely_abstract_creative_concept") == transform_string(":scaffolding_absolutely_abstract_creative_concept")
1365
- transform_string("association :absolutely_abstract_creative_concept")
1365
+ transform_string(" association :absolutely_abstract_creative_concept")
1366
1366
  else
1367
- transform_string("association :absolutely_abstract_creative_concept, factory: :scaffolding_absolutely_abstract_creative_concept")
1367
+ transform_string(" association :absolutely_abstract_creative_concept, factory: :scaffolding_absolutely_abstract_creative_concept")
1368
1368
  end
1369
1369
 
1370
- scaffold_replace_line_in_file("./test/factories/scaffolding/completely_concrete/tangible_things.rb", content, "absolutely_abstract_creative_concept { nil }")
1370
+ scaffold_replace_line_in_file("./test/factories/scaffolding/completely_concrete/tangible_things.rb", content, " absolutely_abstract_creative_concept { nil }")
1371
1371
 
1372
1372
  add_has_many_association
1373
1373
 
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.7.9
4
+ version: 1.7.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Culver
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-29 00:00:00.000000000 Z
11
+ date: 2024-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: standard