Module: RubyStation::Helper::Rails

Defined in:
lib/ruby-station/helper/rails.rb

Overview

This module helps invoking Rails apps. During execution of the application, Rails writes data to many directories (db/, log/, tmp/). So this helper copies all the files into data_dir and invoke the app within the directory.

Be sure to run ‘rake db:migrate RAILS_ENV=produciton’ before you create the gem of your app.

Examples:

RubyStation::Helper::Rails.run

Class Method Summary collapse

Class Method Details

.install(app_dir) ⇒ Object

Ensure your app is copied to data_dir. It does nothing if already copied.



36
37
38
39
40
41
42
43
44
# File 'lib/ruby-station/helper/rails.rb', line 36

def self.install(app_dir)
  # Note: "." is needed, otherwise copied as data_dir/appname/*
  from = File.join(app_dir, ".")
  to = RubyStation.data_dir

  unless installed?(from, to)
    FileUtils.cp_r(from, to)
  end
end

.installed?(from, to) ⇒ Boolean

Checks if the files are copied

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/ruby-station/helper/rails.rb', line 49

def self.installed?(from, to)
  # Note: Too simple?
  File.exist?(File.join(to, "main.rb"))
end

.runObject

Copy files (if needed) and start Rails server.



24
25
26
27
28
29
30
# File 'lib/ruby-station/helper/rails.rb', line 24

def self.run
  caller_path = caller[0][/^(.*):/, 1]
  self.install(File.dirname(caller_path))

  Dir.chdir(RubyStation.data_dir)
  self.start_server
end

.start_serverObject

Start Rails with ‘script/server’.



57
58
59
60
61
# File 'lib/ruby-station/helper/rails.rb', line 57

def self.start_server
  Dir.chdir(RubyStation.data_dir)
  # TODO: Support Windows
  exec "./script/server -p #{RubyStation.port} -e production"
end