Class: Brut::CLI::Apps::Scaffold::DbModel

Inherits:
BaseCommand show all
Defined in:
lib/brut/cli/apps/scaffold.rb

Instance Attribute Summary

Attributes inherited from Commands::BaseCommand

#parent_command

Instance Method Summary collapse

Methods inherited from BaseCommand

#bootstrap?, #default_rack_env, #opts

Methods inherited from Commands::BaseCommand

#accepts, #argv, #bootstrap?, #commands, #default_rack_env, #delegate_to_command, #env, #env_vars, #execute, #name, #options, #opts, #puts, #stdin, #system!, #theme

Instance Method Details

#args_descriptionObject



634
# File 'lib/brut/cli/apps/scaffold.rb', line 634

def args_description = "model_name..."

#descriptionObject



633
# File 'lib/brut/cli/apps/scaffold.rb', line 633

def description = "Creates a DB models, factories, and a single placeholder migration"

#detailed_descriptionObject



636
# File 'lib/brut/cli/apps/scaffold.rb', line 636

def detailed_description  = "Creates empty versions of the files you'd need to access a database table or tables, along with a migration to, in theory, create those tables. Do note that this will guess at external id prefixes"

#runObject



638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
# File 'lib/brut/cli/apps/scaffold.rb', line 638

def run
  if argv.length == 0
    puts "You must provide one or more model names"
    return 1
  end
  db_module = ModuleName.from_string("DB")
  actions = argv.map { |arg|
    ModuleName.from_string(arg)
  }.map { |module_name|
    module_name.in_module(db_module)
  }.map do |model_name|
    {
      class_name: model_name.to_s,
      path: model_name.path_from(Brut.container.data_models_src_dir),
      prefix: model_name.parts_of_module[1].to_s[0,2].downcase,
      spec_path: model_name.path_from(Brut.container.data_models_specs_dir, extname: ".spec.rb"),
      factory_path: model_name.path_from(Brut.container.app_specs_dir / "factories", extname: ".factory.rb"),
      factory_name: model_name.parts_of_module[1..-1].map(&:underscorized).join("_"),
    }
  end
  migration_name = "create_" + argv.join("_").gsub(/[^\w]/,"_").gsub(/__/,"_")
  if options.dry_run?
    puts "Would create the following DB models:"
    actions.each do |action|
      puts "#{action[:class_name]}"
      puts "  prefix:  #{action[:prefix]}"
      puts "  in:      #{action[:path]}"
      puts "  spec:    #{action[:spec_path]}"
      puts "  factory: #{action[:factory_path]}"
      puts "     name: #{action[:factory_name]}"

    end
    puts "Would create a migration file"
    puts "  via:   brut db new_migration #{migration_name}"
  else
    system!("brut db new_migration #{migration_name}")
    actions.each do |action|
      FileUtils.mkdir_p action[:path].dirname
      puts "Creating #{action[:class_name]} in #{action[:path].relative_path_from(Brut.container.project_root)}"
      File.open(action[:path].to_s,"w") do |file|
        file.puts %{class #{action[:class_name]} < AppDataModel
  has_external_id :#{action[:prefix]} # !IMPORTANT: Make sure this is unique amongst your DB models
end}
      end
      FileUtils.mkdir_p action[:spec_path].dirname
      puts "Creating spec for #{action[:class_name]} in #{action[:spec_path].relative_path_from(Brut.container.project_root)}"
      File.open(action[:spec_path].to_s,"w") do |file|
        file.puts %{require "spec_helper"
RSpec.describe #{action[:class_name]} do
  # Remove this if you decide to put logic on
  # your model
  implementation_is_trivial
end}
      end
      FileUtils.mkdir_p action[:factory_path].dirname
      puts "Creating factory for #{action[:class_name]} in #{action[:factory_path].relative_path_from(Brut.container.project_root)}"
      File.open(action[:factory_path].to_s,"w") do |file|
        file.puts %{FactoryBot.define do
  factory :#{action[:factory_name]}, class: "#{action[:class_name]}" do
# Add attributes here
  end
end

}
      end
    end
  end
  0
end