Class: MegaBar::MegaBarModelsGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- MegaBar::MegaBarModelsGenerator
- Defined in:
- lib/generators/mega_bar/mega_bar_models/mega_bar_models_generator.rb
Constant Summary collapse
- @@notices =
[]
Instance Method Summary collapse
-
#create_controller_file ⇒ Object
in generators, all public methods are run.
- #create_controller_spec_file ⇒ Object
- #create_factory ⇒ Object
- #create_model_file ⇒ Object
- #generate_migration ⇒ Object
- #write_notices ⇒ Object
Instance Method Details
#create_controller_file ⇒ Object
in generators, all public methods are run. Weird, huh?
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/generators/mega_bar/mega_bar_models/mega_bar_models_generator.rb', line 13 def create_controller_file # Check if the controller class already exists full_controller_class_name = the_module_name ? "#{the_module_name}::#{the_controller_name}" : the_controller_name begin full_controller_class_name.constantize @@notices << "Controller #{full_controller_class_name} already exists, skipping controller file creation" return rescue NameError # Controller doesn't exist, proceed with creation end @@notices << "You will have to copy your controller manually over to the megabar gem" if gem_path == '' && modyule == 'MegaBar' template 'generic_controller.rb', "#{gem_path}#{the_controller_file_path}#{the_controller_file_name}.rb" end |
#create_controller_spec_file ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/generators/mega_bar/mega_bar_models/mega_bar_models_generator.rb', line 98 def create_controller_spec_file # Check if the spec file already exists spec_file_path = "#{gem_path}#{the_controller_spec_file_path}#{the_controller_spec_file_name}.rb" if File.exist?(spec_file_path) @@notices << "Controller spec file already exists, skipping spec file creation" return end template 'generic_controller_spec.rb', spec_file_path @@notices << "You will have to copy the spec file yourself manually to the megabar repo's spec/controllers directory" if gem_path == '' && modyule == 'MegaBar' end |
#create_factory ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/generators/mega_bar/mega_bar_models/mega_bar_models_generator.rb', line 110 def create_factory # Check if the factory file already exists factory_file_path = "#{gem_path}#{the_factory_file_path}#{the_model_file_name}.rb" if File.exist?(factory_file_path) @@notices << "Factory file already exists, skipping factory file creation" return end @@notices << "You will have to copy the factory file yourself manually to the megabar repo's spec/internal/factories directory" if gem_path == '' && modyule == 'MegaBar' template 'generic_factory.rb', factory_file_path end |
#create_model_file ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/generators/mega_bar/mega_bar_models/mega_bar_models_generator.rb', line 29 def create_model_file # Check if the model class already exists full_class_name = the_module_name ? "#{the_module_name}::#{classname}" : classname begin full_class_name.constantize @@notices << "Model #{full_class_name} already exists, skipping model file creation" return rescue NameError # Class doesn't exist, proceed with creation end template 'generic_model.rb', "#{gem_path}#{the_model_file_path}#{the_model_file_name}.rb" @@notices << "You will have to copy your model files manually over to the megabar gem" if gem_path == '' && modyule == 'MegaBar' end |
#generate_migration ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/generators/mega_bar/mega_bar_models/mega_bar_models_generator.rb', line 45 def generate_migration # Check if the table already exists (skip in test environment to avoid connection issues) begin if ActiveRecord::Base.connection.table_exists?(the_table_name) @@notices << "Table #{the_table_name} already exists, skipping migration creation" return end rescue => e # In test environment, connection might not be available, so skip the check if Rails.env.test? @@notices << "Skipping table existence check in test environment: #{e.}" else raise e end end # In test environment, generate migration in test-specific directory if Rails.env.test? migration_name = "create_#{the_table_name}" migration_file = generate_migration_file(migration_name) @@notices << "Migration created in test directory: #{migration_file}" else # In production, try multiple approaches to generate migration migration_created = false # Method 1: Try Rails generator begin generate 'migration create_' + the_table_name migration_created = true @@notices << "Migration created via Rails generator" rescue => e @@notices << "Rails generator failed: #{e.}" end # Method 2: If Rails generator failed, create migration manually unless migration_created begin migration_name = "create_#{the_table_name}" migration_file = generate_migration_file_production(migration_name) @@notices << "Migration created manually: #{migration_file}" migration_created = true rescue => e @@notices << "Manual migration creation failed: #{e.}" end end # Method 3: If module name exists, note that manual copying might be needed if the_module_name && migration_created @@notices << "You may need to copy migrations manually to the megabar gem" end end end |
#write_notices ⇒ Object
122 123 124 |
# File 'lib/generators/mega_bar/mega_bar_models/mega_bar_models_generator.rb', line 122 def write_notices # todo .. take @@notices and write it to a db? or a file? hmm.. end |