Class: Gbc::ModelInspector
- Inherits:
-
Object
- Object
- Gbc::ModelInspector
- Defined in:
- lib/generators/gbc/trestle/model_inspector.rb
Class Method Summary collapse
-
.find_active_record_model(model_name) ⇒ Class?
Checks if a given model name corresponds to a loaded class and is an ActiveRecord model.
-
.get_model_database_attributes(model_class) ⇒ Hash<String, String>
Retrieves the database attributes (column names and types) for a given model.
Class Method Details
.find_active_record_model(model_name) ⇒ Class?
Checks if a given model name corresponds to a loaded class and is an ActiveRecord model.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/generators/gbc/trestle/model_inspector.rb', line 7 def self.find_active_record_model(model_name) # Convert model name string to a constant (class) model_class = model_name.safe_constantize # Check if the class exists and inherits from ActiveRecord::Base if model_class.is_a?(Class) && model_class < ActiveRecord::Base return model_class else puts "Info: Model '#{model_name}' not found or is not an ActiveRecord model." return nil end rescue NameError # Handle cases where the constant doesn't exist at all puts "Info: Model '#{model_name}' class not defined." return nil end |
.get_model_database_attributes(model_class) ⇒ Hash<String, String>
Retrieves the database attributes (column names and types) for a given model.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/generators/gbc/trestle/model_inspector.rb', line 30 def self.get_model_database_attributes(model_class) unless model_class.is_a?(Class) && model_class < ActiveRecord::Base puts "Error: Provided argument is not a valid ActiveRecord model class." return {} end attributes = {} begin # `columns` method returns an array of ActiveRecord::ConnectionAdapters::Column objects model_class.columns.each do |column| attributes[column.name] = column.type.to_s # e.g., "string", "integer", "datetime" end rescue ActiveRecord::StatementInvalid => e puts "Error fetching attributes for #{model_class.name}: #{e.}" puts "This might happen if the database table for the model does not exist yet." return {} rescue NoMethodError => e puts "Error: #{e.}. Ensure the Rails environment is fully loaded and the model has a table." return {} end attributes end |