Module: IGem::Rails

Included in:
Rails
Defined in:
lib/igem/rails.rb

Overview

Run rails server and generator subcommands from the Rails console

This is a solution based on blockgiven.tumblr.com/post/5161067729/rails-server Can be used to run rails server, rails generate from the Rails console thus speeding up Rails server startup to ~ 1 second.

Usage:

Rails.server                     to start the rails server
Rails.generate                   to list generators
Rails.generate "model", "user"   to use a generator
Rails.update   "model", "user"   to update the generated code
Rails.destroy  "model", "user"   to remove the generated code

NOTE: after Rails.server, you cannot use Control-C anymore in the console because it first stops the server, secondly stops the process

Instance Method Summary collapse

Instance Method Details

#destroy(*args) ⇒ Object Also known as: d



32
33
34
35
36
37
38
39
40
# File 'lib/igem/rails.rb', line 32

def destroy *args
  args = args[0].split(" ") if args.count == 1
  args = args.map{|e|e.to_s}
  require "rails/generators"
  Rails::Generators.help && return if args.empty?
  name = args.shift
  args << "--orm=active_record" if args.none? {|a|a =~ /--orm/}
  Rails::Generators.invoke name, args, :behavior => :revoke
end

#generate(*args) ⇒ Object Also known as: g



20
21
22
23
24
25
26
27
28
# File 'lib/igem/rails.rb', line 20

def generate *args
  args = args[0].split(" ") if args.count == 1
  args = args.map{|e|e.to_s}
  require "rails/generators"
  Rails::Generators.help && return if args.empty?
  name = args.shift
  args << "--orm=active_record" if args.none? {|a|a =~ /--orm/}
  Rails::Generators.invoke name, args, :behavior => :invoke
end

#server(options = {:Port => 3000}) ⇒ Object Also known as: s



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/igem/rails.rb', line 54

def server options={:Port => 3000}
  require "rails/commands/server"

  return @server if defined? @server and @server.alive?

  if defined? @server and not @server.alive?
    ObjectSpace.each_object(TCPServer){|s| s.close if !s.closed? && s.addr[1] == options[:Port]}
  end

  @server = Thread.new do
    options[:Port] = options[:Port].to_s

    server = Rails::Server.new
    server.options.merge options
    server.start
  end
end

#update(*args) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/igem/rails.rb', line 44

def update *args
  args = args[0].split(" ") if args.count == 1
  args = args.map{|e|e.to_s}
  require "rails/generators"
  Rails::Generators.help && return if args.empty?
  name = args.shift
  args << "--orm=active_record" if args.none? {|a|a =~ /--orm/}
  Rails::Generators.invoke name, args, :behavior => :skip
end