Class: Trails::Generate

Inherits:
Thor
  • Object
show all
Defined in:
lib/cli.rb

Instance Method Summary collapse

Instance Method Details

#controller(name) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cli.rb', line 38

def controller(name)
  file_name = name.pluralize.underscore
  controller_name = "#{name.pluralize.camelize}Controller"
  controller_path = File.join(PROJECT_ROOT, "app/controllers/#{file_name}_controller.rb")
  view_dir = File.join(PROJECT_ROOT, "app/views/#{file_name}")
  raise "Controller already created" if File.exist?(controller_path)

  File.open(controller_path, "w") do |file|
    file.write("class #{controller_name} < ApplicationController\n")
    file.write("end\n")
  end
  Dir.mkdir(view_dir) unless Dir.exist?(view_dir)

  puts "Controller #{controller_name} can be found at #{controller_path}"
end

#migration(name) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/cli.rb', line 11

def migration(name)
  file_name = name
  timestamp = Time.now.to_i

  path = File.join(PROJECT_ROOT, "db/migrations/#{timestamp}_#{file_name}.sql")
  raise "Migration already created" if File.exist?(path)

  File.new(path, "w")
  puts "Migration #{file_name} can be found at #{path}"
end

#model(name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cli.rb', line 23

def model(name)
  model_path = File.join(PROJECT_ROOT, "app/models/#{name.underscore}.rb")
  raise "Model already created" if File.exist?(model_path)

  File.open(model_path, "w") do |file|
    file.write("class #{name.camelize} < DynamicArchive::Base\n")
    file.write("  self.finalize!\n")
    file.write("end\n")
  end

  migration(name)
  puts "Model #{name.camelize} can be found at #{model_path}"
end