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

Inherits:
Command
  • Object
show all
Defined in:
lib/brut/cli/apps/scaffold.rb

Instance Method Summary collapse

Methods inherited from Command

#after_bootstrap, args, #args, #before_execute, command_name, default_env, #delegate_to_commands, description, detailed_description, env_var, #err, #global_options, #handle_bootstrap_exception, #initialize, name_matches?, option_parser, #options, opts, #out, requires_project_env, requires_project_env?, #system!

Methods included from Framework::Errors

#abstract_method!, #bug!

Methods included from I18n::ForCLI

#capture, #html_escape, #safe

Methods included from I18n::BaseMethods

#l, #t, #t_direct, #this_field_value

Methods included from ExecutionResults

#abort_execution, #as_execution_result, #cli_usage_error, #continue_execution, #show_cli_usage, #stop_execution

Constructor Details

This class inherits a constructor from Brut::CLI::Command

Instance Method Details

#executeObject



622
623
624
625
626
627
628
629
630
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
# File 'lib/brut/cli/apps/scaffold.rb', line 622

def execute
  if @args.length == 0
    return abort_execution("You must provide a model name")
  end
  db_module = ModuleName.from_string("DB")
  actions = @args.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_" + @args.join("_").gsub(/[^\w]/,"_").gsub(/__/,"_")
  if global_options.dry_run?
    @out.puts "Would create the following DB models:"
    actions.each do |action|
      @out.puts "#{action[:class_name]}"
      @out.puts "  prefix:  #{action[:prefix]}"
      @out.puts "  in:      #{action[:path]}"
      @out.puts "  spec:    #{action[:spec_path]}"
      @out.puts "  factory: #{action[:factory_path]}"
      @out.puts "     name: #{action[:factory_name]}"

    end
    @out.puts "Would create a migration file"
    @out.puts "  via:   bin/db new_migration #{migration_name}"
  else
    system!("bin/db new_migration #{migration_name}")
    actions.each do |action|
      FileUtils.mkdir_p action[:path].dirname
      @out.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
      @out.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
      @out.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