Skip to content

Commit

Permalink
rework connection generator interface
Browse files Browse the repository at this point in the history
  • Loading branch information
thedumbtechguy committed Jan 2, 2025
1 parent 4f03d14 commit 5b21ad1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

module PlutoniumGenerators
module Concerns
module ResourceSelector
def self.included(base)
# base.send :class_option, :resources, type: :array, desc: "List of resource model names if applicable"
base.send :argument, :resources, type: :array, optional: true, default: [],
desc: "List of model names if applicable"
end

private

def available_resources(source_module)
Plutonium.eager_load_rails!

source_module.constantize.descendants.reject do |model|
next true if model.abstract_class?
next true if source_module == "ApplicationRecord" &&
model.ancestors.any? { |ancestor| ancestor.to_s.end_with?("::ResourceRecord") }
end.map(&:to_s).sort
end

def select_resources(source_module, prompt: "Select resources")
resources = available_resources(source_module)
error "No resources found" if resources.blank?

self.prompt.multi_select(prompt, resources)
end

def resources_selection(prompt: nil)
ivar = :@resources_selection
return instance_variable_get(ivar) if instance_variable_defined?(ivar)

# Convert comma-separated string to array if from command line
value = resources.map(&:classify)
if value.empty?
source_feature = feature_option :src, prompt: "Select source feature"
source_module = (source_feature == "main_app") ? "ApplicationRecord" : "#{source_feature.camelize}::ResourceRecord"
value = select_resources(source_module, prompt: prompt || "Select #{source_module} resources")
end

instance_variable_set(ivar, value)
value
end
end
end
end
21 changes: 7 additions & 14 deletions lib/generators/pu/res/conn/conn_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,20 @@ module Pu
module Res
class ConnGenerator < Rails::Generators::Base
include PlutoniumGenerators::Generator
include PlutoniumGenerators::Concerns::ResourceSelector

source_root File.expand_path("templates", __dir__)

desc "Create a connection between a resource and an app"
desc(
"Create a connection between a resource and a portal\n\n" \
"e.g. rails g pu:res:conn todo --dest=dashboard_portal"
)

# argument :name

def start
source_feature = feature_option :src, prompt: "Select source feature"
source_module = (source_feature == "main_app") ? "ApplicationRecord" : "#{source_feature.camelize}::ResourceRecord"

Plutonium.eager_load_rails!

available_resources = source_module.constantize.descendants.reject do |model|
next true if model.abstract_class?
next true if source_module == "ApplicationRecord" && model.ancestors.any? { |ancestor| ancestor.to_s.end_with?("::ResourceRecord") }
end.map(&:to_s).sort
error "No resources found" if available_resources.blank?
selected_resources = prompt.multi_select("Select resources", available_resources)

@app_namespace = select_portal.camelize
selected_resources = resources_selection
@app_namespace = portal_option(:dest, prompt: "Select destination portal").camelize

selected_resources.each do |resource|
@resource_class = resource
Expand Down

0 comments on commit 5b21ad1

Please sign in to comment.