Class: Natra::Generators::ModelGenerator
- Inherits:
-
Thor::Group
- Object
- Thor::Group
- Natra::Generators::ModelGenerator
- Includes:
- Thor::Actions
- Defined in:
- lib/natra/generators/model/model_generator.rb
Instance Attribute Summary collapse
-
#class_name ⇒ Object
readonly
Returns the value of attribute class_name.
-
#file_name ⇒ Object
readonly
Returns the value of attribute file_name.
-
#migration_class_name ⇒ Object
readonly
Returns the value of attribute migration_class_name.
-
#migration_name ⇒ Object
readonly
Returns the value of attribute migration_name.
-
#model_name ⇒ Object
readonly
Returns the value of attribute model_name.
-
#table_name ⇒ Object
readonly
Returns the value of attribute table_name.
Class Method Summary collapse
Instance Method Summary collapse
Instance Attribute Details
#class_name ⇒ Object (readonly)
Returns the value of attribute class_name.
7 8 9 |
# File 'lib/natra/generators/model/model_generator.rb', line 7 def class_name @class_name end |
#file_name ⇒ Object (readonly)
Returns the value of attribute file_name.
7 8 9 |
# File 'lib/natra/generators/model/model_generator.rb', line 7 def file_name @file_name end |
#migration_class_name ⇒ Object (readonly)
Returns the value of attribute migration_class_name.
7 8 9 |
# File 'lib/natra/generators/model/model_generator.rb', line 7 def migration_class_name @migration_class_name end |
#migration_name ⇒ Object (readonly)
Returns the value of attribute migration_name.
7 8 9 |
# File 'lib/natra/generators/model/model_generator.rb', line 7 def migration_name @migration_name end |
#model_name ⇒ Object (readonly)
Returns the value of attribute model_name.
7 8 9 |
# File 'lib/natra/generators/model/model_generator.rb', line 7 def model_name @model_name end |
#table_name ⇒ Object (readonly)
Returns the value of attribute table_name.
7 8 9 |
# File 'lib/natra/generators/model/model_generator.rb', line 7 def table_name @table_name end |
Class Method Details
.source_root ⇒ Object
15 16 17 |
# File 'lib/natra/generators/model/model_generator.rb', line 15 def self.source_root File.dirname(__FILE__) end |
Instance Method Details
#create_migration ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/natra/generators/model/model_generator.rb', line 41 def create_migration return unless [:migration] migration_files = Dir.entries('db/migrate').reject { |path| File.directory? path } if duplicate = migration_files.find { |file| file.include?(migration_name) } say_status :identical, "db/migrate/#{duplicate}", :blue else version = Time.now.utc.strftime('%Y%m%d%H%M%S') migration_file_name = "#{version}_#{migration_name}.rb" template 'migration.rb.erb', File.join('db/migrate', migration_file_name) end end |
#create_model ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/natra/generators/model/model_generator.rb', line 33 def create_model unless model_name == name say "[WARNING] The model name '#{name}' was recognized as a plural, using the singular '#{model_name}' instead." end template 'model.rb.erb', File.join('app/models', "#{file_name}.rb") end |
#setup ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/natra/generators/model/model_generator.rb', line 19 def setup @model_name = name.singularize @class_name = model_name.camel_case @file_name = model_name.underscore @table_name = file_name.pluralize @migration_name = "create_#{table_name}" @migration_class_name = migration_name.camel_case attributes.map! do |attribute| field = attribute.split(':') { name: field[0], type: field[1] || 'string' } end end |