Skip to content

Commit

Permalink
more wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jagthedrummer committed Jun 5, 2024
1 parent 293667d commit ab7d656
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def run
end

child = argv[0]
child_attribute_name = child.underscore.tr("/", "_")

primary_parent = argv[1].split("class_name=").last.split(",").first.split("}").first
primary_parent_attribute_name = argv[1].split("{").first.delete_suffix("_id")

Expand All @@ -48,6 +50,9 @@ def run
puts "Generating model with '#{generation_command}'".green
`#{generation_command}`

# TODO: Maybe this should go in a transformer
# TODO: Actually manipulate the migration

string_to_replace = "t.references :#{primary_parent_attribute_name}, null: false, foreign_key: true"
string_to_replace_with = "t.references :#{primary_parent_attribute_name}, null: false, foreign_key: { to_table: #{primary_parent.tableize} }"

Expand All @@ -69,18 +74,16 @@ def run
attributes = attributes.map { |attribute| attribute.gsub("{", ":super_select{") }
attributes = attributes.map { |attribute| attribute.gsub("}", ",required}") }

transformer = Scaffolding::Transformer.new(child, [primary_parent], @options, primary_parent_attribute_name)
transformer = Scaffolding::Transformer.new(child, [primary_parent], @options)

# We need this transformer to reflect on the class names _just_ between e.g. `Project` and `Projects::Tag`, without the join model.
has_many_through_transformer = Scaffolding::Transformer.new(secondary_parent, [primary_parent], @options, primary_parent_attribute_name)
has_many_through_transformer = Scaffolding::Transformer.new(secondary_parent, [primary_parent], @options, child_attribute_name, secondary_parent_attribute_name)

# We need this transformer to reflect on the association between `Projects::Tag` and `Projects::AppliedTag` backwards.
inverse_transformer = Scaffolding::Transformer.new(child, [secondary_parent], @options, secondary_parent_attribute_name)
inverse_transformer = Scaffolding::Transformer.new(child, [secondary_parent], @options)

# We need this transformer to reflect on the class names _just_ between e.g. `Projects::Tag` and `Project`, without the join model.
inverse_has_many_through_transformer = Scaffolding::Transformer.new(primary_parent, [secondary_parent], @options, secondary_parent_attribute_name)

debugger
inverse_has_many_through_transformer = Scaffolding::Transformer.new(primary_parent, [secondary_parent], @options, child_attribute_name, primary_parent_attribute_name)

# However, for the first attribute, we actually don't need the scope validator (and can't really implement it).
attributes[0] = attributes[0].gsub("}", ",unscoped}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
class Scaffolding::ClassNamesTransformer
attr_accessor :child, :parent, :namespace
attr_accessor :child, :parent, :namespace, :child_attribute_name, :parent_attribute_name

def initialize(child, parent, namespace = "account")
def initialize(child, parent, namespace = "account", child_attribute_name, parent_attribute_name)
self.child = child
self.parent = parent
self.namespace = namespace
self.child_attribute_name = child_attribute_name
self.parent_attribute_name = parent_attribute_name
end

def belongs_to_needs_class_definition?
Expand Down Expand Up @@ -171,7 +173,7 @@ def replacement_for(string)
when "absolutely_abstract_creative_concepts"
parent_class_name_in_context.underscore.tr("/", "_").pluralize
when "completely_concrete_tangible_things"
class_name_in_parent_context.underscore.tr("/", "_").pluralize
parent_attribute_name&.pluralize || class_name_in_parent_context.underscore.tr("/", "_").pluralize
when "absolutely_abstract/creative_concepts"
parent_class_name_in_context.underscore.pluralize
when "completely_concrete/tangible_things"
Expand Down
18 changes: 13 additions & 5 deletions bullet_train-super_scaffolding/lib/scaffolding/transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require "scaffolding/attribute"

class Scaffolding::Transformer
attr_accessor :child, :parent, :parents, :class_names_transformer, :cli_options, :additional_steps, :namespace, :suppress_could_not_find, :attribute_name
attr_accessor :child, :parent, :parents, :class_names_transformer, :cli_options, :additional_steps, :namespace, :suppress_could_not_find, :child_attribute_name, :parent_attribute_name

def update_models_abstract_class
end
Expand Down Expand Up @@ -37,15 +37,16 @@ def no_parent?
def update_action_models_abstract_class(targets_n)
end

def initialize(child, parents, cli_options = {}, attribute_name = parents.first.underscore)
def initialize(child, parents, cli_options = {}, child_attribute_name = nil, parent_attribute_name = nil)
self.child = child
self.parent = parents.first
self.parents = parents
self.namespace = cli_options["namespace"] || "account"
self.class_names_transformer = Scaffolding::ClassNamesTransformer.new(child, parent, namespace)
self.class_names_transformer = Scaffolding::ClassNamesTransformer.new(child, parent, namespace, child_attribute_name, parent_attribute_name)
self.cli_options = cli_options
self.additional_steps = []
self.attribute_name = attribute_name
self.child_attribute_name = child_attribute_name
self.parent_attribute_name = parent_attribute_name
end

RUBY_NEW_FIELDS_PROCESSING_HOOK = "# 🚅 super scaffolding will insert processing for new fields above this line."
Expand Down Expand Up @@ -606,7 +607,14 @@ def add_has_many_association

def add_has_many_through_associations(has_many_through_transformer)
has_many_association = add_has_many_association
has_many_through_string = has_many_through_transformer.transform_string("has_many :completely_concrete_tangible_things, through: :$HAS_MANY_ASSOCIATION")
has_many_through_parts = [
"has_many :completely_concrete_tangible_things",
"through: :$HAS_MANY_ASSOCIATION"
]
if has_many_through_transformer.parent_attribute_name.present?
has_many_through_parts << "class_name: \"Scaffolding::CompletelyConcrete::TangibleThing\""
end
has_many_through_string = has_many_through_transformer.transform_string(has_many_through_parts.join(", "))
has_many_through_string.gsub!("$HAS_MANY_ASSOCIATION", has_many_association)
add_line_to_file(transform_string("./app/models/scaffolding/absolutely_abstract/creative_concept.rb"), has_many_through_string, HAS_MANY_HOOK, prepend: true)
end
Expand Down

0 comments on commit ab7d656

Please sign in to comment.