Namespace
Methods
A
B
C
E
M
R
S
T
U
Instance Public methods
add_to_config(str)
# File railties/test/isolation/abstract_unit.rb, line 256
def add_to_config(str)
  environment = File.read("#{app_path}/config/application.rb")
  if environment =~ /(\n\s*end\s*end\s*)\Z/
    File.open("#{app_path}/config/application.rb", 'w') do |f|
      f.puts $` + "\n#{str}\n" + $1
    end
  end
end
add_to_env_config(env, str)
# File railties/test/isolation/abstract_unit.rb, line 265
def add_to_env_config(env, str)
  environment = File.read("#{app_path}/config/environments/#{env}.rb")
  if environment =~ /(\n\s*end\s*)\Z/
    File.open("#{app_path}/config/environments/#{env}.rb", 'w') do |f|
      f.puts $` + "\n#{str}\n" + $1
    end
  end
end
add_to_top_of_config(str)
# File railties/test/isolation/abstract_unit.rb, line 247
def add_to_top_of_config(str)
  environment = File.read("#{app_path}/config/application.rb")
  if environment =~ /(Rails::Application\s*)/
    File.open("#{app_path}/config/application.rb", 'w') do |f|
      f.puts $` + $1 + "\n#{str}\n" + $'
    end
  end
end
app_file(path, contents, mode = 'w')
# File railties/test/isolation/abstract_unit.rb, line 288
def app_file(path, contents, mode = 'w')
  FileUtils.mkdir_p File.dirname("#{app_path}/#{path}")
  File.open("#{app_path}/#{path}", mode) do |f|
    f.puts contents
  end
end
boot_rails()
# File railties/test/isolation/abstract_unit.rb, line 313
def boot_rails
end
build_app(options = {})

Build an application by invoking the generator and going through the whole stack.

# File railties/test/isolation/abstract_unit.rb, line 104
def build_app(options = {})
  @prev_rails_env = ENV['RAILS_ENV']
  ENV['RAILS_ENV']       =   "development"
  ENV['SECRET_KEY_BASE'] ||= SecureRandom.hex(16)

  FileUtils.rm_rf(app_path)
  FileUtils.cp_r(app_template_path, app_path)

  # Delete the initializers unless requested
  unless options[:initializers]
    Dir["#{app_path}/config/initializers/**/*.rb"].each do |initializer|
      File.delete(initializer)
    end
  end

  gemfile_path = "#{app_path}/Gemfile"
  if options[:gemfile].blank? && File.exist?(gemfile_path)
    File.delete gemfile_path
  end

  routes = File.read("#{app_path}/config/routes.rb")
  if routes =~ /(\n\s*end\s*)\Z/
    File.open("#{app_path}/config/routes.rb", 'w') do |f|
      f.puts $` + "\nActiveSupport::Deprecation.silence { match ':controller(/:action(/:id))(.:format)', via: :all }\n" + $1
    end
  end

  File.open("#{app_path}/config/database.yml", "w") do |f|
    f.puts <<-YAML
    default: &default
      adapter: sqlite3
      pool: 5
      timeout: 5000
    development:
      <<: *default
      database: db/development.sqlite3
    test:
      <<: *default
      database: db/test.sqlite3
    production:
      <<: *default
      database: db/production.sqlite3
    YAML
  end

  add_to_config <<-RUBY
    config.eager_load = false
    config.session_store :cookie_store, key: "_myapp_session"
    config.active_support.deprecation = :log
    config.active_support.test_order = :random
    config.action_controller.allow_forgery_protection = false
    config.log_level = :info
  RUBY
end
controller(name, contents)
# File railties/test/isolation/abstract_unit.rb, line 299
def controller(name, contents)
  app_file("app/controllers/#{name}_controller.rb", contents)
end
engine(name)
# File railties/test/isolation/abstract_unit.rb, line 224
def engine(name)
  dir = "#{app_path}/random/#{name}"
  FileUtils.mkdir_p(dir)

  app = File.readlines("#{app_path}/config/application.rb")
  app.insert(2, "$:.unshift(\"#{dir}/lib\")")
  app.insert(3, "require #{name.inspect}")

  File.open("#{app_path}/config/application.rb", 'r+') do |f|
    f.puts app
  end

  Bukkit.new(dir).tap do |bukkit|
    yield bukkit if block_given?
  end
end
make_basic_app()

Make a very basic app, without creating the whole directory structure. This is faster and simpler than the method above.

# File railties/test/isolation/abstract_unit.rb, line 165
def make_basic_app
  require "rails"
  require "action_controller/railtie"
  require "action_view/railtie"
  require 'action_dispatch/middleware/flash'

  @app = Class.new(Rails::Application)
  @app.config.eager_load = false
  @app.secrets.secret_key_base = "3b7cd727ee24e8444053437c36cc66c4"
  @app.config.session_store :cookie_store, key: "_myapp_session"
  @app.config.active_support.deprecation = :log
  @app.config.active_support.test_order = :random
  @app.config.log_level = :info

  yield @app if block_given?
  @app.initialize!

  @app.routes.draw do
    get "/" => "omg#index"
  end

  require 'rack/test'
  extend ::Rack::Test::Methods
end
remove_file(path)
# File railties/test/isolation/abstract_unit.rb, line 295
def remove_file(path)
  FileUtils.rm_rf "#{app_path}/#{path}"
end
remove_from_config(str)
# File railties/test/isolation/abstract_unit.rb, line 274
def remove_from_config(str)
  remove_from_file("#{app_path}/config/application.rb", str)
end
remove_from_env_config(env, str)
# File railties/test/isolation/abstract_unit.rb, line 278
def remove_from_env_config(env, str)
  remove_from_file("#{app_path}/config/environments/#{env}.rb", str)
end
remove_from_file(file, str)
# File railties/test/isolation/abstract_unit.rb, line 282
def remove_from_file(file, str)
  contents = File.read(file)
  contents.sub!(/#{str}/, '')
  File.write(file, contents)
end
script(script)
# File railties/test/isolation/abstract_unit.rb, line 241
def script(script)
  Dir.chdir(app_path) do
    %x`#{Gem.ruby} #{app_path}/bin/rails #{script}`
  end
end
simple_controller()
# File railties/test/isolation/abstract_unit.rb, line 190
def simple_controller
  controller :foo, <<-RUBY
    class FooController < ApplicationController
      def index
        render text: "foo"
      end
    end
  RUBY

  app_file 'config/routes.rb', <<-RUBY
    Rails.application.routes.draw do
      get ':controller(/:action)'
    end
  RUBY
end
teardown_app()
# File railties/test/isolation/abstract_unit.rb, line 159
def teardown_app
  ENV['RAILS_ENV'] = @prev_rails_env if @prev_rails_env
end
use_frameworks(arr)
# File railties/test/isolation/abstract_unit.rb, line 303
def use_frameworks(arr)
  to_remove = [:actionmailer, :activerecord] - arr

  if to_remove.include?(:activerecord)
    remove_from_config 'config.active_record.*'
  end

  $:.reject! {|path| path =~ %r'/(#{to_remove.join('|')})/' }
end