Skip to content

Commit

Permalink
fix: generators can run without prior migrations (#2861)
Browse files Browse the repository at this point in the history
Co-authored-by: Paul Bob <[email protected]>
Co-authored-by: Paul Bob <[email protected]>
  • Loading branch information
3 people authored Jun 21, 2024
1 parent ba598ff commit 4a85c71
Show file tree
Hide file tree
Showing 3 changed files with 339 additions and 278 deletions.
36 changes: 36 additions & 0 deletions lib/generators/avo/all_resources_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require_relative "base_generator"

module Generators
module Avo
class AllResourcesGenerator < BaseGenerator
namespace "avo:all_resources"

def task
# Rails.application.eager_load!
# get all models
models = fetch_models
models.delete("ApplicationRecord")

models
.each do |model|
Rails::Generators.invoke "avo:resource", [model.underscore], {}
rescue => e
puts "Error: #{e.message}"
end
end

no_tasks do
def fetch_models
model_files = Dir[Rails.root.join("app/models/**/*.rb")]
model_files.map do |file|
model_name = file.sub(Rails.root.join("app/models/").to_s, "").sub(".rb", "")
model_name.camelize.constantize
model_name.camelize
rescue NameError
nil
end.compact
end
end
end
end
end
9 changes: 6 additions & 3 deletions lib/generators/avo/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ def create_initializer_file
route "mount Avo::Engine, at: Avo.configuration.root_path"

template "initializer/avo.tt", "config/initializers/avo.rb"

create_resources
end

def create_resources
if defined?(User).present?
Rails::Generators.invoke("avo:resource", ["user", "-q"], {destination_root: Rails.root })
no_tasks do
def create_resources
if defined?(User).present?
Rails::Generators.invoke("avo:resource", ["user", "-q"], {destination_root: Rails.root})
end
end

if defined?(Account) && Account.is_a?(ActiveRecord::Base)
Expand Down
Loading

0 comments on commit 4a85c71

Please sign in to comment.