lib/generators/wcc/model_generator.rb in wcc-contentful-app-0.4.0.pre.rc vs lib/generators/wcc/model_generator.rb in wcc-contentful-app-1.0.0.pre.rc1

- old
+ new

@@ -3,28 +3,34 @@ module Wcc class ModelGenerator < Rails::Generators::Base source_root File.expand_path('templates', __dir__) argument :model, type: :string - VALID_MODELS = %w[menu page].freeze + VALID_MODELS = + Dir.glob("#{__dir__}/templates/*") + .select { |f| File.directory? f } + .map { |f| File.basename f } + .sort + .freeze def initialize(*) super return if VALID_MODELS.include?(singular) - raise ArgumentError, "Model must be #{VALID_MODELS.to_sentence}" + raise ArgumentError, "Model must be one of #{VALID_MODELS.to_sentence}" end def ensure_migration_tools_installed in_root do run 'npm init -y' unless File.exist?('package.json') package = JSON.parse(File.read('package.json')) deps = package['dependencies'] - unless deps.try(:[], 'contentful-migration-cli').present? - run 'npm install --save watermarkchurch/migration-cli ts-node typescript contentful-export' + unless deps.try(:[], '@watermarkchurch/contentful-migration').present? + run 'npm install --save @watermarkchurch/contentful-migration ts-node ' \ + 'typescript contentful-export' end end end def ensure_wrapper_script_in_bin_dir @@ -62,12 +68,16 @@ return if inside('config/initializers') { File.exist?('wcc_contentful.rb') } copy_file 'wcc_contentful.rb', 'config/initializers/wcc_contentful.rb' end - def create_model_migration - copy_file "#{singular}/generated_add_#{plural}.ts", + def create_model_migrations + copy_file "#{singular}/migrations/generated_add_#{plural}.ts", "db/migrate/#{timestamp}01_generated_add_#{plural}.ts" + + Dir.glob("#{__dir__}/templates/#{singular}/migrations/*.rb").each do |f| + copy_file f, "db/migrate/#{timestamp}_#{File.basename(f)}" + end end def drop_model_overrides_in_app_models directory "#{singular}/models", 'app/models' end