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

Inherits:
Commands::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 Commands::BaseCommand

#accepts, #argv, #bootstrap?, #commands, #default_command, #default_command_class, #default_rack_env, #env, #env_vars, #execute, #name, #options, #opts, #puts, #stderr, #stdin, #stdout, #system!

Instance Method Details

#args_descriptionObject



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

def args_description = "model_name..."

#descriptionObject



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

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

#detailed_descriptionObject



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

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



631
632
633
634
635
636
637
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
# File 'lib/brut/cli/apps/scaffold.rb', line 631

def run
  if argv.length == 0
    return abort_execution("You must provide a model name")
  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 global_options.dry_run?
    @stdout.puts "Would create the following DB models:"
    actions.each do |action|
      @stdout.puts "#{action[:class_name]}"
      @stdout.puts "  prefix:  #{action[:prefix]}"
      @stdout.puts "  in:      #{action[:path]}"
      @stdout.puts "  spec:    #{action[:spec_path]}"
      @stdout.puts "  factory: #{action[:factory_path]}"
      @stdout.puts "     name: #{action[:factory_name]}"

    end
    @stdout.puts "Would create a migration file"
    @stdout.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
      @stdout.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
      @stdout.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
      @stdout.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