class Minirails::RailsApp

Public Instance Methods

call() click to toggle source
Calls superclass method Minirails::RackApp#call
# File lib/minirails.rb, line 163
def call
  migrate
  super
end
init() click to toggle source
# File lib/minirails.rb, line 154
def init
  $stderr.puts "--> loading rails"
  require "action_controller"
  require "rails"
  $stderr.puts "--> loading app"

  @endpoint = endpoint
end

Protected Instance Methods

build() click to toggle source
# File lib/minirails.rb, line 170
def build
  # basic rails app bootstrap
  app = Class.new(Rails::Application) do
    config.eager_load = false
    config.secret_key_base = SecureRandom.hex
    config.logger = Logger.new(STDERR)
  end

  # ActiveRecord specific configuration
  if app.config.respond_to?(:active_record)
    app.config.active_record.dump_schema_after_migration = false
  end

  # initialize app
  app.initialize!

  # execute 'define' block
  block.call(app)

  # and finally return app object
  app
end
endpoint() click to toggle source
# File lib/minirails.rb, line 211
def endpoint
  build
rescue NameError => e
  # autoloading has some crazy weird error
  # and we can't just require activerecord upfront
  # because if app is not using it there will
  # be no database configuration
  case e.message
  when /uninitialized constant.*ActiveRecord/
    $stderr.puts "--> loading activerecord"
    require "active_record/railtie"
  else
    raise e
  end

  endpoint
end
migrate() click to toggle source
# File lib/minirails.rb, line 193
def migrate
  # ActiveRecord post functions
  if @endpoint.config.respond_to?(:active_record)
    # load rake tasks
    @endpoint.load_tasks

    # create and migrate database
    Rake::Task["db:drop"].invoke
    Rake::Task["db:create"].invoke
    # run migrations in order they were defined in source file
    ActiveRecord::Migration.descendants.sort_by {|c|
      c.instance_methods(false).map {|m|
        c.instance_method(m).source_location.last
      }.min
    }.each {|e| e.migrate :up }
  end
end