-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rework connection generator interface
- Loading branch information
1 parent
4f03d14
commit 5b21ad1
Showing
2 changed files
with
55 additions
and
14 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
lib/generators/pu/lib/plutonium_generators/concerns/resource_selector.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters