Class: Spork::AppFramework::Rails

Inherits:
Spork::AppFramework
  • Object
show all
Defined in:
lib/spork/app_framework/rails.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.present?Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
# File 'lib/spork/app_framework/rails.rb', line 87

def self.present?
  # TODO - Simplifiy this to only target Rails 3.1
  File.exist?("config/environment.rb") &&
    (File.read("config/environment.rb").include?('RAILS_GEM_VERSION') ||
     (File.exist?("config/application.rb") &&
      File.read("config/application.rb").include?("Rails::Application")))
end

Instance Method Details

#application_fileObject



22
23
24
# File 'lib/spork/app_framework/rails.rb', line 22

def application_file
  @application_file ||= File.join(File.dirname(environment_file), 'application')
end

#boot_fileObject



18
19
20
# File 'lib/spork/app_framework/rails.rb', line 18

def boot_file
  @boot_file ||= File.join(File.dirname(environment_file), 'boot')
end

#deprecated_versionObject



34
35
36
37
38
39
40
41
42
# File 'lib/spork/app_framework/rails.rb', line 34

def deprecated_version
  @version ||= (
    if /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/.match(environment_contents)
      $1
    else
      nil
    end
  )
end

#entry_pointObject Also known as: environment_file



12
13
14
# File 'lib/spork/app_framework/rails.rb', line 12

def entry_point
  @entry_point ||= File.expand_path("config/environment.rb", Dir.pwd)
end

#environment_contentsObject



26
27
28
# File 'lib/spork/app_framework/rails.rb', line 26

def environment_contents
  @environment_contents ||= File.read(environment_file)
end

#preload(&block) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/spork/app_framework/rails.rb', line 3

def preload(&block)
  STDERR.puts "Preloading Rails environment"
  STDERR.flush
  ENV["RAILS_ENV"] ||= 'test'
  preload_rails
  yield
  ActiveRecord::Base.remove_connection if defined?(ActiveRecord)
end

#preload_railsObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/spork/app_framework/rails.rb', line 44

def preload_rails
  if deprecated_version && (not /^3/.match(deprecated_version))
    puts "This version of spork only supports Rails 3. To use spork with rails 2.3.x, downgrade to spork 0.8.x."
    exit 1
  end
  require application_file
  ::Rails.application
  ::Rails::Engine.class_eval do
    def eager_load!
      # turn off eager_loading, all together
    end
  end
  # Spork.trap_method(::AbstractController::Helpers::ClassMethods, :helper)
  Spork.trap_method(::ActiveModel::Observing::ClassMethods, :instantiate_observers) if defined?(ActiveModel::Observing)
  Spork.each_run { ActiveRecord::Base.establish_connection rescue nil } if Object.const_defined?(:ActiveRecord)


  AbstractController::Helpers::ClassMethods.module_eval do
    def helper(*args, &block)
      ([args].flatten - [:all]).each do |arg|
        next unless arg.is_a?(String)
        filename = arg + "_helper"
        unless ::ActiveSupport::Dependencies.search_for_file(filename)
          # this error message must raise in the format such that LoadError#path returns the filename
          e = LoadError.new("Missing helper file helpers/%s.rb" % filename)
          if e.respond_to?(:path)
            e.instance_variable_set("@path", "helpers/%s.rb" % filename)
          end
          raise e
        end
      end

      Spork.each_run(false) do
        modules_for_helpers(args).each do |mod|
          add_template_helper(mod)
        end

        _helpers.module_eval(&block) if block_given?
      end
    end
  end
end

#vendorObject



30
31
32
# File 'lib/spork/app_framework/rails.rb', line 30

def vendor
  @vendor ||= File.expand_path("vendor/rails", Dir.pwd)
end