Module: MultiRails::RailsAppHelper

Defined in:
lib/multi_rails/rails_app_helper.rb

Constant Summary collapse

RAILS_DIRECTORIES =
%w(app config public test)
REQUIRE_LINE =
%[require File.expand_path('config/rails_version') if File.exist?("config/rails_version.rb")].freeze

Class Method Summary collapse

Class Method Details

.add_require_line_to_environment_fileObject

Raises:



79
80
81
82
83
84
85
86
87
88
# File 'lib/multi_rails/rails_app_helper.rb', line 79

def add_require_line_to_environment_file
  raise MultiRailsError, "Can't find environment.rb file was looking at path: #{environment_file}" unless File.exist?(environment_file)
  unless first_environment_line == REQUIRE_LINE
    original_content = File.read(environment_file)
    File.open(environment_file, 'r+') do |f| 
      f.puts REQUIRE_LINE
      f.print original_content
    end
  end
end

.bootstrap_for_railsObject

Do a one time bootstrap to let MultiRails do its thing – we aren’t putting this in the general MultiRails rake file to keep it from showing up in contexts where it doesn’t make sense (ie when testing a plugin).



23
24
25
26
# File 'lib/multi_rails/rails_app_helper.rb', line 23

def bootstrap_for_rails
  set_rails_root
  add_require_line_to_environment_file
end

.clean_upObject



39
40
41
42
# File 'lib/multi_rails/rails_app_helper.rb', line 39

def clean_up
  FileUtils.rm(rails_gem_version_file) if within_rails_app? && File.exist?(rails_gem_version_file)
  rename_vendor_rails_to_original
end

.current_dir_contains_rails_dirs?Boolean

Returns:

  • (Boolean)


54
55
56
57
58
# File 'lib/multi_rails/rails_app_helper.rb', line 54

def current_dir_contains_rails_dirs?
  if RAILS_DIRECTORIES.all? { |rails_dir| Dir.entries(Dir.pwd).include?(rails_dir) }
    Dir.pwd
  end
end

.environment_fileObject



94
95
96
# File 'lib/multi_rails/rails_app_helper.rb', line 94

def environment_file
  File.expand_path(File.join(RAILS_ROOT, "config/environment.rb"))
end

.find_rails_root_dirObject



50
51
52
# File 'lib/multi_rails/rails_app_helper.rb', line 50

def find_rails_root_dir
  if current_dir_contains_rails_dirs? then Dir.pwd end
end

.first_environment_lineObject



90
91
92
# File 'lib/multi_rails/rails_app_helper.rb', line 90

def first_environment_line
  File.open(environment_file).readline.strip
end

.init_for_rails_app(version) ⇒ Object



44
45
46
47
48
# File 'lib/multi_rails/rails_app_helper.rb', line 44

def init_for_rails_app(version)
  set_rails_root
  write_rails_gem_version_file(version)
  rename_vendor_rails_if_necessary
end

.rails_gem_version_fileObject



67
68
69
# File 'lib/multi_rails/rails_app_helper.rb', line 67

def rails_gem_version_file
  File.expand_path("#{RAILS_ROOT}/config/rails_version.rb")
end

.rename_vendor_rails_if_necessaryObject



71
72
73
# File 'lib/multi_rails/rails_app_helper.rb', line 71

def rename_vendor_rails_if_necessary
  File.rename(vendor_rails, vendor_rails_off) if File.directory?(vendor_rails)
end

.rename_vendor_rails_to_originalObject



75
76
77
# File 'lib/multi_rails/rails_app_helper.rb', line 75

def rename_vendor_rails_to_original
  File.rename(vendor_rails_off, vendor_rails) if File.directory?(vendor_rails_off)
end

.run(task) ⇒ Object

Run the appropriate task or method for MultiRails This is called from the MultiRails command line runner



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

def run(task)
  if task == "bootstrap"
    self.bootstrap_for_rails
  else
    ENV["MULTIRAILS_FOR_RAILS_APP"] = "true"
    Rake::Task["test:multi_rails:#{task}"].invoke
  end
end

.set_rails_rootObject

Make sure we have RAILS_ROOT set - will try to find it dynamically if its not set.



29
30
31
32
33
34
35
36
37
# File 'lib/multi_rails/rails_app_helper.rb', line 29

def set_rails_root
  if Object.const_defined?("RAILS_ROOT")
    RAILS_ROOT
  else
    Object.const_set("RAILS_ROOT", find_rails_root_dir)
  end
  raise("Must have a valid RAILS_ROOT.") unless Object.const_defined?("RAILS_ROOT") && RAILS_ROOT
  RAILS_ROOT
end

.vendor_railsObject



98
99
100
# File 'lib/multi_rails/rails_app_helper.rb', line 98

def vendor_rails
  "#{RAILS_ROOT}/vendor/rails"
end

.vendor_rails_offObject



102
103
104
# File 'lib/multi_rails/rails_app_helper.rb', line 102

def vendor_rails_off
  "#{RAILS_ROOT}/vendor/rails.off"
end

.write_rails_gem_version_file(version) ⇒ Object

Write out a file which is loaded later, so that the RAILS_GEM_VERSION gets set in the correct process



61
62
63
64
65
# File 'lib/multi_rails/rails_app_helper.rb', line 61

def write_rails_gem_version_file(version)
  File.open(rails_gem_version_file, 'w') do |file|
    file << %|RAILS_GEM_VERSION = '#{version}' unless Object.const_defined?("RAILS_GEM_VERSION")|
  end
end