lib/hanami/commands/generate/model.rb in hanami-0.9.2 vs lib/hanami/commands/generate/model.rb in hanami-1.0.0.beta1

- old
+ new

@@ -1,32 +1,42 @@ require 'hanami/commands/generate/abstract' +require 'hanami/commands/generate/migration' module Hanami module Commands class Generate class Model < Abstract - attr_reader :input, :model_name + attr_reader :input, :model_name, :table_name def initialize(options, model_name) super(options) @input = Utils::String.new(model_name).underscore @model_name = Utils::String.new(@input).classify + @table_name = Utils::String.new(@input).pluralize + unless skip_migration? + Components.resolve('model.configuration') + end + assert_model_name! end def map_templates add_mapping('entity.rb.tt', entity_path) add_mapping('repository.rb.tt', repository_path) + unless skip_migration? + add_mapping('migration.rb.tt', migration_path) + end add_mapping("entity_spec.#{ test_framework.framework }.tt", entity_spec_path,) add_mapping("repository_spec.#{ test_framework.framework }.tt", repository_spec_path) end def template_options { - model_name: model_name + model_name: model_name, + table_name: table_name } end private @@ -55,10 +65,14 @@ unless model_name.match(/^[a-z]/i) raise ArgumentError.new("Invalid model name. The model name shouldn't begin with a number.") end end + def skip_migration? + options.fetch(:skip_migration, false) + end + def model_root Pathname.new('lib').join(project_name) end # @since 0.5.0 @@ -71,10 +85,18 @@ # @api private def repository_path model_root.join('repositories', "#{ model_name_underscored }_repository.rb").to_s end + # @since 0.9.1 + # @api private + def migration_path + timestamp = Time.now.utc.strftime(Migration::TIMESTAMP_FORMAT) + filename = Migration::FILENAME_PATTERN % { timestamp: timestamp, name: "create_#{table_name}"} + Hanami::Model.configuration.migrations.join(filename) + end + # @since 0.5.0 # @api private def entity_spec_path target_path.join('spec', project_name, 'entities', "#{ model_name_underscored }_spec.rb") end @@ -88,15 +110,9 @@ # @since 0.5.0 # @api private def model_name_underscored input - end - - # @since 0.8.0 - # @api private - def project_name - Utils::String.new(Hanami::Environment.new.project_name).underscore end end end end end