Skip to content

Instantly share code, notes, and snippets.

@cie
Created August 1, 2012 16:05
Show Gist options
  • Save cie/3228233 to your computer and use it in GitHub Desktop.
Save cie/3228233 to your computer and use it in GitHub Desktop.
IRails - run Rails server and generate from the Rails console
# IRails - Run rails server and generator subcommands from the Rails console
#
# This is a solution based on http://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
#
module Rails
def self.generate *args
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
def self.destroy *args
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
def self.update *args
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
def self.server options={}
require "rails/commands/server"
Thread.new do
server = Rails::Server.new
server.options.merge options
server.start
end
end
end
@cie
Copy link
Author

cie commented Aug 1, 2012

Also see iGem for running rake, rspec, etc. from the rails console

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment