Class: Hatio::Generators::ResourceModelGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/hatio/resource_model/resource_model_generator.rb

Instance Method Summary collapse

Instance Method Details

#generate_modelObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/generators/hatio/resource_model/resource_model_generator.rb', line 15

def generate_model
  begin
    raise "Not allowed empty bundle name" if (!options.bundle || options.bundle.empty?)
    unless ['uuid', 'meaningful', 'auto-increment', 'none'].include?(options.id_type)
      raise "Invalid --id-type option [uuid|meaningful|auto-increment|none]" 
    end
        
    @id_type = options.id_type      
    @bundle_name = options.bundle
    bundle_app_path = "vendor/bundles/#{@bundle_name}"
    model_path = "#{bundle_app_path}/app/models"
    migration_path = "#{bundle_app_path}/db/migrate"
            
    entity = Entity.find_by_name(class_name)
    @attributes = entity.entity_columns
    @all_attrs = @attributes.map { |attr| ':' + attr.name }.join(',')
    @pkColumn = @attributes.find { |attr| attr.pk == true }
    @pkColumnName = @pkColumn ? @pkColumn.name : 'id'
            
    template "model.rb", "#{model_path}/#{singular_name}.rb"
    template "migration.rb", "#{migration_path}/#{Hatio::Generators::MigrationUtil.next_migration_number}_create_#{table_name}.rb"
        
    puts "\nSuccess"
  rescue StandardError => e
    puts "\nError : #{e}"
  end
end