class Corneal::Generators::ModelGenerator

Attributes

class_name[R]
file_name[R]
migration_class_name[R]
migration_name[R]
model_name[R]
table_name[R]

Public Class Methods

source_root() click to toggle source
# File lib/corneal/generators/model/model_generator.rb, line 17
def self.source_root
  File.dirname(__FILE__)
end

Public Instance Methods

create_migration() click to toggle source
# File lib/corneal/generators/model/model_generator.rb, line 43
def create_migration
  return unless options[:migration]

  migration_files = Dir.entries("db/migrate").select { |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() click to toggle source
# File lib/corneal/generators/model/model_generator.rb, line 35
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() click to toggle source
# File lib/corneal/generators/model/model_generator.rb, line 21
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