Class: Grails::Generate

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

Instance Method Summary collapse

Instance Method Details

#controller(name) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/commands.rb', line 36

def controller(name)
  controller_path = File.join(project_root, "app/controllers/#{file.pluralize.underscore}_controlller.rb")
  controller_name = "#{name.pluralize.camelcase}Controller"
  view_dir = File.join(project_root, "app/views/#{file.pluralize.underscore}")
  raise "Controller already exists" if File.exist?(controller_path)

  File.open(controller_path, "w") do |file|
    file.write("class #{controller_name} < ApplicationController\n")
    file.write("end")
  end
  Dir.mkdir(view_dir) unless Dir.exist?(view_dir)
  puts "New controller file #{name} can be found at #{controller_path}"
end

#migration(name) ⇒ Object



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

def migration(name)
  file_name = name
  timestamp = Time.now.to_i
  path = File.join(project_root, "db/migrations/#{timestamp}_#{name}.sql")
  File.new(path, "w")

  puts "New migration file #{name} can be found at #{path}"
end

#model(name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/commands.rb', line 21

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

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

  migration(name)
  puts "New migration file #{name} can be found at #{path}"
end