Class: Rage::CLI::CodeGenerator

Inherits:
Base
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/rage/cli/code_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



7
8
9
# File 'lib/rage/cli/code_generator.rb', line 7

def self.source_root
  File.expand_path("../templates", __dir__)
end

Instance Method Details

#controller(name = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rage/cli/code_generator.rb', line 30

def controller(name = nil)
  return help("controller") if name.nil?

  setup
  unless defined?(ActiveSupport::Inflector)
    raise LoadError, <<~ERR
      ActiveSupport::Inflector is required to run this command. Add the following line to your Gemfile:
      gem "activesupport", require: "active_support/inflector"
    ERR
  end

  # remove trailing Controller if already present
  normalized_name = name.sub(/_?controller$/i, "")
  @controller_name = "#{normalized_name.camelize}Controller"
  file_name = "#{normalized_name.underscore}_controller.rb"

  template("controller-template/controller.rb", "app/controllers/#{file_name}")
end

#migration(name = nil) ⇒ Object



12
13
14
15
16
17
# File 'lib/rage/cli/code_generator.rb', line 12

def migration(name = nil)
  return help("migration") if name.nil?

  setup
  Rake::Task["db:new_migration"].invoke(name)
end

#model(name = nil) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/rage/cli/code_generator.rb', line 20

def model(name = nil)
  return help("model") if name.nil?

  setup
  migration("create_#{name.pluralize}")
  @model_name = name.classify
  template("model-template/model.rb", "app/models/#{name.singularize.underscore}.rb")
end