Class: ActiveRecord::Generators::ModelGenerator

Inherits:
Base show all
Defined in:
activerecord/lib/rails/generators/active_record/model/model_generator.rb

Overview

:nodoc:

Instance Attribute Summary

Attributes included from Rails::Generators::Migration

#migration_class_name, #migration_file_name, #migration_number

Instance Method Summary collapse

Methods inherited from Base

base_root, next_migration_number

Methods included from Rails::Generators::Migration

#migration_template

Methods included from ActiveSupport::Concern

#append_features, extended, #included

Methods inherited from Rails::Generators::NamedBase

#initialize

Methods inherited from Rails::Generators::Base

base_root, class_option, default_source_root, desc, hide!, hook_for, inherited, namespace, remove_hook_for, source_root

Methods included from Rails::Generators::Actions

#add_source, #capify!, #environment, #gem, #gem_group, #generate, #git, #initialize, #initializer, #lib, #rake, #rakefile, #readme, #route, #vendor

Constructor Details

This class inherits a constructor from Rails::Generators::NamedBase

Instance Method Details

#accessible_attributesObject


37
38
39
# File 'activerecord/lib/rails/generators/active_record/model/model_generator.rb', line 37

def accessible_attributes
  attributes.reject(&:reference?)
end

#attributes_with_indexObject


33
34
35
# File 'activerecord/lib/rails/generators/active_record/model/model_generator.rb', line 33

def attributes_with_index
  attributes.select { |a| !a.reference? && a.has_index? }
end

#create_migration_fileObject

creates the migration file for the model.


18
19
20
21
22
# File 'activerecord/lib/rails/generators/active_record/model/model_generator.rb', line 18

def create_migration_file
  return unless options[:migration] && options[:parent].nil?
  attributes.each { |a| a.attr_options.delete(:index) if a.reference? && !a.has_index? } if options[:indexes] == false
  migration_template "../../migration/templates/create_table_migration.rb", "db/migrate/create_#{table_name}.rb"
end

#create_model_fileObject


24
25
26
# File 'activerecord/lib/rails/generators/active_record/model/model_generator.rb', line 24

def create_model_file
  template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
end

#create_module_fileObject


28
29
30
31
# File 'activerecord/lib/rails/generators/active_record/model/model_generator.rb', line 28

def create_module_file
  return if regular_class_path.empty?
  template 'module.rb', File.join('app/models', "#{class_path.join('/')}.rb") if behavior == :invoke
end